Files
SGF/packages/general-ledger/src/CostSchema.php
T
2026-11-04 09:37:20 +02:00

20 lines
1.5 KiB
PHP

<?php
namespace Tempo\Finance\GeneralLedger;
use Illuminate\Database\ConnectionInterface;
final class CostSchema
{
public static function install(ConnectionInterface $db):void
{
LedgerSchema::assertSynthetic($db);
if(!str_starts_with($db->getDatabaseName(),'payable_sgf_')||$db->getSchemaBuilder()->hasTable('gl_cost_sources')||$db->table('gl_proposals')->exists()||$db->table('gl_journals')->exists())throw new \LogicException('New empty SGF cost ledger required');
$db->transaction(function()use($db){
$db->statement('CREATE TABLE gl_cost_sources(id bigserial PRIMARY KEY,tenant_id bigint NOT NULL,company_id bigint NOT NULL,unit_id bigint NOT NULL,order_id bigint NOT NULL REFERENCES wo_orders(id),material_origin text NOT NULL,time_origin text NOT NULL,period_key varchar(80) NOT NULL,created_by bigint NOT NULL,reason text NOT NULL CHECK(length(reason)>=3),created_at timestamp NOT NULL,UNIQUE(tenant_id,company_id,unit_id,order_id))');
$db->unprepared(<<<'SQL'
CREATE FUNCTION gl_cost_source_scope() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NOT EXISTS(SELECT 1 FROM wo_orders WHERE id=NEW.order_id AND tenant_id=NEW.tenant_id AND company_id=NEW.company_id AND unit_id=NEW.unit_id) THEN RAISE EXCEPTION 'GL_COST_SCOPE'; END IF; RETURN NEW; END $$;
CREATE TRIGGER gl_cost_source_scope BEFORE INSERT ON gl_cost_sources FOR EACH ROW EXECUTE FUNCTION gl_cost_source_scope();
CREATE TRIGGER gl_immutable BEFORE UPDATE OR DELETE ON gl_cost_sources FOR EACH ROW EXECUTE FUNCTION gl_append_only();
SQL);
});
}
}