fix: zero division error while making LCV
diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
index b3af309..111a0861 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
@@ -55,7 +55,6 @@
self.get_items_from_purchase_receipts()
self.set_applicable_charges_on_item()
- self.validate_applicable_charges_for_item()
def check_mandatory(self):
if not self.get("purchase_receipts"):
@@ -115,6 +114,13 @@
total_item_cost += item.get(based_on_field)
for item in self.get("items"):
+ if not total_item_cost and not item.get(based_on_field):
+ frappe.throw(
+ _(
+ "It's not possible to distribute charges equally when total amount is zero, please set 'Distribute Charges Based On' as 'Quantity'"
+ )
+ )
+
item.applicable_charges = flt(
flt(item.get(based_on_field)) * (flt(self.total_taxes_and_charges) / flt(total_item_cost)),
item.precision("applicable_charges"),
@@ -162,6 +168,7 @@
)
def on_submit(self):
+ self.validate_applicable_charges_for_item()
self.update_landed_cost()
def on_cancel(self):
diff --git a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py
index 979b5c4..00fa168 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py
+++ b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py
@@ -175,6 +175,59 @@
)
self.assertEqual(last_sle_after_landed_cost.stock_value - last_sle.stock_value, 50.0)
+ def test_landed_cost_voucher_for_zero_purchase_rate(self):
+ "Test impact of LCV on future stock balances."
+ from erpnext.stock.doctype.item.test_item import make_item
+
+ item = make_item("LCV Stock Item", {"is_stock_item": 1})
+ warehouse = "Stores - _TC"
+
+ pr = make_purchase_receipt(
+ item_code=item.name,
+ warehouse=warehouse,
+ qty=10,
+ rate=0,
+ posting_date=add_days(frappe.utils.nowdate(), -2),
+ )
+
+ self.assertEqual(
+ frappe.db.get_value(
+ "Stock Ledger Entry",
+ {"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "is_cancelled": 0},
+ "stock_value_difference",
+ ),
+ 0,
+ )
+
+ lcv = make_landed_cost_voucher(
+ company=pr.company,
+ receipt_document_type="Purchase Receipt",
+ receipt_document=pr.name,
+ charges=100,
+ distribute_charges_based_on="Distribute Manually",
+ do_not_save=True,
+ )
+
+ lcv.get_items_from_purchase_receipts()
+ lcv.items[0].applicable_charges = 100
+ lcv.save()
+ lcv.submit()
+
+ self.assertTrue(
+ frappe.db.exists(
+ "Stock Ledger Entry",
+ {"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "is_cancelled": 0},
+ )
+ )
+ self.assertEqual(
+ frappe.db.get_value(
+ "Stock Ledger Entry",
+ {"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "is_cancelled": 0},
+ "stock_value_difference",
+ ),
+ 100,
+ )
+
def test_landed_cost_voucher_against_purchase_invoice(self):
pi = make_purchase_invoice(
@@ -516,7 +569,7 @@
lcv = frappe.new_doc("Landed Cost Voucher")
lcv.company = args.company or "_Test Company"
- lcv.distribute_charges_based_on = "Amount"
+ lcv.distribute_charges_based_on = args.distribute_charges_based_on or "Amount"
lcv.set(
"purchase_receipts",
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index af0d148..53e8053 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -1121,13 +1121,25 @@
account.expense_account, {"amount": 0.0, "base_amount": 0.0}
)
- item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][account.expense_account][
- "amount"
- ] += (account.amount * item.get(based_on_field) / total_item_cost)
+ if total_item_cost > 0:
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][
+ account.expense_account
+ ]["amount"] += (
+ account.amount * item.get(based_on_field) / total_item_cost
+ )
- item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][account.expense_account][
- "base_amount"
- ] += (account.base_amount * item.get(based_on_field) / total_item_cost)
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][
+ account.expense_account
+ ]["base_amount"] += (
+ account.base_amount * item.get(based_on_field) / total_item_cost
+ )
+ else:
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][
+ account.expense_account
+ ]["amount"] += item.applicable_charges
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][
+ account.expense_account
+ ]["base_amount"] += item.applicable_charges
return item_account_wise_cost