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

Implement Office C7 full-text OCR provenance UI

davoodmehraban89 committed Oct 1, 2026 at 00:24 UTC db6f885ba92a0744c183642510576b581b2e366d
1 file changed +40 -6
js/office-mature.js
+40 -6
@@ -100,25 +100,59 @@ function canOffice(c){return typeof hasFinoraCapability==='function'&&hasFinoraC
100 function renderArchiveIndex(){
101 const root=document.getElementById('view-office');if(!root)return;
102 const rows=window.officeArchiveIndex();
103 + const canArchive=canOffice('archive')||canOffice('configure');
104 const body=rows.length?rows.map(r=>{
105 const tags=Array.isArray(r.tags)?r.tags.join('، '):'';
105 - const classify=(canOffice('archive')||canOffice('configure'))?'<button class="btn btn-secondary btn-inline" onclick="officeClassifyPrompt(\''+oe(r.correspondence_id)+'\')">طبقه‌بندی</button> ':'';
106 - const link=(canOffice('edit')||canOffice('refer'))?'<button class="btn btn-secondary btn-inline" onclick="officeLinkPrompt(\''+oe(r.correspondence_id)+'\')">ارتباط</button>':'';
107 - return '<tr><td><strong>'+oe(r.register_number||'—')+'</strong></td><td>'+oe(r.subject||'—')+'</td><td>'+oe(r.folder||'—')+'</td><td>'+oe(r.classification_code||'—')+'</td><td>'+oe(tags||'—')+'</td><td>'+Number(r.related_count||0).toLocaleString('fa-IR')+'</td><td><button class="btn btn-secondary btn-inline" onclick="officeOpenRecord(\''+oe(r.correspondence_id)+'\')">مشاهده</button> '+classify+link+'</td></tr>'
108 - }).join(''):'<tr><td colspan="7" class="af-empty">مکاتبه‌ای مطابق فیلتر آرشیو یافت نشد.</td></tr>';
109 - root.innerHTML='<div class="af-head"><div><h1>جست‌وجو و آرشیو مکاتبات</h1><p>جست‌وجوی سرورمحور بر اساس فراداده، پوشه و کد طبقه‌بندی؛ ارتباط پاسخ/مرتبط نیز به‌صورت سابقه غیرقابل‌تغییر ثبت می‌شود.</p></div></div><div class="card"><div class="form-row"><div class="form-group" style="grid-column:span 2"><label>جست‌وجو</label><input id="office-archive-q" class="form-control" placeholder="شماره، موضوع، طرف مکاتبه، برچسب"></div><div class="form-group"><label>پوشه</label><input id="office-archive-folder" class="form-control"></div><div class="form-group"><label>کد طبقه‌بندی</label><input id="office-archive-code" class="form-control"></div></div><button class="btn btn-primary btn-inline" onclick="officeLoadArchiveIndex(true)">جست‌وجو</button></div><div class="card"><div class="table-responsive"><table><thead><tr><th>ثبت</th><th>موضوع</th><th>پوشه</th><th>کد</th><th>برچسب‌ها</th><th>ارتباط‌ها</th><th>عملیات</th></tr></thead><tbody>'+body+'</tbody></table></div></div>';
106 + const classify=canArchive?'<button class="btn btn-secondary btn-inline" onclick="officeClassifyPrompt(\''+oe(r.correspondence_id)+'\')">طبقه‌بندی</button> ':'';
107 + const link=(canOffice('edit')||canOffice('refer'))?'<button class="btn btn-secondary btn-inline" onclick="officeLinkPrompt(\''+oe(r.correspondence_id)+'\')">ارتباط</button> ':'';
108 + const ocrState=r.ocr_state==='corrected'?'اصلاح انسانی · نسخه '+Number(r.ocr_version||0).toLocaleString('fa-IR'):r.ocr_state==='provider_output'?'خروجی OCR · نسخه '+Number(r.ocr_version||0).toLocaleString('fa-IR'):'بدون متن OCR';
109 + const ocrActions=canArchive?'<button class="btn btn-secondary btn-inline" onclick="officeRecordOcrText(\''+oe(r.correspondence_id)+'\')">نسخه OCR</button> '+(r.ocr_state!=='none'?'<button class="btn btn-secondary btn-inline" onclick="officeCorrectOcrText(\''+oe(r.correspondence_id)+'\')">اصلاح متن</button> ':''):'';
110 + return '<tr><td><strong>'+oe(r.register_number||'—')+'</strong></td><td>'+oe(r.subject||'—')+'</td><td>'+oe(r.folder||'—')+'</td><td>'+oe(r.classification_code||'—')+'</td><td>'+oe(tags||'—')+'</td><td>'+ocrState+(r.ocr_source_engine?' · '+oe(r.ocr_source_engine):'')+'</td><td>'+Number(r.search_rank||0).toLocaleString('fa-IR',{maximumFractionDigits:3})+'</td><td>'+Number(r.related_count||0).toLocaleString('fa-IR')+'</td><td><button class="btn btn-secondary btn-inline" onclick="officeOpenRecord(\''+oe(r.correspondence_id)+'\')">مشاهده</button> '+classify+link+ocrActions+'</td></tr>'
111 + }).join(''):'<tr><td colspan="9" class="af-empty">مکاتبه‌ای مطابق فیلتر آرشیو یافت نشد.</td></tr>';
112 + root.innerHTML='<div class="af-head"><div><h1>جست‌وجوی تمام‌متن و آرشیو</h1><p>جست‌وجوی رتبه‌بندی‌شده روی شماره، موضوع، طرف مکاتبه، متن نامه، طبقه‌بندی و آخرین متن OCR. ثبت OCR در C7 فقط provenance/correction است و به معنی تأیید دقت OCR فارسی نیست.</p></div></div><div class="card"><div class="form-row"><div class="form-group" style="grid-column:span 2"><label>جست‌وجوی تمام‌متن</label><input id="office-archive-q" class="form-control" placeholder="مثال: فاکتور تهران یا &quot;قرارداد خدمات&quot;"></div><div class="form-group"><label>پوشه</label><input id="office-archive-folder" class="form-control"></div><div class="form-group"><label>کد طبقه‌بندی</label><input id="office-archive-code" class="form-control"></div></div><button class="btn btn-primary btn-inline" onclick="officeLoadArchiveIndex(true)">جست‌وجو</button></div><div class="card"><div class="table-responsive"><table><thead><tr><th>ثبت</th><th>موضوع</th><th>پوشه</th><th>کد</th><th>برچسب‌ها</th><th>OCR</th><th>رتبه</th><th>ارتباط‌ها</th><th>عملیات</th></tr></thead><tbody>'+body+'</tbody></table></div></div>';
113 }
114 window.officeLoadArchiveIndex=async function(render=true){
115 if(!currentUser?.organizationId)return [];
116 const q=document.getElementById('office-archive-q')?.value||null;
117 const folder=document.getElementById('office-archive-folder')?.value||null;
118 const code=document.getElementById('office-archive-code')?.value||null;
116 - const {data,error}=await sb.rpc('office_archive_search',{p_organization_id:currentUser.organizationId,p_query:q,p_folder:folder,p_classification_code:code,p_related_to:null});
119 + const {data,error}=await sb.rpc('office_archive_full_text_search',{p_organization_id:currentUser.organizationId,p_query:q,p_folder:folder,p_classification_code:code,p_related_to:null});
120 if(error){console.error('office archive search failed',error);if(render)alert('جست‌وجوی آرشیو ناموفق بود: '+error.message);return []}
121 window.FINORA_ARCHIVE_INDEX=Array.isArray(data)?data:[];
122 if(render)renderArchiveIndex();
123 return window.FINORA_ARCHIVE_INDEX;
124 };
125 +window.officeRecordOcrText=async function(correspondenceId){
126 + if(!(canOffice('archive')||canOffice('configure')))return alert('برای ثبت متن OCR دسترسی آرشیو لازم است.');
127 + const engine=String(prompt('نام موتور/منبع OCR:')||'').trim();if(!engine)return false;
128 + const sourceRef=String(prompt('مرجع فایل/اسکن (اختیاری):')||'').trim();
129 + const rawText=String(prompt('متن خام OCR:')||'').trim();if(!rawText)return false;
130 + const confidenceRaw=String(prompt('Confidence اختیاری بین 0 و 1:')||'').trim();
131 + let confidence=null;if(confidenceRaw){confidence=Number(confidenceRaw);if(!Number.isFinite(confidence)||confidence<0||confidence>1)return alert('Confidence باید بین 0 و 1 باشد.')}
132 + const {error}=await sb.rpc('office_record_ocr_text',{
133 + p_organization_id:currentUser.organizationId,p_correspondence_id:correspondenceId,p_mode:'provider_output',
134 + p_raw_text:rawText,p_corrected_text:null,p_source_engine:engine,p_source_ref:sourceRef,p_confidence:confidence
135 + });
136 + if(error){console.error('office OCR provenance failed',error);return alert('ثبت متن OCR انجام نشد: '+error.message)}
137 + await window.officeLoadArchiveIndex(true);
138 + alert('خروجی OCR با provenance غیرقابل‌تغییر ثبت شد. دقت OCR فارسی در C7 تأیید نشده است.');
139 + return true;
140 +};
141 +window.officeCorrectOcrText=async function(correspondenceId){
142 + if(!(canOffice('archive')||canOffice('configure')))return alert('برای اصلاح OCR دسترسی آرشیو لازم است.');
143 + const {data,error}=await sb.rpc('office_latest_ocr_text',{p_organization_id:currentUser.organizationId,p_correspondence_id:correspondenceId});
144 + if(error)return alert('دریافت متن OCR ناموفق بود: '+error.message);
145 + const latest=Array.isArray(data)?data[0]:null;if(!latest)return alert('برای این نامه هنوز متن OCR ثبت نشده است.');
146 + const corrected=String(prompt('متن اصلاح‌شده انسانی:',latest.effective_text||'')||'').trim();if(!corrected)return false;
147 + const {error:saveError}=await sb.rpc('office_record_ocr_text',{
148 + p_organization_id:currentUser.organizationId,p_correspondence_id:correspondenceId,p_mode:'human_correction',
149 + p_raw_text:null,p_corrected_text:corrected,p_source_engine:null,p_source_ref:null,p_confidence:null
150 + });
151 + if(saveError){console.error('office OCR correction failed',saveError);return alert('اصلاح متن OCR ثبت نشد: '+saveError.message)}
152 + await window.officeLoadArchiveIndex(true);
153 + alert('اصلاح انسانی به‌عنوان نسخه جدید ثبت شد؛ متن خام OCR قبلی حفظ شده است.');
154 + return true;
155 +};
156 window.officeClassifyPrompt=async function(correspondenceId){
157 if(!(canOffice('archive')||canOffice('configure')))return alert('برای طبقه‌بندی آرشیو دسترسی لازم را ندارید.');
158 const folder=String(prompt('پوشه آرشیو:')||'').trim();