fix: persist redistributed additional costs during stock entry repost (backport #58433) (#58532)

* fix: persist redistributed additional costs during stock entry repost (#58433)

* fix: persist redistributed additional costs during stock entry repost

* test: cover additional cost persistence on stock entry recalculation

(cherry picked from commit 074c84e880)

# Conflicts:
#	erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py

* chore: fix conflicts

Removed unused import 'flt' from test file.

---------

Co-authored-by: Afsal Syed <146159709+Afsalsyed@users.noreply.github.com>
Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
This commit is contained in:
mergify[bot]
2026-08-28 10:42:27 +00:00
committed by GitHub
parent 1aa7528119
commit c4e7cfa6a0
2 changed files with 54 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ from unittest.mock import MagicMock, call
import frappe
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, add_to_date, now, nowdate, today
from frappe.utils import add_days, add_to_date, flt, now, nowdate, today
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
from erpnext.accounts.utils import repost_gle_for_stock_vouchers
@@ -475,6 +475,56 @@ class TestRepostItemValuation(FrappeTestCase, StockTestMixin):
# incoming rate after reposting should be 150
self.assertSLEs(se, [{"incoming_rate": 150}])
def test_recalculate_stock_entry_additional_cost_updates_all_incoming_rows(self):
from erpnext.stock.stock_ledger import update_entries_after
company = frappe.db.get_value("Warehouse", "Stores - TCP1", "company")
warehouse = "Stores - TCP1"
items = [
self.make_item(f"_Test Repost Addl Cost {x}", {"is_stock_item": 1}).name for x in ("A", "B", "C")
]
for item_code in items:
make_stock_entry(item_code=item_code, target=warehouse, company=company, qty=100, rate=10)
transfer = make_stock_entry(company=company, purpose="Material Transfer", do_not_save=True)
transfer.from_warehouse = warehouse
transfer.to_warehouse = warehouse
transfer.items = []
for item_code in items:
transfer.append(
"items",
{
"item_code": item_code,
"qty": 100,
"s_warehouse": warehouse,
"t_warehouse": warehouse,
"uom": "Nos",
"conversion_factor": 1,
},
)
transfer.append(
"additional_costs",
{
"expense_account": "Expenses Included In Valuation - TCP1",
"description": "freight",
"amount": 100,
},
)
transfer.insert()
transfer.submit()
first_row = transfer.items[0]
frappe.db.set_value("Stock Entry Detail", first_row.name, "basic_rate", first_row.basic_rate + 1)
update_entries_after.recalculate_amounts_in_stock_entry(MagicMock(), transfer.name, first_row.name)
transfer.load_from_db()
detail_additional_cost = sum(row.additional_cost for row in transfer.items)
net_added_to_stock = sum(row.amount - row.basic_amount for row in transfer.items)
self.assertEqual(flt(detail_additional_cost, 2), flt(transfer.total_additional_costs, 2))
self.assertEqual(flt(net_added_to_stock, 2), flt(transfer.total_additional_costs, 2))
def test_repost_multi_line_moving_average_return(self):
from erpnext.controllers.sales_and_purchase_return import make_return_doc

View File

@@ -1406,12 +1406,14 @@ class update_entries_after:
stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True)
stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False)
stock_entry.db_update()
update_additional_cost_rows = bool(stock_entry.get("additional_costs"))
for d in stock_entry.items:
# Update only the row that matches the voucher_detail_no or the row containing the FG/Scrap Item.
# Additional costs are redistributed across all incoming rows.
if (
d.name == voucher_detail_no
or (not d.s_warehouse and d.t_warehouse)
or stock_entry.purpose in ["Manufacture", "Repack"]
or (update_additional_cost_rows and d.t_warehouse)
):
d.db_update()