CREATE TYPE "MaterialUsageKind" AS ENUM ('CONSUMED', 'WASTED'); CREATE UNIQUE INDEX "production_batches_id_bom_id_organization_id_key" ON "production_batches"("id", "bom_id", "organization_id"); CREATE UNIQUE INDEX "product_bom_lines_id_bom_id_organization_id_key" ON "product_bom_lines"("id", "bom_id", "organization_id"); CREATE TABLE "production_material_usage" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "batch_id" UUID NOT NULL, "bom_id" UUID NOT NULL, "bom_line_id" UUID NOT NULL, "recorded_by_id" UUID NOT NULL, "kind" "MaterialUsageKind" NOT NULL, "quantity" DECIMAL(12,3) NOT NULL, "reason" VARCHAR(500), "idempotency_key" UUID NOT NULL, "request_hash" CHAR(64) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "production_material_usage_pkey" PRIMARY KEY ("id"), CONSTRAINT "production_material_usage_values" CHECK ( "quantity" > 0 AND "request_hash" ~ '^[a-f0-9]{64}$' AND ("kind" = 'CONSUMED' OR ("reason" IS NOT NULL AND LENGTH(TRIM("reason")) > 0)) ) ); CREATE UNIQUE INDEX "production_material_usage_idempotency_key" ON "production_material_usage"("organization_id", "idempotency_key"); CREATE INDEX "production_material_usage_batch_created_idx" ON "production_material_usage"("organization_id", "batch_id", "created_at", "id"); ALTER TABLE "production_material_usage" ADD CONSTRAINT "production_material_usage_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "production_material_usage" ADD CONSTRAINT "production_material_usage_batch_scope_fkey" FOREIGN KEY ("batch_id", "bom_id", "organization_id") REFERENCES "production_batches"("id", "bom_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "production_material_usage" ADD CONSTRAINT "production_material_usage_line_scope_fkey" FOREIGN KEY ("bom_line_id", "bom_id", "organization_id") REFERENCES "product_bom_lines"("id", "bom_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "production_material_usage" ADD CONSTRAINT "production_material_usage_actor_scope_fkey" FOREIGN KEY ("recorded_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE FUNCTION guard_production_material_usage() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Production material usage is immutable'; END IF; IF NOT EXISTS ( SELECT 1 FROM production_batches WHERE id = NEW.batch_id AND bom_id = NEW.bom_id AND organization_id = NEW.organization_id AND status = 'IN_PROGRESS' ) THEN RAISE EXCEPTION 'Material usage requires a started batch'; END IF; RETURN NEW; END; $$; CREATE TRIGGER production_material_usage_guard BEFORE INSERT OR UPDATE OR DELETE ON production_material_usage FOR EACH ROW EXECUTE FUNCTION guard_production_material_usage(); UPDATE "roles" SET "permissions" = array_append("permissions", 'production.material.record') WHERE "is_system" = true AND "name" = 'Owner' AND NOT ('production.material.record' = ANY("permissions"));