init
This commit is contained in:
@@ -1,662 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `Users` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`mobileNumber` CHAR(11) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`firstName` VARCHAR(191) NOT NULL,
|
||||
`lastName` VARCHAR(191) NOT NULL,
|
||||
`roleId` INTEGER NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Users_mobileNumber_key`(`mobileNumber`),
|
||||
INDEX `Users_roleId_fkey`(`roleId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Roles` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`permissions` JSON NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
UNIQUE INDEX `Roles_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Otp_Codes` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`mobileNumber` VARCHAR(20) NOT NULL,
|
||||
`code` VARCHAR(10) NOT NULL,
|
||||
`used` BOOLEAN NOT NULL DEFAULT false,
|
||||
`expiresAt` TIMESTAMP(0) NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
|
||||
INDEX `Otp_Codes_mobileNumber_idx`(`mobileNumber`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Refresh_Tokens` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`tokenHash` VARCHAR(255) NOT NULL,
|
||||
`userId` INTEGER NOT NULL,
|
||||
`revoked` BOOLEAN NOT NULL DEFAULT false,
|
||||
`expiresAt` TIMESTAMP(0) NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
|
||||
INDEX `Refresh_Tokens_userId_idx`(`userId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Bank_Branches` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`code` VARCHAR(10) NOT NULL,
|
||||
`address` VARCHAR(500) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`bankId` INTEGER NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Bank_Branches_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Bank_Accounts` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`accountNumber` VARCHAR(20) NULL,
|
||||
`cardNumber` VARCHAR(16) NULL,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`iban` VARCHAR(34) NULL,
|
||||
`branchId` INTEGER NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
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,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`location` VARCHAR(255) 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,
|
||||
`isPointOfSale` BOOLEAN NOT NULL DEFAULT false,
|
||||
|
||||
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,
|
||||
`code` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`fromInventoryId` INTEGER NOT NULL,
|
||||
`toInventoryId` INTEGER NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Inventory_Transfers_code_key`(`code`),
|
||||
INDEX `Inventory_Transfers_fromInventoryId_fkey`(`fromInventoryId`),
|
||||
INDEX `Inventory_Transfers_toInventoryId_fkey`(`toInventoryId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Inventory_Transfer_Items` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`count` DECIMAL(10, 0) NOT NULL,
|
||||
`productId` INTEGER NOT NULL,
|
||||
`transferId` INTEGER NOT NULL,
|
||||
|
||||
INDEX `Inventory_Transfer_Items_productId_fkey`(`productId`),
|
||||
INDEX `Inventory_Transfer_Items_transferId_fkey`(`transferId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Banks` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`shortName` VARCHAR(3) NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
UNIQUE INDEX `Banks_shortName_key`(`shortName`),
|
||||
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',
|
||||
`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 `Orders_orderNumber_key`(`orderNumber`),
|
||||
INDEX `Orders_customerId_idx`(`customerId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Order_Items` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`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),
|
||||
`orderId` INTEGER NOT NULL,
|
||||
`productId` INTEGER NOT NULL,
|
||||
|
||||
INDEX `Order_Items_orderId_idx`(`orderId`),
|
||||
INDEX `Order_Items_productId_idx`(`productId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Customers` (
|
||||
`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 `Customers_mobileNumber_key`(`mobileNumber`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Trigger_Logs` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`message` TEXT NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`name` TEXT NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Product_Variants` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) 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, 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),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`productId` INTEGER NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Product_Variants_barcode_key`(`barcode`),
|
||||
INDEX `Product_Variants_productId_fkey`(`productId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Products` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`sku` VARCHAR(100) NULL,
|
||||
`barcode` VARCHAR(100) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`brandId` INTEGER NULL,
|
||||
`categoryId` INTEGER NULL,
|
||||
`salePrice` DECIMAL(15, 0) NOT NULL DEFAULT 0.00,
|
||||
`minimumStockAlertLevel` DECIMAL(10, 0) NOT NULL DEFAULT 1.00,
|
||||
|
||||
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`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Product_brands` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`imageUrl` VARCHAR(255) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Product_categories` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`imageUrl` VARCHAR(255) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Purchase_Receipts` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(100) NOT NULL,
|
||||
`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,
|
||||
|
||||
UNIQUE INDEX `Purchase_Receipts_code_key`(`code`),
|
||||
INDEX `Purchase_Receipts_inventoryId_fkey`(`inventoryId`),
|
||||
INDEX `Purchase_Receipts_supplierId_fkey`(`supplierId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Purchase_Receipt_Items` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`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`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Purchase_Receipt_Payments` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`amount` DECIMAL(15, 2) NOT NULL,
|
||||
`paymentMethod` ENUM('CASH', 'CARD', 'BANK', 'CHECK', 'OTHER') 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`),
|
||||
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;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Stock_Movements` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`type` ENUM('IN', 'OUT', 'ADJUST') 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(15, 2) NOT NULL,
|
||||
`supplierId` INTEGER NULL,
|
||||
`remainedInStock` DECIMAL(10, 0) NOT NULL DEFAULT 0.00,
|
||||
`counterInventoryId` INTEGER NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
|
||||
INDEX `Stock_Movements_inventoryId_fkey`(`inventoryId`),
|
||||
INDEX `Stock_Movements_productId_fkey`(`productId`),
|
||||
INDEX `Stock_Movements_counterInventoryId_fkey`(`counterInventoryId`),
|
||||
INDEX `Stock_Movements_customerId_fkey`(`customerId`),
|
||||
INDEX `Stock_Movements_supplierId_fkey`(`supplierId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Stock_Balance` (
|
||||
`quantity` DECIMAL(14, 3) NOT NULL DEFAULT 0.000,
|
||||
`totalCost` DECIMAL(14, 2) NOT NULL DEFAULT 0.00,
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`avgCost` DECIMAL(14, 2) NOT NULL DEFAULT 0.00,
|
||||
`inventoryId` INTEGER NOT NULL,
|
||||
`productId` INTEGER NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
|
||||
INDEX `Stock_Balance_productId_idx`(`productId`),
|
||||
INDEX `Stock_Balance_inventoryId_idx`(`inventoryId`),
|
||||
UNIQUE INDEX `Stock_Balance_productId_inventoryId_key`(`productId`, `inventoryId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Stock_Adjustments` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`adjustedQuantity` DECIMAL(10, 0) NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`productId` INTEGER NOT NULL,
|
||||
`inventoryId` INTEGER NOT NULL,
|
||||
|
||||
INDEX `Stock_Adjustments_inventoryId_fkey`(`inventoryId`),
|
||||
INDEX `Stock_Adjustments_productId_fkey`(`productId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
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,
|
||||
|
||||
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
|
||||
ALTER TABLE `Users` ADD CONSTRAINT `Users_roleId_fkey` FOREIGN KEY (`roleId`) REFERENCES `Roles`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Refresh_Tokens` ADD CONSTRAINT `Refresh_Tokens_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `Users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Bank_Branches` ADD CONSTRAINT `Bank_Branches_bankId_fkey` FOREIGN KEY (`bankId`) REFERENCES `Banks`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- 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;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Inventory_Transfers` ADD CONSTRAINT `Inventory_Transfers_toInventoryId_fkey` FOREIGN KEY (`toInventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Inventory_Transfer_Items` ADD CONSTRAINT `Inventory_Transfer_Items_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
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 `Orders` ADD CONSTRAINT `Orders_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
|
||||
|
||||
-- AddForeignKey
|
||||
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 `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;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Products` ADD CONSTRAINT `Products_brandId_fkey` FOREIGN KEY (`brandId`) REFERENCES `Product_brands`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Products` ADD CONSTRAINT `Products_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `Product_categories`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Purchase_Receipts` ADD CONSTRAINT `Purchase_Receipts_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Purchase_Receipts` ADD CONSTRAINT `Purchase_Receipts_supplierId_fkey` FOREIGN KEY (`supplierId`) REFERENCES `Suppliers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Purchase_Receipt_Items` ADD CONSTRAINT `Purchase_Receipt_Items_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Purchase_Receipt_Items` ADD CONSTRAINT `Purchase_Receipt_Items_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_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 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;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Movements` ADD CONSTRAINT `Stock_Movements_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Movements` ADD CONSTRAINT `Stock_Movements_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Movements` ADD CONSTRAINT `Stock_Movements_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Movements` ADD CONSTRAINT `Stock_Movements_supplierId_fkey` FOREIGN KEY (`supplierId`) REFERENCES `Suppliers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Balance` ADD CONSTRAINT `Stock_Balance_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Balance` ADD CONSTRAINT `Stock_Balance_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Adjustments` ADD CONSTRAINT `Stock_Adjustments_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
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 `Stock_Reservations` ADD CONSTRAINT `Stock_Reservations_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
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;
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
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;
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
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;
|
||||
@@ -1,8 +0,0 @@
|
||||
/*
|
||||
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';
|
||||
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the `Bank_Account_Balance` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropForeignKey
|
||||
ALTER TABLE `Bank_Account_Balance` DROP FOREIGN KEY `Bank_Account_Balance_bankAccountId_fkey`;
|
||||
|
||||
-- DropTable
|
||||
DROP TABLE `Bank_Account_Balance`;
|
||||
@@ -1,17 +0,0 @@
|
||||
CREATE OR REPLACE VIEW Stock_Available_View AS
|
||||
SELECT
|
||||
sb.productId,
|
||||
sb.inventoryId,
|
||||
sb.quantity AS physicalQuantity,
|
||||
COALESCE(SUM(sr.quantity), 0) AS reservedQuantity,
|
||||
(
|
||||
sb.quantity - COALESCE(SUM(sr.quantity), 0)
|
||||
) AS availableQuantity
|
||||
FROM
|
||||
Stock_Balance sb
|
||||
LEFT JOIN Stock_Reservations sr ON sr.productId = sb.productId
|
||||
AND sr.inventoryId = sb.inventoryId
|
||||
GROUP BY
|
||||
sb.productId,
|
||||
sb.inventoryId,
|
||||
sb.quantity;
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `expiresAt` on the `Stock_Reservations` table. All the data in the column will be lost.
|
||||
- Added the required column `posAccountId` to the `Orders` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE `Orders` ADD COLUMN `posAccountId` INTEGER NOT NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `Stock_Reservations` DROP COLUMN `expiresAt`;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX `Orders_posAccountId_idx` ON `Orders`(`posAccountId`);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Orders` ADD CONSTRAINT `Orders_posAccountId_fkey` FOREIGN KEY (`posAccountId`) REFERENCES `Pos_Accounts`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Stock_Reservations` ADD CONSTRAINT `Stock_Reservations_orderId_fkey` FOREIGN KEY (`orderId`) REFERENCES `Orders`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,155 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `Goods` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`sku` VARCHAR(100) NOT NULL,
|
||||
`localSku` VARCHAR(100) NOT NULL,
|
||||
`barcode` VARCHAR(100) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`categoryId` INTEGER NULL,
|
||||
`baseSalePrice` DECIMAL(15, 0) NOT NULL DEFAULT 0.00,
|
||||
|
||||
UNIQUE INDEX `Goods_localSku_key`(`localSku`),
|
||||
UNIQUE INDEX `Goods_barcode_key`(`barcode`),
|
||||
INDEX `Goods_categoryId_idx`(`categoryId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Good_categories` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`imageUrl` VARCHAR(255) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Customers` (
|
||||
`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,
|
||||
`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 `Customers_mobileNumber_key`(`mobileNumber`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Trigger_Logs` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`message` TEXT NOT NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`name` TEXT NOT NULL,
|
||||
|
||||
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,
|
||||
|
||||
UNIQUE INDEX `Sales_Invoices_code_key`(`code`),
|
||||
INDEX `Sales_Invoices_customerId_idx`(`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, 0) NOT NULL,
|
||||
`unitPrice` DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
|
||||
`totalAmount` DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`invoiceId` INTEGER NOT NULL,
|
||||
`goodId` INTEGER NOT NULL,
|
||||
`serviceId` INTEGER NOT NULL,
|
||||
|
||||
INDEX `Sales_Invoice_Items_invoiceId_goodId_idx`(`invoiceId`, `goodId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- 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;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Services` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`sku` VARCHAR(100) NOT NULL,
|
||||
`barcode` VARCHAR(100) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
`categoryId` INTEGER NULL,
|
||||
`baseSalePrice` DECIMAL(15, 0) NOT NULL DEFAULT 0.00,
|
||||
|
||||
UNIQUE INDEX `Services_sku_key`(`sku`),
|
||||
UNIQUE INDEX `Services_barcode_key`(`barcode`),
|
||||
INDEX `Services_categoryId_idx`(`categoryId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Service_categories` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`imageUrl` VARCHAR(255) NULL,
|
||||
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
|
||||
`updatedAt` TIMESTAMP(0) NOT NULL,
|
||||
`deletedAt` TIMESTAMP(0) NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Goods` ADD CONSTRAINT `Goods_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `Good_categories`(`id`) 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_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_goodId_fkey` FOREIGN KEY (`goodId`) REFERENCES `Goods`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Sales_Invoice_Items` ADD CONSTRAINT `Sales_Invoice_Items_serviceId_fkey` FOREIGN KEY (`serviceId`) REFERENCES `Services`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- 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;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Services` ADD CONSTRAINT `Services_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `Service_categories`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,344 +0,0 @@
|
||||
-- ------------------------------------------
|
||||
-- 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, 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, 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;
|
||||
|
||||
-- ------------------------------------------
|
||||
-- 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,
|
||||
unitPrice,
|
||||
totalCost,
|
||||
referenceType,
|
||||
referenceId,
|
||||
productId,
|
||||
inventoryId,
|
||||
avgCost,
|
||||
supplierId,
|
||||
remainedInStock,
|
||||
createdAt
|
||||
)
|
||||
VALUES (
|
||||
'IN',
|
||||
NEW.count,
|
||||
NEW.unitPrice,
|
||||
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,
|
||||
unitPrice,
|
||||
totalCost,
|
||||
referenceType,
|
||||
referenceId,
|
||||
productId,
|
||||
inventoryId,
|
||||
avgCost,
|
||||
remainedInStock,
|
||||
customerId,
|
||||
createdAt
|
||||
)
|
||||
VALUES (
|
||||
'OUT',
|
||||
NEW.count,
|
||||
NEW.unitPrice,
|
||||
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.unitPrice, 0) * NEW.quantity,
|
||||
COALESCE(NEW.unitPrice, 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.unitPrice,
|
||||
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.unitPrice,
|
||||
NEW.totalCost,
|
||||
NEW.inventoryId,
|
||||
NOW()
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
quantity = quantity - NEW.quantity,
|
||||
totalCost = totalCost - NEW.totalCost,
|
||||
avgCost = totalCost / quantity;
|
||||
|
||||
END IF;
|
||||
|
||||
END;
|
||||
@@ -1,54 +0,0 @@
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
mobileNumber String @unique @db.Char(11)
|
||||
password String
|
||||
firstName String
|
||||
lastName String
|
||||
roleId Int
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
refreshTokens RefreshToken[]
|
||||
role Role @relation("User_Role", fields: [roleId], references: [id], onUpdate: NoAction)
|
||||
|
||||
@@index([roleId], map: "Users_roleId_fkey")
|
||||
@@map("Users")
|
||||
}
|
||||
|
||||
model Role {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @unique @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
permissions Json?
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
users User[] @relation("User_Role")
|
||||
|
||||
@@map("Roles")
|
||||
}
|
||||
|
||||
model OtpCode {
|
||||
id Int @id @default(autoincrement())
|
||||
mobileNumber String @db.VarChar(20)
|
||||
code String @db.VarChar(10)
|
||||
used Boolean @default(false)
|
||||
expiresAt DateTime @db.Timestamp(0)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
|
||||
@@index([mobileNumber])
|
||||
@@map("Otp_Codes")
|
||||
}
|
||||
|
||||
model RefreshToken {
|
||||
id Int @id @default(autoincrement())
|
||||
tokenHash String @db.VarChar(255)
|
||||
userId Int
|
||||
revoked Boolean @default(false)
|
||||
expiresAt DateTime @db.Timestamp(0)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([userId])
|
||||
@@map("Refresh_Tokens")
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
model BankBranch {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
code String @unique() @db.VarChar(10)
|
||||
address String? @db.VarChar(500)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
bankId Int
|
||||
|
||||
bank Bank @relation("bank_branches", fields: [bankId], references: [id])
|
||||
bankAccounts BankAccount[] @relation("Bank_Accounts_branchId_fkey")
|
||||
|
||||
@@map("Bank_Branches")
|
||||
}
|
||||
|
||||
model BankAccount {
|
||||
id Int @id @default(autoincrement())
|
||||
accountNumber String? @unique @db.VarChar(20)
|
||||
cardNumber String? @unique @db.VarChar(16)
|
||||
name String @db.VarChar(255)
|
||||
iban String? @unique @db.VarChar(34)
|
||||
branchId Int
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
|
||||
branch BankBranch @relation("Bank_Accounts_branchId_fkey", fields: [branchId], references: [id])
|
||||
inventoryBankAccounts InventoryBankAccount[]
|
||||
purchaseReceiptPayments PurchaseReceiptPayments[]
|
||||
bankAccountTransactions BankAccountTransaction[]
|
||||
|
||||
@@map("Bank_Accounts")
|
||||
}
|
||||
|
||||
model BankAccountTransaction {
|
||||
id Int @id @default(autoincrement())
|
||||
bankAccountId Int
|
||||
type BankAccountTransactionType
|
||||
amount Decimal @db.Decimal(15, 2)
|
||||
balanceAfter Decimal @db.Decimal(15, 2)
|
||||
referenceId Int
|
||||
referenceType BankTransactionRefType
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
|
||||
bankAccount BankAccount @relation(fields: [bankAccountId], references: [id])
|
||||
|
||||
@@index([bankAccountId])
|
||||
@@map("Bank_Account_Transactions")
|
||||
}
|
||||
@@ -1,23 +1,3 @@
|
||||
enum OrderStatus {
|
||||
PENDING
|
||||
REJECTED
|
||||
CANCELED
|
||||
DONE
|
||||
}
|
||||
|
||||
enum MovementType {
|
||||
IN
|
||||
OUT
|
||||
ADJUST
|
||||
}
|
||||
|
||||
enum MovementReferenceType {
|
||||
PURCHASE
|
||||
SALES
|
||||
ADJUSTMENT
|
||||
INVENTORY_TRANSFER
|
||||
}
|
||||
|
||||
enum PaymentMethodType {
|
||||
CASH
|
||||
CARD
|
||||
@@ -26,34 +6,8 @@ enum PaymentMethodType {
|
||||
OTHER
|
||||
}
|
||||
|
||||
enum LedgerSourceType {
|
||||
PURCHASE
|
||||
PAYMENT
|
||||
ADJUSTMENT
|
||||
REFUND
|
||||
}
|
||||
|
||||
enum PaymentType {
|
||||
PAYMENT
|
||||
REFUND
|
||||
}
|
||||
|
||||
enum PurchaseReceiptStatus {
|
||||
UNPAID
|
||||
PARTIALLY_PAID
|
||||
PAID
|
||||
}
|
||||
|
||||
enum BankAccountTransactionType {
|
||||
DEPOSIT
|
||||
WITHDRAWAL
|
||||
}
|
||||
|
||||
enum BankTransactionRefType {
|
||||
PURCHASE_PAYMENT
|
||||
PURCHASE_REFUND
|
||||
POS_SALE
|
||||
POS_REFUND
|
||||
BANK_TRANSFER
|
||||
MANUAL_ADJUSTMENT
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
model Good {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
sku String @db.VarChar(100)
|
||||
localSku String @unique() @db.VarChar(100)
|
||||
barcode String? @unique() @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
categoryId Int?
|
||||
baseSalePrice Decimal @default(0.00) @db.Decimal(15, 0)
|
||||
category GoodCategory? @relation(fields: [categoryId], references: [id])
|
||||
salesInvoiceItems SalesInvoiceItem[]
|
||||
|
||||
@@index([categoryId])
|
||||
@@map("Goods")
|
||||
}
|
||||
|
||||
model GoodCategory {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
imageUrl String? @db.VarChar(255)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
goods Good[]
|
||||
|
||||
@@map("Good_categories")
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
model Inventory {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
location String? @db.VarChar(255)
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
isPointOfSale Boolean @default(false)
|
||||
inventoryTransfersFrom InventoryTransfer[] @relation("Inventory_From")
|
||||
inventoryTransfersTo InventoryTransfer[] @relation("Inventory_To")
|
||||
purchaseReceipts PurchaseReceipt[]
|
||||
stockAdjustments StockAdjustment[] @relation("Inventory_Stock_Adjustments")
|
||||
stockBalances StockBalance[] @relation("StockBalance_inventory")
|
||||
counterStockMovements StockMovement[] @relation("StockMovement_CounterInventory")
|
||||
stockMovements StockMovement[] @relation("StockMovement_Inventory")
|
||||
inventoryBankAccounts InventoryBankAccount[]
|
||||
stockReservations StockReservation[]
|
||||
|
||||
@@map("Inventories")
|
||||
}
|
||||
|
||||
model InventoryBankAccount {
|
||||
inventoryId Int
|
||||
bankAccountId Int
|
||||
|
||||
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
||||
bankAccount BankAccount @relation(fields: [bankAccountId], references: [id])
|
||||
posAccounts PosAccount[]
|
||||
purchaseReceiptPayments PurchaseReceiptPayments[]
|
||||
|
||||
@@id([inventoryId, bankAccountId])
|
||||
@@index([bankAccountId])
|
||||
@@map("Inventory_Bank_Accounts")
|
||||
}
|
||||
|
||||
model PosAccount {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
code String @unique() @db.VarChar(10)
|
||||
description String? @db.VarChar(500)
|
||||
bankAccountId Int
|
||||
inventoryId Int
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
|
||||
inventoryBankAccount InventoryBankAccount @relation(fields: [inventoryId, bankAccountId], references: [inventoryId, bankAccountId])
|
||||
salesInvoices SalesInvoice[]
|
||||
orders Order[]
|
||||
|
||||
@@index([inventoryId])
|
||||
@@map("Pos_Accounts")
|
||||
}
|
||||
|
||||
model InventoryTransfer {
|
||||
id Int @id @default(autoincrement())
|
||||
code String @unique @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
fromInventoryId Int
|
||||
toInventoryId Int
|
||||
items InventoryTransferItem[] @relation("InventoryTransfer_Items")
|
||||
fromInventory Inventory @relation("Inventory_From", fields: [fromInventoryId], references: [id])
|
||||
toInventory Inventory @relation("Inventory_To", fields: [toInventoryId], references: [id])
|
||||
|
||||
@@index([fromInventoryId], map: "Inventory_Transfers_fromInventoryId_fkey")
|
||||
@@index([toInventoryId], map: "Inventory_Transfers_toInventoryId_fkey")
|
||||
@@map("Inventory_Transfers")
|
||||
}
|
||||
|
||||
model InventoryTransferItem {
|
||||
id Int @id @default(autoincrement())
|
||||
count Decimal @db.Decimal(10, 0)
|
||||
productId Int
|
||||
transferId Int
|
||||
product Product @relation("InventoryTransferItem_Product", fields: [productId], references: [id])
|
||||
transfer InventoryTransfer @relation("InventoryTransfer_Items", fields: [transferId], references: [id])
|
||||
|
||||
@@index([productId], map: "Inventory_Transfer_Items_productId_fkey")
|
||||
@@index([transferId], map: "Inventory_Transfer_Items_transferId_fkey")
|
||||
@@map("Inventory_Transfer_Items")
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
model Bank {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
shortName String @unique() @db.VarChar(3)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
|
||||
bankBranches BankBranch[] @relation("bank_branches")
|
||||
|
||||
@@map("Banks")
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
model Order {
|
||||
id Int @id @default(autoincrement())
|
||||
orderNumber String @unique @db.VarChar(100)
|
||||
status OrderStatus @default(PENDING)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
customerId Int?
|
||||
posAccountId Int
|
||||
|
||||
customer Customer? @relation(fields: [customerId], references: [id], onUpdate: NoAction)
|
||||
posAccount PosAccount @relation(fields: [posAccountId], references: [id], onUpdate: NoAction)
|
||||
|
||||
orderItems OrderItem[]
|
||||
stockReservations StockReservation[]
|
||||
|
||||
@@index([posAccountId])
|
||||
@@index([customerId])
|
||||
@@map("Orders")
|
||||
}
|
||||
|
||||
model OrderItem {
|
||||
id Int @id @default(autoincrement())
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
orderId Int
|
||||
productId Int
|
||||
|
||||
order Order @relation(fields: [orderId], references: [id], onUpdate: NoAction)
|
||||
product Product @relation(fields: [productId], references: [id], onUpdate: NoAction)
|
||||
|
||||
@@index([orderId])
|
||||
@@index([productId])
|
||||
@@map("Order_Items")
|
||||
}
|
||||
+11
-16
@@ -1,20 +1,15 @@
|
||||
model Customer {
|
||||
id Int @id @default(autoincrement())
|
||||
firstName String @db.VarChar(255)
|
||||
lastName String @db.VarChar(255)
|
||||
email String? @db.VarChar(255)
|
||||
mobileNumber String @unique @db.Char(11)
|
||||
address String? @db.Text
|
||||
city String? @db.VarChar(100)
|
||||
state String? @db.VarChar(100)
|
||||
country String? @db.VarChar(100)
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
orders Order[] @relation()
|
||||
stockMovements StockMovement[] @relation()
|
||||
salesInvoices SalesInvoice[]
|
||||
id Int @id @default(autoincrement())
|
||||
firstName String @db.VarChar(255)
|
||||
lastName String @db.VarChar(255)
|
||||
email String? @db.VarChar(255)
|
||||
mobileNumber String @unique @db.Char(11)
|
||||
address String? @db.Text
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
salesInvoices SalesInvoice[]
|
||||
|
||||
@@map("Customers")
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
model ProductVariant {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
basePrice Decimal @db.Decimal(15, 2)
|
||||
salePrice Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
barcode String? @unique() @db.VarChar(100)
|
||||
imageUrl String? @db.VarChar(255)
|
||||
unit String? @db.VarChar(10)
|
||||
quantity Decimal? @default(0.00) @db.Decimal(10, 0)
|
||||
alertQuantity Decimal? @default(5.00) @db.Decimal(10, 0)
|
||||
isActive Boolean @default(true)
|
||||
isFeatured Boolean @default(false)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
productId Int
|
||||
product Product @relation("Product_Variant", fields: [productId], references: [id], onUpdate: NoAction)
|
||||
|
||||
@@index([productId], map: "Product_Variants_productId_fkey")
|
||||
@@map("Product_Variants")
|
||||
}
|
||||
|
||||
model Product {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
sku String? @unique() @db.VarChar(100)
|
||||
barcode String? @unique() @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
brandId Int?
|
||||
categoryId Int?
|
||||
salePrice Decimal @default(0.00) @db.Decimal(15, 0)
|
||||
minimumStockAlertLevel Decimal @default(1.00) @db.Decimal(10, 0)
|
||||
inventoryTransferItems InventoryTransferItem[] @relation("InventoryTransferItem_Product")
|
||||
variants ProductVariant[] @relation("Product_Variant")
|
||||
brand ProductBrand? @relation("Product_Brand", fields: [brandId], references: [id], onUpdate: NoAction)
|
||||
category ProductCategory? @relation("Product_Category", fields: [categoryId], references: [id], onUpdate: NoAction)
|
||||
purchaseReceiptItems PurchaseReceiptItem[] @relation("Product_PurchaseReceiptItems")
|
||||
stockAdjustments StockAdjustment[] @relation("Product_Stock_Adjustments")
|
||||
stockBalances StockBalance[] @relation("StockBalance_Product")
|
||||
stockMovements StockMovement[] @relation("StockMovement_Product")
|
||||
salesInvoiceItems SalesInvoiceItem[]
|
||||
stockReservations StockReservation[]
|
||||
orderItems OrderItem[]
|
||||
|
||||
@@index([brandId], map: "Products_brandId_fkey")
|
||||
@@index([categoryId], map: "Products_categoryId_fkey")
|
||||
@@map("Products")
|
||||
}
|
||||
|
||||
model ProductBrand {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
imageUrl String? @db.VarChar(255)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
products Product[] @relation("Product_Brand")
|
||||
|
||||
@@map("Product_brands")
|
||||
}
|
||||
|
||||
model ProductCategory {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
imageUrl String? @db.VarChar(255)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
products Product[] @relation("Product_Category")
|
||||
|
||||
@@map("Product_categories")
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
model PurchaseReceipt {
|
||||
id Int @id @default(autoincrement())
|
||||
code String @unique @db.VarChar(100)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
paidAmount Decimal @default(0.00) @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
status PurchaseReceiptStatus @default(UNPAID)
|
||||
supplierId Int
|
||||
inventoryId Int
|
||||
items PurchaseReceiptItem[] @relation("PurchaseReceipt_Items")
|
||||
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
||||
supplier Supplier @relation(fields: [supplierId], references: [id])
|
||||
payments PurchaseReceiptPayments[]
|
||||
|
||||
@@index([inventoryId], map: "Purchase_Receipts_inventoryId_fkey")
|
||||
@@index([supplierId], map: "Purchase_Receipts_supplierId_fkey")
|
||||
@@map("Purchase_Receipts")
|
||||
}
|
||||
|
||||
model PurchaseReceiptItem {
|
||||
id Int @id @default(autoincrement())
|
||||
count Decimal @db.Decimal(10, 0)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
receiptId Int
|
||||
productId Int
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
product Product @relation("Product_PurchaseReceiptItems", fields: [productId], references: [id])
|
||||
receipt PurchaseReceipt @relation("PurchaseReceipt_Items", fields: [receiptId], references: [id])
|
||||
|
||||
@@index([productId], map: "Purchase_Receipt_Items_productId_fkey")
|
||||
@@index([receiptId], map: "Purchase_Receipt_Items_receiptId_fkey")
|
||||
@@map("Purchase_Receipt_Items")
|
||||
}
|
||||
|
||||
model PurchaseReceiptPayments {
|
||||
id Int @id @default(autoincrement())
|
||||
amount Decimal @db.Decimal(15, 2)
|
||||
paymentMethod PaymentMethodType
|
||||
type PaymentType
|
||||
bankAccountId Int
|
||||
receiptId Int
|
||||
payedAt DateTime @db.Timestamp(0)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
|
||||
receipt PurchaseReceipt @relation(fields: [receiptId], references: [id])
|
||||
bankAccount BankAccount @relation(fields: [bankAccountId], references: [id])
|
||||
inventoryBankAccount InventoryBankAccount? @relation(fields: [inventoryBankAccountInventoryId, inventoryBankAccountBankAccountId], references: [inventoryId, bankAccountId])
|
||||
inventoryBankAccountInventoryId Int?
|
||||
inventoryBankAccountBankAccountId Int?
|
||||
|
||||
@@index([receiptId], map: "Purchase_Receipt_Payments_receiptId_fkey")
|
||||
@@map("Purchase_Receipt_Payments")
|
||||
}
|
||||
@@ -6,14 +6,11 @@ model SalesInvoice {
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
customerId Int?
|
||||
posAccountId Int
|
||||
customer Customer? @relation(fields: [customerId], references: [id])
|
||||
posAccount PosAccount @relation(fields: [posAccountId], references: [id])
|
||||
items SalesInvoiceItem[]
|
||||
salesInvoicePayments SalesInvoicePayment[]
|
||||
|
||||
@@index([customerId])
|
||||
@@index([posAccountId])
|
||||
@@map("Sales_Invoices")
|
||||
}
|
||||
|
||||
@@ -24,12 +21,13 @@ model SalesInvoiceItem {
|
||||
totalAmount Decimal @default(0.00) @db.Decimal(15, 2)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
invoiceId Int
|
||||
productId Int
|
||||
goodId Int
|
||||
serviceId Int
|
||||
invoice SalesInvoice @relation(fields: [invoiceId], references: [id])
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
good Good? @relation(fields: [goodId], references: [id])
|
||||
service Service? @relation(fields: [serviceId], references: [id])
|
||||
|
||||
@@index([invoiceId])
|
||||
@@index([productId])
|
||||
@@index([invoiceId, goodId])
|
||||
@@map("Sales_Invoice_Items")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
model Service {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
sku String @unique() @db.VarChar(100)
|
||||
barcode String? @unique() @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
categoryId Int?
|
||||
baseSalePrice Decimal @default(0.00) @db.Decimal(15, 0)
|
||||
category ServiceCategory? @relation(fields: [categoryId], references: [id])
|
||||
salesInvoiceItems SalesInvoiceItem[]
|
||||
|
||||
@@index([categoryId])
|
||||
@@map("Services")
|
||||
}
|
||||
|
||||
model ServiceCategory {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(100)
|
||||
description String? @db.Text
|
||||
imageUrl String? @db.VarChar(255)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
services Service[]
|
||||
|
||||
@@map("Service_categories")
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
model StockMovement {
|
||||
id Int @id @default(autoincrement())
|
||||
type MovementType
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalCost Decimal @db.Decimal(15, 2)
|
||||
referenceType MovementReferenceType
|
||||
referenceId String
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
productId Int
|
||||
inventoryId Int
|
||||
avgCost Decimal @db.Decimal(15, 2)
|
||||
supplierId Int?
|
||||
remainedInStock Decimal @default(0.00) @db.Decimal(10, 0)
|
||||
counterInventoryId Int?
|
||||
customerId Int?
|
||||
counterInventory Inventory? @relation("StockMovement_CounterInventory", fields: [counterInventoryId], references: [id])
|
||||
customer Customer? @relation(fields: [customerId], references: [id])
|
||||
inventory Inventory @relation("StockMovement_Inventory", fields: [inventoryId], references: [id])
|
||||
product Product @relation("StockMovement_Product", fields: [productId], references: [id])
|
||||
supplier Supplier? @relation("StockMovement_Supplier", fields: [supplierId], references: [id])
|
||||
|
||||
@@index([inventoryId], map: "Stock_Movements_inventoryId_fkey")
|
||||
@@index([productId], map: "Stock_Movements_productId_fkey")
|
||||
@@index([counterInventoryId], map: "Stock_Movements_counterInventoryId_fkey")
|
||||
@@index([customerId], map: "Stock_Movements_customerId_fkey")
|
||||
@@index([supplierId], map: "Stock_Movements_supplierId_fkey")
|
||||
@@map("Stock_Movements")
|
||||
}
|
||||
|
||||
model StockBalance {
|
||||
quantity Decimal @default(0.000) @db.Decimal(14, 3)
|
||||
totalCost Decimal @default(0.00) @db.Decimal(14, 2)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
avgCost Decimal @default(0.00) @db.Decimal(14, 2)
|
||||
inventoryId Int
|
||||
productId Int
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
id Int @id @default(autoincrement())
|
||||
inventory Inventory @relation("StockBalance_inventory", fields: [inventoryId], references: [id])
|
||||
product Product @relation("StockBalance_Product", fields: [productId], references: [id])
|
||||
|
||||
@@unique([productId, inventoryId])
|
||||
@@index([productId])
|
||||
@@index([inventoryId])
|
||||
@@map("Stock_Balance")
|
||||
}
|
||||
|
||||
model StockAdjustment {
|
||||
id Int @id @default(autoincrement())
|
||||
adjustedQuantity Decimal @db.Decimal(10, 0)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
productId Int
|
||||
inventoryId Int
|
||||
inventory Inventory @relation("Inventory_Stock_Adjustments", fields: [inventoryId], references: [id])
|
||||
product Product @relation("Product_Stock_Adjustments", fields: [productId], references: [id])
|
||||
|
||||
@@index([inventoryId], map: "Stock_Adjustments_inventoryId_fkey")
|
||||
@@index([productId], map: "Stock_Adjustments_productId_fkey")
|
||||
@@map("Stock_Adjustments")
|
||||
}
|
||||
|
||||
model StockReservation {
|
||||
id Int @id @default(autoincrement())
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
productId Int
|
||||
inventoryId Int
|
||||
orderId Int
|
||||
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([inventoryId])
|
||||
@@index([productId])
|
||||
@@map("Stock_Reservations")
|
||||
}
|
||||
|
||||
view StockAvailableView {
|
||||
productId Int
|
||||
inventoryId Int
|
||||
physicalQuantity Decimal @db.Decimal(10, 0)
|
||||
reservedQuantity Decimal @db.Decimal(10, 0)
|
||||
availableQuantity Decimal @db.Decimal(10, 0)
|
||||
|
||||
@@map("Stock_Available_View")
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
model Supplier {
|
||||
id Int @id @default(autoincrement())
|
||||
firstName String @db.VarChar(255)
|
||||
lastName String @db.VarChar(255)
|
||||
email String? @db.VarChar(255)
|
||||
mobileNumber String @unique @db.Char(11)
|
||||
address String? @db.Text
|
||||
city String? @db.VarChar(100)
|
||||
state String? @db.VarChar(100)
|
||||
country String? @db.VarChar(100)
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
stockMovements StockMovement[] @relation("StockMovement_Supplier")
|
||||
receipts PurchaseReceipt[]
|
||||
ledger SupplierLedger[]
|
||||
|
||||
@@map("Suppliers")
|
||||
}
|
||||
|
||||
model SupplierLedger {
|
||||
id Int @id @default(autoincrement())
|
||||
description String? @db.Text
|
||||
debit Decimal @default(0) @db.Decimal(15, 2)
|
||||
credit Decimal @default(0) @db.Decimal(15, 2)
|
||||
balance Decimal @db.Decimal(15, 2)
|
||||
sourceType LedgerSourceType
|
||||
sourceId Int
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
|
||||
supplierId Int
|
||||
supplier Supplier @relation(fields: [supplierId], references: [id])
|
||||
|
||||
@@index([supplierId])
|
||||
@@map("Supplier_Ledger")
|
||||
}
|
||||
Reference in New Issue
Block a user