سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 06:45 UTC
test(phase1a): initialize counters from historic maxima in two years and rollback gapless allocation
davoodmehraban89 committed
Oct 16, 2026 at 17:02 UTC
d8e506486cf0ae6badbacd9ea08c96469e69a664
1 file changed
+67
supabase/tests/phase1a_stage1_counter_two_fiscal.sql
new
+67
@@ -0,0 +1,67 @@
1
+-- Two fiscal years, immutable legacy numbers and transaction rollback gaplessness.
2
+-- 100% disposable Supabase CLI; every fixture is rolled back.
3
+\set ON_ERROR_STOP on
4
+BEGIN;
5
+INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
6
+VALUES('12345678-aaaa-4aaa-8aaa-123456789012','00000000-0000-0000-0000-000000000000',
7
+'authenticated','authenticated','phase1a-counter-fy@example.invalid',now(),'{}'::jsonb);
8
+SELECT set_config('phase1a_counter.org',(SELECT organization_id::text FROM public.organization_members
9
+ WHERE user_id='12345678-aaaa-4aaa-8aaa-123456789012' LIMIT 1),true);
10
+INSERT INTO public.records(organization_id,owner_id,collection,id,data)
11
+VALUES(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012',
12
+ 'companies','COMP_COUNTER','{"name":"Two-year Counter Company"}');
13
+INSERT INTO public.records(organization_id,owner_id,collection,id,data)
14
+VALUES
15
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012',
16
+ 'fiscalYears','FY_FIRST','{"companyId":"COMP_COUNTER","startDate":"1405/01/01","endDate":"1405/06/31","status":"open"}'),
17
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012',
18
+ 'fiscalYears','FY_SECOND','{"companyId":"COMP_COUNTER","startDate":"1405/07/01","endDate":"1405/12/29","status":"open"}');
19
+INSERT INTO public.records(organization_id,owner_id,collection,id,data)
20
+VALUES
21
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012',
22
+ 'accounts','COUNTER_DEBIT','{"companyId":"COMP_COUNTER","postingAllowed":true,"active":true}'),
23
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012',
24
+ 'accounts','COUNTER_CREDIT','{"companyId":"COMP_COUNTER","postingAllowed":true,"active":true}');
25
+-- Historic pre-1A numbered posted vouchers: deliberately seeded while mode off.
26
+INSERT INTO public.records(organization_id,owner_id,collection,id,data)
27
+VALUES
28
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012','journalVouchers',
29
+ 'OLD_FIRST_40','{"companyId":"COMP_COUNTER","fiscalYearId":"FY_FIRST","status":"posted","number":"40","date":"1405/03/01"}'),
30
+(current_setting('phase1a_counter.org')::uuid,'12345678-aaaa-4aaa-8aaa-123456789012','journalVouchers',
31
+ 'OLD_SECOND_11','{"companyId":"COMP_COUNTER","fiscalYearId":"FY_SECOND","status":"posted","number":"11","date":"1405/09/01"}');
32
+INSERT INTO private.finora_phase1a_guard_modes(organization_id,mode)
33
+VALUES(current_setting('phase1a_counter.org')::uuid,'enforce');
34
+SET LOCAL ROLE authenticated;
35
+SELECT set_config('request.jwt.claim.sub','12345678-aaaa-4aaa-8aaa-123456789012',true);
36
+SELECT public.finora_journal_create_draft(current_setting('phase1a_counter.org')::uuid,'NEW_FIRST_41',
37
+ '{"companyId":"COMP_COUNTER","fiscalYearId":"FY_FIRST","date":"1405/05/01"}',
38
+ '[{"accountId":"COUNTER_DEBIT","debit":9900,"credit":0},{"accountId":"COUNTER_CREDIT","debit":0,"credit":9900}]');
39
+SELECT public.finora_journal_create_draft(current_setting('phase1a_counter.org')::uuid,'NEW_SECOND_12',
40
+ '{"companyId":"COMP_COUNTER","fiscalYearId":"FY_SECOND","date":"1405/10/01"}',
41
+ '[{"accountId":"COUNTER_DEBIT","debit":18000,"credit":0},{"accountId":"COUNTER_CREDIT","debit":0,"credit":18000}]');
42
+-- Consume and ROLLBACK an allocated number: the counter must roll back too.
43
+SAVEPOINT rollback_number;
44
+SELECT public.finora_journal_post(current_setting('phase1a_counter.org')::uuid,'NEW_FIRST_41',1);
45
+ROLLBACK TO SAVEPOINT rollback_number;
46
+SELECT public.finora_journal_post(current_setting('phase1a_counter.org')::uuid,'NEW_FIRST_41',1);
47
+SELECT public.finora_journal_post(current_setting('phase1a_counter.org')::uuid,'NEW_SECOND_12',1);
48
+DO $test$
49
+DECLARE v_first text;v_second text;counter_first bigint;counter_second bigint;
50
+BEGIN
51
+ SELECT data->>'number' INTO v_first FROM public.records
52
+ WHERE organization_id=current_setting('phase1a_counter.org')::uuid
53
+ AND collection='journalVouchers' AND id='NEW_FIRST_41';
54
+ SELECT data->>'number' INTO v_second FROM public.records
55
+ WHERE organization_id=current_setting('phase1a_counter.org')::uuid
56
+ AND collection='journalVouchers' AND id='NEW_SECOND_12';
57
+ SELECT last_no INTO counter_first FROM private.finora_phase1a_journal_counters
58
+ WHERE organization_id=current_setting('phase1a_counter.org')::uuid AND fiscal_year_id='FY_FIRST';
59
+ SELECT last_no INTO counter_second FROM private.finora_phase1a_journal_counters
60
+ WHERE organization_id=current_setting('phase1a_counter.org')::uuid AND fiscal_year_id='FY_SECOND';
61
+ IF v_first<>'41' OR v_second<>'12' OR counter_first<>41 OR counter_second<>12 THEN
62
+ RAISE EXCEPTION 'FINORA_STAGE1_COUNTER_BOOTSTRAP_OR_ROLLBACK_FAILED first=% second=% counters=%,%',
63
+ v_first,v_second,counter_first,counter_second;END IF;
64
+ RAISE NOTICE 'FINORA_STAGE1_COUNTER_PASS years=2 first_legacy=40 next=41 second_legacy=11 next=12 rollback_consumed=0';
65
+END $test$;
66
+RESET ROLE;
67
+ROLLBACK;