سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 01:45 UTC
test: assert sale and purchase source links commit atomically with posting
davoodmehraban89 committed
Nov 17, 2026 at 14:18 UTC
e915c2ae444c64f158e250b496cbd62ebff3db24
1 file changed
+14
-2
supabase/tests/wp1_phase1a_stage1_sale_purchase_negatives.sql
+14
-2
@@ -53,7 +53,7 @@ WHERE m.user_id='cccc3333-3333-4333-8333-333333333333'
53
AND m.organization_id=current_setting('finora_test.org_a')::uuid AND r.role_key='phase1a_commerce_only';
54
INSERT INTO public.records(organization_id,owner_id,collection,id,data)
55
VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
56
-'invoices','SOURCE_C_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"cash","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1}'::jsonb);
56
+'invoices','SOURCE_C_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"cash","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1,"accountingPostingPending":true,"accountingPostingPendingAt":"2026-11-17T10:00:00Z"}'::jsonb);
57
-- A pro-forma document must never post as a sale.
58
INSERT INTO public.records(organization_id,owner_id,collection,id,data)
59
VALUES(current_setting('finora_test.org_a')::uuid,
@@ -70,7 +70,7 @@ INSERT INTO public.records(organization_id,owner_id,collection,id,data) VALUES
70
'accounts','ACC_AP','{"companyId":"COMP_STAGE_1","code":"2101","title":"حسابهای پرداختنی","systemRole":"payable_control","accountType":"liability","normalBalance":"credit","level":"detail","parentId":"21","active":true,"postingAllowed":true}');
71
INSERT INTO public.records(organization_id,owner_id,collection,id,data)
72
VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
73
-'purchases','PUR_NEG_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"credit","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1,"currencyCode":"IRR"}'::jsonb);
73
+'purchases','PUR_NEG_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"credit","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1,"currencyCode":"IRR","accountingPostingPending":true,"accountingPostingPendingAt":"2026-11-17T10:00:00Z"}'::jsonb);
74
DO $t$ BEGIN IF private.finora_phase1a_mode(current_setting('finora_test.org_a')::uuid)<>'enforce' THEN
75
RAISE EXCEPTION 'FINORA_NEGATIVE_GUARD_NOT_ENFORCE';END IF;END $t$;
76
SET LOCAL ROLE authenticated;
@@ -117,6 +117,18 @@ BEGIN
117
r:=public.finora_journal_autopost(org,'S_POST_1','invoices','SOURCE_C_1',hdr_s,NULL);
118
v:=(r#>>'{voucher,version}')::bigint;
119
IF r#>>'{voucher,status}'<>'posted' THEN RAISE EXCEPTION 'FINORA_SALE_NOT_POSTED';END IF;
120
+ IF NOT EXISTS(SELECT 1 FROM public.records WHERE organization_id=org AND collection='invoices'
121
+ AND id='SOURCE_C_1' AND data->>'journalVoucherId'='S_POST_1'
122
+ AND data->>'accountingStatus'='posted' AND data->>'documentStatus'='posted'
123
+ AND NOT data ? 'accountingPostingPending') THEN
124
+ RAISE EXCEPTION 'FINORA_SALE_SOURCE_LINK_NOT_ATOMIC';END IF;
125
+ r:=public.finora_journal_autopost(org,'P_POST_1','purchases','PUR_NEG_1',hdr_p,NULL);
126
+ IF r#>>'{voucher,status}'<>'posted' THEN RAISE EXCEPTION 'FINORA_PURCHASE_NOT_POSTED';END IF;
127
+ IF NOT EXISTS(SELECT 1 FROM public.records WHERE organization_id=org AND collection='purchases'
128
+ AND id='PUR_NEG_1' AND data->>'journalVoucherId'='P_POST_1'
129
+ AND data->>'accountingStatus'='posted' AND data->>'documentStatus'='posted'
130
+ AND NOT data ? 'accountingPostingPending') THEN
131
+ RAISE EXCEPTION 'FINORA_PURCHASE_SOURCE_LINK_NOT_ATOMIC';END IF;
132
PERFORM public.finora_journal_reverse(org,'S_POST_1','S_REVERSE_1','1405/07/24',
133
'برگشت حسابداری تست',v,NULL);
134
r:=public.finora_journal_autopost(org,'S_POST_AGAIN','invoices','SOURCE_C_1',hdr_s,NULL);