@davoodmehraban89 / Factor-easy / commits / 2513a49
Fork 0

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

fix(phase1a): allow sequential partial reversal vouchers without relaxing normal source unique key

davoodmehraban89 committed Oct 16, 2026 at 17:27 UTC 2513a49b2b79da3dc2d1448de5923f5790e98689
1 file changed +13
supabase/migrations/20261016101500_phase1a_journal_core.sql
+13
@@ -6,6 +6,19 @@ CREATE TABLE IF NOT EXISTS private.finora_phase1a_journal_counters(
6 PRIMARY KEY(organization_id,fiscal_year_id));
7 REVOKE ALL ON private.finora_phase1a_journal_counters FROM PUBLIC,anon,authenticated;
8
9 +-- Historic unique source-version guard must exclude reversal vouchers.
10 +-- Multiple partial reversals of one posted original must coexist; unique
11 +-- reversal voucher IDs and aggregate remaining-line checks prevent replay/overrun.
12 +-- The original journal row and its posted lines are never changed.
13 +DROP INDEX IF EXISTS public.ux_finora_journal_source_version;
14 +CREATE UNIQUE INDEX ux_finora_journal_source_version ON public.records
15 + (owner_id,(data->>'companyId'),(data->>'sourceType'),(data->>'sourceId'),
16 + ((coalesce(data->>'sourceVersion','1'))::integer))
17 +WHERE collection='journalVouchers'
18 + AND data->>'status' IN ('posted','reversed')
19 + AND coalesce(data->>'sourceId','')<>''
20 + AND data->>'sourceType'<>'reversal';
21 +
22 -- Historic vouchers remain unchanged. Covers original posted identifiers too,
23 -- so posting corrected data requires a NEW business source ID.
24 CREATE UNIQUE INDEX IF NOT EXISTS ux_finora_phase1a_active_source