@davoodmehraban89 / Factor-easy / commits / 764d970
Fork 0

سامانه صدور فاکتور و حسابداری

feat(stage1): fixed purchase accounting rules and independent 1000-purchase oracle

davoodmehraban89 committed Oct 17, 2026 at 10:45 UTC 764d970996b52804856d1da4d42a700c1118f386
4 files changed +132 -3
.github/workflows/phase1a-supabase-parity.yml
+35 -2
@@ -54,6 +54,7 @@ jobs:
54 mv supabase/migrations/20261016102500_phase1a_account_role_protection.sql /tmp/finora-phase1a-migrations/
55 mv supabase/migrations/20261016103000_phase1a_sale_monetary_contract.sql /tmp/finora-phase1a-migrations/
56 mv supabase/migrations/20261016103100_phase1a_autopost_server_authority.sql /tmp/finora-phase1a-migrations/
57 + mv supabase/migrations/20261016103500_phase1a_purchase_fixed_contract.sql /tmp/finora-phase1a-migrations/
58 - name: Start isolated Supabase stack and replay baseline migrations
59 id: local_stack
60 run: supabase start -x inbucket
@@ -91,6 +92,7 @@ jobs:
92 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016102500_phase1a_account_role_protection.sql
93 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016103000_phase1a_sale_monetary_contract.sql
94 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016103100_phase1a_autopost_server_authority.sql
95 + PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016103500_phase1a_purchase_fixed_contract.sql
96 - name: RED FIRST — sale with 900-series balanced unrelated accounts must be rejected
97 run: PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f supabase/tests/phase1a_stage1_autopost_semantic_red.sql
98 - name: Stage 1 enforce/audit accounting role sync, owner RPC and append-only audit
@@ -177,7 +179,7 @@ jobs:
179 p.write_bytes(b[:-2]+b'\n')
180 PY
181 mkdir -p /tmp/finora-stage1-migrations
180 - mv supabase/migrations/2026101610{0000,0500,1000,1500,2000,2500,3000,3100}_*.sql /tmp/finora-stage1-migrations/
182 + mv supabase/migrations/2026101610{0000,0500,1000,1500,2000,2500,3000,3100,3500}_*.sql /tmp/finora-stage1-migrations/
183 supabase start -x inbucket
184 - name: Apply Stage 1 SQL only to disposable database
185 run: |
@@ -208,7 +210,7 @@ jobs:
210 if b.endswith(b'\\n'): p.write_bytes(b[:-2]+b'\n')
211 PY
212 mkdir -p /tmp/finora-oracle-migrations
211 - mv supabase/migrations/2026101610{0000,0500,1000,1500,2000,2500,3000,3100}_*.sql /tmp/finora-oracle-migrations/
213 + mv supabase/migrations/2026101610{0000,0500,1000,1500,2000,2500,3000,3100,3500}_*.sql /tmp/finora-oracle-migrations/
214 supabase start -x inbucket
215 - name: Apply Stage 1 SQL on disposable local database
216 run: |
@@ -217,3 +219,34 @@ jobs:
219 done
220 - name: Sale — independently compare 1000 source postings, seed 20261017 and enforce
221 run: node scripts/phase1a-autopost-oracle.mjs sale
222 +
223 + autopost-purchase-independent-oracle:
224 + name: stage1-purchase-independent-1000-oracle
225 + runs-on: ubuntu-latest
226 + timeout-minutes: 40
227 + steps:
228 + - uses: actions/checkout@v4
229 + - uses: supabase/setup-cli@v1
230 + with:
231 + version: 2.121.0
232 + - name: Verify offline migration preflight
233 + run: node scripts/phase1a-migration-preflight.mjs
234 + - name: Prepare local-only Supabase and stage migrations
235 + run: |
236 + supabase init --force
237 + python3 - <<'PY'
238 + from pathlib import Path
239 + for p in Path('supabase/migrations').glob('*.sql'):
240 + b=p.read_bytes()
241 + if b.endswith(b'\\n'): p.write_bytes(b[:-2]+b'\n')
242 + PY
243 + mkdir -p /tmp/finora-oracle-migrations
244 + mv supabase/migrations/2026101610{0000,0500,1000,1500,2000,2500,3000,3100,3500}_*.sql /tmp/finora-oracle-migrations/
245 + supabase start -x inbucket
246 + - name: Apply Stage 1 SQL on disposable local database
247 + run: |
248 + for migration in /tmp/finora-oracle-migrations/*.sql; do
249 + PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f "$migration"
250 + done
251 + - name: Purchase — independently compare 1000 source postings, seed 20261017 and enforce
252 + run: node scripts/phase1a-autopost-oracle.mjs purchase
scripts/phase1a-migration-preflight.mjs
+1 -1
@@ -19,7 +19,7 @@ const approvedRemote=historic.split('## Production history entries absent from t
19 const approvedLocal=historic.split('## Tracked repo files absent by exact filename')[1].split('**Required before migration:**')[0];
20 const allowProd=new Set([...approvedRemote.matchAll(/\x60([0-9]{14})_[^\x60]+\.sql\x60/g)].map(m=>m[1]));
21 const allowLocal=new Set([...approvedLocal.matchAll(/\x60([0-9]{14})_[^\x60]+\.sql\x60/g)].map(m=>m[1]));
22 -for(const v of ['20261007170000','20261016100000','20261016100500','20261016101000','20261016101500','20261016102000','20261016102500','20261016103000','20261016103100'])allowLocal.add(v);
22 +for(const v of ['20261007170000','20261016100000','20261016100500','20261016101000','20261016101500','20261016102000','20261016102500','20261016103000','20261016103100','20261016103500'])allowLocal.add(v);
23 const unexpectedProd=onlyProd.filter(x=>!allowProd.has(x.version));
24 const unexpectedLocal=onlyRepo.filter(x=>!allowLocal.has(x.version));
25 const terminal=files.filter(x=>fs.readFileSync(path.join('supabase/migrations',x)).toString().endsWith('\\n'));
supabase/migrations/20261016103500_phase1a_purchase_fixed_contract.sql new
+40
@@ -0,0 +1,40 @@
1 +-- FINORA Stage1 fixed purchase-v1: debit purchase cost and recoverable VAT,
2 +-- credit selected cash or payable. Never infer accounts from title/code.
3 +CREATE OR REPLACE FUNCTION private.finora_phase1a_purchase_lines(
4 + org uuid,company text,source jsonb,ver bigint)
5 +RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
6 +AS $f$
7 +DECLARE cost_account text;credit_account text;tax_account text;
8 + amounts jsonb;result jsonb;v_net numeric;v_tax numeric;v_total numeric;
9 +BEGIN
10 + cost_account:=private.finora_phase1a_resolve_role(org,company,'purchase_default',ver);
11 + credit_account:=private.finora_phase1a_resolve_role(org,company,
12 + CASE WHEN source->>'paymentMethod'='credit' THEN 'payable_control' ELSE 'cash_default' END,ver);
13 + amounts:=private.finora_phase1a_document_amounts(source,'purchase');
14 + v_net:=(amounts->>'net')::numeric;
15 + v_tax:=(amounts->>'vat')::numeric;
16 + v_total:=(amounts->>'total')::numeric;
17 + IF v_net<=0 THEN RAISE EXCEPTION 'FINORA_SOURCE_NET_AMOUNT_INVALID' USING ERRCODE='23514';END IF;
18 + result:=jsonb_build_array(jsonb_build_object(
19 + 'accountId',cost_account,'debit',v_net,'credit',0));
20 + IF v_tax>0 THEN
21 + tax_account:=private.finora_phase1a_resolve_role(org,company,'vat_recoverable',ver);
22 + result:=result||jsonb_build_array(jsonb_build_object(
23 + 'accountId',tax_account,'debit',v_tax,'credit',0));
24 + END IF;
25 + result:=result||jsonb_build_array(jsonb_build_object(
26 + 'accountId',credit_account,'debit',0,'credit',v_total));
27 + RETURN result;
28 +END $f$;
29 +
30 +CREATE OR REPLACE FUNCTION private.finora_phase1a_expected_lines(
31 + org uuid,company text,typ text,source jsonb,ver bigint)
32 +RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
33 +AS $f$
34 +BEGIN
35 + IF typ='sale' THEN RETURN private.finora_phase1a_sale_lines(org,company,source,ver);END IF;
36 + IF typ='purchase' THEN RETURN private.finora_phase1a_purchase_lines(org,company,source,ver);END IF;
37 + RAISE EXCEPTION 'FINORA_AUTOPOST_CONTRACT_UNSUPPORTED' USING ERRCODE='23514';
38 +END $f$;
39 +REVOKE ALL ON FUNCTION private.finora_phase1a_purchase_lines(uuid,text,jsonb,bigint)
40 +FROM PUBLIC,anon,authenticated;
supabase/tests/phase1a_stage1_account_role_controls.sql
+56
@@ -167,4 +167,60 @@ DO $q$ BEGIN
167 RAISE EXCEPTION 'FINORA_PUBLISH_AUDIT_MISSING';END IF;
168 RAISE NOTICE 'FINORA_ACCOUNT_ROLE_GUARDS_PASS';
169 END $q$;
170 +
171 +-- Revalidate at post: an owner publishing a new mapping invalidates draft pin.
172 +DO $t$
173 +DECLARE org uuid:=current_setting('finora_test.org_a')::uuid;
174 + header jsonb:='{"companyId":"COMP_STAGE_1","fiscalYearId":"FY_STAGE_1","date":"1405/07/24","sourceType":"sale","sourceId":"SOURCE_C_1","sourceVersion":"1"}';
175 + ls jsonb;d jsonb;
176 +BEGIN
177 + IF private.finora_phase1a_mode(org)<>'enforce' THEN
178 + RAISE EXCEPTION 'FINORA_GUARD_NOT_ENFORCE';END IF;
179 + PERFORM set_config('request.jwt.claim.sub','aaaa1111-1111-4111-8111-111111111111',true);
180 + ls:=private.finora_phase1a_sale_lines(org,'COMP_STAGE_1',
181 + (SELECT data FROM public.records WHERE organization_id=org
182 + AND collection='invoices' AND id='SOURCE_C_1'),1);
183 + d:=private.finora_phase1a_create_core(org,'ROLE_DRAFT_BEFORE_CHANGE',header,ls);
184 + UPDATE public.records SET data=data||'{"postingContractRule":"sale-v1","postingContractVersion":1}'::jsonb
185 + WHERE organization_id=org AND collection='journalVouchers' AND id='ROLE_DRAFT_BEFORE_CHANGE';
186 +END $t$;
187 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
188 +SELECT organization_id,owner_id,'accounts','ACC_STAGE_3',
189 + data||'{"code":"1105","title":"صندوق جایگزین تست"}'::jsonb
190 +FROM public.records WHERE organization_id=current_setting('finora_test.org_a')::uuid
191 + AND collection='accounts' AND id='ACC_STAGE_1';
192 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
193 +SELECT organization_id,owner_id,'accounts','ACC_ONLY_B',data||'{"code":"1109"}'::jsonb
194 +FROM public.records WHERE organization_id=current_setting('finora_test.org_b')::uuid
195 + AND collection='accounts' AND id='ACC_STAGE_1';
196 +SET LOCAL ROLE authenticated;
197 +DO $t$
198 +DECLARE org uuid:=current_setting('finora_test.org_a')::uuid;v jsonb;
199 +BEGIN
200 + v:=public.finora_phase1a_publish_role_assignments(org,'COMP_STAGE_1',
201 + '{"cash_default":"ACC_STAGE_3","revenue_default":"ACC_STAGE_2"}'::jsonb,
202 + 1,'Owner changes the cash role binding');
203 + IF (v->>'version')::bigint<>2 THEN RAISE EXCEPTION 'FINORA_ROLE_VERSION_NOT_INCREMENTED';END IF;
204 + BEGIN
205 + PERFORM public.finora_journal_post(org,'ROLE_DRAFT_BEFORE_CHANGE',2);
206 + RAISE EXCEPTION 'FINORA_POST_ACCEPTED_CHANGED_ROLE';
207 + EXCEPTION WHEN serialization_failure THEN
208 + IF SQLERRM NOT LIKE '%FINORA_POSTING_CONTRACT_CHANGED%' THEN RAISE;END IF;
209 + END;
210 + BEGIN
211 + PERFORM public.finora_phase1a_publish_role_assignments(org,'COMP_STAGE_1',
212 + '{"cash_default":"ACC_ONLY_B"}'::jsonb,2,'Cross-organization account');
213 + RAISE EXCEPTION 'FINORA_CROSS_ORG_ACCOUNT_ACCEPTED';
214 + EXCEPTION WHEN foreign_key_violation THEN
215 + IF SQLERRM NOT LIKE '%FINORA_POSTING_ACCOUNT_OUT_OF_SCOPE%' THEN RAISE;END IF;
216 + END;
217 +END $t$;
218 +RESET ROLE;
219 +DO $t$ BEGIN
220 + IF EXISTS(SELECT 1 FROM private.finora_phase1a_journal_counters
221 + WHERE organization_id=current_setting('finora_test.org_a')::uuid
222 + AND fiscal_year_id='FY_STAGE_1') THEN
223 + RAISE EXCEPTION 'FINORA_REJECTED_POST_CONSUMED_NUMBER';END IF;
224 + RAISE NOTICE 'FINORA_ACCOUNT_ROLE_GUARDS_AND_DRAFT_PIN_PASS';
225 +END $t$;
226 ROLLBACK;