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

fix(purchases): recover correction and cancellation after committed reversal

davoodmehraban89 committed Nov 17, 2026 at 21:58 UTC 480eb53a6a00cc645b2f803aa33b6884ea70eaf5
1 file changed +24 -2
js/purchases.js
+24 -2
@@ -142,8 +142,23 @@
142 return number;
143 }
144 window.returnPostedPurchaseAsDraft=async function(id,reason){
145 - const {purchase:p,voucher:v}=purchasePilotEligibility(id),why=String(reason||'').trim();
145 + const why=String(reason||'').trim();
146 if(!why)throw new Error('دلیل اصلاح خرید الزامی است.');
147 + if(!requireWrite()||!currentUser)throw new Error('مجوز اصلاح خرید ندارید.');
148 + const p=myPurchases().find(x=>x.id===id&&x.companyId===afCompanyId());
149 + const v=jeSourceVoucher(p);
150 + if(!p||!v||v.sourceType!=='purchase'||v.sourceId!==p.id||v.status!=='posted')
151 + throw new Error('سند قطعی خرید و مرجع حسابداری آن تطبیق ندارد.');
152 + const effective=jeRpcEffectiveStatus(v);
153 + if(effective==='posted')purchasePilotEligibility(id);
154 + else if(effective!=='reversed')throw new Error('وضعیت برگشت خرید برای اصلاح قابل قبول نیست.');
155 + if(effective==='reversed'){
156 + const existing=myPurchases().find(x=>x.correctsPurchaseId===p.id&&x.correctionReversalId);
157 + if(existing){
158 + await invoicePilotAwaitSync(['journalVouchers|'+v.id,'journalVouchers|'+existing.correctionReversalId,'purchases|'+existing.id]);
159 + refreshAllSurfaces();return existing.id;
160 + }
161 + }
162 const number=purchasePilotNumber(p),now=new Date().toISOString(),newId=afId('PUR');
163 const draft={...p,id:newId,number,date:getJalaliNumeric(),documentStatus:'draft',accountingStatus:'draft',createdAt:now,updatedAt:now,
164 accountingVersion:1,documentVersion:1,correctsPurchaseId:p.id,correctsVoucherId:v.id,correctionOriginalNumber:p.number,correctionReason:why};
@@ -156,8 +171,15 @@
171 return draft.id;
172 };
173 window.cancelPostedPurchasePilot=async function(id,reason){
159 - const {voucher:v}=purchasePilotEligibility(id),why=String(reason||'').trim();
174 + const why=String(reason||'').trim();
175 if(!why)throw new Error('دلیل ابطال الزامی است.');
176 + if(!requireWrite()||!currentUser)throw new Error('مجوز ابطال خرید ندارید.');
177 + const p=myPurchases().find(x=>x.id===id&&x.companyId===afCompanyId()),v=jeSourceVoucher(p);
178 + if(!p||!v||v.sourceType!=='purchase'||v.sourceId!==p.id||v.status!=='posted')
179 + throw new Error('سند قطعی خرید و مرجع حسابداری آن تطبیق ندارد.');
180 + const effective=jeRpcEffectiveStatus(v);
181 + if(effective==='posted')purchasePilotEligibility(id);
182 + else if(effective!=='reversed')throw new Error('وضعیت برگشت خرید برای ابطال قابل قبول نیست.');
183 const reversal=await jeRpcReverse(v.id,getJalaliNumeric(),'ابطال خرید پایلوت: '+why);
184 await invoicePilotAwaitSync(['journalVouchers|'+v.id,'journalVouchers|'+reversal.id]);
185 refreshAllSurfaces();return reversal.id;