Skip to content

Commit

Permalink
fix: zero division error while making LCV
Browse files Browse the repository at this point in the history
(cherry picked from commit 80e94a0)
  • Loading branch information
rohitwaghchaure authored and mergify[bot] committed Feb 23, 2023
1 parent b37695f commit 1859be6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def validate(self):
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"):
Expand Down Expand Up @@ -115,6 +114,13 @@ def set_applicable_charges_on_item(self):
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"),
Expand Down Expand Up @@ -162,6 +168,7 @@ def validate_applicable_charges_for_item(self):
)

def on_submit(self):
self.validate_applicable_charges_for_item()
self.update_landed_cost()

def on_cancel(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,59 @@ def test_landed_cost_voucher_stock_impact(self):
)
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(
Expand Down Expand Up @@ -516,7 +569,7 @@ def make_landed_cost_voucher(**args):

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",
Expand Down
24 changes: 18 additions & 6 deletions erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,13 +1134,25 @@ def get_item_account_wise_additional_cost(purchase_document):
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

Expand Down

0 comments on commit 1859be6

Please sign in to comment.