feat(statistics): implement top alert stocks, top last sales, top supplier debts, and top selling products endpoints with SQL queries

This commit is contained in:
2026-01-04 13:45:26 +03:30
parent eb6e0e7a0d
commit a2db2daa70
72 changed files with 11934 additions and 2559 deletions
@@ -1 +0,0 @@
-- This is an empty migration.
@@ -1,347 +0,0 @@
-- AUTO-GENERATED MYSQL TRIGGER DUMP
-- Generated at: 2025-12-22T15:32:20.184Z
-- ------------------------------------------
-- Trigger: trg_transfer_item_after_insert
-- Event: INSERT
-- Table: Inventory_Transfer_Items
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_transfer_item_after_insert`;
CREATE TRIGGER `trg_transfer_item_after_insert` AFTER INSERT ON `Inventory_Transfer_Items` FOR EACH ROW begin
DECLARE fromInv INT;
DECLARE toInv INT;
DECLARE _avgCost DECIMAL(10,2);
DECLARE latestQuantityInOrigin DECIMAL(10,2);
DECLARE latestQuantityInDestination DECIMAL(10,2);
SELECT fromInventoryId, toInventoryId INTO fromInv, toInv
FROM Inventory_Transfers WHERE id = NEW.transferId;
SELECT COALESCE(avgCost, 0), COALESCE(quantity, 0) INTO _avgCost, latestQuantityInOrigin FROM Stock_Balance
WHERE ProductId = NEW.productId AND inventoryId = fromInv LIMIT 1;
SELECT COALESCE(quantity, 0) INTO latestQuantityInDestination FROM Stock_Balance
WHERE ProductId = NEW.productId AND inventoryId = toInv LIMIT 1;
-- OUT from source
INSERT INTO Stock_Movements
(type, quantity, fee, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
VALUES
('OUT', NEW.count, _avgCost, _avgCost*NEW.count, _avgCost, 'INVENTORY_TRANSFER', NEW.transferId, NEW.productId, fromInv, toInv, NOW(), latestQuantityInOrigin-NEW.count);
-- IN to destination
INSERT INTO Stock_Movements
(type, quantity, fee, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
VALUES
('IN', NEW.count,_avgCost, _avgCost*NEW.count,_avgCost, 'INVENTORY_TRANSFER', NEW.transferId, NEW.productId, toInv, fromInv, NOW(), latestQuantityInOrigin-NEW.count);
end;
-- ------------------------------------------
-- Trigger: trg_purchase_receipt_item_after_insert
-- Event: INSERT
-- Table: Purchase_Receipt_Items
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_purchase_receipt_item_after_insert`;
CREATE TRIGGER `trg_purchase_receipt_item_after_insert` AFTER INSERT ON `Purchase_Receipt_Items` FOR EACH ROW BEGIN DECLARE latestQuantity DECIMAL(10, 2) DEFAULT 0;
DECLARE invId INT;
DECLARE suppId INT;
-- Get inventory & supplier from receipt
SELECT inventoryId, supplierId
INTO invId, suppId
FROM Purchase_Receipts
WHERE id = NEW.receiptId;
-- Get current stock quantity (if exists)
SELECT COALESCE(quantity, 0)
INTO latestQuantity
FROM Stock_Balance sb
WHERE sb.inventoryId = invId
AND sb.productId = NEW.productId
LIMIT 1;
-- Insert stock movement
INSERT INTO Stock_Movements (
type,
quantity,
fee,
totalCost,
referenceType,
referenceId,
productId,
inventoryId,
avgCost,
supplierId,
remainedInStock,
createdAt
)
VALUES (
'IN',
NEW.count,
NEW.fee,
NEW.total,
'PURCHASE',
NEW.receiptId,
NEW.productId,
invId,
CASE
WHEN NEW.count = 0 THEN 0
ELSE NEW.total / NEW.count
END
,
suppId,
latestQuantity + NEW.count,
NOW()
);
END;
-- ------------------------------------------
-- Trigger: trg_sales_invoice_items_before_insert
-- Event: INSERT
-- Table: Sales_Invoice_Items
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_sales_invoice_items_before_insert`;
CREATE TRIGGER `trg_sales_invoice_items_before_insert` BEFORE INSERT ON `Sales_Invoice_Items` FOR EACH ROW begin DECLARE current_stock DECIMAL(10, 2);
DECLARE inventory_id INT;
SELECT inventoryId INTO inventory_id
FROM Sales_Invoices si
WHERE si.id = NEW.invoiceId
LIMIT 1;
SELECT COALESCE(quantity, 0) INTO current_stock
FROM Stock_Balance sb
WHERE productId = NEW.productId AND sb.inventoryId = inventory_id
LIMIT 1;
IF NEW.count > current_stock THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'موجودی کالا در انبار کافی نیست.';
END IF;
end;
-- ------------------------------------------
-- Trigger: trg_sales_invoice_items_after_insert
-- Event: INSERT
-- Table: Sales_Invoice_Items
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_sales_invoice_items_after_insert`;
CREATE TRIGGER `trg_sales_invoice_items_after_insert` AFTER INSERT ON `Sales_Invoice_Items` FOR EACH ROW begin DECLARE current_stock DECIMAL(10, 2);
DECLARE inventory_id INT;
DECLARE customer_id INT;
SELECT inventoryId , customerId INTO inventory_id, customer_id
FROM Sales_Invoices si
WHERE si.id = NEW.invoiceId
LIMIT 1;
SELECT COALESCE(quantity, 0) INTO current_stock
FROM Stock_Balance sb
WHERE productId = NEW.productId AND sb.inventoryId = inventory_id
LIMIT 1;
INSERT INTO Stock_Movements (
type,
quantity,
fee,
totalCost,
referenceType,
referenceId,
productId,
inventoryId,
avgCost,
remainedInStock,
customerId,
createdAt
)
VALUES (
'OUT',
NEW.count,
NEW.fee,
NEW.total,
'SALES',
NEW.invoiceId,
NEW.productId,
inventory_id,
CASE
WHEN NEW.count = 0 THEN 0
ELSE NEW.total / NEW.count
END,
current_stock + NEW.count,
customer_id,
NOW()
);
END;
-- ------------------------------------------
-- Trigger: trg_stock_transfer
-- Event: INSERT
-- Table: Stock_Movements
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_stock_transfer`;
CREATE TRIGGER `trg_stock_transfer` AFTER INSERT ON `Stock_Movements` FOR EACH ROW BEGIN IF NEW.referenceType = 'INVENTORY_TRANSFER' THEN IF NEW.type = 'IN' THEN
INSERT INTO
Stock_Balance (
productId,
inventoryId,
quantity,
totalCost,
avgCost,
updatedAt
)
VALUES (
NEW.productId,
NEW.inventoryId,
NEW.quantity,
NEW.totalCost,
CASE
WHEN NEW.quantity = 0 THEN 0
ELSE NEW.totalCost / NEW.quantity
END,
NOW()
)
ON DUPLICATE KEY UPDATE
quantity = quantity + NEW.quantity,
totalCost = totalCost + NEW.totalCost,
avgCost = CASE
WHEN (quantity + NEW.quantity) = 0 THEN 0
ELSE (totalCost + NEW.totalCost) / (quantity + NEW.quantity)
END,
updatedAt = NOW();
END IF;
IF NEW.type = 'OUT' THEN IF EXISTS (
SELECT 1
FROM Stock_Balance sb
WHERE
sb.productId = NEW.productId
AND sb.inventoryId = NEW.inventoryId
) THEN
UPDATE Stock_Balance sb
SET
sb.quantity = sb.quantity - NEW.quantity,
sb.totalCost = sb.totalCost - (sb.avgCost * NEW.quantity),
sb.updatedAt = NOW()
WHERE
sb.productId = NEW.productId
AND sb.inventoryId = NEW.inventoryId;
ELSE
INSERT INTO
Stock_Balance (
productId,
inventoryId,
quantity,
totalCost,
avgCost,
updatedAt
)
VALUES (
NEW.productId,
NEW.inventoryId,
- NEW.quantity,
- COALESCE(NEW.fee, 0) * NEW.quantity,
COALESCE(NEW.fee, 0),
NOW()
);
END IF;
END IF;
END IF;
END;
-- ------------------------------------------
-- Trigger: trg_stock_purchase_insert
-- Event: INSERT
-- Table: Stock_Movements
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_stock_purchase_insert`;
CREATE TRIGGER `trg_stock_purchase_insert` AFTER INSERT ON `Stock_Movements` FOR EACH ROW BEGIN IF NEW.referenceType = 'PURCHASE' THEN
INSERT INTO
Stock_Balance (
productId,
quantity,
avgCost,
totalCost,
inventoryId,
updatedAt
)
VALUES (
NEW.productId,
NEW.quantity,
NEW.fee,
NEW.totalCost,
NEW.inventoryId,
NOW()
)
ON DUPLICATE KEY UPDATE
quantity = quantity + NEW.quantity,
totalCost = totalCost + NEW.totalCost,
avgCost = totalCost / quantity;
END IF;
END;
-- ------------------------------------------
-- Trigger: trg_stock_sale_insert
-- Event: INSERT
-- Table: Stock_Movements
-- ------------------------------------------
DROP TRIGGER IF EXISTS `trg_stock_sale_insert`;
CREATE TRIGGER `trg_stock_sale_insert` AFTER INSERT ON `Stock_Movements` FOR EACH ROW BEGIN IF NEW.referenceType = 'SALES' THEN
INSERT INTO
Stock_Balance (
productId,
quantity,
avgCost,
totalCost,
inventoryId,
updatedAt
)
VALUES (
NEW.productId,
NEW.quantity,
NEW.fee,
NEW.totalCost,
NEW.inventoryId,
NOW()
)
ON DUPLICATE KEY UPDATE
quantity = quantity - NEW.quantity,
totalCost = totalCost - NEW.totalCost,
avgCost = totalCost / quantity;
END IF;
END;
@@ -1,18 +0,0 @@
/*
Warnings:
- You are about to drop the column `bankAccountId` on the `Inventories` table. All the data in the column will be lost.
- You are about to drop the `_Bank_Accounts_inventoryId_fkey` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE `_Bank_Accounts_inventoryId_fkey` DROP FOREIGN KEY `_Bank_Accounts_inventoryId_fkey_A_fkey`;
-- DropForeignKey
ALTER TABLE `_Bank_Accounts_inventoryId_fkey` DROP FOREIGN KEY `_Bank_Accounts_inventoryId_fkey_B_fkey`;
-- AlterTable
ALTER TABLE `Inventories` DROP COLUMN `bankAccountId`;
-- DropTable
DROP TABLE `_Bank_Accounts_inventoryId_fkey`;
@@ -1,158 +0,0 @@
/*
Warnings:
- You are about to alter the column `count` on the `Inventory_Transfer_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `totalAmount` on the `Orders` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `basePrice` on the `Product_Variants` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `salePrice` on the `Product_Variants` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `quantity` on the `Product_Variants` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `alertQuantity` on the `Product_Variants` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `salePrice` on the `Products` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,0)`.
- You are about to alter the column `count` on the `Purchase_Receipt_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `fee` on the `Purchase_Receipt_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `total` on the `Purchase_Receipt_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `amount` on the `Purchase_Receipt_Payments` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to drop the column `isSettled` on the `Purchase_Receipts` table. All the data in the column will be lost.
- You are about to alter the column `totalAmount` on the `Purchase_Receipts` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `paidAmount` on the `Purchase_Receipts` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `count` on the `Sales_Invoice_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `fee` on the `Sales_Invoice_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `total` on the `Sales_Invoice_Items` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `totalAmount` on the `Sales_Invoices` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `adjustedQuantity` on the `Stock_Adjustments` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `quantity` on the `Stock_Movements` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `fee` on the `Stock_Movements` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `totalCost` on the `Stock_Movements` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `avgCost` on the `Stock_Movements` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `remainedInStock` on the `Stock_Movements` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(10,0)`.
- You are about to alter the column `debit` on the `Supplier_Ledger` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `credit` on the `Supplier_Ledger` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- You are about to alter the column `balance` on the `Supplier_Ledger` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(15,2)`.
- A unique constraint covering the columns `[iban]` on the table `Bank_Accounts` will be added. If there are existing duplicate values, this will fail.
- Made the column `bankAccountId` on table `Pos_Accounts` required. This step will fail if there are existing NULL values in that column.
- Added the required column `inventoryId` to the `Purchase_Receipt_Payments` table without a default value. This is not possible if the table is not empty.
- Added the required column `type` to the `Purchase_Receipt_Payments` table without a default value. This is not possible if the table is not empty.
- Made the column `bankAccountId` on table `Purchase_Receipt_Payments` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE `Pos_Accounts`
DROP FOREIGN KEY `Pos_Accounts_bankAccountId_fkey`;
-- DropForeignKey
ALTER TABLE `Pos_Accounts`
DROP FOREIGN KEY `Pos_Accounts_bankAccountId_inventoryId_fkey`;
-- DropForeignKey
ALTER TABLE `Pos_Accounts`
DROP FOREIGN KEY `Pos_Accounts_inventoryId_fkey`;
-- DropForeignKey
ALTER TABLE `Purchase_Receipt_Payments`
DROP FOREIGN KEY `Purchase_Receipt_Payments_bankAccountId_fkey`;
-- DropIndex
DROP INDEX `Pos_Accounts_bankAccountId_inventoryId_fkey` ON `Pos_Accounts`;
-- DropIndex
DROP INDEX `Purchase_Receipt_Payments_bankAccountId_fkey` ON `Purchase_Receipt_Payments`;
-- AlterTable
ALTER TABLE `Inventory_Transfer_Items`
MODIFY `count` DECIMAL(10, 0) NOT NULL;
-- AlterTable
ALTER TABLE `Orders` MODIFY `totalAmount` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Pos_Accounts` MODIFY `bankAccountId` INTEGER NOT NULL;
-- AlterTable
ALTER TABLE `Product_Variants`
MODIFY `basePrice` DECIMAL(15, 2) NOT NULL,
MODIFY `salePrice` DECIMAL(15, 2) NOT NULL,
MODIFY `quantity` DECIMAL(10, 0) NULL DEFAULT 0.00,
MODIFY `alertQuantity` DECIMAL(10, 0) NULL DEFAULT 5.00;
-- AlterTable
ALTER TABLE `Products`
MODIFY `salePrice` DECIMAL(15, 0) NOT NULL DEFAULT 0.00;
-- AlterTable
ALTER TABLE `Purchase_Receipt_Items`
MODIFY `count` DECIMAL(10, 0) NOT NULL,
MODIFY `fee` DECIMAL(15, 2) NOT NULL,
MODIFY `total` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Purchase_Receipt_Payments`
ADD COLUMN `inventoryId` INTEGER NOT NULL,
ADD COLUMN `type` ENUM('PAYMENT', 'REFUND') NOT NULL,
MODIFY `amount` DECIMAL(15, 2) NOT NULL,
MODIFY `bankAccountId` INTEGER NOT NULL;
-- AlterTable
ALTER TABLE `Purchase_Receipts`
DROP COLUMN `isSettled`,
ADD COLUMN `status` ENUM(
'UNPAID',
'PARTIALLY_PAID',
'PAID'
) NOT NULL DEFAULT 'UNPAID',
MODIFY `totalAmount` DECIMAL(15, 2) NOT NULL,
MODIFY `paidAmount` DECIMAL(15, 2) NOT NULL DEFAULT 0.00;
-- AlterTable
ALTER TABLE `Sales_Invoice_Items`
MODIFY `count` DECIMAL(10, 0) NOT NULL,
MODIFY `fee` DECIMAL(15, 2) NOT NULL,
MODIFY `total` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Sales_Invoices`
MODIFY `totalAmount` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Stock_Adjustments`
MODIFY `adjustedQuantity` DECIMAL(10, 0) NOT NULL;
-- AlterTable
ALTER TABLE `Stock_Movements`
MODIFY `quantity` DECIMAL(10, 0) NOT NULL,
MODIFY `fee` DECIMAL(15, 2) NOT NULL,
MODIFY `totalCost` DECIMAL(15, 2) NOT NULL,
MODIFY `avgCost` DECIMAL(15, 2) NOT NULL,
MODIFY `remainedInStock` DECIMAL(10, 0) NOT NULL DEFAULT 0.00;
-- AlterTable
ALTER TABLE `Supplier_Ledger`
MODIFY `debit` DECIMAL(15, 2) NOT NULL DEFAULT 0,
MODIFY `credit` DECIMAL(15, 2) NOT NULL DEFAULT 0,
MODIFY `balance` DECIMAL(15, 2) NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX `Bank_Accounts_iban_key` ON `Bank_Accounts` (`iban`);
-- AddForeignKey
ALTER TABLE `Pos_Accounts`
ADD CONSTRAINT `Pos_Accounts_inventoryId_bankAccountId_fkey` FOREIGN KEY (
`inventoryId`,
`bankAccountId`
) REFERENCES `Inventory_Bank_Accounts` (
`inventoryId`,
`bankAccountId`
) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Purchase_Receipt_Payments`
ADD CONSTRAINT `Purchase_Receipt_Payments_inventoryId_bankAccountId_fkey` FOREIGN KEY (
`inventoryId`,
`bankAccountId`
) REFERENCES `Inventory_Bank_Accounts` (
`inventoryId`,
`bankAccountId`
) ON DELETE RESTRICT ON UPDATE CASCADE;
-- RenameIndex
ALTER TABLE `Supplier_Ledger`
RENAME INDEX `Supplier_Ledger_supplierId_fkey` TO `Supplier_Ledger_supplierId_idx`;
@@ -1,2 +0,0 @@
-- CreateIndex
CREATE INDEX `Pos_Accounts_inventoryId_idx` ON `Pos_Accounts`(`inventoryId`);
@@ -1,14 +0,0 @@
/*
Warnings:
- You are about to drop the column `inventoryId` on the `Purchase_Receipt_Payments` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE `Purchase_Receipt_Payments` DROP FOREIGN KEY `Purchase_Receipt_Payments_inventoryId_bankAccountId_fkey`;
-- DropIndex
DROP INDEX `Purchase_Receipt_Payments_inventoryId_bankAccountId_fkey` ON `Purchase_Receipt_Payments`;
-- AlterTable
ALTER TABLE `Purchase_Receipt_Payments` DROP COLUMN `inventoryId`;
@@ -1,2 +0,0 @@
-- AddForeignKey
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -1,50 +0,0 @@
/*
Warnings:
- You are about to drop the column `inventoryId` on the `Sales_Invoices` table. All the data in the column will be lost.
- Added the required column `posAccountId` to the `Sales_Invoices` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE `Sales_Invoices` DROP FOREIGN KEY `Sales_Invoices_inventoryId_fkey`;
-- DropIndex
DROP INDEX `Sales_Invoices_inventoryId_fkey` ON `Sales_Invoices`;
-- AlterTable
ALTER TABLE `Purchase_Receipt_Payments` ADD COLUMN `inventoryBankAccountBankAccountId` INTEGER NULL,
ADD COLUMN `inventoryBankAccountInventoryId` INTEGER NULL;
-- AlterTable
ALTER TABLE `Sales_Invoices` DROP COLUMN `inventoryId`,
ADD COLUMN `posAccountId` INTEGER NOT NULL;
-- CreateIndex
CREATE INDEX `Sales_Invoices_posAccountId_idx` ON `Sales_Invoices`(`posAccountId`);
-- AddForeignKey
ALTER TABLE `Sales_Invoices` ADD CONSTRAINT `Sales_Invoices_posAccountId_fkey` FOREIGN KEY (`posAccountId`) REFERENCES `Pos_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_inventoryBankAccountInventoryId_i_fkey` FOREIGN KEY (`inventoryBankAccountInventoryId`, `inventoryBankAccountBankAccountId`) REFERENCES `Inventory_Bank_Accounts`(`inventoryId`, `bankAccountId`) ON DELETE SET NULL ON UPDATE CASCADE;
-- RenameIndex
ALTER TABLE `Orders` RENAME INDEX `Orders_customerId_fkey` TO `Orders_customerId_idx`;
-- RenameIndex
ALTER TABLE `Product_Variants` RENAME INDEX `products_barcode_unique` TO `Product_Variants_barcode_key`;
-- RenameIndex
ALTER TABLE `Products` RENAME INDEX `products_barcode_unique` TO `Products_barcode_key`;
-- RenameIndex
ALTER TABLE `Products` RENAME INDEX `products_sku_unique` TO `Products_sku_key`;
-- RenameIndex
ALTER TABLE `Sales_Invoice_Items` RENAME INDEX `Sales_Invoice_Items_invoiceId_fkey` TO `Sales_Invoice_Items_invoiceId_idx`;
-- RenameIndex
ALTER TABLE `Sales_Invoice_Items` RENAME INDEX `Sales_Invoice_Items_productId_fkey` TO `Sales_Invoice_Items_productId_idx`;
-- RenameIndex
ALTER TABLE `Sales_Invoices` RENAME INDEX `Sales_Invoices_customerId_fkey` TO `Sales_Invoices_customerId_idx`;
@@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE `Products` ADD COLUMN `minimumStockAlertLevel` DECIMAL(10, 0) NOT NULL DEFAULT 1.00;
@@ -84,9 +84,35 @@ CREATE TABLE `Bank_Accounts` (
UNIQUE INDEX `Bank_Accounts_accountNumber_key`(`accountNumber`),
UNIQUE INDEX `Bank_Accounts_cardNumber_key`(`cardNumber`),
UNIQUE INDEX `Bank_Accounts_iban_key`(`iban`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Bank_Account_Transactions` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`bankAccountId` INTEGER NOT NULL,
`type` ENUM('DEPOSIT', 'WITHDRAWAL') NOT NULL,
`amount` DECIMAL(15, 2) NOT NULL,
`balanceAfter` DECIMAL(15, 2) NOT NULL,
`referenceId` INTEGER NOT NULL,
`referenceType` ENUM('PURCHASE_PAYMENT', 'PURCHASE_REFUND', 'POS_SALE', 'POS_REFUND', 'BANK_TRANSFER', 'MANUAL_ADJUSTMENT') NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
INDEX `Bank_Account_Transactions_bankAccountId_idx`(`bankAccountId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Bank_Account_Balance` (
`bankAccountId` INTEGER NOT NULL,
`balance` DECIMAL(15, 2) NOT NULL,
`updatedAt` TIMESTAMP(0) NOT NULL,
UNIQUE INDEX `Bank_Account_Balance_bankAccountId_key`(`bankAccountId`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Inventories` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
@@ -97,11 +123,36 @@ CREATE TABLE `Inventories` (
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
`isPointOfSale` BOOLEAN NOT NULL DEFAULT false,
`bankAccountId` INTEGER NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Inventory_Bank_Accounts` (
`inventoryId` INTEGER NOT NULL,
`bankAccountId` INTEGER NOT NULL,
INDEX `Inventory_Bank_Accounts_bankAccountId_idx`(`bankAccountId`),
PRIMARY KEY (`inventoryId`, `bankAccountId`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Pos_Accounts` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`code` VARCHAR(10) NOT NULL,
`description` VARCHAR(500) NULL,
`bankAccountId` INTEGER NOT NULL,
`inventoryId` INTEGER NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
UNIQUE INDEX `Pos_Accounts_code_key`(`code`),
INDEX `Pos_Accounts_inventoryId_idx`(`inventoryId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Inventory_Transfers` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
@@ -120,7 +171,7 @@ CREATE TABLE `Inventory_Transfers` (
-- CreateTable
CREATE TABLE `Inventory_Transfer_Items` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`count` DECIMAL(10, 2) NOT NULL,
`count` DECIMAL(10, 0) NOT NULL,
`productId` INTEGER NOT NULL,
`transferId` INTEGER NOT NULL,
@@ -129,31 +180,6 @@ CREATE TABLE `Inventory_Transfer_Items` (
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Inventory_Bank_Accounts` (
`inventoryId` INTEGER NOT NULL,
`bankAccountId` INTEGER NOT NULL,
INDEX `Inventory_Bank_Accounts_bankAccountId_idx`(`bankAccountId`),
PRIMARY KEY (`inventoryId`, `bankAccountId`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Pos_Accounts` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`code` VARCHAR(10) NOT NULL,
`description` VARCHAR(500) NULL,
`bankAccountId` INTEGER NULL,
`inventoryId` INTEGER NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
UNIQUE INDEX `Pos_Accounts_code_key`(`code`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Banks` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
@@ -168,38 +194,34 @@ CREATE TABLE `Banks` (
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Suppliers` (
CREATE TABLE `Orders` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(255) NOT NULL,
`lastName` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NULL,
`mobileNumber` CHAR(11) NOT NULL,
`address` TEXT NULL,
`city` VARCHAR(100) NULL,
`state` VARCHAR(100) NULL,
`country` VARCHAR(100) NULL,
`isActive` BOOLEAN NOT NULL DEFAULT true,
`orderNumber` VARCHAR(100) NOT NULL,
`status` ENUM('PENDING', 'REJECT', 'DONE') NOT NULL DEFAULT 'PENDING',
`totalAmount` DECIMAL(15, 2) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
`customerId` INTEGER NULL,
UNIQUE INDEX `Suppliers_mobileNumber_key`(`mobileNumber`),
UNIQUE INDEX `Orders_orderNumber_key`(`orderNumber`),
INDEX `Orders_customerId_idx`(`customerId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Supplier_Ledger` (
CREATE TABLE `Order_Items` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`description` TEXT NULL,
`debit` DECIMAL(10, 2) NOT NULL,
`credit` DECIMAL(10, 2) NOT NULL,
`balance` DECIMAL(10, 2) NOT NULL,
`sourceType` ENUM('PURCHASE', 'PAYMENT', 'ADJUSTMENT', 'REFUND') NOT NULL,
`sourceId` INTEGER NOT NULL,
`quantity` DECIMAL(10, 0) NOT NULL,
`unitPrice` DECIMAL(15, 2) NOT NULL,
`totalPrice` DECIMAL(15, 2) NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`supplierId` INTEGER NOT NULL,
`orderId` INTEGER NOT NULL,
`productId` INTEGER NOT NULL,
INDEX `Supplier_Ledger_supplierId_fkey`(`supplierId`),
INDEX `Order_Items_orderId_idx`(`orderId`),
INDEX `Order_Items_productId_idx`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
@@ -223,56 +245,6 @@ CREATE TABLE `Customers` (
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Orders` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`orderNumber` VARCHAR(100) NOT NULL,
`status` ENUM('PENDING', 'REJECT', 'DONE') NOT NULL DEFAULT 'PENDING',
`paymentMethod` ENUM('CASH', 'CARD', 'BANK', 'CHECK', 'OTHER') NOT NULL DEFAULT 'CARD',
`totalAmount` DECIMAL(10, 2) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
`customerId` INTEGER NOT NULL,
UNIQUE INDEX `Orders_orderNumber_key`(`orderNumber`),
INDEX `Orders_customerId_fkey`(`customerId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Sales_Invoices` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`code` VARCHAR(100) NOT NULL,
`totalAmount` DECIMAL(10, 2) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`customerId` INTEGER NULL,
`inventoryId` INTEGER NOT NULL,
UNIQUE INDEX `Sales_Invoices_code_key`(`code`),
INDEX `Sales_Invoices_inventoryId_fkey`(`inventoryId`),
INDEX `Sales_Invoices_customerId_fkey`(`customerId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Sales_Invoice_Items` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`count` DECIMAL(10, 2) NOT NULL,
`fee` DECIMAL(10, 2) NOT NULL,
`total` DECIMAL(10, 2) NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`invoiceId` INTEGER NOT NULL,
`productId` INTEGER NOT NULL,
INDEX `Sales_Invoice_Items_invoiceId_fkey`(`invoiceId`),
INDEX `Sales_Invoice_Items_productId_fkey`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Trigger_Logs` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
@@ -287,14 +259,14 @@ CREATE TABLE `Trigger_Logs` (
CREATE TABLE `Product_Variants` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`basePrice` DECIMAL(10, 2) NOT NULL,
`salePrice` DECIMAL(10, 2) NOT NULL,
`basePrice` DECIMAL(15, 2) NOT NULL,
`salePrice` DECIMAL(15, 2) NOT NULL,
`description` TEXT NULL,
`barcode` VARCHAR(100) NULL,
`imageUrl` VARCHAR(255) NULL,
`unit` VARCHAR(10) NULL,
`quantity` DECIMAL(10, 2) NULL DEFAULT 0.00,
`alertQuantity` DECIMAL(10, 2) NULL DEFAULT 5.00,
`quantity` DECIMAL(10, 0) NULL DEFAULT 0.00,
`alertQuantity` DECIMAL(10, 0) NULL DEFAULT 5.00,
`isActive` BOOLEAN NOT NULL DEFAULT true,
`isFeatured` BOOLEAN NOT NULL DEFAULT false,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
@@ -302,7 +274,7 @@ CREATE TABLE `Product_Variants` (
`deletedAt` TIMESTAMP(0) NULL,
`productId` INTEGER NOT NULL,
UNIQUE INDEX `products_barcode_unique`(`barcode`),
UNIQUE INDEX `Product_Variants_barcode_key`(`barcode`),
INDEX `Product_Variants_productId_fkey`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
@@ -319,10 +291,11 @@ CREATE TABLE `Products` (
`deletedAt` TIMESTAMP(0) NULL,
`brandId` INTEGER NULL,
`categoryId` INTEGER NULL,
`salePrice` DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
`salePrice` DECIMAL(15, 0) NOT NULL DEFAULT 0.00,
`minimumStockAlertLevel` DECIMAL(10, 0) NOT NULL DEFAULT 1.00,
UNIQUE INDEX `products_sku_unique`(`sku`),
UNIQUE INDEX `products_barcode_unique`(`barcode`),
UNIQUE INDEX `Products_sku_key`(`sku`),
UNIQUE INDEX `Products_barcode_key`(`barcode`),
INDEX `Products_brandId_fkey`(`brandId`),
INDEX `Products_categoryId_fkey`(`categoryId`),
PRIMARY KEY (`id`)
@@ -358,12 +331,12 @@ CREATE TABLE `Product_categories` (
CREATE TABLE `Purchase_Receipts` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`code` VARCHAR(100) NOT NULL,
`totalAmount` DECIMAL(10, 2) NOT NULL,
`paidAmount` DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
`isSettled` BOOLEAN NOT NULL DEFAULT false,
`totalAmount` DECIMAL(15, 2) NOT NULL,
`paidAmount` DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`status` ENUM('UNPAID', 'PARTIALLY_PAID', 'PAID') NOT NULL DEFAULT 'UNPAID',
`supplierId` INTEGER NOT NULL,
`inventoryId` INTEGER NOT NULL,
@@ -376,13 +349,13 @@ CREATE TABLE `Purchase_Receipts` (
-- CreateTable
CREATE TABLE `Purchase_Receipt_Items` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`count` DECIMAL(10, 2) NOT NULL,
`fee` DECIMAL(10, 2) NOT NULL,
`total` DECIMAL(10, 2) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`count` DECIMAL(10, 0) NOT NULL,
`unitPrice` DECIMAL(15, 2) NOT NULL,
`totalPrice` DECIMAL(15, 2) NOT NULL,
`receiptId` INTEGER NOT NULL,
`productId` INTEGER NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
INDEX `Purchase_Receipt_Items_productId_fkey`(`productId`),
INDEX `Purchase_Receipt_Items_receiptId_fkey`(`receiptId`),
@@ -392,16 +365,63 @@ CREATE TABLE `Purchase_Receipt_Items` (
-- CreateTable
CREATE TABLE `Purchase_Receipt_Payments` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`amount` DECIMAL(10, 2) NOT NULL,
`amount` DECIMAL(15, 2) NOT NULL,
`paymentMethod` ENUM('CASH', 'CARD', 'BANK', 'CHECK', 'OTHER') NOT NULL,
`bankAccountId` INTEGER NULL,
`description` TEXT NULL,
`payedAt` TIMESTAMP(0) NOT NULL,
`type` ENUM('PAYMENT', 'REFUND') NOT NULL,
`bankAccountId` INTEGER NOT NULL,
`receiptId` INTEGER NOT NULL,
`payedAt` TIMESTAMP(0) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`inventoryBankAccountInventoryId` INTEGER NULL,
`inventoryBankAccountBankAccountId` INTEGER NULL,
INDEX `Purchase_Receipt_Payments_receiptId_fkey`(`receiptId`),
INDEX `Purchase_Receipt_Payments_bankAccountId_fkey`(`bankAccountId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Sales_Invoices` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`code` VARCHAR(100) NOT NULL,
`totalAmount` DECIMAL(15, 2) NOT NULL,
`description` TEXT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`customerId` INTEGER NULL,
`posAccountId` INTEGER NOT NULL,
UNIQUE INDEX `Sales_Invoices_code_key`(`code`),
INDEX `Sales_Invoices_customerId_idx`(`customerId`),
INDEX `Sales_Invoices_posAccountId_idx`(`posAccountId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Sales_Invoice_Items` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`count` DECIMAL(10, 0) NOT NULL,
`unitPrice` DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
`totalPrice` DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`invoiceId` INTEGER NOT NULL,
`productId` INTEGER NOT NULL,
INDEX `Sales_Invoice_Items_invoiceId_idx`(`invoiceId`),
INDEX `Sales_Invoice_Items_productId_idx`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `SalesInvoicePayment` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`invoiceId` INTEGER NOT NULL,
`amount` DECIMAL(15, 2) NOT NULL,
`paymentMethod` ENUM('CASH', 'CARD', 'BANK', 'CHECK', 'OTHER') NOT NULL,
`paidAt` DATETIME(3) NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
INDEX `SalesInvoicePayment_invoiceId_idx`(`invoiceId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
@@ -409,17 +429,17 @@ CREATE TABLE `Purchase_Receipt_Payments` (
CREATE TABLE `Stock_Movements` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`type` ENUM('IN', 'OUT', 'ADJUST') NOT NULL,
`quantity` DECIMAL(10, 2) NOT NULL,
`fee` DECIMAL(10, 2) NOT NULL,
`totalCost` DECIMAL(10, 2) NOT NULL,
`quantity` DECIMAL(10, 0) NOT NULL,
`unitPrice` DECIMAL(15, 2) NOT NULL,
`totalCost` DECIMAL(15, 2) NOT NULL,
`referenceType` ENUM('PURCHASE', 'SALES', 'ADJUSTMENT', 'INVENTORY_TRANSFER') NOT NULL,
`referenceId` VARCHAR(191) NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`productId` INTEGER NOT NULL,
`inventoryId` INTEGER NOT NULL,
`avgCost` DECIMAL(10, 2) NOT NULL,
`avgCost` DECIMAL(15, 2) NOT NULL,
`supplierId` INTEGER NULL,
`remainedInStock` DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
`remainedInStock` DECIMAL(10, 0) NOT NULL DEFAULT 0.00,
`counterInventoryId` INTEGER NULL,
`customerId` INTEGER NULL,
@@ -451,7 +471,7 @@ CREATE TABLE `Stock_Balance` (
-- CreateTable
CREATE TABLE `Stock_Adjustments` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`adjustedQuantity` DECIMAL(10, 2) NOT NULL,
`adjustedQuantity` DECIMAL(10, 0) NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`productId` INTEGER NOT NULL,
`inventoryId` INTEGER NOT NULL,
@@ -462,12 +482,54 @@ CREATE TABLE `Stock_Adjustments` (
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `_Bank_Accounts_inventoryId_fkey` (
`A` INTEGER NOT NULL,
`B` INTEGER NOT NULL,
CREATE TABLE `Stock_Reservations` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`quantity` DECIMAL(10, 0) NOT NULL,
`expiresAt` TIMESTAMP(0) NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`productId` INTEGER NOT NULL,
`inventoryId` INTEGER NOT NULL,
`orderId` INTEGER NOT NULL,
UNIQUE INDEX `_Bank_Accounts_inventoryId_fkey_AB_unique`(`A`, `B`),
INDEX `_Bank_Accounts_inventoryId_fkey_B_index`(`B`)
INDEX `Stock_Reservations_inventoryId_idx`(`inventoryId`),
INDEX `Stock_Reservations_productId_idx`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Suppliers` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(255) NOT NULL,
`lastName` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NULL,
`mobileNumber` CHAR(11) NOT NULL,
`address` TEXT NULL,
`city` VARCHAR(100) NULL,
`state` VARCHAR(100) NULL,
`country` VARCHAR(100) NULL,
`isActive` BOOLEAN NOT NULL DEFAULT true,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`updatedAt` TIMESTAMP(0) NOT NULL,
`deletedAt` TIMESTAMP(0) NULL,
UNIQUE INDEX `Suppliers_mobileNumber_key`(`mobileNumber`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Supplier_Ledger` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`description` TEXT NULL,
`debit` DECIMAL(15, 2) NOT NULL DEFAULT 0,
`credit` DECIMAL(15, 2) NOT NULL DEFAULT 0,
`balance` DECIMAL(15, 2) NOT NULL,
`sourceType` ENUM('PURCHASE', 'PAYMENT', 'ADJUSTMENT', 'REFUND') NOT NULL,
`sourceId` INTEGER NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`supplierId` INTEGER NOT NULL,
INDEX `Supplier_Ledger_supplierId_idx`(`supplierId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
@@ -482,6 +544,21 @@ ALTER TABLE `Bank_Branches` ADD CONSTRAINT `Bank_Branches_bankId_fkey` FOREIGN K
-- AddForeignKey
ALTER TABLE `Bank_Accounts` ADD CONSTRAINT `Bank_Accounts_branchId_fkey` FOREIGN KEY (`branchId`) REFERENCES `Bank_Branches`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Bank_Account_Transactions` ADD CONSTRAINT `Bank_Account_Transactions_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Bank_Account_Balance` ADD CONSTRAINT `Bank_Account_Balance_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Inventory_Bank_Accounts` ADD CONSTRAINT `Inventory_Bank_Accounts_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Inventory_Bank_Accounts` ADD CONSTRAINT `Inventory_Bank_Accounts_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Pos_Accounts` ADD CONSTRAINT `Pos_Accounts_inventoryId_bankAccountId_fkey` FOREIGN KEY (`inventoryId`, `bankAccountId`) REFERENCES `Inventory_Bank_Accounts`(`inventoryId`, `bankAccountId`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Inventory_Transfers` ADD CONSTRAINT `Inventory_Transfers_fromInventoryId_fkey` FOREIGN KEY (`fromInventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -495,37 +572,13 @@ ALTER TABLE `Inventory_Transfer_Items` ADD CONSTRAINT `Inventory_Transfer_Items_
ALTER TABLE `Inventory_Transfer_Items` ADD CONSTRAINT `Inventory_Transfer_Items_transferId_fkey` FOREIGN KEY (`transferId`) REFERENCES `Inventory_Transfers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Inventory_Bank_Accounts` ADD CONSTRAINT `Inventory_Bank_Accounts_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `Orders` ADD CONSTRAINT `Orders_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Inventory_Bank_Accounts` ADD CONSTRAINT `Inventory_Bank_Accounts_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `Order_Items` ADD CONSTRAINT `Order_Items_orderId_fkey` FOREIGN KEY (`orderId`) REFERENCES `Orders`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Pos_Accounts` ADD CONSTRAINT `Pos_Accounts_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Pos_Accounts` ADD CONSTRAINT `Pos_Accounts_bankAccountId_inventoryId_fkey` FOREIGN KEY (`bankAccountId`, `inventoryId`) REFERENCES `Inventory_Bank_Accounts`(`bankAccountId`, `inventoryId`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Pos_Accounts` ADD CONSTRAINT `Pos_Accounts_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Supplier_Ledger` ADD CONSTRAINT `Supplier_Ledger_supplierId_fkey` FOREIGN KEY (`supplierId`) REFERENCES `Suppliers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Orders` ADD CONSTRAINT `Orders_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Sales_Invoices` ADD CONSTRAINT `Sales_Invoices_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoices` ADD CONSTRAINT `Sales_Invoices_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoice_Items` ADD CONSTRAINT `Sales_Invoice_Items_invoiceId_fkey` FOREIGN KEY (`invoiceId`) REFERENCES `Sales_Invoices`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoice_Items` ADD CONSTRAINT `Sales_Invoice_Items_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `Order_Items` ADD CONSTRAINT `Order_Items_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Product_Variants` ADD CONSTRAINT `Product_Variants_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
@@ -552,7 +605,25 @@ ALTER TABLE `Purchase_Receipt_Items` ADD CONSTRAINT `Purchase_Receipt_Items_rece
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_receiptId_fkey` FOREIGN KEY (`receiptId`) REFERENCES `Purchase_Receipts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_bankAccountId_fkey` FOREIGN KEY (`bankAccountId`) REFERENCES `Bank_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Purchase_Receipt_Payments` ADD CONSTRAINT `Purchase_Receipt_Payments_inventoryBankAccountInventoryId_i_fkey` FOREIGN KEY (`inventoryBankAccountInventoryId`, `inventoryBankAccountBankAccountId`) REFERENCES `Inventory_Bank_Accounts`(`inventoryId`, `bankAccountId`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoices` ADD CONSTRAINT `Sales_Invoices_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoices` ADD CONSTRAINT `Sales_Invoices_posAccountId_fkey` FOREIGN KEY (`posAccountId`) REFERENCES `Pos_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoice_Items` ADD CONSTRAINT `Sales_Invoice_Items_invoiceId_fkey` FOREIGN KEY (`invoiceId`) REFERENCES `Sales_Invoices`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Sales_Invoice_Items` ADD CONSTRAINT `Sales_Invoice_Items_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `SalesInvoicePayment` ADD CONSTRAINT `SalesInvoicePayment_invoiceId_fkey` FOREIGN KEY (`invoiceId`) REFERENCES `Sales_Invoices`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Stock_Movements` ADD CONSTRAINT `Stock_Movements_counterInventoryId_fkey` FOREIGN KEY (`counterInventoryId`) REFERENCES `Inventories`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
@@ -582,7 +653,10 @@ ALTER TABLE `Stock_Adjustments` ADD CONSTRAINT `Stock_Adjustments_inventoryId_fk
ALTER TABLE `Stock_Adjustments` ADD CONSTRAINT `Stock_Adjustments_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `_Bank_Accounts_inventoryId_fkey` ADD CONSTRAINT `_Bank_Accounts_inventoryId_fkey_A_fkey` FOREIGN KEY (`A`) REFERENCES `Bank_Accounts`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `Stock_Reservations` ADD CONSTRAINT `Stock_Reservations_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `_Bank_Accounts_inventoryId_fkey` ADD CONSTRAINT `_Bank_Accounts_inventoryId_fkey_B_fkey` FOREIGN KEY (`B`) REFERENCES `Inventories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `Stock_Reservations` ADD CONSTRAINT `Stock_Reservations_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Supplier_Ledger` ADD CONSTRAINT `Supplier_Ledger_supplierId_fkey` FOREIGN KEY (`supplierId`) REFERENCES `Suppliers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,21 @@
/*
Warnings:
- You are about to drop the column `totalPrice` on the `Order_Items` table. All the data in the column will be lost.
- You are about to drop the column `totalPrice` on the `Purchase_Receipt_Items` table. All the data in the column will be lost.
- You are about to drop the column `totalPrice` on the `Sales_Invoice_Items` table. All the data in the column will be lost.
- Added the required column `totalAmount` to the `Order_Items` table without a default value. This is not possible if the table is not empty.
- Added the required column `totalAmount` to the `Purchase_Receipt_Items` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `Order_Items` DROP COLUMN `totalPrice`,
ADD COLUMN `totalAmount` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Purchase_Receipt_Items` DROP COLUMN `totalPrice`,
ADD COLUMN `totalAmount` DECIMAL(15, 2) NOT NULL;
-- AlterTable
ALTER TABLE `Sales_Invoice_Items` DROP COLUMN `totalPrice`,
ADD COLUMN `totalAmount` DECIMAL(15, 2) NOT NULL DEFAULT 0.00;
@@ -0,0 +1,27 @@
/*
Warnings:
- You are about to drop the `SalesInvoicePayment` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE `SalesInvoicePayment` DROP FOREIGN KEY `SalesInvoicePayment_invoiceId_fkey`;
-- DropTable
DROP TABLE `SalesInvoicePayment`;
-- CreateTable
CREATE TABLE `Sales_Invoice_Payments` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`invoiceId` INTEGER NOT NULL,
`amount` DECIMAL(15, 2) NOT NULL,
`paymentMethod` ENUM('CASH', 'CARD', 'BANK', 'CHECK', 'OTHER') NOT NULL,
`paidAt` DATETIME(3) NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
INDEX `Sales_Invoice_Payments_invoiceId_idx`(`invoiceId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `Sales_Invoice_Payments` ADD CONSTRAINT `Sales_Invoice_Payments_invoiceId_fkey` FOREIGN KEY (`invoiceId`) REFERENCES `Sales_Invoices`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,8 @@
/*
Warnings:
- The values [REJECT] on the enum `Orders_status` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterTable
ALTER TABLE `Orders` MODIFY `status` ENUM('PENDING', 'REJECTED', 'CANCELED', 'DONE') NOT NULL DEFAULT 'PENDING';
+10 -10
View File
@@ -25,13 +25,13 @@ DECLARE fromInv INT;
-- OUT from source
INSERT INTO Stock_Movements
(type, quantity, fee, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
(type, quantity, unitPrice, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
VALUES
('OUT', NEW.count, _avgCost, _avgCost*NEW.count, _avgCost, 'INVENTORY_TRANSFER', NEW.transferId, NEW.productId, fromInv, toInv, NOW(), latestQuantityInOrigin-NEW.count);
-- IN to destination
INSERT INTO Stock_Movements
(type, quantity, fee, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
(type, quantity, unitPrice, totalCost, avgCost, referenceType, referenceId, productId, inventoryId, counterInventoryId, createdAt, remainedInStock)
VALUES
('IN', NEW.count,_avgCost, _avgCost*NEW.count,_avgCost, 'INVENTORY_TRANSFER', NEW.transferId, NEW.productId, toInv, fromInv, NOW(), latestQuantityInOrigin-NEW.count);
end;
@@ -66,7 +66,7 @@ DECLARE invId INT;
INSERT INTO Stock_Movements (
type,
quantity,
fee,
unitPrice,
totalCost,
referenceType,
referenceId,
@@ -80,7 +80,7 @@ DECLARE invId INT;
VALUES (
'IN',
NEW.count,
NEW.fee,
NEW.unitPrice,
NEW.total,
'PURCHASE',
NEW.receiptId,
@@ -157,7 +157,7 @@ DECLARE customer_id INT;
INSERT INTO Stock_Movements (
type,
quantity,
fee,
unitPrice,
totalCost,
referenceType,
referenceId,
@@ -171,7 +171,7 @@ DECLARE customer_id INT;
VALUES (
'OUT',
NEW.count,
NEW.fee,
NEW.unitPrice,
NEW.total,
'SALES',
NEW.invoiceId,
@@ -260,8 +260,8 @@ VALUES (
NEW.productId,
NEW.inventoryId,
- NEW.quantity,
- COALESCE(NEW.fee, 0) * NEW.quantity,
COALESCE(NEW.fee, 0),
- COALESCE(NEW.unitPrice, 0) * NEW.quantity,
COALESCE(NEW.unitPrice, 0),
NOW()
);
@@ -294,7 +294,7 @@ INSERT INTO
VALUES (
NEW.productId,
NEW.quantity,
NEW.fee,
NEW.unitPrice,
NEW.totalCost,
NEW.inventoryId,
NOW()
@@ -329,7 +329,7 @@ INSERT INTO
VALUES (
NEW.productId,
NEW.quantity,
NEW.fee,
NEW.unitPrice,
NEW.totalCost,
NEW.inventoryId,
NOW()