manicanldes-backend/prisma/migrations/20260929173800_production_m.../migration.sql

85 lines
6.6 KiB
PL/PgSQL

ALTER TABLE "material_stock_ledger" DROP CONSTRAINT "material_stock_ledger_quantity";
ALTER TYPE "MaterialStockKind" RENAME TO "MaterialStockKind_old";
CREATE TYPE "MaterialStockKind" AS ENUM ('RECEIPT_ACCEPTED', 'PRODUCTION_CONSUMED', 'PRODUCTION_WASTED');
ALTER TABLE "material_stock_ledger" ALTER COLUMN "kind" TYPE "MaterialStockKind" USING "kind"::text::"MaterialStockKind";
DROP TYPE "MaterialStockKind_old";
CREATE TABLE "production_stock_postings" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"batch_id" UUID NOT NULL,
"warehouse_id" UUID NOT NULL,
"posted_by_id" UUID NOT NULL,
"idempotency_key" UUID NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "production_stock_postings_pkey" PRIMARY KEY ("id"),
CONSTRAINT "production_stock_postings_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
);
CREATE UNIQUE INDEX "production_stock_postings_batch_id_key" ON "production_stock_postings"("batch_id");
CREATE UNIQUE INDEX "production_stock_postings_batch_id_organization_id_key" ON "production_stock_postings"("batch_id", "organization_id");
CREATE UNIQUE INDEX "production_stock_postings_organization_id_idempotency_key_key" ON "production_stock_postings"("organization_id", "idempotency_key");
CREATE UNIQUE INDEX "production_stock_postings_id_organization_id_key" ON "production_stock_postings"("id", "organization_id");
ALTER TABLE "production_stock_postings" ADD CONSTRAINT "production_stock_postings_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_stock_postings" ADD CONSTRAINT "production_stock_postings_batch_id_organization_id_fkey" FOREIGN KEY ("batch_id", "organization_id") REFERENCES "production_batches"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_stock_postings" ADD CONSTRAINT "production_stock_postings_warehouse_id_organization_id_fkey" FOREIGN KEY ("warehouse_id", "organization_id") REFERENCES "warehouses"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_stock_postings" ADD CONSTRAINT "production_stock_postings_posted_by_id_organization_id_fkey" FOREIGN KEY ("posted_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "material_stock_ledger" ALTER COLUMN "posting_id" DROP NOT NULL;
ALTER TABLE "material_stock_ledger" ALTER COLUMN "receipt_line_id" DROP NOT NULL;
ALTER TABLE "material_stock_ledger" ADD COLUMN "production_posting_id" UUID;
ALTER TABLE "material_stock_ledger" ADD COLUMN "usage_event_id" UUID;
CREATE UNIQUE INDEX "material_stock_ledger_usage_event_id_key" ON "material_stock_ledger"("usage_event_id");
ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_production_scope_fkey" FOREIGN KEY ("production_posting_id", "organization_id") REFERENCES "production_stock_postings"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_usage_fkey" FOREIGN KEY ("usage_event_id") REFERENCES "production_material_usage"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_source" CHECK (
("kind" = 'RECEIPT_ACCEPTED' AND "delta" > 0 AND "posting_id" IS NOT NULL AND "receipt_line_id" IS NOT NULL AND "production_posting_id" IS NULL AND "usage_event_id" IS NULL)
OR ("kind" IN ('PRODUCTION_CONSUMED', 'PRODUCTION_WASTED') AND "delta" < 0 AND "posting_id" IS NULL AND "receipt_line_id" IS NULL AND "production_posting_id" IS NOT NULL AND "usage_event_id" IS NOT NULL)
);
CREATE FUNCTION guard_production_stock_posting() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Production stock postings are immutable'; END IF;
IF NOT EXISTS (
SELECT 1 FROM production_batches b WHERE b.id = NEW.batch_id
AND b.organization_id = NEW.organization_id AND b.status = 'COMPLETED'
) THEN RAISE EXCEPTION 'Production batch must be completed before stock posting'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER production_stock_postings_guard BEFORE INSERT OR UPDATE OR DELETE ON production_stock_postings FOR EACH ROW EXECUTE FUNCTION guard_production_stock_posting();
CREATE OR REPLACE FUNCTION guard_material_stock_ledger() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Material stock ledger is immutable'; END IF;
IF NEW.kind = 'RECEIPT_ACCEPTED' THEN
IF NOT EXISTS (
SELECT 1 FROM material_receipt_postings p
JOIN goods_receipt_lines l ON l.receipt_id = p.receipt_id AND l.id = NEW.receipt_line_id AND l.organization_id = NEW.organization_id
JOIN purchase_order_lines ol ON ol.id = l.purchase_order_line_id AND ol.organization_id = NEW.organization_id
JOIN receipt_inspections i ON i.receipt_line_id = l.id AND i.organization_id = NEW.organization_id
JOIN material_stock_items s ON s.id = NEW.stock_item_id AND s.organization_id = NEW.organization_id
WHERE p.id = NEW.posting_id AND p.organization_id = NEW.organization_id
AND s.warehouse_id = p.warehouse_id AND s.material_id = ol.material_id
AND NEW.material_id = ol.material_id AND NEW.delta = i.accepted_quantity
) THEN RAISE EXCEPTION 'Material receipt stock entry does not match accepted QC'; END IF;
ELSE
IF NOT EXISTS (
SELECT 1 FROM production_stock_postings p
JOIN production_material_usage u ON u.batch_id = p.batch_id AND u.id = NEW.usage_event_id AND u.organization_id = NEW.organization_id
JOIN product_bom_lines bl ON bl.id = u.bom_line_id AND bl.bom_id = u.bom_id AND bl.organization_id = NEW.organization_id
JOIN material_stock_items s ON s.id = NEW.stock_item_id AND s.organization_id = NEW.organization_id
WHERE p.id = NEW.production_posting_id AND p.organization_id = NEW.organization_id
AND s.warehouse_id = p.warehouse_id AND s.material_id = bl.material_id
AND NEW.material_id = bl.material_id AND NEW.delta = -u.quantity
AND ((NEW.kind = 'PRODUCTION_CONSUMED' AND u.kind = 'CONSUMED') OR (NEW.kind = 'PRODUCTION_WASTED' AND u.kind = 'WASTED'))
) THEN RAISE EXCEPTION 'Production stock entry does not match material usage'; END IF;
END IF;
UPDATE material_stock_items SET on_hand = on_hand + NEW.delta
WHERE id = NEW.stock_item_id AND organization_id = NEW.organization_id
AND on_hand + NEW.delta BETWEEN 0 AND 999999999.999;
IF NOT FOUND THEN RAISE EXCEPTION 'Material stock balance outside bounds'; END IF;
RETURN NEW;
END;
$$;