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

test(accounting): prove bidirectional source-voucher draft deletion

davoodmehraban89 committed Nov 18, 2026 at 09:11 UTC ab14e46abec4b8ef1f36c4f219d9a9ba6d99bc6d
1 file changed +51
supabase/tests/wp4_journal_document_lifecycle.sql
+51
@@ -122,4 +122,55 @@ BEGIN
122 RAISE NOTICE 'WP4_TOMBSTONE_AND_NONREUSE_PASS';
123 END $$;
124
125 +-- Source <-> voucher draft deletion is atomic in both directions.
126 +DO $
127 +DECLARE org uuid; company text; fy text; atf bigint;
128 +BEGIN
129 + SELECT id INTO org FROM public.organizations WHERE owner_user_id='b9000000-0000-4000-8000-000000000001' LIMIT 1;
130 + SELECT id INTO company FROM public.records WHERE organization_id=org AND collection='companies' LIMIT 1;
131 + SELECT id INTO fy FROM public.records WHERE organization_id=org AND collection='fiscalYears'
132 + AND data->>'companyId'=company AND coalesce(data->>'status','open')<>'locked' LIMIT 1;
133 +
134 + INSERT INTO public.records(owner_id,organization_id,collection,id,data)
135 + VALUES(auth.uid(),org,'invoices','WP4_INV_A',
136 + jsonb_build_object('companyId',company,'date',
137 + (SELECT data->>'startDate' FROM public.records WHERE organization_id=org AND collection='fiscalYears' AND id=fy),
138 + 'documentStatus','draft','accountingStatus','draft'));
139 +
140 + INSERT INTO public.records(owner_id,organization_id,collection,id,data)
141 + VALUES(auth.uid(),org,'journalVouchers','WP4_SOURCE_V_A',
142 + jsonb_build_object('companyId',company,'fiscalYearId',fy,
143 + 'date',(SELECT data->>'startDate' FROM public.records WHERE organization_id=org AND collection='fiscalYears' AND id=fy),
144 + 'sourceType','sale','sourceId','WP4_INV_A','status','draft'));
145 +
146 + IF NOT EXISTS(SELECT 1 FROM public.records WHERE organization_id=org AND collection='journalVouchers'
147 + AND id='WP4_SOURCE_V_A' AND data->>'atfNumber' IS NOT NULL) THEN
148 + RAISE EXCEPTION 'WP4_SOURCE_VOUCHER_IDENTITY_MISSING';
149 + END IF;
150 +
151 + DELETE FROM public.records WHERE organization_id=org AND collection='journalVouchers' AND id='WP4_SOURCE_V_A';
152 + IF EXISTS(SELECT 1 FROM public.records WHERE organization_id=org AND collection='invoices' AND id='WP4_INV_A')
153 + THEN RAISE EXCEPTION 'WP4_VOUCHER_DELETE_DID_NOT_DELETE_SOURCE'; END IF;
154 + IF NOT EXISTS(SELECT 1 FROM public.finora_journal_document_registry
155 + WHERE organization_id=org AND voucher_id='WP4_SOURCE_V_A' AND state='deleted')
156 + THEN RAISE EXCEPTION 'WP4_SOURCE_DELETE_TOMBSTONE_MISSING'; END IF;
157 +
158 + INSERT INTO public.records(owner_id,organization_id,collection,id,data)
159 + VALUES(auth.uid(),org,'invoices','WP4_INV_B',
160 + jsonb_build_object('companyId',company,'date',
161 + (SELECT data->>'startDate' FROM public.records WHERE organization_id=org AND collection='fiscalYears' AND id=fy),
162 + 'documentStatus','draft','accountingStatus','draft'));
163 +
164 + INSERT INTO public.records(owner_id,organization_id,collection,id,data)
165 + VALUES(auth.uid(),org,'journalVouchers','WP4_SOURCE_V_B',
166 + jsonb_build_object('companyId',company,'fiscalYearId',fy,
167 + 'date',(SELECT data->>'startDate' FROM public.records WHERE organization_id=org AND collection='fiscalYears' AND id=fy),
168 + 'sourceType','sale','sourceId','WP4_INV_B','status','draft'));
169 +
170 + DELETE FROM public.records WHERE organization_id=org AND collection='invoices' AND id='WP4_INV_B';
171 + IF EXISTS(SELECT 1 FROM public.records WHERE organization_id=org AND collection='journalVouchers' AND id='WP4_SOURCE_V_B')
172 + THEN RAISE EXCEPTION 'WP4_SOURCE_DELETE_DID_NOT_DELETE_VOUCHER'; END IF;
173 + RAISE NOTICE 'WP4_SOURCE_VOUCHER_ATOMIC_DELETE_PASS';
174 +END $;
175 +
176 ROLLBACK;