سامانه صدور فاکتور و حسابداری
Phase 5: reconcile inventory counts to accounting [skip ci]
davoodmehraban89 committed
Sep 29, 2026 at 20:56 UTC
a6bd40e120c86d9489e53252976e243fcb0db531
1 file changed
+8
-4
js/phase5-enterprise.js
+8
-4
@@ -49,13 +49,17 @@ function p5SaveWarehouse(){
49
function p5PostMovement(){
50
const cid=p5Need();if(!cid)return;const m={id:p5Id('SM'),ownerUserId:currentUser.id,companyId:cid,kind:document.getElementById('p5-mv-kind').value,productId:document.getElementById('p5-mv-product').value,warehouseId:document.getElementById('p5-mv-from').value,toWarehouseId:document.getElementById('p5-mv-to').value,qty:Number(toEnDigits(document.getElementById('p5-mv-qty').value))||0,unitCost:Number(toEnDigits(document.getElementById('p5-mv-cost').value))||0,date:document.getElementById('p5-mv-date').value||getJalaliNumeric(),reference:document.getElementById('p5-mv-ref').value.trim(),status:'posted',postedAt:new Date().toISOString()};
51
try{P5Domain.validateMovement(m);if(['adjustment_out','transfer'].includes(m.kind)&&p5Stock(m.productId,m.warehouseId)<m.qty)throw new Error('موجودی انبار برای خروج کافی نیست.');}catch(e){return alert(e.message)}
52
- datastore.stockMovements.push(m);try{if(['adjustment_in','adjustment_out'].includes(m.kind)&&m.unitCost>0){const inv=jeAccountByCode('1103'),other=jeAccountByCode(m.kind==='adjustment_in'?'4201':'5101'),value=m.qty*m.unitCost;if(!inv||!other)throw new Error('حساب موجودی/تعدیل در کدینگ تکمیل نیست.');const lines=m.kind==='adjustment_in'?[{accountId:inv.id,debit:value,credit:0,description:'تعدیل موجودی',dimensions:[]},{accountId:other.id,debit:0,credit:value,description:'تعدیل افزایشی موجودی',dimensions:[]}]:[{accountId:other.id,debit:value,credit:0,description:'تعدیل کاهشی موجودی',dimensions:[]},{accountId:inv.id,debit:0,credit:value,description:'خروج تعدیلی موجودی',dimensions:[]}];const v=jeCreateDraft({date:m.date,description:'تعدیل موجودی — '+(m.reference||m.id),sourceType:'inventory_adjustment',sourceId:m.id,lines});jePost(v.id);m.journalVoucherId=v.id;m.accountingStatus='posted';}p5Audit('post','stockMovement',m.id,null,m);saveDatastore();p5RenderInventory();}catch(e){datastore.stockMovements=datastore.stockMovements.filter(x=>x.id!==m.id);saveDatastore();alert(e.message)}
52
+ datastore.stockMovements.push(m);try{p5PostInventoryValue(m);p5Audit('post','stockMovement',m.id,null,m);saveDatastore();p5RenderInventory();}catch(e){datastore.stockMovements=datastore.stockMovements.filter(x=>x.id!==m.id);saveDatastore();alert(e.message)}
53
+}
54
+function p5PostInventoryValue(m){
55
+ if(!['adjustment_in','adjustment_out'].includes(m.kind)||!(Number(m.unitCost)>0))return null;const inv=jeAccountByCode('1103'),other=jeAccountByCode(m.kind==='adjustment_in'?'4201':'5101'),value=Number(m.qty)*Number(m.unitCost);if(!inv||!other)throw new Error('حساب موجودی/تعدیل در کدینگ تکمیل نیست.');
56
+ const lines=m.kind==='adjustment_in'?[{accountId:inv.id,debit:value,credit:0,description:'تعدیل موجودی',dimensions:[]},{accountId:other.id,debit:0,credit:value,description:'تعدیل افزایشی موجودی',dimensions:[]}]:[{accountId:other.id,debit:value,credit:0,description:'تعدیل کاهشی موجودی',dimensions:[]},{accountId:inv.id,debit:0,credit:value,description:'خروج تعدیلی موجودی',dimensions:[]}];
57
+ const v=jeCreateDraft({date:m.date,description:'تعدیل موجودی — '+(m.reference||m.id),sourceType:'inventory_adjustment',sourceId:m.id,lines});jePost(v.id);m.journalVoucherId=v.id;m.accountingStatus='posted';return v;
58
}
59
function p5SaveCount(){
60
const cid=p5Need();if(!cid)return;const productId=document.getElementById('p5-count-product').value,warehouseId=document.getElementById('p5-count-wh').value,physicalQty=Number(toEnDigits(document.getElementById('p5-count-qty').value));if(!productId||!warehouseId||!Number.isFinite(physicalQty)||physicalQty<0)return alert('اطلاعات شمارش معتبر نیست.');
56
- const systemQty=p5Stock(productId,warehouseId),diff=physicalQty-systemQty,r={id:p5Id('IC'),ownerUserId:currentUser.id,companyId:cid,productId,warehouseId,date:getJalaliNumeric(),systemQty,physicalQty,difference:diff,status:'posted',postedAt:new Date().toISOString()};datastore.inventoryCounts.push(r);
57
- if(diff){datastore.stockMovements.push({id:p5Id('SM'),ownerUserId:currentUser.id,companyId:cid,kind:diff>0?'adjustment_in':'adjustment_out',productId,warehouseId,toWarehouseId:'',qty:Math.abs(diff),unitCost:p5AverageCost(productId),date:r.date,reference:'انبارگردانی '+r.id,status:'posted',sourceType:'inventory_count',sourceId:r.id,postedAt:r.postedAt})}
58
- p5Audit('post','inventoryCount',r.id,null,r);saveDatastore();p5RenderInventory();
61
+ const systemQty=p5Stock(productId,warehouseId),diff=physicalQty-systemQty,r={id:p5Id('IC'),ownerUserId:currentUser.id,companyId:cid,productId,warehouseId,date:getJalaliNumeric(),systemQty,physicalQty,difference:diff,status:'posted',postedAt:new Date().toISOString()};let m=null;datastore.inventoryCounts.push(r);
62
+ try{if(diff){m={id:p5Id('SM'),ownerUserId:currentUser.id,companyId:cid,kind:diff>0?'adjustment_in':'adjustment_out',productId,warehouseId,toWarehouseId:'',qty:Math.abs(diff),unitCost:p5AverageCost(productId),date:r.date,reference:'انبارگردانی '+r.id,status:'posted',sourceType:'inventory_count',sourceId:r.id,postedAt:r.postedAt};datastore.stockMovements.push(m);p5PostInventoryValue(m);r.journalVoucherId=m.journalVoucherId||''}p5Audit('post','inventoryCount',r.id,null,r);saveDatastore();p5RenderInventory()}catch(e){datastore.inventoryCounts=datastore.inventoryCounts.filter(x=>x.id!==r.id);if(m)datastore.stockMovements=datastore.stockMovements.filter(x=>x.id!==m.id);saveDatastore();alert(e.message)}
63
}
64
function p5EnsureAssetAccounts(){
65
const cid=afCompanyId();if(!cid)return;const specs=[['15','داراییهای ثابت','subsidiary','debit','1'],['1501','دارایی ثابت مشهود','detail','debit','15'],['1502','استهلاک انباشته دارایی ثابت','detail','credit','15'],['62','هزینه استهلاک','subsidiary','debit','6'],['6201','هزینه استهلاک دارایی ثابت','detail','debit','62']];const byCode=Object.fromEntries(getMyAccounts().map(a=>[a.code,a]));let ch=false;