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

Harden Office C7 OCR source provenance

* Archive C7 OCR source attachment binding * Require C7 OCR source attachment binding * Bind C7 OCR provenance UI to real attachments * Refresh C7 attachment-bound OCR runtime

davoodmehraban89 committed Oct 1, 2026 at 00:34 UTC b1c800c540e1181ffbe3efc32998c77e0c8f9fd1
4 files changed +61 -3
js/bootstrap.js
+1 -1
@@ -1,2 +1,2 @@
1 function finoraLoadScript(src){return new Promise((resolve,reject)=>{const s=document.createElement('script');s.src=src;s.onload=resolve;s.onerror=()=>reject(new Error('Failed to load '+src));document.head.appendChild(s)})}
2 -window.addEventListener('DOMContentLoaded',async()=>{try{await finoraLoadScript('js/organization-rbac.js?v=20260930-slice-b-v1');await finoraLoadScript('js/organization-invitations.js?v=20260930-eli3-v1');await finoraLoadScript('js/organization-enterprise-ux.js?v=20260930-eli4-v1');await finoraLoadScript('js/platform-modules.js?v=20261001-platform-c6-v1');await finoraLoadScript('js/office-automation.js?v=20260930-c4-v1');await finoraLoadScript('js/office-rbac.js?v=20260930-slice-b-v1');await finoraLoadScript('js/office-mature.js?v=20261001-c7-v1');await finoraLoadScript('js/organization-shell.js?v=20260930-eli4-v1')}catch(e){console.error('Finora platform extension load failed',e)}if(!currentUser)initAuthSystem();});
2 +window.addEventListener('DOMContentLoaded',async()=>{try{await finoraLoadScript('js/organization-rbac.js?v=20260930-slice-b-v1');await finoraLoadScript('js/organization-invitations.js?v=20260930-eli3-v1');await finoraLoadScript('js/organization-enterprise-ux.js?v=20260930-eli4-v1');await finoraLoadScript('js/platform-modules.js?v=20261001-platform-c6-v1');await finoraLoadScript('js/office-automation.js?v=20260930-c4-v1');await finoraLoadScript('js/office-rbac.js?v=20260930-slice-b-v1');await finoraLoadScript('js/office-mature.js?v=20261001-c7-v2');await finoraLoadScript('js/organization-shell.js?v=20260930-eli4-v1')}catch(e){console.error('Finora platform extension load failed',e)}if(!currentUser)initAuthSystem();});
js/office-mature.js
+7 -2
@@ -125,7 +125,12 @@ window.officeLoadArchiveIndex=async function(render=true){
125 };
126 window.officeRecordOcrEvidence=async function(correspondenceId){
127 if(!(canOffice('archive')||canOffice('configure')))return alert('برای ثبت Evidence OCR دسترسی لازم را ندارید.');
128 - const sourceRef=String(prompt('مرجع فایل/پیوست منبع OCR:')||'').trim();if(!sourceRef)return;
128 + const attachments=(datastore.correspondenceAttachments||[]).filter(x=>x.correspondenceId===correspondenceId);
129 + if(!attachments.length)return alert('برای ثبت OCR ابتدا باید یک پیوست/اسکن واقعی به همین نامه متصل باشد.');
130 + const choices=attachments.map((x,i)=>({n:i+1,x}));
131 + const pick=Number(prompt('فایل منبع OCR را انتخاب کنید:\n'+choices.map(z=>z.n+') '+(z.x.fileName||z.x.id)).join('\n')));
132 + const chosen=choices.find(z=>z.n===pick);if(!chosen)return;
133 + const sourceRef=chosen.x.id;
134 const provider=String(prompt('نام موتور/ارائه‌دهنده OCR:')||'').trim();if(!provider)return;
135 const language=String(prompt('زبان خروجی (مثلاً fa):')||'fa').trim();
136 const confidenceRaw=String(prompt('Confidence بین 0 و 1 (اختیاری):')||'').trim();
@@ -133,7 +138,7 @@ window.officeRecordOcrEvidence=async function(correspondenceId){
138 const text=String(prompt('متن خروجی OCR را وارد کنید:')||'');if(!text.trim())return;
139 const {error}=await sb.rpc('office_record_ocr_evidence',{p_organization_id:currentUser.organizationId,p_correspondence_id:correspondenceId,p_source_ref:sourceRef,p_provider:provider,p_language:language,p_confidence:confidence,p_text:text});
140 if(error){console.error('office OCR evidence failed',error);return alert('ثبت Evidence OCR انجام نشد: '+error.message)}
136 - await window.officeLoadArchiveIndex(true);alert('خروجی OCR با provenance ثبت شد. این قابلیت خودِ OCR را اجرا نمی‌کند.');
141 + await window.officeLoadArchiveIndex(true);alert('خروجی OCR به پیوست واقعی همین نامه متصل و با provenance ثبت شد. این قابلیت خودِ OCR را اجرا نمی‌کند.');
142 };
143 window.officeCorrectOcrEvidence=async function(ocrEvidenceId){
144 if(!(canOffice('archive')||canOffice('configure')))return alert('برای اصلاح OCR دسترسی لازم را ندارید.');
scripts/office-slice-c7-check.mjs
+2
@@ -13,6 +13,8 @@ need(m,/to_tsvector/i,'Postgres full-text document');
13 need(m,/private\.can_access_record/i,'record authorization');
14 need(m,/archive query too long/i,'bounded search query');
15 need(m,/archive result limit out of range/i,'bounded result count');
16 +need(m,/correspondenceAttachments/i,'OCR source attachment binding');
17 +need(m,/OCR source attachment not found for correspondence/i,'reject unbound OCR source reference');
18 need(m,/real Persian OCR|does not perform OCR|no OCR/i,'OCR accuracy/provider boundary');
19 const ui=fs.readFileSync('js/office-mature.js','utf8');
20 need(ui,/officeRecordOcrEvidence/i,'OCR evidence UI');
supabase/migrations/20260930205919_office_c7_bind_ocr_source_attachment.sql new
+51
@@ -0,0 +1,51 @@
1 +
2 +create or replace function private.office_record_ocr_evidence_impl(
3 + p_organization_id uuid,p_correspondence_id text,p_source_ref text,p_provider text,p_language text,p_confidence numeric,p_text text
4 +) returns text language plpgsql security definer set search_path='' as $$
5 +declare
6 + v_uid uuid:=(select auth.uid());
7 + v_source public.records%rowtype;
8 + v_attachment public.records%rowtype;
9 + v_id text;
10 +begin
11 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
12 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
13 + or not (private.has_org_capability(p_organization_id,'office_automation','archive')
14 + or private.has_org_capability(p_organization_id,'office_automation','configure'))
15 + then raise exception 'archive permission required' using errcode='42501'; end if;
16 +
17 + select * into v_source from public.records r
18 + where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
19 + if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
20 + then raise exception 'correspondence not found or not readable' using errcode='42501'; end if;
21 +
22 + if length(trim(coalesce(p_source_ref,'')))<1 or length(p_source_ref)>500
23 + then raise exception 'invalid OCR source reference' using errcode='22023'; end if;
24 +
25 + select * into v_attachment from public.records a
26 + where a.organization_id=p_organization_id
27 + and a.collection='correspondenceAttachments'
28 + and a.id=trim(p_source_ref)
29 + and a.data->>'correspondenceId'=p_correspondence_id
30 + and nullif(trim(coalesce(a.data->>'storagePath','')),'') is not null
31 + limit 1;
32 + if not found then raise exception 'OCR source attachment not found for correspondence' using errcode='42501'; end if;
33 +
34 + if length(trim(coalesce(p_provider,'')))<1 or length(p_provider)>120 then raise exception 'invalid OCR provider' using errcode='22023'; end if;
35 + if length(trim(coalesce(p_language,'')))<1 or length(p_language)>32 then raise exception 'invalid OCR language' using errcode='22023'; end if;
36 + if p_confidence is not null and (p_confidence<0 or p_confidence>1) then raise exception 'invalid OCR confidence' using errcode='22023'; end if;
37 + if length(trim(coalesce(p_text,'')))<1 or length(p_text)>200000 then raise exception 'invalid OCR text' using errcode='22023'; end if;
38 +
39 + v_id:='OCR_'||replace(gen_random_uuid()::text,'-','');
40 + insert into public.records(organization_id,owner_id,collection,id,data)
41 + values(p_organization_id,v_uid,'correspondenceAudit',v_id,
42 + jsonb_build_object(
43 + 'event','ocr_text_captured','correspondenceId',p_correspondence_id,
44 + 'sourceRef',v_attachment.id,'sourceStoragePath',v_attachment.data->>'storagePath',
45 + 'provider',trim(p_provider),'language',trim(p_language),'confidence',p_confidence,'rawText',p_text,
46 + 'actorUserId',v_uid,'at',clock_timestamp(),'performsOcr',false,'persianAccuracyVerified',false
47 + ));
48 + return v_id;
49 +end $$;
50 +revoke all on function private.office_record_ocr_evidence_impl(uuid,text,text,text,text,numeric,text) from public,anon;
51 +grant execute on function private.office_record_ocr_evidence_impl(uuid,text,text,text,text,numeric,text) to authenticated;