Skip to content

Commit

Permalink
fix: incoming rate for sales return with Moving Average valuation met…
Browse files Browse the repository at this point in the history
…hod (#38849)
  • Loading branch information
rohitwaghchaure authored Dec 19, 2023
1 parent 2184e8e commit 7fdac62
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
8 changes: 5 additions & 3 deletions erpnext/controllers/selling_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from erpnext.controllers.stock_controller import StockController
from erpnext.stock.doctype.item.item import set_item_default
from erpnext.stock.get_item_details import get_bin_details, get_conversion_factor
from erpnext.stock.utils import get_incoming_rate
from erpnext.stock.utils import get_incoming_rate, get_valuation_method


class SellingController(StockController):
Expand Down Expand Up @@ -422,11 +422,13 @@ def set_incoming_rate(self):

items = self.get("items") + (self.get("packed_items") or [])
for d in items:
if not self.get("return_against"):
if not self.get("return_against") or (
get_valuation_method(d.item_code) == "Moving Average" and self.get("is_return")
):
# Get incoming rate based on original item cost based on valuation method
qty = flt(d.get("stock_qty") or d.get("actual_qty"))

if not (self.get("is_return") and d.incoming_rate):
if not d.incoming_rate:
d.incoming_rate = get_incoming_rate(
{
"item_code": d.item_code,
Expand Down
50 changes: 50 additions & 0 deletions erpnext/stock/doctype/delivery_note/test_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,56 @@ def test_non_internal_transfer_delivery_note(self):
dn.reload()
self.assertFalse(dn.items[0].target_warehouse)

def test_sales_return_valuation_for_moving_average(self):
item_code = make_item(
"_Test Item Sales Return with MA", {"is_stock_item": 1, "valuation_method": "Moving Average"}
).name

make_stock_entry(
item_code=item_code,
target="_Test Warehouse - _TC",
qty=5,
basic_rate=100.0,
posting_date=add_days(nowdate(), -5),
)
dn = create_delivery_note(
item_code=item_code, qty=5, rate=500, posting_date=add_days(nowdate(), -4)
)
self.assertEqual(dn.items[0].incoming_rate, 100.0)

make_stock_entry(
item_code=item_code,
target="_Test Warehouse - _TC",
qty=5,
basic_rate=200.0,
posting_date=add_days(nowdate(), -3),
)
make_stock_entry(
item_code=item_code,
target="_Test Warehouse - _TC",
qty=5,
basic_rate=300.0,
posting_date=add_days(nowdate(), -2),
)

dn1 = create_delivery_note(
is_return=1,
item_code=item_code,
return_against=dn.name,
qty=-5,
rate=500,
company=dn.company,
expense_account="Cost of Goods Sold - _TC",
cost_center="Main - _TC",
do_not_submit=1,
posting_date=add_days(nowdate(), -1),
)

# (300 * 5) + (200 * 5) = 2500
# 2500 / 10 = 250

self.assertAlmostEqual(dn1.items[0].incoming_rate, 250.0)


def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note")
Expand Down

0 comments on commit 7fdac62

Please sign in to comment.