Add SQL query for average cost retrieval and generate TriggerLog model

This commit is contained in:
2025-12-14 10:15:57 +03:30
parent 41d7dd8802
commit 9d7d94b5f8
45 changed files with 3742 additions and 440 deletions
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to alter the column `quantity` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
- You are about to alter the column `totalCost` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
- You are about to alter the column `avgCost` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
*/
-- AlterTable
ALTER TABLE `Stock_Balance` MODIFY `quantity` DECIMAL(10, 2) NOT NULL,
MODIFY `totalCost` DECIMAL(10, 2) NOT NULL,
MODIFY `avgCost` DECIMAL(10, 2) NOT NULL;
@@ -0,0 +1,21 @@
/*
Warnings:
- The primary key for the `Stock_Balance` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `ProductId` on the `Stock_Balance` table. All the data in the column will be lost.
- Added the required column `inventoryId` to the `Stock_Balance` table without a default value. This is not possible if the table is not empty.
- Added the required column `productId` to the `Stock_Balance` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `Stock_Balance` DROP PRIMARY KEY,
DROP COLUMN `ProductId`,
ADD COLUMN `inventoryId` INTEGER NOT NULL,
ADD COLUMN `productId` INTEGER NOT NULL,
ADD PRIMARY KEY (`productId`);
-- 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_Balance` ADD CONSTRAINT `Stock_Balance_inventoryId_fkey` FOREIGN KEY (`inventoryId`) REFERENCES `Inventories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `Inventories` ADD COLUMN `isPointOfSale` BOOLEAN NOT NULL DEFAULT false;
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `Stock_Movements` MODIFY `referenceType` ENUM('PURCHASE', 'SALES', 'ADJUSTMENT', 'INVENTORY_TRANSFER') NOT NULL;
@@ -0,0 +1,77 @@
/*
Warnings:
- The primary key for the `Stock_Balance` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to alter the column `quantity` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(14,3)`.
- You are about to alter the column `totalCost` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(14,2)`.
- You are about to alter the column `avgCost` on the `Stock_Balance` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `Decimal(14,2)`.
- A unique constraint covering the columns `[productId,inventoryId]` on the table `Stock_Balance` will be added. If there are existing duplicate values, this will fail.
- Added the required column `id` to the `Stock_Balance` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE `Stock_Balance` DROP FOREIGN KEY `Stock_Balance_inventoryId_fkey`;
-- DropForeignKey
ALTER TABLE `Stock_Balance` DROP FOREIGN KEY `Stock_Balance_productId_fkey`;
-- AlterTable
ALTER TABLE `Products` ADD COLUMN `stockBalanceId` INTEGER NULL;
-- AlterTable
ALTER TABLE `Stock_Balance` DROP PRIMARY KEY,
ADD COLUMN `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
ADD COLUMN `id` INTEGER NOT NULL AUTO_INCREMENT,
MODIFY `quantity` DECIMAL(14, 3) NOT NULL DEFAULT 0,
MODIFY `totalCost` DECIMAL(14, 2) NOT NULL DEFAULT 0,
MODIFY `updatedAt` DATETIME(3) NOT NULL,
MODIFY `avgCost` DECIMAL(14, 2) NOT NULL DEFAULT 0,
ADD PRIMARY KEY (`id`);
-- CreateTable
CREATE TABLE `Trigger_Logs` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`message` TEXT NOT NULL,
`createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `_StockBalance_product` (
`A` INTEGER NOT NULL,
`B` INTEGER NOT NULL,
UNIQUE INDEX `_StockBalance_product_AB_unique`(`A`, `B`),
INDEX `_StockBalance_product_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `_StockBalance_inventory` (
`A` INTEGER NOT NULL,
`B` INTEGER NOT NULL,
UNIQUE INDEX `_StockBalance_inventory_AB_unique`(`A`, `B`),
INDEX `_StockBalance_inventory_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateIndex
CREATE INDEX `Stock_Balance_productId_idx` ON `Stock_Balance`(`productId`);
-- CreateIndex
CREATE UNIQUE INDEX `Stock_Balance_productId_inventoryId_key` ON `Stock_Balance`(`productId`, `inventoryId`);
-- AddForeignKey
ALTER TABLE `_StockBalance_product` ADD CONSTRAINT `_StockBalance_product_A_fkey` FOREIGN KEY (`A`) REFERENCES `Products`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `_StockBalance_product` ADD CONSTRAINT `_StockBalance_product_B_fkey` FOREIGN KEY (`B`) REFERENCES `Stock_Balance`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `_StockBalance_inventory` ADD CONSTRAINT `_StockBalance_inventory_A_fkey` FOREIGN KEY (`A`) REFERENCES `Inventories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `_StockBalance_inventory` ADD CONSTRAINT `_StockBalance_inventory_B_fkey` FOREIGN KEY (`B`) REFERENCES `Stock_Balance`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- RenameIndex
ALTER TABLE `Stock_Balance` RENAME INDEX `Stock_Balance_inventoryId_fkey` TO `Stock_Balance_inventoryId_idx`;
@@ -0,0 +1,12 @@
/*
Warnings:
- Added the required column `name` to the `Trigger_Logs` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `Stock_Balance` MODIFY `updatedAt` TIMESTAMP(0) NOT NULL,
MODIFY `createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0);
-- AlterTable
ALTER TABLE `Trigger_Logs` ADD COLUMN `name` TEXT NOT NULL;
@@ -0,0 +1,40 @@
/*
Warnings:
- You are about to drop the column `stockBalanceId` on the `Products` table. All the data in the column will be lost.
- You are about to drop the `_StockBalance_inventory` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `_StockBalance_product` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE `_StockBalance_inventory` DROP FOREIGN KEY `_StockBalance_inventory_A_fkey`;
-- DropForeignKey
ALTER TABLE `_StockBalance_inventory` DROP FOREIGN KEY `_StockBalance_inventory_B_fkey`;
-- DropForeignKey
ALTER TABLE `_StockBalance_product` DROP FOREIGN KEY `_StockBalance_product_A_fkey`;
-- DropForeignKey
ALTER TABLE `_StockBalance_product` DROP FOREIGN KEY `_StockBalance_product_B_fkey`;
-- AlterTable
ALTER TABLE `Products` DROP COLUMN `stockBalanceId`;
-- AlterTable
ALTER TABLE `Stock_Movements` ADD COLUMN `supplierId` INTEGER NULL;
-- DropTable
DROP TABLE `_StockBalance_inventory`;
-- DropTable
DROP TABLE `_StockBalance_product`;
-- 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_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT 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;
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `remainedInStock` to the `Stock_Movements` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `Stock_Movements` ADD COLUMN `remainedInStock` DECIMAL(10, 2) NOT NULL;
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `Stock_Movements` MODIFY `remainedInStock` DECIMAL(10, 2) NOT NULL DEFAULT 0;