CREATE UNIQUE INDEX "purchase_order_lines_id_purchase_order_id_organization_id_key" ON "purchase_order_lines"("id", "purchase_order_id", "organization_id"); CREATE TABLE "goods_receipts" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "purchase_order_id" UUID NOT NULL, "created_by_id" UUID NOT NULL, "delivery_note" VARCHAR(80), "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "goods_receipts_pkey" PRIMARY KEY ("id") ); CREATE UNIQUE INDEX "goods_receipts_id_purchase_order_id_organization_id_key" ON "goods_receipts"("id", "purchase_order_id", "organization_id"); CREATE INDEX "goods_receipts_organization_id_purchase_order_id_created_at_idx" ON "goods_receipts"("organization_id", "purchase_order_id", "created_at", "id"); ALTER TABLE "goods_receipts" ADD CONSTRAINT "goods_receipts_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "goods_receipts" ADD CONSTRAINT "goods_receipts_purchase_order_id_organization_id_fkey" FOREIGN KEY ("purchase_order_id", "organization_id") REFERENCES "purchase_orders"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "goods_receipts" ADD CONSTRAINT "goods_receipts_created_by_id_organization_id_fkey" FOREIGN KEY ("created_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "goods_receipt_lines" ( "id" UUID NOT NULL, "receipt_id" UUID NOT NULL, "purchase_order_id" UUID NOT NULL, "organization_id" UUID NOT NULL, "purchase_order_line_id" UUID NOT NULL, "quantity" DECIMAL(12,3) NOT NULL, CONSTRAINT "goods_receipt_lines_pkey" PRIMARY KEY ("id"), CONSTRAINT "goods_receipt_lines_quantity" CHECK ("quantity" > 0) ); CREATE UNIQUE INDEX "goods_receipt_lines_receipt_id_purchase_order_line_id_key" ON "goods_receipt_lines"("receipt_id", "purchase_order_line_id"); CREATE UNIQUE INDEX "goods_receipt_lines_id_organization_id_key" ON "goods_receipt_lines"("id", "organization_id"); CREATE INDEX "goods_receipt_lines_organization_id_purchase_order_line_id_idx" ON "goods_receipt_lines"("organization_id", "purchase_order_line_id"); ALTER TABLE "goods_receipt_lines" ADD CONSTRAINT "goods_receipt_lines_receipt_id_purchase_order_id_organizat_fkey" FOREIGN KEY ("receipt_id", "purchase_order_id", "organization_id") REFERENCES "goods_receipts"("id", "purchase_order_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "goods_receipt_lines" ADD CONSTRAINT "goods_receipt_lines_purchase_order_line_id_purchase_order__fkey" FOREIGN KEY ("purchase_order_line_id", "purchase_order_id", "organization_id") REFERENCES "purchase_order_lines"("id", "purchase_order_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "receipt_inspections" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "receipt_line_id" UUID NOT NULL, "inspected_by_id" UUID NOT NULL, "accepted_quantity" DECIMAL(12,3) NOT NULL, "rejected_quantity" DECIMAL(12,3) NOT NULL, "rejection_reason" VARCHAR(500), "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "receipt_inspections_pkey" PRIMARY KEY ("id"), CONSTRAINT "receipt_inspections_quantities" CHECK ("accepted_quantity" >= 0 AND "rejected_quantity" >= 0 AND ("rejected_quantity" = 0 OR "rejection_reason" IS NOT NULL)) ); CREATE UNIQUE INDEX "receipt_inspections_receipt_line_id_key" ON "receipt_inspections"("receipt_line_id"); CREATE UNIQUE INDEX "receipt_inspections_receipt_line_id_organization_id_key" ON "receipt_inspections"("receipt_line_id", "organization_id"); ALTER TABLE "receipt_inspections" ADD CONSTRAINT "receipt_inspections_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "receipt_inspections" ADD CONSTRAINT "receipt_inspections_receipt_line_id_organization_id_fkey" FOREIGN KEY ("receipt_line_id", "organization_id") REFERENCES "goods_receipt_lines"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "receipt_inspections" ADD CONSTRAINT "receipt_inspections_inspected_by_id_organization_id_fkey" FOREIGN KEY ("inspected_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE FUNCTION verify_receipt_inspection() RETURNS trigger AS $$ BEGIN IF NEW.accepted_quantity + NEW.rejected_quantity <> (SELECT quantity FROM goods_receipt_lines WHERE id = NEW.receipt_line_id) THEN RAISE EXCEPTION 'Inspection quantities must equal receipt quantity'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER receipt_inspection_quantity BEFORE INSERT ON receipt_inspections FOR EACH ROW EXECUTE FUNCTION verify_receipt_inspection(); CREATE FUNCTION reject_receipt_mutation() RETURNS trigger AS $$ BEGIN RAISE EXCEPTION 'Receipt records are immutable'; END; $$ LANGUAGE plpgsql; CREATE TRIGGER goods_receipts_immutable BEFORE UPDATE OR DELETE ON goods_receipts FOR EACH ROW EXECUTE FUNCTION reject_receipt_mutation(); CREATE TRIGGER goods_receipt_lines_immutable BEFORE UPDATE OR DELETE ON goods_receipt_lines FOR EACH ROW EXECUTE FUNCTION reject_receipt_mutation(); CREATE TRIGGER receipt_inspections_immutable BEFORE UPDATE OR DELETE ON receipt_inspections FOR EACH ROW EXECUTE FUNCTION reject_receipt_mutation();