سامانه صدور فاکتور و حسابداری
fix: reconcile legacy posted-source guard with canonical RPC projection
davoodmehraban89 committed
Nov 17, 2026 at 17:27 UTC
c523b9709de74901c06318ef01ab2a4a0cf9f41b
1 file changed
+88
supabase/migrations/20261117120000_wp1_stage3_canonical_source_link_projection.sql
+88
@@ -77,3 +77,91 @@ $guard$;
77
78
REVOKE ALL ON FUNCTION private.finora_stage3_guard_posted_source()
79
FROM PUBLIC, anon, authenticated;
80
+
81
+
82
+-- The earlier production guard also protects invoices/purchases by their linked voucher.
83
+-- Permit only a canonical, voucher-derived status/link projection; all commercial
84
+-- payload fields and all other source collections remain strictly immutable.
85
+CREATE OR REPLACE FUNCTION public.finora_guard_posted_source_records()
86
+RETURNS trigger
87
+LANGUAGE plpgsql
88
+SECURITY INVOKER
89
+SET search_path = public
90
+AS $$
91
+DECLARE
92
+ vid text := coalesce(old.data->>'journalVoucherId','');
93
+ voucher_status text := '';
94
+ v_voucher public.records%rowtype;
95
+ v_effective_status text;
96
+ v_projection_keys text[] := ARRAY[
97
+ 'journalVoucherId','accountingStatus','accountingPostedAt',
98
+ 'documentStatus','documentPostedAt','documentPostedBy',
99
+ 'documentVersion','updatedAt',
100
+ 'accountingPostingPending','accountingPostingPendingAt'
101
+ ]::text[];
102
+BEGIN
103
+ IF old.collection NOT IN ('invoices','purchases','payments','expenses','cheques','contractStatements') THEN
104
+ RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
105
+ END IF;
106
+
107
+ IF vid <> '' THEN
108
+ SELECT coalesce(r.data->>'status','') INTO voucher_status
109
+ FROM public.records r
110
+ WHERE r.organization_id=old.organization_id
111
+ AND r.owner_id=old.owner_id
112
+ AND r.collection='journalVouchers'
113
+ AND r.id=vid;
114
+ END IF;
115
+
116
+ IF old.collection IN ('invoices','purchases')
117
+ AND tg_op='UPDATE'
118
+ AND (coalesce(old.data->>'accountingStatus','')='posted'
119
+ OR voucher_status IN ('posted','reversed')) THEN
120
+ SELECT r.* INTO v_voucher
121
+ FROM public.records r
122
+ WHERE r.organization_id=old.organization_id
123
+ AND r.owner_id=old.owner_id
124
+ AND r.collection='journalVouchers'
125
+ AND r.id=vid
126
+ AND r.data->>'sourceType'=CASE old.collection WHEN 'invoices' THEN 'sale' ELSE 'purchase' END
127
+ AND r.data->>'sourceId'=old.id
128
+ AND r.data->>'status' IN ('posted','reversed');
129
+
130
+ IF FOUND
131
+ AND (old.data - v_projection_keys) IS NOT DISTINCT FROM (new.data - v_projection_keys) THEN
132
+ SELECT s.effective_status INTO v_effective_status
133
+ FROM public.finora_journal_reversal_status s
134
+ WHERE s.organization_id=old.organization_id AND s.voucher_id=v_voucher.id;
135
+ v_effective_status:=coalesce(v_effective_status,v_voucher.data->>'status');
136
+
137
+ IF new.data->>'journalVoucherId' IS NOT DISTINCT FROM v_voucher.id
138
+ AND new.data->>'accountingStatus' IS NOT DISTINCT FROM
139
+ (CASE WHEN v_effective_status='reversed' THEN 'reversed' ELSE 'posted' END)
140
+ AND new.data->>'documentStatus' IS NOT DISTINCT FROM
141
+ (CASE WHEN v_effective_status='reversed' THEN 'reversed' ELSE 'posted' END)
142
+ AND new.data->>'accountingPostedAt' IS NOT DISTINCT FROM v_voucher.data->>'postedAt'
143
+ AND new.data->>'documentPostedAt' IS NOT DISTINCT FROM v_voucher.data->>'postedAt'
144
+ AND new.data->>'documentPostedBy' IS NOT DISTINCT FROM
145
+ coalesce(v_voucher.data->>'postedBy',old.data->>'documentPostedBy')
146
+ AND coalesce(new.data->>'documentVersion','1') IS NOT DISTINCT FROM
147
+ coalesce(old.data->>'documentVersion','1')
148
+ AND NOT (new.data ? 'accountingPostingPending')
149
+ AND NOT (new.data ? 'accountingPostingPendingAt') THEN
150
+ RETURN new;
151
+ END IF;
152
+ END IF;
153
+ END IF;
154
+
155
+ source_locked :=
156
+ coalesce(old.data->>'accountingStatus','')='posted'
157
+ OR (old.collection='cheques' and coalesce(old.data->>'status','') in ('cleared','bounced','cancelled'))
158
+ OR (old.collection='contractStatements' and coalesce(old.data->>'status','') in ('posted','reversed'))
159
+ OR voucher_status in ('posted','reversed');
160
+
161
+ IF NOT source_locked THEN
162
+ RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
163
+ END IF;
164
+
165
+ IF tg_op='UPDATE' AND new.data=old.data THEN RETURN new; END IF;
166
+ RAISE EXCEPTION 'source record linked to posted/reversed accounting history is immutable';
167
+END $$;