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

Implement Office C2 referral work queue and actions

davoodmehraban89 committed Sep 30, 2026 at 18:57 UTC e6278172703d2a60d818fe6298b2528b59d83716
1 file changed +78 -2
js/office-mature.js
+78 -2
@@ -1,5 +1,7 @@
1 -/* Office Slice C1 mature correspondence read evidence. */
1 +/* Office Slice C1/C2 mature correspondence evidence + actionable referrals. */
2 (function(){
3 +function oe(v){return typeof esc==='function'?esc(v):String(v??'')}
4 +function faDate(v){if(!v)return '—';try{return new Date(v).toLocaleString('fa-IR')}catch(_){return String(v)}}
5 window.officeReadEvidenceFor=function(correspondenceId){return (datastore.correspondenceAudit||[]).filter(x=>x.event==='read'&&x.correspondenceId===correspondenceId)};
6 window.officeMarkReadEvidence=async function(correspondenceId){
7 if(!currentUser?.organizationId||!correspondenceId)return false;
@@ -17,4 +19,78 @@ window.officeOpenRecord=async function(id){
19 }
20 return baseOfficeOpenRecord(id);
21 };
20 -})();
22 +
23 +window.FINORA_REFERRAL_QUEUE=[];
24 +window.officeReferralWorkQueue=function(){return Array.isArray(window.FINORA_REFERRAL_QUEUE)?window.FINORA_REFERRAL_QUEUE:[]};
25 +window.officeLoadReferralQueue=async function(render=true){
26 + if(!currentUser?.organizationId)return [];
27 + const {data,error}=await sb.rpc('office_referral_work_queue',{p_organization_id:currentUser.organizationId});
28 + if(error){console.error('office referral queue failed',error);if(render)alert('دریافت کارتابل ارجاعات ناموفق بود: '+error.message);return []}
29 + window.FINORA_REFERRAL_QUEUE=Array.isArray(data)?data:[];
30 + if(render)renderReferralWork();
31 + return window.FINORA_REFERRAL_QUEUE;
32 +};
33 +window.officeActOnReferral=async function(referralId,action){
34 + if(!currentUser?.organizationId||!referralId)return false;
35 + const label=action==='acknowledged'?'تأیید دریافت':'تکمیل ارجاع';
36 + const note=prompt('یادداشت '+label+' (اختیاری):')||'';
37 + const {error}=await sb.rpc('office_act_on_referral',{
38 + p_organization_id:currentUser.organizationId,p_referral_id:referralId,p_action:action,p_note:note
39 + });
40 + if(error){console.error('office referral action failed',error);alert(label+' انجام نشد: '+error.message);return false}
41 + await window.officeLoadReferralQueue(false);
42 + renderReferralWork();
43 + alert(label+' با سابقه غیرقابل‌تغییر ثبت شد.');
44 + return true;
45 +};
46 +function renderReferralWork(){
47 + const root=document.getElementById('view-office');if(!root)return;
48 + const rows=window.officeReferralWorkQueue();
49 + const body=rows.length?rows.map(r=>{
50 + const mine=String(r.to_user_id||'')===String(currentUser?.id||'');
51 + const status=r.state==='completed'?'تکمیل‌شده':r.state==='acknowledged'?'دریافت‌شده':'ارسال‌شده';
52 + const actions=mine&&r.state==='sent'
53 + ?'<button class="btn btn-primary btn-inline" onclick="officeActOnReferral(\''+oe(r.referral_id)+'\',\'acknowledged\')">تأیید دریافت</button>'
54 + :mine&&r.state==='acknowledged'
55 + ?'<button class="btn btn-primary btn-inline" onclick="officeActOnReferral(\''+oe(r.referral_id)+'\',\'completed\')">تکمیل</button>'
56 + :'';
57 + return '<tr><td><strong>'+oe(r.register_number||'—')+'</strong></td><td>'+oe(r.subject||'—')+'</td><td>'+status+(r.overdue?' <strong>· معوق</strong>':'')+'</td><td>'+faDate(r.due_at)+'</td><td>'+oe(r.note||'—')+'</td><td><button class="btn btn-secondary btn-inline" onclick="officeOpenRecord(\''+oe(r.correspondence_id)+'\')">نامه</button> '+actions+'</td></tr>'
58 + }).join(''):'<tr><td colspan="6" class="af-empty">ارجاع فعالی برای نمایش وجود ندارد.</td></tr>';
59 + root.innerHTML='<div class="af-head"><div><h1>ارجاعات و پیگیری</h1><p>کارتابل سرورمحور ارجاعات؛ وضعیت دریافت، تکمیل و مهلت پیگیری از سابقه غیرقابل‌تغییر محاسبه می‌شود.</p></div><div><button class="btn btn-secondary btn-inline" onclick="officeLoadReferralQueue(true)">بازخوانی</button></div></div><div class="card"><div class="table-responsive"><table><thead><tr><th>نامه</th><th>موضوع</th><th>وضعیت</th><th>مهلت</th><th>یادداشت</th><th>عملیات</th></tr></thead><tbody>'+body+'</tbody></table></div></div>';
60 +}
61 +const c1OfficeRender=window.officeRender;
62 +window.officeRender=function(task='inbox'){
63 + if(task==='work'){
64 + c1OfficeRender('inbox');
65 + renderReferralWork();
66 + window.officeLoadReferralQueue(true);
67 + return;
68 + }
69 + return c1OfficeRender(task);
70 +};
71 +
72 +window.officeReferPrompt=async function(correspondenceId){
73 + if(typeof hasFinoraCapability==='function'&&!hasFinoraCapability('office_automation','refer'))return alert('برای ارجاع دسترسی سازمانی لازم را ندارید.');
74 + const {data:members,error}=await sb.from('organization_members').select('id,user_id,position_title,is_owner').eq('organization_id',currentUser.organizationId).eq('status','active');
75 + if(error)return alert(error.message);
76 + const ids=(members||[]).map(x=>x.user_id),profiles=ids.length?(await sb.from('profiles').select('id,full_name,email').in('id',ids)).data||[]:[];
77 + const choices=(members||[]).filter(x=>x.user_id!==currentUser.id).map((m,i)=>({n:i+1,m,p:profiles.find(p=>p.id===m.user_id)}));
78 + if(!choices.length)return alert('عضو دیگری برای ارجاع در این سازمان وجود ندارد.');
79 + const text=choices.map(x=>x.n+') '+(x.p?.full_name||x.p?.email||x.m.position_title||'کاربر')).join('\n');
80 + const pick=Number(prompt('ارجاع به کدام عضو؟\n'+text)),chosen=choices.find(x=>x.n===pick);if(!chosen)return;
81 + const note=prompt('یادداشت ارجاع (اختیاری):')||'';
82 + const dueRaw=String(prompt('مهلت ارجاع (اختیاری، نمونه 2026-10-01T12:00):')||'').trim();
83 + let dueAt=null;
84 + if(dueRaw){const d=new Date(dueRaw);if(Number.isNaN(d.getTime()))return alert('فرمت مهلت معتبر نیست.');dueAt=d.toISOString()}
85 + const {error:rpcError}=await sb.rpc('office_refer_correspondence_v2',{
86 + p_organization_id:currentUser.organizationId,p_correspondence_id:correspondenceId,
87 + p_to_member_id:chosen.m.id,p_note:note,p_due_at:dueAt
88 + });
89 + if(rpcError){
90 + console.error('office referral failed',rpcError);
91 + if(/target member cannot read correspondence/i.test(rpcError.message||''))return alert('این کاربر طبق سطح دسترسی/دامنه/محرمانگی مجاز به خواندن این نامه نیست.');
92 + return alert('ارجاع انجام نشد: '+rpcError.message);
93 + }
94 + await pullAll();alert('ارجاع ثبت شد و در کارتابل پیگیری قابل مشاهده است.');officeRender('work');
95 +};
96 +})();
\ No newline at end of file