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

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

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

Synced Dec 4, 01:45 UTC

fix(pilot): prevent reversed and draft purchase double counting in stock and project costs

davoodmehraban89 committed Oct 14, 2026 at 08:52 UTC d1dced1e1e7b058f8e13690ec39d8a63c2c5c659
1 file changed +5 -2
js/accounting.js
+5 -2
@@ -1,11 +1,14 @@
1 // Derived inventory and project profitability. No mutable stock side effects.
2 (function(){
3 const n=v=>Number(v)||0;
4 +// Historical unlinked purchase snapshots keep their legacy treatment; a reversed
5 +// posting or unposted corrective draft must not create inventory or project cost.
6 +function purchaseAffectsBalance(p){const state=typeof purchaseDocumentStatus==='function'?purchaseDocumentStatus(p):'legacy_review';return state==='posted'||state==='legacy_review';}
7 function productMovement(productId){
8 const opening=n(getMyProducts().find(p=>p.id===productId)?.stock);
9 const companies=getMyCompanies(),active=getMySettings().default_company_id||companies[0]?.id||'';
10 const legacyOk=companies.length===1;
8 - const purchased=getMyPurchases().filter(p=>!active||p.companyId===active||(!p.companyId&&legacyOk)).reduce((sum,p)=>sum+(p.items||[]).filter(i=>(i.prodId||i.productId)===productId).reduce((s,i)=>s+n(i.qty??i.quantity),0),0);
11 + const purchased=getMyPurchases().filter(p=>(!active||p.companyId===active||(!p.companyId&&legacyOk))&&purchaseAffectsBalance(p)).reduce((sum,p)=>sum+(p.items||[]).filter(i=>(i.prodId||i.productId)===productId).reduce((s,i)=>s+n(i.qty??i.quantity),0),0);
12 const sold=getMyInvoices().filter(p=>(!active||p.companyId===active||(!p.companyId&&legacyOk))&&typeof invoiceDocumentStatus==='function'&&invoiceDocumentStatus(p)==='posted').reduce((sum,p)=>sum+(p.items||[]).filter(i=>(i.prodId||i.productId)===productId).reduce((s,i)=>s+n(i.qty??i.quantity),0),0);
13 return {opening,purchased,sold,available:opening+purchased-sold};
14 }
@@ -13,7 +16,7 @@ function getInventorySnapshot(){return getMyProducts().map(p=>({productId:p.id,c
16 function getProjectFinancials(projectId){
17 const companies=getMyCompanies(),active=getMySettings().default_company_id||companies[0]?.id||'',legacyOk=companies.length===1,companyOk=i=>!active||i.companyId===active||(!i.companyId&&legacyOk);
18 const sales=getMyInvoices().filter(i=>companyOk(i)&&i.projectId===projectId&&typeof invoiceDocumentStatus==='function'&&invoiceDocumentStatus(i)==='posted').reduce((s,i)=>s+n(i.grandTotal),0);
16 - const purchases=getMyPurchases().filter(i=>companyOk(i)&&i.costCenterId===projectId).reduce((s,i)=>s+n(i.grandTotal),0);
19 + const purchases=getMyPurchases().filter(i=>companyOk(i)&&purchaseAffectsBalance(i)&&(i.projectId===projectId||i.costCenterId===projectId)).reduce((s,i)=>s+n(i.grandTotal),0);
20 const expenses=getMyExpenses().filter(e=>companyOk(e)&&e.projectId===projectId&&e.kind==='expense').reduce((s,e)=>s+n(e.amount),0);
21 const income=getMyExpenses().filter(e=>companyOk(e)&&e.projectId===projectId&&e.kind==='income').reduce((s,e)=>s+n(e.amount),0);
22 const receipts=getMyPayments().filter(p=>companyOk(p)&&p.projectId===projectId&&p.direction==='inbound').reduce((s,p)=>s+n(p.amount),0);