getDriverName() !== 'pgsql' || $db->getSchemaBuilder()->hasTable('customer_deliveries') || ! $db->getSchemaBuilder()->hasTable('cr_invoices') || ! $db->getSchemaBuilder()->hasTable('stock_operations')) { throw new \LogicException('CUSTOMER_DELIVERY_SCHEMA_CONTEXT'); } $db->transaction(function () use ($db): void { $db->unprepared(<<<'SQL' CREATE TABLE customer_deliveries( id bigserial PRIMARY KEY,tenant_id bigint NOT NULL REFERENCES tenants(id),company_id bigint NOT NULL DEFAULT 0 CHECK(company_id=0),unit_id bigint NOT NULL DEFAULT 0 CHECK(unit_id=0), invoice_id bigint NOT NULL,invoice_line_no integer NOT NULL CHECK(invoice_line_no BETWEEN 1 AND 50),stock_operation_id bigint NOT NULL REFERENCES stock_operations(id), quantity_milli bigint NOT NULL CHECK(quantity_milli>0),operation_key uuid NOT NULL,payload_hash char(64) NOT NULL,created_by bigint NOT NULL REFERENCES users(id),reason text NOT NULL,created_at timestamp NOT NULL, UNIQUE(tenant_id,operation_key),UNIQUE(stock_operation_id),FOREIGN KEY(invoice_id,tenant_id,company_id,unit_id) REFERENCES cr_invoices(id,tenant_id,company_id,unit_id) ); CREATE FUNCTION customer_delivery_guard() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE i cr_invoices%ROWTYPE; o stock_operations%ROWTYPE; line jsonb; delivered bigint; movements bigint; physical bigint; BEGIN SELECT * INTO STRICT i FROM cr_invoices WHERE id=NEW.invoice_id AND tenant_id=NEW.tenant_id AND company_id=NEW.company_id AND unit_id=NEW.unit_id FOR UPDATE; SELECT * INTO STRICT o FROM stock_operations WHERE id=NEW.stock_operation_id AND tenant_id=NEW.tenant_id; line := i.snapshot->'amounts'->'lines'->(NEW.invoice_line_no-1); SELECT coalesce(sum(quantity_milli),0) INTO delivered FROM customer_deliveries WHERE tenant_id=NEW.tenant_id AND company_id=NEW.company_id AND unit_id=NEW.unit_id AND invoice_id=NEW.invoice_id AND invoice_line_no=NEW.invoice_line_no; SELECT count(*),coalesce(sum(quantity_milli),0) INTO movements,physical FROM stock_movements WHERE tenant_id=NEW.tenant_id AND stock_operation_id=o.id AND warehouse_id=o.warehouse_id AND stock_item_id=o.stock_item_id AND movement_type='issue'; IF line IS NULL OR (line->>'line_no')::integer IS DISTINCT FROM NEW.invoice_line_no OR (line->>'stock_item_id')::bigint IS DISTINCT FROM o.stock_item_id OR delivered>(line->>'quantity_milli')::bigint-NEW.quantity_milli OR o.kind<>'issue' OR o.quantity_milli<>NEW.quantity_milli OR o.created_by<>NEW.created_by OR (o.snapshot->'sales_invoice'->>'invoice_id')::bigint IS DISTINCT FROM i.id OR (o.snapshot->'sales_invoice'->>'line_no')::integer IS DISTINCT FROM NEW.invoice_line_no OR o.snapshot->'sales_invoice'->>'invoice_revision' IS DISTINCT FROM i.revision OR movements<>1 OR physical<>-NEW.quantity_milli THEN RAISE EXCEPTION 'CUSTOMER_DELIVERY_SOURCE_OR_QUANTITY_MISMATCH' USING ERRCODE='23514'; END IF; RETURN NEW; END $$; CREATE TRIGGER customer_delivery_guard BEFORE INSERT ON customer_deliveries FOR EACH ROW EXECUTE FUNCTION customer_delivery_guard(); CREATE FUNCTION customer_delivery_preserve() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'CUSTOMER_DELIVERY_APPEND_ONLY'; END $$; CREATE TRIGGER customer_delivery_preserve BEFORE UPDATE OR DELETE ON customer_deliveries FOR EACH ROW EXECUTE FUNCTION customer_delivery_preserve(); SQL); }); } }