سامانه صدور فاکتور و حسابداری
fix(db): preserve effective reversal state on autopost replay
davoodmehraban89 committed
Nov 17, 2026 at 14:21 UTC
ad62e9983b83b2e5810e08ea8899ace77b74c6a4
1 file changed
+4
-2
supabase/migrations/20261117110000_wp1_autopost_source_link_atomic.sql
+4
-2
@@ -7,7 +7,7 @@ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
7
AS $f$
8
DECLARE typ text:=p_header->>'sourceType';coll text;source public.records%rowtype;
9
cfg_version bigint;lines jsonb;draft jsonb;posted jsonb;voucher public.records%rowtype;
10
- prior public.records%rowtype;
10
+ prior public.records%rowtype;v_prior_effective_status text;
11
BEGIN
12
IF typ NOT IN ('sale','purchase') THEN
13
RAISE EXCEPTION 'FINORA_AUTOPOST_CONTRACT_UNSUPPORTED' USING ERRCODE='23514';END IF;
@@ -34,9 +34,11 @@ BEGIN
34
AND data->>'sourceVersion'=coalesce(p_header->>'sourceVersion','1')
35
AND data->>'status' IN ('posted','reversed') LIMIT 1 FOR SHARE;
36
IF FOUND THEN
37
+ SELECT effective_status INTO v_prior_effective_status FROM public.finora_journal_reversal_status
38
+ WHERE organization_id=p_organization_id AND voucher_id=prior.id;
39
UPDATE public.records r SET data=(r.data-'accountingPostingPending'-'accountingPostingPendingAt')||
40
jsonb_build_object('journalVoucherId',prior.id,
39
- 'accountingStatus',CASE WHEN prior.data->>'status'='reversed' THEN 'reversed' ELSE 'posted' END,
41
+ 'accountingStatus',CASE WHEN coalesce(v_prior_effective_status,prior.data->>'status')='reversed' THEN 'reversed' ELSE 'posted' END,
42
'accountingPostedAt',coalesce(prior.data->>'postedAt',r.data->>'accountingPostedAt'),
43
'documentStatus',CASE WHEN prior.data->>'status'='reversed' THEN 'reversed' ELSE 'posted' END,
44
'documentPostedAt',coalesce(prior.data->>'postedAt',r.data->>'documentPostedAt'),