سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 11:45 UTC
fix(accounting): repair source draft creation and revalidate persisted source context
davoodmehraban89 committed
Nov 24, 2026 at 10:41 UTC
25281be1a0a720bf8c839aa5cd3cd125cbe6ad63
1 file changed
+46
-22
js/journal-rpc.js
+46
-22
@@ -251,36 +251,60 @@ async function jeRpcDeleteSourceDraft(kind,sourceId,reason){
251
return data;
252
}
253
254
+// Serialize source-draft creation before the asynchronous source flush.
255
+// The server remains authoritative for the linked draft and its lines.
256
+let jeRpcSourceDraftBusy=false;
257
async function jeRpcCreateSourceDraft(kind,sourceId){
255
- if(!requireWrite())throw new Error('مجوز ثبت پیشنویس حسابداری ندارید.');
256
- const collection=kind==='sale'?'invoices':kind==='purchase'?'purchases':'';
257
- if(!collection)throw new Error('نوع سند منبع برای پیشنویس پشتیبانی نمیشود.');
258
- const org=await jeRpcAssertContext(),source=jeRpcSourceRecord(collection,sourceId);
259
- if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
260
- throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
261
- const date=jeRpcDate(source.date),fy=jeFiscalForDate(date);
262
- if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ منبع پیدا نشد.');
263
- const sourceVersion=Number(source.accountingVersion||1);
264
- if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه منبع معتبر نیست.');
265
- if(!source.journalVoucherId){await jeRpcPersistSourceRecord(collection,sourceId);source=jeRpcSourceRecord(collection,sourceId);}\n const voucherId=source.journalVoucherId||afId('JV');
266
- const header={
258
+ if(jeRpcSourceDraftBusy)throw new Error('ایجاد پیشنویس منبع در حال اجراست؛ درخواست تکراری ارسال نشد.');
259
+ jeRpcSourceDraftBusy=true;
260
+ try{
261
+ if(!requireWrite())throw new Error('مجوز ثبت پیشنویس حسابداری ندارید.');
262
+ const collection=kind==='sale'?'invoices':kind==='purchase'?'purchases':'';
263
+ if(!collection)throw new Error('نوع سند منبع برای پیشنویس پشتیبانی نمیشود.');
264
+ const org=await jeRpcAssertContext(),actor=currentUser.id,company=afCompanyId();
265
+ function checkedSource(){
266
+ if(currentUser?.id!==actor||currentUser?.organizationId!==org||afCompanyId()!==company)
267
+ throw new Error('حساب، سازمان یا شرکت فعال هنگام ایجاد پیشنویس تغییر کرد.');
268
+ const row=jeRpcSourceRecord(collection,sourceId);
269
+ if(!company||!row||String(row.id)!==String(sourceId)||row.ownerUserId!==actor||row.companyId!==company)
270
+ throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
271
+ if((row.documentStatus||'draft')!=='draft'||(row.accountingStatus||'draft')!=='draft')
272
+ throw new Error('فقط رکورد منبع موقت قابل ثبت بهعنوان پیشنویس حسابداری است.');
273
+ const date=jeRpcDate(row.date),fy=jeFiscalForDate(date);
274
+ if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ منبع پیدا نشد.');
275
+ const version=Number(row.accountingVersion??1);
276
+ if(!Number.isSafeInteger(version)||version<1)throw new Error('نسخه منبع معتبر نیست.');
277
+ return {row,date,fy,version};
278
+ }
279
+ let state=checkedSource();
280
+ if(!state.row.journalVoucherId){
281
+ await jeRpcPersistSourceRecord(collection,sourceId);
282
+ if(await jeRpcAssertContext()!==org)throw new Error('سازمان فعال هنگام ایجاد پیشنویس تغییر کرد.');
283
+ // Sync may replace the record or revise the version/date. Never build
284
+ // a financial header from stale pre-flush state.
285
+ state=checkedSource();
286
+ }
287
+ const {row:source,date,fy,version:sourceVersion}=state;
288
+ const voucherId=source.journalVoucherId||afId('JV');
289
+ const header={
290
companyId:source.companyId,fiscalYearId:fy.id,date,
291
description:String(source.description||((kind==='sale'?'فروش ':'خرید ')+(source.number||source.id))),
292
reference:String(source.reference||source.number||''),
293
voucherType:kind,sourceType:kind,sourceId:String(source.id),sourceVersion:String(sourceVersion)
271
- };
272
- const data=await jeRpcCall('finora_journal_create_source_draft',{
294
+ };
295
+ const data=await jeRpcCall('finora_journal_create_source_draft',{
296
p_organization_id:org,p_voucher_id:voucherId,
297
p_source_collection:collection,p_source_id:String(source.id),
298
p_header:header,p_lines:[]
276
- });
277
- await jeRpcRefresh(org);
278
- const voucher=data?.voucher||getMyJournalVouchers().find(v=>v.id===voucherId);
279
- if(!voucher?.id)throw new Error('پیشنویس حسابداری روی سرور ایجاد شد اما پاسخ معتبر دریافت نشد؛ قبل از تکرار وضعیت را تازهسازی کنید.');
280
- const linked=jeRpcSourceRecord(collection,sourceId);
281
- if(!linked||linked.journalVoucherId!==voucher.id||linked.accountingStatus!=='draft'||linked.documentStatus!=='draft')
282
- throw new Error('ارتباط پیشنویس منبع و سند حسابداری از سرور تأیید نشد.');
283
- return voucher;
299
+ });
300
+ await jeRpcRefresh(org);
301
+ const voucher=data?.voucher||getMyJournalVouchers().find(v=>v.id===voucherId);
302
+ if(!voucher?.id||voucher.status!=='draft')throw new Error('پاسخ معتبر سند موقت دریافت نشد؛ قبل از تکرار وضعیت را تازهسازی کنید.');
303
+ const linked=checkedSource().row;
304
+ if(linked.journalVoucherId!==voucher.id||linked.accountingStatus!=='draft'||linked.documentStatus!=='draft')
305
+ throw new Error('ارتباط پیشنویس منبع و سند حسابداری از سرور تأیید نشد.');
306
+ return voucher;
307
+ }finally{jeRpcSourceDraftBusy=false;}
308
}
309
310
async function jeRpcAutoPost(kind,sourceId){