diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 8bc3b50d198..8d933dd8ed5 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -155,7 +155,7 @@ class StockController(AccountsController): ) ) - def get_item_wise_inventory_account_map(self, company): + def get_item_wise_inventory_account_map(self): inventory_account_map = frappe._dict() for table in ["items", "packed_items", "supplied_items"]: if not self.get(table): @@ -190,8 +190,7 @@ class StockController(AccountsController): ).format(bold(item_code)) ) - if account_dict: - return account_dict + return account_dict if not warehouse_field: warehouse_field = "warehouse" @@ -207,7 +206,7 @@ class StockController(AccountsController): def get_inventory_account_map(self): if self.use_item_inventory_account: - return self.get_item_wise_inventory_account_map(self.company) + return self.get_item_wise_inventory_account_map() return get_warehouse_account_map(self.company) @@ -718,11 +717,11 @@ class StockController(AccountsController): item_row, inventory_account_map, warehouse_field="target_warehouse" ) - warehouse_asset_account = _inv_dict["account"] + warehouse_asset_account = _inv_dict.get("account") if _inv_dict else None elif self.get("is_internal_supplier"): _inv_dict = self.get_inventory_account_dict(item_row, inventory_account_map) - warehouse_asset_account = _inv_dict["account"] + warehouse_asset_account = _inv_dict.get("account") if _inv_dict else None expense_account = frappe.get_cached_value("Company", self.company, "default_expense_account") if not expense_account: @@ -2095,7 +2094,7 @@ def get_item_wise_inventory_account_map(rows, company): inventory_map = frappe._dict() for row in rows: - item_code = row.rm_item_code if hasattr(row, "rm_item_code") else row.item_code + item_code = row.rm_item_code if hasattr(row, "rm_item_code") and row.rm_item_code else row.item_code if not item_code: continue diff --git a/erpnext/controllers/tests/test_item_wise_inventory_account.py b/erpnext/controllers/tests/test_item_wise_inventory_account.py index 01ae69265ac..dc8a9798fe9 100644 --- a/erpnext/controllers/tests/test_item_wise_inventory_account.py +++ b/erpnext/controllers/tests/test_item_wise_inventory_account.py @@ -6,7 +6,7 @@ from collections import defaultdict import frappe from frappe.tests import IntegrationTestCase -from frappe.utils import cint +from frappe.utils import add_days, cint, today from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record @@ -167,6 +167,98 @@ class TestItemWiseInventoryAccount(IntegrationTestCase): self.assertEqual(sle_value, gl_value, f"GL Entry not created for {item_code} correctly") + def test_item_account_for_backdated_purchase_receipt(self): + items = { + "Bottle Item A": {"is_stock_item": 1}, + } + + for item_name, item_data in items.items(): + item = make_item( + item_name, + properties=item_data, + ) + + account = self.add_inventory_account(item) + items[item_name]["account"] = account + + make_purchase_receipt( + item_code="Bottle Item A", + qty=5, + rate=100, + warehouse=self.default_warehouse, + company=self.company, + ) + + dn = create_delivery_note( + item_code="Bottle Item A", + qty=5, + rate=200, + warehouse=self.default_warehouse, + company=self.company, + cost_center=frappe.db.get_value("Company", self.company, "cost_center"), + expense_account=frappe.db.get_value("Company", self.company, "default_expense_account"), + ) + + for row in items: + item_code = row + account = items[item_code]["account"] + + sle_value = frappe.db.get_value( + "Stock Ledger Entry", + {"voucher_type": "Delivery Note", "voucher_no": dn.name, "item_code": item_code}, + "stock_value_difference", + ) + + gl_value = ( + frappe.db.get_value( + "GL Entry", + { + "voucher_type": "Delivery Note", + "voucher_no": dn.name, + "account": account, + }, + "credit", + ) + * -1 + ) + + self.assertEqual(sle_value, gl_value, f"GL Entry not created for {item_code} correctly") + + make_purchase_receipt( + item_code="Bottle Item A", + posting_date=add_days(today(), -1), + qty=5, + rate=200, + warehouse=self.default_warehouse, + company=self.company, + ) + + for row in items: + item_code = row + account = items[item_code]["account"] + + sle_value = frappe.db.get_value( + "Stock Ledger Entry", + {"voucher_type": "Delivery Note", "voucher_no": dn.name, "item_code": item_code}, + "stock_value_difference", + ) + + gl_value = ( + frappe.db.get_value( + "GL Entry", + { + "voucher_type": "Delivery Note", + "voucher_no": dn.name, + "account": account, + }, + "credit", + ) + * -1 + ) + + self.assertEqual(sle_value, gl_value, f"GL Entry not created for {item_code} correctly") + self.assertEqual(sle_value, 1000.0 * -1, f"GL Entry not created for {item_code} correctly") + def test_item_group_account_for_purchase_receipt_entry(self): items = { "Stock Item C": {"is_stock_item": 1, "item_group": "Test Item Group C"}, diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py index 9a6fe76ecfe..815c4002cf1 100644 --- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py +++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py @@ -616,6 +616,7 @@ class SubcontractingReceipt(SubcontractingController): supplied_items_details.setdefault(item.reference_name, []).append( frappe._dict( { + "item_code": item.rm_item_code, "amount": item.amount, "expense_account": item.expense_account, "cost_center": item.cost_center,