Skip to content

Commit

Permalink
Merge pull request #35718 from frappe/mergify/bp/version-14-hotfix/pr…
Browse files Browse the repository at this point in the history
…-35711

fix: incorrect gl entries for standalone debit note (backport #35711)
  • Loading branch information
rohitwaghchaure authored Jun 16, 2023
2 parents 55a8be5 + 697fcef commit 7339e44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
22 changes: 15 additions & 7 deletions erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,6 @@ def test_standalone_return_using_pi(self):
gle_filters={"account": "Stock In Hand - TCP1"},
)

# assert loss booked in COGS
self.assertGLEs(
return_pi,
[{"credit": 0, "debit": 200}],
gle_filters={"account": "Cost of Goods Sold - TCP1"},
)

def test_return_with_lcv(self):
from erpnext.controllers.sales_and_purchase_return import make_return_doc
from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import (
Expand Down Expand Up @@ -1662,6 +1655,21 @@ def test_batch_expiry_for_purchase_invoice(self):

self.assertTrue(return_pi.docstatus == 1)

def test_gl_entries_for_standalone_debit_note(self):
make_purchase_invoice(qty=5, rate=500, update_stock=True)

returned_inv = make_purchase_invoice(qty=-5, rate=5, update_stock=True, is_return=True)

# override the rate with valuation rate
sle = frappe.get_all(
"Stock Ledger Entry",
fields=["stock_value_difference", "actual_qty"],
filters={"voucher_no": returned_inv.name},
)[0]

rate = flt(sle.stock_value_difference) / flt(sle.actual_qty)
self.assertAlmostEqual(returned_inv.items[0].rate, rate)


def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
gl_entries = frappe.db.sql(
Expand Down
26 changes: 26 additions & 0 deletions erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def get_feed(self):
return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, self.grand_total)

def validate(self):
self.set_rate_for_standalone_debit_note()

super(BuyingController, self).validate()
if getattr(self, "supplier", None) and not self.supplier_name:
self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name")
Expand Down Expand Up @@ -72,6 +74,30 @@ def onload(self):
),
)

def set_rate_for_standalone_debit_note(self):
if self.get("is_return") and self.get("update_stock") and not self.return_against:
for row in self.items:

# override the rate with valuation rate
row.rate = get_incoming_rate(
{
"item_code": row.item_code,
"warehouse": row.warehouse,
"posting_date": self.get("posting_date"),
"posting_time": self.get("posting_time"),
"qty": row.qty,
"serial_and_batch_bundle": row.get("serial_and_batch_bundle"),
"company": self.company,
"voucher_type": self.doctype,
"voucher_no": self.name,
},
raise_error_if_no_rate=False,
)

row.discount_percentage = 0.0
row.discount_amount = 0.0
row.margin_rate_or_amount = 0.0

def set_missing_values(self, for_validate=False):
super(BuyingController, self).set_missing_values(for_validate)

Expand Down

0 comments on commit 7339e44

Please sign in to comment.