@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 3a5cd9b
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 09:15 UTC

refactor(accounting): separate source draft creation from final posting

davoodmehraban89 committed Nov 18, 2026 at 09:04 UTC 3a5cd9bfae3f8534366e7effe4877596bfa6b26c
1 file changed +54 -61
js/journal-rpc.js
+54 -61
@@ -220,71 +220,64 @@ async function jeRpcPersistSourceRecord(collection,id){
220 }
221 throw new Error('تأیید ذخیره رکورد منبع از سرور دریافت نشد؛ ثبت حسابداری انجام نشد یا وضعیت باید پس از بارگذاری مجدد بررسی شود.');
222 }
223 -async function jeRpcAutoPost(kind,sourceId){
224 - if(jeRpcAutoPostBusy)throw new Error('ثبت خودکار همین منبع در حال اجراست؛ از ارسال دوباره درخواست خودداری کنید.');
225 - jeRpcAutoPostBusy=true;
226 - try{
227 - if(!requireWrite())throw new Error('مجوز قطعی‌سازی حسابداری ندارید.');
223 +async function jeRpcCreateSourceDraft(kind,sourceId){
224 + if(!requireWrite())throw new Error('مجوز ثبت پیش‌نویس حسابداری ندارید.');
225 const collection=kind==='sale'?'invoices':kind==='purchase'?'purchases':'';
229 - if(!collection)throw new Error('نوع ثبت خودکار پشتیبانی نمی‌شود.');
230 - const org=await jeRpcAssertContext();
231 - let source=jeRpcSourceRecord(collection,sourceId);
226 + if(!collection)throw new Error('نوع سند منبع برای پیش‌نویس پشتیبانی نمی‌شود.');
227 + const org=await jeRpcAssertContext(),source=jeRpcSourceRecord(collection,sourceId);
228 if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
229 throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
234 - if(source.journalVoucherId){
235 - let existing=getMyJournalVouchers().find(v=>v.id===source.journalVoucherId);
236 - if(!existing){await jeRpcRefresh(org);source=jeRpcSourceRecord(collection,sourceId);existing=getMyJournalVouchers().find(v=>v.id===source?.journalVoucherId);}
237 - if(existing?.status==='posted'&&jeRpcEffectiveStatus(existing)!=='reversed'){delete source.accountingPostingPending;delete source.accountingPostingPendingAt;return existing;}
238 - if(existing&&jeRpcEffectiveStatus(existing)==='reversed')throw new Error('سند منبع قبلاً برگشت حسابداری شده است؛ ثبت مجدد مجاز نیست.');
239 - throw new Error('رکورد منبع از قبل به سندی متصل است؛ وضعیت اتصال باید بررسی شود.');
240 - }
241 - if(!Number.isSafeInteger(Number(source.grandTotal))||Number(source.grandTotal)<=0)
242 - throw new Error('مبلغ کل منبع باید عدد صحیح مثبت و امن باشد.');
243 - if(!String(source.date||'').trim())throw new Error('تاریخ سند منبع ثبت نشده است؛ پیش از قطعی‌سازی تاریخ معتبر وارد کنید.');
244 - const date=jeRpcDate(source.date);
245 - if(!/^\d{4}\/\d{2}\/\d{2}$/.test(date))throw new Error('تاریخ سند منبع معتبر نیست.');
246 - const fy=jeFiscalForDate(date);
247 - if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ رکورد منبع پیدا نشد.');
230 + const date=jeRpcDate(source.date),fy=jeFiscalForDate(date);
231 + if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ منبع پیدا نشد.');
232 const sourceVersion=Number(source.accountingVersion||1);
249 - if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه رویداد منبع معتبر نیست.');
250 - // Commit the source draft first. The transient freeze marker is local-only;
251 - // stripOwner deliberately excludes it from generic sync payloads.
252 - await jeRpcPersistSourceRecord(collection,sourceId);
253 - source=jeRpcSourceRecord(collection,sourceId);
254 - source.accountingPostingPending=true;
255 - source.accountingPostingPendingAt=source.accountingPostingPendingAt||new Date().toISOString();
256 - const header={companyId:source.companyId,fiscalYearId:fy.id,date,
257 - description:String(source.description||((kind==='sale'?'فروش ':'خرید ')+(source.number||source.id))),
258 - reference:String(source.reference||source.number||''),voucherType:kind,
259 - sourceType:kind,sourceId:String(source.id),sourceVersion:String(sourceVersion)};
260 - let result;
261 - try{
262 - result=await jeRpcCall('finora_journal_autopost',{
263 - p_organization_id:org,p_voucher_id:afId('JV'),p_source_collection:collection,
264 - p_source_id:String(source.id),p_header:header,p_lines:[]
265 - });
266 - }catch(err){
267 - // A returned database error or a pre-submit rejection is definitive. A
268 - // transport failure after submission remains frozen until retry/reload.
269 - if(err?.code||err?.rpcSubmitted===false){
270 - delete source.accountingPostingPending;delete source.accountingPostingPendingAt;
271 - }
272 - throw err;
273 - }
274 - const voucher=result?.voucher;
275 - if(!voucher?.id)throw new Error('پاسخ ثبت خودکار سرور فاقد شناسه سند است؛ وضعیت را پیش از تکرار بررسی کنید.');
276 - if(voucher.status!=='posted'){
277 - await jeRpcRefresh(org);
278 - throw new Error('این منبع قبلاً برگشت حسابداری شده است؛ ثبت مجدد آن مجاز نیست.');
279 - }
233 + if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه منبع معتبر نیست.');
234 + const voucherId=source.journalVoucherId||afId('JV');
235 + const header={
236 + 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||''),
239 + voucherType:kind,sourceType:kind,sourceId:String(source.id),sourceVersion:String(sourceVersion)
240 + };
241 + const data=await jeRpcCall('finora_journal_create_source_draft',{
242 + p_organization_id:org,p_voucher_id:voucherId,
243 + p_source_collection:collection,p_source_id:String(source.id),
244 + p_header:header,p_lines:[]
245 + });
246 await jeRpcRefresh(org);
281 - if(jeRpcEffectiveStatus(voucher)==='reversed')throw new Error('این منبع قبلاً برگشت حسابداری شده است؛ ثبت مجدد آن مجاز نیست.');
282 - const reconciledSource=jeRpcSourceRecord(collection,sourceId);
283 - if(!reconciledSource||reconciledSource.journalVoucherId!==voucher.id||
284 - reconciledSource.accountingStatus!=='posted'||reconciledSource.documentStatus!=='posted'){
285 - if(reconciledSource){reconciledSource.accountingPostingPending=true;reconciledSource.accountingPostingPendingAt=new Date().toISOString();}
286 - throw new Error('سند قطعی ثبت شد اما پیوند منبع در همان تراکنش سرور تأیید نشد؛ ثبت مجدد یا ویرایش نکنید و وضعیت را بررسی کنید.');
287 - }
247 + const voucher=data?.voucher||getMyJournalVouchers().find(v=>v.id===voucherId);
248 + if(!voucher?.id)throw new Error('پیش‌نویس حسابداری روی سرور ایجاد شد اما پاسخ معتبر دریافت نشد؛ قبل از تکرار وضعیت را تازه‌سازی کنید.');
249 + const linked=jeRpcSourceRecord(collection,sourceId);
250 + if(!linked||linked.journalVoucherId!==voucher.id||linked.accountingStatus!=='draft'||linked.documentStatus!=='draft')
251 + throw new Error('ارتباط پیش‌نویس منبع و سند حسابداری از سرور تأیید نشد.');
252 return voucher;
289 - }finally{jeRpcAutoPostBusy=false;}
253 }
254 +
255 +async function jeRpcAutoPost(kind,sourceId){
256 + if(jeRpcAutoPostBusy)throw new Error('ثبت حسابداری همین منبع در حال اجراست؛ از ارسال دوباره درخواست خودداری کنید.');
257 + jeRpcAutoPostBusy=true;
258 + try{
259 + if(!requireWrite())throw new Error('مجوز قطعی‌سازی حسابداری ندارید.');
260 + const collection=kind==='sale'?'invoices':kind==='purchase'?'purchases':'';
261 + if(!collection)throw new Error('نوع ثبت حسابداری پشتیبانی نمی‌شود.');
262 + const org=await jeRpcAssertContext();
263 + let source=jeRpcSourceRecord(collection,sourceId);
264 + if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
265 + throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
266 +
267 + if(source.journalVoucherId){
268 + let existing=getMyJournalVouchers().find(v=>v.id===source.journalVoucherId);
269 + if(!existing){await jeRpcRefresh(org);source=jeRpcSourceRecord(collection,sourceId);existing=getMyJournalVouchers().find(v=>v.id===source?.journalVoucherId);}
270 + if(existing?.status==='posted'&&jeRpcEffectiveStatus(existing)!=='reversed'){return existing;}
271 + if(existing&&jeRpcEffectiveStatus(existing)==='reversed')throw new Error('سند منبع قبلاً برگشت حسابداری شده است؛ ثبت مجدد مجاز نیست.');
272 + if(!existing||existing.status!=='draft')throw new Error('وضعیت سند حسابداری منبع قابل قطعی‌سازی نیست.');
273 + return await jeRpcPost(existing.id);
274 + }
275 +
276 + // Source is persisted as draft first. The accounting voucher is then created
277 + // as a draft, and ONLY the explicit post action makes it final.
278 + await jeRpcPersistSourceRecord(collection,sourceId);
279 + source=jeRpcSourceRecord(collection,sourceId);
280 + const draft=await jeRpcCreateSourceDraft(kind,sourceId);
281 + return await jeRpcPost(draft.id);
282 + }finally{jeRpcAutoPostBusy=false;}
283 +}
\ No newline at end of file