Files
cross-financial-management/prisma/migrations/20260903131116_simplify_stock_module/migration.sql
T
Joshua OosthuizenandClaude Sonnet 5 b137176c40 Add Stock catalog (items/warehouses) and fix session persistence
Stock is scoped to a catalog + cost centers only, per direction that
physical GRV/stock-take/recipe transactions belong in an external
system (Matrix), not this accounting app. Stock Control/GRN/Variance/
PPV GL accounts are now plain postable accounts rather than locked
control accounts.

Also fixes session loss on page refresh: auth stores now persist to
localStorage, and ProtectedRoute waits for rehydration before deciding
to redirect to login.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JWqiUSojMLUoN7mrJqdEbN
2026-09-04 10:35:04 +02:00

97 lines
4.1 KiB
PL/PgSQL

-- Simplifies Stock down to a catalog (StockItem) + cost centers (Warehouse) + a SOH view
-- (StockLevel) for a future Matrix feed to populate. Removes the GRV / Stock Take / Recipe
-- production transactional workflows entirely — physical stock transactions belong in Matrix,
-- not in this accounting system.
-- Data cleanup before structural changes (dev/test fixtures from the removed GRV-matching flow).
DELETE FROM "APInvoiceLine" WHERE "expenseGLAccountId" IS NULL;
-- Demote the Stock-related control accounts (1200 Stock, 2050 GRN Accrual, 5200 Stock Variance,
-- 5300 Purchase Price Variance) back to plain, manually-postable accounts rather than deleting
-- them — several already have real posted JournalEntryLine rows against them from testing, and
-- posted ledger history is never deleted. With the GRV/Stock-Take/Recipe workflows gone, nothing
-- else in this app owns these accounts, and leaving them locked as control accounts would make
-- them permanently unpostable by anyone.
UPDATE "GLAccount" SET "isControlAccount" = false, "controlType" = NULL
WHERE "controlType" IN ('STOCK', 'GRN', 'STOCK_VARIANCE', 'PURCHASE_PRICE_VARIANCE');
-- AlterEnum
BEGIN;
CREATE TYPE "ControlAccountType_new" AS ENUM ('AR', 'AP', 'TAX_OUTPUT', 'TAX_INPUT', 'BANK', 'RETAINED_EARNINGS');
ALTER TABLE "GLAccount" ALTER COLUMN "controlType" TYPE "ControlAccountType_new" USING ("controlType"::text::"ControlAccountType_new");
ALTER TYPE "ControlAccountType" RENAME TO "ControlAccountType_old";
ALTER TYPE "ControlAccountType_new" RENAME TO "ControlAccountType";
DROP TYPE "ControlAccountType_old";
COMMIT;
-- DropForeignKey
ALTER TABLE "APInvoiceLine" DROP CONSTRAINT "APInvoiceLine_expenseGLAccountId_fkey";
ALTER TABLE "APInvoiceLine" DROP CONSTRAINT "APInvoiceLine_grvLineId_fkey";
ALTER TABLE "GoodsReceivedVoucher" DROP CONSTRAINT "GoodsReceivedVoucher_createdById_fkey";
ALTER TABLE "GoodsReceivedVoucher" DROP CONSTRAINT "GoodsReceivedVoucher_supplierId_fkey";
ALTER TABLE "GoodsReceivedVoucher" DROP CONSTRAINT "GoodsReceivedVoucher_warehouseId_fkey";
ALTER TABLE "GoodsReceivedVoucherLine" DROP CONSTRAINT "GoodsReceivedVoucherLine_grvId_fkey";
ALTER TABLE "GoodsReceivedVoucherLine" DROP CONSTRAINT "GoodsReceivedVoucherLine_itemId_fkey";
ALTER TABLE "JournalEntryLine" DROP CONSTRAINT "JournalEntryLine_stockMovementId_fkey";
ALTER TABLE "Recipe" DROP CONSTRAINT "Recipe_outputItemId_fkey";
ALTER TABLE "RecipeComponent" DROP CONSTRAINT "RecipeComponent_componentItemId_fkey";
ALTER TABLE "RecipeComponent" DROP CONSTRAINT "RecipeComponent_recipeId_fkey";
ALTER TABLE "StockAdjustment" DROP CONSTRAINT "StockAdjustment_createdById_fkey";
ALTER TABLE "StockAdjustment" DROP CONSTRAINT "StockAdjustment_warehouseId_fkey";
ALTER TABLE "StockAdjustmentLine" DROP CONSTRAINT "StockAdjustmentLine_adjustmentId_fkey";
ALTER TABLE "StockAdjustmentLine" DROP CONSTRAINT "StockAdjustmentLine_itemId_fkey";
ALTER TABLE "StockMovement" DROP CONSTRAINT "StockMovement_createdById_fkey";
ALTER TABLE "StockMovement" DROP CONSTRAINT "StockMovement_itemId_fkey";
ALTER TABLE "StockMovement" DROP CONSTRAINT "StockMovement_warehouseId_fkey";
-- DropIndex
DROP INDEX "APInvoiceLine_grvLineId_idx";
-- AlterTable
ALTER TABLE "APInvoiceLine" DROP COLUMN "grvLineId",
ALTER COLUMN "expenseGLAccountId" SET NOT NULL;
-- AlterTable
ALTER TABLE "JournalEntryLine" DROP COLUMN "stockMovementId";
-- AlterTable
ALTER TABLE "StockItem" DROP COLUMN "costingMethod",
DROP COLUMN "manualCost";
-- AlterTable
ALTER TABLE "StockLevel" DROP COLUMN "averageCost",
DROP COLUMN "lastCost";
-- DropTable
DROP TABLE "GoodsReceivedVoucher";
-- DropTable
DROP TABLE "GoodsReceivedVoucherLine";
-- DropTable
DROP TABLE "Recipe";
-- DropTable
DROP TABLE "RecipeComponent";
-- DropTable
DROP TABLE "StockAdjustment";
-- DropTable
DROP TABLE "StockAdjustmentLine";
-- DropTable
DROP TABLE "StockMovement";
-- DropEnum
DROP TYPE "CostingMethod";
-- DropEnum
DROP TYPE "StockDocumentStatus";
-- DropEnum
DROP TYPE "StockMovementType";
-- AddForeignKey
ALTER TABLE "APInvoiceLine" ADD CONSTRAINT "APInvoiceLine_expenseGLAccountId_fkey" FOREIGN KEY ("expenseGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;