سامانه صدور فاکتور و حسابداری
fix(accounting): pin journal RPCs to active tenant context
davoodmehraban89 committed
Nov 16, 2026 at 07:11 UTC
3e88b4758e935982e4d4f260ec9c8fe1710a3f85
2 files changed
+23
-12
js/journal-rpc.js
+16
-9
@@ -7,8 +7,10 @@ let jeRpcStatusRefreshToken=0;
7
8
async function jeRpcAssertContext(){
9
if(!currentUser?.id||!currentUser?.organizationId)throw new Error('سازمان فعال برای عملیات دفتر کل مشخص نیست.');
10
+ const uid=currentUser.id,org=currentUser.organizationId;
11
if(typeof window.requireFreshFinoraMembership==='function'&&!await window.requireFreshFinoraMembership())throw new Error('عضویت فعال سازمان تأیید نشد.');
11
- return currentUser.organizationId;
12
+ if(currentUser?.id!==uid||currentUser?.organizationId!==org)throw new Error('سازمان فعال هنگام اجرای عملیات تغییر کرد؛ دوباره تلاش کنید.');
13
+ return org;
14
}
15
function jeRpcDate(value){
16
const date=toEnDigits(String(value||getJalaliNumeric()).trim()).slice(0,10);
@@ -30,14 +32,19 @@ function jeRpcLines(lines){
32
}))}));
33
}
34
async function jeRpcCall(name,args){
33
- await jeRpcAssertContext();
35
+ const activeOrg=await jeRpcAssertContext();
36
+ if(args.p_organization_id&&args.p_organization_id!==activeOrg)throw new Error('سازمان فعال تغییر کرده است؛ عملیات برای جلوگیری از ثبت بینسازمانی متوقف شد.');
37
if(syncPending||syncRunning)throw new Error('ابتدا همگامسازی تغییرات دیگر را کامل کنید؛ عملیات دفتر کل برای جلوگیری از بازنویسی داده معلق متوقف شد.');
38
const {data,error}=await sb.rpc(name,args);
39
if(error){const e=new Error(error.message||'عملیات دفتر کل در سرور انجام نشد.');e.code=error.code||'';throw e;}
40
return data;
41
}
39
-async function jeRpcRefresh(){
40
- try{await pullAll();}catch(err){const e=new Error('عملیات در سرور ثبت شد، اما تازهسازی اطلاعات کامل نشد. صفحه را تازه کنید و پیش از ارسال مجدد، وضعیت سند را بررسی کنید.');e.code='FINORA_RPC_COMMITTED_REFRESH_FAILED';throw e;}
42
+async function jeRpcRefresh(expectedOrg){
43
+ try{
44
+ if(currentUser?.organizationId!==expectedOrg)throw new Error('FINORA_ORGANIZATION_CONTEXT_CHANGED');
45
+ await pullAll();
46
+ if(currentUser?.organizationId!==expectedOrg)throw new Error('FINORA_ORGANIZATION_CONTEXT_CHANGED');
47
+ }catch(err){const e=new Error('عملیات در سرور ثبت شد، اما سازمان/اطلاعات صفحه تغییر کرد. صفحه را تازه کنید و پیش از ارسال مجدد، وضعیت سند را بررسی کنید.');e.code='FINORA_RPC_COMMITTED_REFRESH_FAILED';throw e;}
48
await jeRpcRefreshEffectiveStatuses();
49
}
50
async function jeRpcRefreshEffectiveStatuses(){
@@ -58,7 +65,7 @@ async function jeRpcCreateDraft(input){
65
const data=await jeRpcCall('finora_journal_create_draft',{
66
p_organization_id:org,p_voucher_id:id,p_header:header,p_lines:jeRpcLines(input.lines)
67
});
61
- await jeRpcRefresh();
68
+ await jeRpcRefresh(org);
69
return data?.voucher||getMyJournalVouchers().find(x=>x.id===id)||data;
70
}
71
async function jeRpcUpdateDraft(voucherId,input){
@@ -71,7 +78,7 @@ async function jeRpcUpdateDraft(voucherId,input){
78
p_organization_id:org,p_voucher_id:voucherId,p_expected_version:Number(v._serverVersion),
79
p_header:header,p_lines:jeRpcLines(input.lines)
80
});
74
- await jeRpcRefresh();
81
+ await jeRpcRefresh(org);
82
return data?.voucher||getMyJournalVouchers().find(x=>x.id===voucherId)||data;
83
}
84
async function jeRpcPost(voucherId){
@@ -82,7 +89,7 @@ async function jeRpcPost(voucherId){
89
const data=await jeRpcCall('finora_journal_post',{
90
p_organization_id:org,p_voucher_id:voucherId,p_expected_version:Number(v._serverVersion)
91
});
85
- await jeRpcRefresh();
92
+ await jeRpcRefresh(org);
93
return data?.voucher||getMyJournalVouchers().find(x=>x.id===voucherId)||data;
94
}
95
async function jeRpcDeleteDraft(voucherId){
@@ -93,7 +100,7 @@ async function jeRpcDeleteDraft(voucherId){
100
const data=await jeRpcCall('finora_journal_delete_draft',{
101
p_organization_id:org,p_voucher_id:voucherId,p_expected_version:Number(v._serverVersion)
102
});
96
- await jeRpcRefresh();
103
+ await jeRpcRefresh(org);
104
return data;
105
}
106
async function jeRpcReverse(voucherId,date,reason){
@@ -109,7 +116,7 @@ async function jeRpcReverse(voucherId,date,reason){
116
p_date:jeRpcDate(date),p_reason:String(reason||'').trim(),
117
p_expected_version:Number(v._serverVersion),p_allocations:null
118
});
112
- await jeRpcRefresh();
119
+ await jeRpcRefresh(org);
120
return data;
121
}
122
async function jeRpcCopyVoucher(voucherId,date){
js/journal-ui.js
+7
-3
@@ -57,10 +57,14 @@ async function jeDeleteUi(id){if(!confirm('پیشنویس حذف شود؟'))r
57
function jeStatusLabel(v){return v==='draft'?'پیشنویس':v==='posted'?'قطعی':v==='reversed'?'برگشتشده':v}
58
function jeTypeLabel(v){return({manual:'عادی',adjustment:'اصلاحی',opening:'افتتاحیه',closing:'اختتامیه',copy:'کپی',reversal:'برگشت'})[v]||v||'عادی'}
59
function jeJournalActions(v,status){
60
- if(status==='draft')return '<button class="btn btn-success btn-inline" onclick="jePostUi(\''+v.id+'\')">قطعی</button> '+((v.sourceType||'manual')==='manual'&&!v.sourceId?'<button class="btn btn-secondary btn-inline" onclick="jeLoadDraftUi(\''+v.id+'\')">ویرایش موقت</button> ':'')+'<button class="btn btn-danger btn-inline" onclick="jeDeleteUi(\''+v.id+'\')">حذف موقت</button> ';
60
+ const sourceType=v.sourceType||'manual';
61
+ if(status==='draft'){
62
+ if(sourceType!=='manual'||v.sourceId)return '<span class="af-hint">سند منبع؛ عملیات از گردشکار منبع انجام میشود</span> ';
63
+ return '<button class="btn btn-success btn-inline" onclick="jePostUi(\\''+v.id+'\\')">قطعی</button> <button class="btn btn-secondary btn-inline" onclick="jeLoadDraftUi(\\''+v.id+'\\')">ویرایش موقت</button> <button class="btn btn-danger btn-inline" onclick="jeDeleteUi(\\''+v.id+'\\')">حذف موقت</button> ';
64
+ }
65
if(status==='posted'){
62
- if((v.sourceType||'manual')==='manual'&&!v.sourceId)return '<button class="btn btn-secondary btn-inline" onclick="jeCancelPostedUi(\''+v.id+'\')">ابطال / سند معکوس</button> ';
63
- return '<button class="btn btn-secondary btn-inline" onclick="jeReverseUi(\''+v.id+'\')">سند معکوس</button> ';
66
+ if(sourceType==='manual'&&!v.sourceId)return '<button class="btn btn-secondary btn-inline" onclick="jeCancelPostedUi(\\''+v.id+'\\')">ابطال / سند معکوس</button> ';
67
+ return '<button class="btn btn-secondary btn-inline" onclick="jeReverseUi(\\''+v.id+'\\')">سند معکوس</button> ';
68
}
69
if(status==='partially_reversed')return '<span class="af-hint">برگشت جزئی؛ تکمیل تخصیص در مرحله بعد</span> ';
70
return '';