69 lines
5.2 KiB
PL/PgSQL
69 lines
5.2 KiB
PL/PgSQL
CREATE TABLE "finished_goods_receipts" (
|
|
"id" UUID NOT NULL,
|
|
"organization_id" UUID NOT NULL,
|
|
"batch_id" UUID NOT NULL,
|
|
"warehouse_id" UUID NOT NULL,
|
|
"stock_item_id" UUID NOT NULL,
|
|
"posted_by_id" UUID NOT NULL,
|
|
"quantity" INTEGER NOT NULL,
|
|
"idempotency_key" UUID NOT NULL,
|
|
"request_hash" CHAR(64) NOT NULL,
|
|
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
CONSTRAINT "finished_goods_receipts_pkey" PRIMARY KEY ("id"),
|
|
CONSTRAINT "finished_goods_receipts_quantity" CHECK ("quantity" BETWEEN 1 AND 2000000000),
|
|
CONSTRAINT "finished_goods_receipts_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
|
|
);
|
|
CREATE UNIQUE INDEX "finished_goods_receipts_batch_id_key" ON "finished_goods_receipts"("batch_id");
|
|
CREATE UNIQUE INDEX "finished_goods_receipts_batch_id_organization_id_key" ON "finished_goods_receipts"("batch_id", "organization_id");
|
|
CREATE UNIQUE INDEX "finished_goods_receipts_organization_id_idempotency_key_key" ON "finished_goods_receipts"("organization_id", "idempotency_key");
|
|
CREATE UNIQUE INDEX "finished_goods_receipts_id_organization_id_key" ON "finished_goods_receipts"("id", "organization_id");
|
|
ALTER TABLE "finished_goods_receipts" ADD CONSTRAINT "finished_goods_receipts_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "finished_goods_receipts" ADD CONSTRAINT "finished_goods_receipts_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 "finished_goods_receipts" ADD CONSTRAINT "finished_goods_receipts_warehouse_id_organization_id_fkey" FOREIGN KEY ("warehouse_id", "organization_id") REFERENCES "warehouses"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "finished_goods_receipts" ADD CONSTRAINT "finished_goods_receipts_stock_item_id_organization_id_fkey" FOREIGN KEY ("stock_item_id", "organization_id") REFERENCES "stock_items"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "finished_goods_receipts" ADD CONSTRAINT "finished_goods_receipts_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 "stock_ledger" ADD COLUMN "finished_goods_receipt_id" UUID;
|
|
CREATE UNIQUE INDEX "stock_ledger_finished_goods_receipt_id_key" ON "stock_ledger"("finished_goods_receipt_id");
|
|
CREATE UNIQUE INDEX "stock_ledger_finished_goods_receipt_id_organization_id_key" ON "stock_ledger"("finished_goods_receipt_id", "organization_id");
|
|
ALTER TABLE "stock_ledger" ADD CONSTRAINT "stock_ledger_finished_goods_scope_fkey" FOREIGN KEY ("finished_goods_receipt_id", "organization_id") REFERENCES "finished_goods_receipts"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
CREATE FUNCTION guard_finished_goods_receipt() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Finished-goods receipts are immutable'; END IF;
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM production_batches b
|
|
JOIN production_batch_completions c ON c.batch_id = b.id AND c.organization_id = NEW.organization_id
|
|
JOIN product_boms bom ON bom.id = b.bom_id AND bom.organization_id = NEW.organization_id
|
|
JOIN packaging_runs r ON r.batch_id = b.id AND r.organization_id = NEW.organization_id
|
|
JOIN production_stock_postings ps ON ps.batch_id = b.id AND ps.organization_id = NEW.organization_id
|
|
JOIN packaging_stock_postings ks ON ks.run_id = r.id AND ks.organization_id = NEW.organization_id
|
|
JOIN stock_items s ON s.id = NEW.stock_item_id AND s.organization_id = NEW.organization_id
|
|
WHERE b.id = NEW.batch_id AND b.organization_id = NEW.organization_id
|
|
AND b.status = 'COMPLETED' AND c.good_quantity = NEW.quantity
|
|
AND s.variant_id = bom.variant_id AND s.warehouse_id = NEW.warehouse_id
|
|
) THEN RAISE EXCEPTION 'Finished-goods receipt requires completed and stock-posted production and packaging'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER finished_goods_receipts_guard BEFORE INSERT OR UPDATE OR DELETE ON finished_goods_receipts FOR EACH ROW EXECUTE FUNCTION guard_finished_goods_receipt();
|
|
|
|
CREATE FUNCTION post_finished_goods_ledger() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NEW.finished_goods_receipt_id IS NULL THEN RETURN NEW; END IF;
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM finished_goods_receipts r
|
|
WHERE r.id = NEW.finished_goods_receipt_id AND r.organization_id = NEW.organization_id
|
|
AND r.stock_item_id = NEW.stock_item_id AND r.posted_by_id = NEW.actor_id
|
|
AND r.quantity = NEW.delta AND r.idempotency_key = NEW.idempotency_key
|
|
AND r.request_hash = NEW.request_hash AND NEW.reason = 'PRODUCTION_OUTPUT'
|
|
) THEN RAISE EXCEPTION 'Finished-goods stock entry does not match its receipt'; END IF;
|
|
UPDATE stock_items SET on_hand = on_hand + NEW.delta
|
|
WHERE id = NEW.stock_item_id AND organization_id = NEW.organization_id
|
|
AND on_hand <= 2000000000 - NEW.delta;
|
|
IF NOT FOUND THEN RAISE EXCEPTION 'Finished-goods stock capacity exceeded'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER stock_ledger_finished_goods_post BEFORE INSERT ON stock_ledger FOR EACH ROW EXECUTE FUNCTION post_finished_goods_ledger();
|