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

chore: rename source-link projection migration for WP1 harness

davoodmehraban89 committed Nov 17, 2026 at 17:25 UTC 433119af3932051ad0f96c6e8965e27c8ca144dd
1 file changed -77
supabase/migrations/20261117120000_stage3_canonical_source_link_projection.sql deleted
-77
@@ -1,79 +0,0 @@
1 -CREATE OR REPLACE FUNCTION private.finora_stage3_guard_posted_source()
2 -RETURNS trigger
3 -LANGUAGE plpgsql
4 -SECURITY DEFINER
5 -SET search_path = ''
6 -AS $guard$
7 -DECLARE
8 - v_kind text;
9 - v_voucher public.records%rowtype;
10 - v_effective_status text;
11 - v_projection_keys text[] := ARRAY[
12 - 'journalVoucherId','accountingStatus','accountingPostedAt',
13 - 'documentStatus','documentPostedAt','documentPostedBy',
14 - 'documentVersion','updatedAt',
15 - 'accountingPostingPending','accountingPostingPendingAt'
16 - ]::text[];
17 -BEGIN
18 - IF TG_OP NOT IN ('UPDATE','DELETE') THEN
19 - RETURN NEW;
20 - END IF;
21 - IF OLD.collection NOT IN ('invoices','purchases') THEN
22 - RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
23 - END IF;
24 - IF auth.uid() IS NULL
25 - OR private.finora_phase1a_mode(OLD.organization_id) <> 'enforce' THEN
26 - RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
27 - END IF;
28 -
29 - v_kind := CASE OLD.collection WHEN 'invoices' THEN 'sale' ELSE 'purchase' END;
30 - SELECT v.* INTO v_voucher
31 - FROM public.records v
32 - WHERE v.organization_id = OLD.organization_id
33 - AND v.collection = 'journalVouchers'
34 - AND v.data->>'sourceType' = v_kind
35 - AND v.data->>'sourceId' = OLD.id
36 - AND v.data->>'status' IN ('posted','reversed')
37 - ORDER BY v.data->>'postedAt' DESC NULLS LAST, v.id
38 - LIMIT 1;
39 -
40 - IF FOUND THEN
41 - -- A posted source's commercial/accounting inputs are immutable. The only
42 - -- permitted update is the canonical linkage/status projection below.
43 - IF (OLD.data - v_projection_keys) IS DISTINCT FROM (NEW.data - v_projection_keys) THEN
44 - RAISE EXCEPTION 'FINORA_POSTED_SOURCE_IMMUTABLE'
45 - USING ERRCODE = '23514';
46 - END IF;
47 -
48 - SELECT s.effective_status INTO v_effective_status
49 - FROM public.finora_journal_reversal_status s
50 - WHERE s.organization_id = OLD.organization_id
51 - AND s.voucher_id = v_voucher.id;
52 - v_effective_status := coalesce(v_effective_status, v_voucher.data->>'status');
53 -
54 - IF NEW.data->>'journalVoucherId' IS DISTINCT FROM v_voucher.id
55 - OR NEW.data->>'accountingStatus' IS DISTINCT FROM
56 - (CASE WHEN v_effective_status = 'reversed' THEN 'reversed' ELSE 'posted' END)
57 - OR NEW.data->>'documentStatus' IS DISTINCT FROM
58 - (CASE WHEN v_effective_status = 'reversed' THEN 'reversed' ELSE 'posted' END)
59 - OR NEW.data->>'accountingPostedAt' IS DISTINCT FROM v_voucher.data->>'postedAt'
60 - OR NEW.data->>'documentPostedAt' IS DISTINCT FROM v_voucher.data->>'postedAt'
61 - OR NEW.data->>'documentPostedBy' IS DISTINCT FROM
62 - coalesce(v_voucher.data->>'postedBy', OLD.data->>'documentPostedBy', auth.uid()::text)
63 - OR coalesce(NEW.data->>'documentVersion','1') IS DISTINCT FROM
64 - coalesce(OLD.data->>'documentVersion','1')
65 - OR NEW.data ? 'accountingPostingPending'
66 - OR NEW.data ? 'accountingPostingPendingAt' THEN
67 - RAISE EXCEPTION 'FINORA_POSTED_SOURCE_LINK_PROJECTION_INVALID'
68 - USING ERRCODE = '23514';
69 - END IF;
70 - END IF;
71 -
72 - RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
73 -END
74 -$guard$;
75 -
76 -REVOKE ALL ON FUNCTION private.finora_stage3_guard_posted_source()
77 -FROM PUBLIC, anon, authenticated;