@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 48c8c35
Fork 0

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

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

Synced Dec 4, 01:45 UTC

fix: verify atomic source link from server instead of generic sync

davoodmehraban89 committed Nov 17, 2026 at 14:16 UTC 48c8c35c17c602e1fcae16b1b207b24d0c788671
1 file changed +18 -34
js/journal-rpc.js
+18 -34
@@ -207,17 +207,9 @@ async function jeRpcAutoPost(kind,sourceId){
207 if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
208 throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
209 if(source.journalVoucherId){
210 - const existing=getMyJournalVouchers().find(v=>v.id===source.journalVoucherId);
211 - if(existing?.status==='posted'){
212 - source.accountingVersion=Number(source.accountingVersion||1);
213 - source.accountingStatus='posted';source.accountingPostedAt=existing.postedAt||new Date().toISOString();
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 - }
210 + let existing=getMyJournalVouchers().find(v=>v.id===source.journalVoucherId);
211 + if(!existing){await jeRpcRefresh(org);source=jeRpcSourceRecord(collection,sourceId);existing=getMyJournalVouchers().find(v=>v.id===source?.journalVoucherId);}
212 + if(existing?.status==='posted'){delete source.accountingPostingPending;delete source.accountingPostingPendingAt;return existing;}
213 throw new Error('رکورد منبع از قبل به سندی متصل است؛ وضعیت اتصال باید بررسی شود.');
214 }
215 if(!Number.isSafeInteger(Number(source.grandTotal))||Number(source.grandTotal)<=0)
@@ -226,13 +218,12 @@ async function jeRpcAutoPost(kind,sourceId){
218 if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ رکورد منبع پیدا نشد.');
219 const sourceVersion=Number(source.accountingVersion||1);
220 if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه رویداد منبع معتبر نیست.');
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.
221 + // Commit the source draft first. The transient freeze marker is local-only;
222 + // stripOwner deliberately excludes it from generic sync payloads.
223 await jeRpcPersistSourceRecord(collection,sourceId);
224 source=jeRpcSourceRecord(collection,sourceId);
225 + source.accountingPostingPending=true;
226 + source.accountingPostingPendingAt=source.accountingPostingPendingAt||new Date().toISOString();
227 const header={companyId:source.companyId,fiscalYearId:fy.id,date,
228 description:String(source.description||((kind==='sale'?'فروش ':'خرید ')+(source.number||source.id))),
229 reference:String(source.reference||source.number||''),voucherType:kind,
@@ -244,30 +235,23 @@ async function jeRpcAutoPost(kind,sourceId){
235 p_source_id:String(source.id),p_header:header,p_lines:[]
236 });
237 }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.
238 + // A returned database error or a pre-submit rejection is definitive. A
239 + // transport failure after submission remains frozen until retry/reload.
240 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){failedSource.accountingPostingPending=true;failedSource.accountingPostingPendingAt=failedSource.accountingPostingPendingAt||new Date().toISOString();err.pendingMarkerClearError=clearErr?.message||String(clearErr);}}
241 + delete source.accountingPostingPending;delete source.accountingPostingPendingAt;
242 }
243 throw err;
244 }
245 const voucher=result?.voucher;
257 - if(!voucher?.id||voucher.status!=='posted')
258 - throw new Error('پاسخ ثبت خودکار سرور فاقد سند قطعی معتبر است؛ وضعیت منبع را پیش از تکرار بررسی کنید.');
246 + if(!voucher?.id)throw new Error('پاسخ ثبت خودکار سرور فاقد شناسه سند است؛ وضعیت را پیش از تکرار بررسی کنید.');
247 + if(voucher.status!=='posted'){
248 + await jeRpcRefresh(org);
249 + throw new Error('این منبع قبلاً برگشت حسابداری شده است؛ ثبت مجدد آن مجاز نیست.');
250 + }
251 await jeRpcRefresh(org);
252 const reconciledSource=jeRpcSourceRecord(collection,sourceId);
261 - if(!reconciledSource)throw new Error('سند قطعی ثبت شد اما رکورد منبع پس از بازخوانی پیدا نشد؛ وضعیت را بررسی کنید.');
262 - reconciledSource.journalVoucherId=voucher.id;
263 - reconciledSource.accountingVersion=Number(reconciledSource.accountingVersion||1);
264 - reconciledSource.accountingStatus='posted';
265 - reconciledSource.accountingPostedAt=voucher.postedAt||new Date().toISOString();
266 - reconciledSource.documentStatus='posted';
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);
253 + if(!reconciledSource||reconciledSource.journalVoucherId!==voucher.id||
254 + reconciledSource.accountingStatus!=='posted'||reconciledSource.documentStatus!=='posted')
255 + throw new Error('سند قطعی ثبت شد اما پیوند منبع در همان تراکنش سرور تأیید نشد؛ ثبت مجدد نکنید و وضعیت را بررسی کنید.');
256 return voucher;
257 }