Merge pull request #19733 from marination/zero-division-basic-amount

fix: Division by zero error in Stock Entry
This commit is contained in:
Deepesh Garg 2019-12-13 09:23:44 +05:30 committed by GitHub
commit 57e6bcaf43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 2 deletions

View File

@ -622,7 +622,7 @@ frappe.ui.form.on('Stock Entry Detail', {
if(r.message) {
var d = locals[cdt][cdn];
$.each(r.message, function(k, v) {
d[k] = v;
frappe.model.set_value(cdt, cdn, k, v); // qty and it's subsequent fields weren't triggered
});
refresh_field("items");
erpnext.stock.select_batch_and_serial_no(frm, d);

View File

@ -649,6 +649,12 @@ class StockEntry(StockController):
gl_entries = super(StockEntry, self).get_gl_entries(warehouse_account)
total_basic_amount = sum([flt(t.basic_amount) for t in self.get("items") if t.t_warehouse])
divide_based_on = total_basic_amount
if self.get("additional_costs") and not total_basic_amount:
# if total_basic_amount is 0, distribute additional charges based on qty
divide_based_on = sum(item.qty for item in list(self.get("items")))
item_account_wise_additional_cost = {}
for t in self.get("additional_costs"):
@ -656,8 +662,11 @@ class StockEntry(StockController):
if d.t_warehouse:
item_account_wise_additional_cost.setdefault((d.item_code, d.name), {})
item_account_wise_additional_cost[(d.item_code, d.name)].setdefault(t.expense_account, 0.0)
multiply_based_on = d.basic_amount if total_basic_amount else d.qty
item_account_wise_additional_cost[(d.item_code, d.name)][t.expense_account] += \
(t.amount * d.basic_amount) / total_basic_amount
(t.amount * multiply_based_on) / divide_based_on
if item_account_wise_additional_cost:
for d in self.get("items"):

View File

@ -790,6 +790,50 @@ class TestStockEntry(unittest.TestCase):
filters={"voucher_type": "Stock Entry", "voucher_no": mr.name}, fieldname="is_opening")
self.assertEqual(is_opening, "Yes")
def test_total_basic_amount_zero(self):
se = frappe.get_doc({"doctype":"Stock Entry",
"purpose":"Material Receipt",
"stock_entry_type":"Material Receipt",
"posting_date": nowdate(),
"company":"_Test Company with perpetual inventory",
"items":[
{
"item_code":"Basil Leaves",
"description":"Basil Leaves",
"qty": 1,
"basic_rate": 0,
"uom":"Nos",
"t_warehouse": "Stores - TCP1",
"allow_zero_valuation_rate": 1,
"cost_center": "Main - TCP1"
},
{
"item_code":"Basil Leaves",
"description":"Basil Leaves",
"qty": 2,
"basic_rate": 0,
"uom":"Nos",
"t_warehouse": "Stores - TCP1",
"allow_zero_valuation_rate": 1,
"cost_center": "Main - TCP1"
},
],
"additional_costs":[
{"expense_account":"Miscellaneous Expenses - TCP1",
"amount":100,
"description": "miscellanous"}
]
})
se.insert()
se.submit()
self.check_gl_entries("Stock Entry", se.name,
sorted([
["Stock Adjustment - TCP1", 100.0, 0.0],
["Miscellaneous Expenses - TCP1", 0.0, 100.0]
])
)
def make_serialized_item(item_code=None, serial_no=None, target_warehouse=None):
se = frappe.copy_doc(test_records[0])
se.get("items")[0].item_code = item_code or "_Test Serialized Item With Series"