سامانه صدور فاکتور و حسابداری
fix: freeze source edits during ambiguous journal autopost
davoodmehraban89 committed
Nov 17, 2026 at 14:10 UTC
c00b615964e5d42b3806da68f476fc13a01bace6
1 file changed
+26
-7
js/journal-rpc.js
+26
-7
@@ -39,16 +39,17 @@ function jeRpcLines(lines){
39
}
40
async function jeRpcCall(name,args){
41
if(jeRpcBusy)throw new Error('یک عملیات دفتر کل در حال اجراست؛ از ارسال دوباره درخواست خودداری کنید.');
42
- jeRpcBusy=true;
42
+ jeRpcBusy=true;let rpcSubmitted=false;
43
try{
44
const activeOrg=await jeRpcAssertContext();
45
if(args.p_organization_id&&args.p_organization_id!==activeOrg)throw new Error('سازمان فعال تغییر کرده است؛ عملیات برای جلوگیری از ثبت بینسازمانی متوقف شد.');
46
if(syncPending||syncRunning)throw new Error('ابتدا همگامسازی تغییرات دیگر را کامل کنید؛ عملیات دفتر کل برای جلوگیری از بازنویسی داده معلق متوقف شد.');
47
+ rpcSubmitted=true;
48
const {data,error}=await sb.rpc(name,args);
49
if(error){const e=new Error(error.message||'عملیات دفتر کل در سرور انجام نشد.');e.code=error.code||'';throw e;}
50
if(name==='finora_journal_read_graph')jeRpcBusy=false;
51
return data;
51
- }catch(err){jeRpcBusy=false;throw err;}
52
+ }catch(err){if(err&&typeof err==='object')err.rpcSubmitted=rpcSubmitted;jeRpcBusy=false;throw err;}
53
}
54
async function jeRpcRefresh(expectedOrg){
55
try{
@@ -213,6 +214,7 @@ async function jeRpcAutoPost(kind,sourceId){
214
source.documentStatus='posted';source.documentPostedAt=existing.postedAt||new Date().toISOString();
215
source.documentPostedBy=existing.postedBy||currentUser.id;
216
source.documentVersion=Number(source.documentVersion||1);
217
+ delete source.accountingPostingPending;delete source.accountingPostingPendingAt;
218
await jeRpcPersistSourceRecord(collection,sourceId);
219
return existing;
220
}
@@ -224,17 +226,33 @@ async function jeRpcAutoPost(kind,sourceId){
226
if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ رکورد منبع پیدا نشد.');
227
const sourceVersion=Number(source.accountingVersion||1);
228
if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه رویداد منبع معتبر نیست.');
227
- // Ensure the source is committed before the server RPC attempts to lock/read it.
229
+ // Freeze edits/deletes before the RPC so an ambiguous network outcome
230
+ // cannot leave a mutable draft paired with an already-posted voucher.
231
+ source.accountingPostingPending=true;
232
+ source.accountingPostingPendingAt=source.accountingPostingPendingAt||new Date().toISOString();
233
+ // Ensure the source and its pending marker are committed before the RPC.
234
await jeRpcPersistSourceRecord(collection,sourceId);
235
source=jeRpcSourceRecord(collection,sourceId);
236
const header={companyId:source.companyId,fiscalYearId:fy.id,date,
237
description:String(source.description||((kind==='sale'?'فروش ':'خرید ')+(source.number||source.id))),
238
reference:String(source.reference||source.number||''),voucherType:kind,
239
sourceType:kind,sourceId:String(source.id),sourceVersion:String(sourceVersion)};
234
- const result=await jeRpcCall('finora_journal_autopost',{
235
- p_organization_id:org,p_voucher_id:afId('JV'),p_source_collection:collection,
236
- p_source_id:String(source.id),p_header:header,p_lines:[]
237
- });
240
+ let result;
241
+ try{
242
+ result=await jeRpcCall('finora_journal_autopost',{
243
+ p_organization_id:org,p_voucher_id:afId('JV'),p_source_collection:collection,
244
+ p_source_id:String(source.id),p_header:header,p_lines:[]
245
+ });
246
+ }catch(err){
247
+ // A returned PostgreSQL error means the transaction rolled back. A transport
248
+ // failure after submission is ambiguous and must keep the source frozen.
249
+ if(err?.code||err?.rpcSubmitted===false){
250
+ const failedSource=jeRpcSourceRecord(collection,sourceId);
251
+ if(failedSource){delete failedSource.accountingPostingPending;delete failedSource.accountingPostingPendingAt;
252
+ try{await jeRpcPersistSourceRecord(collection,sourceId);}catch(clearErr){err.pendingMarkerClearError=clearErr?.message||String(clearErr);}}
253
+ }
254
+ throw err;
255
+ }
256
const voucher=result?.voucher;
257
if(!voucher?.id||voucher.status!=='posted')
258
throw new Error('پاسخ ثبت خودکار سرور فاقد سند قطعی معتبر است؛ وضعیت منبع را پیش از تکرار بررسی کنید.');
@@ -249,6 +267,7 @@ async function jeRpcAutoPost(kind,sourceId){
267
reconciledSource.documentPostedAt=voucher.postedAt||new Date().toISOString();
268
reconciledSource.documentPostedBy=voucher.postedBy||currentUser.id;
269
reconciledSource.documentVersion=Number(reconciledSource.documentVersion||1);
270
+ delete reconciledSource.accountingPostingPending;delete reconciledSource.accountingPostingPendingAt;
271
await jeRpcPersistSourceRecord(collection,sourceId);
272
return voucher;
273
}