@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / abc9160
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 22:00 UTC

feat: stamp ATF identity on draft source documents

davoodmehraban89 committed Nov 18, 2026 at 08:59 UTC abc916095fef9e8241436faecdd2cf43e2871b5f
1 file changed +36 -1
supabase/migrations/20261118090000_document_lifecycle_atf_and_atomic_delete.sql
+36 -1
@@ -72,6 +72,41 @@ CREATE INDEX IF NOT EXISTS idx_finora_document_atf_source
72 ON public.records(organization_id,(data->>'atfNumber'))
73 WHERE collection IN ('invoices','purchases') AND coalesce(data->>'atfNumber','')<>'';
74
75 +
76 +-- Draft commercial documents receive the same permanent ATF identity at first persistence.
77 +-- This covers the current sales/purchase source adapters; additional document modules
78 +-- will be added only after their own delete/link contracts are tested.
79 +CREATE OR REPLACE FUNCTION private.finora_stamp_source_document_identity()
80 +RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
81 +AS $
82 +DECLARE fy text; tmp text;
83 +BEGIN
84 + IF new.collection NOT IN ('invoices','purchases') THEN RETURN new; END IF;
85 + IF coalesce(new.data->>'atfNumber','')='' THEN
86 + new.data:=new.data||jsonb_build_object('atfNumber',private.finora_next_document_atf(new.organization_id));
87 + END IF;
88 + IF coalesce(new.data->>'documentStatus','draft')='draft' AND coalesce(new.data->>'temporaryNumber','')='' THEN
89 + SELECT id INTO fy FROM public.records
90 + WHERE organization_id=new.organization_id AND collection='fiscalYears'
91 + AND data->>'companyId'=new.data->>'companyId'
92 + AND coalesce(data->>'status','open')<>'locked'
93 + AND coalesce(new.data->>'date','')>=data->>'startDate'
94 + AND coalesce(new.data->>'date','')<=data->>'endDate'
95 + ORDER BY id LIMIT 1;
96 + IF fy IS NOT NULL THEN
97 + tmp:=private.finora_next_document_temp(new.organization_id,fy);
98 + new.data:=new.data||jsonb_build_object('temporaryNumber',tmp);
99 + END IF;
100 + END IF;
101 + RETURN new;
102 +END $;
103 +
104 +DROP TRIGGER IF EXISTS trg_finora_stamp_source_document_identity ON public.records;
105 +CREATE TRIGGER trg_finora_stamp_source_document_identity
106 +BEFORE INSERT ON public.records FOR EACH ROW
107 +EXECUTE FUNCTION private.finora_stamp_source_document_identity();
108 +REVOKE ALL ON FUNCTION private.finora_stamp_source_document_identity() FROM PUBLIC,anon,authenticated;
109 +
110 -- Replace the draft core so every new journal document receives ATF + temporary number.
111 CREATE OR REPLACE FUNCTION private.finora_phase1a_create_core(
112 p_org uuid,p_id text,p_header jsonb,p_lines jsonb)
@@ -356,4 +391,4 @@ BEGIN
391 END $fn$;
392
393 REVOKE ALL ON FUNCTION private.finora_next_document_atf(uuid) FROM PUBLIC,anon,authenticated;
359 -REVOKE ALL ON FUNCTION private.finora_next_document_temp(uuid,text) FROM PUBLIC,anon,authenticated;
394 +REVOKE ALL ON FUNCTION private.finora_next_document_temp(uuid,text) FROM PUBLIC,anon,authenticated;
\ No newline at end of file