Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expense account should be fetched from related asset category (backport #41024) #41389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions erpnext/assets/doctype/asset/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,12 +1738,12 @@ def create_asset(**args):
return asset


def create_asset_category():
def create_asset_category(enable_cwip=1):
asset_category = frappe.new_doc("Asset Category")
asset_category.asset_category_name = "Computers"
asset_category.total_number_of_depreciations = 3
asset_category.frequency_of_depreciation = 3
asset_category.enable_cwip_accounting = 1
asset_category.enable_cwip_accounting = enable_cwip
asset_category.append(
"accounts",
{
Expand Down
3 changes: 3 additions & 0 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ def set_missing_item_details(self, for_validate=False):
# reset pricing rule fields if pricing_rule_removed
item.set(fieldname, value)

elif fieldname == "expense_account" and not item.get("expense_account"):
item.expense_account = value

if self.doctype in ["Purchase Invoice", "Sales Invoice"] and item.meta.get_field(
"is_fixed_asset"
):
Expand Down
4 changes: 3 additions & 1 deletion erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ def process_fixed_asset(self):
def auto_make_assets(self, asset_items):
items_data = get_asset_item_details(asset_items)
messages = []
alert = False

for d in self.items:
if d.is_fixed_asset:
Expand Down Expand Up @@ -761,9 +762,10 @@ def auto_make_assets(self, asset_items):
frappe.bold(d.item_code)
)
)
alert = True

for message in messages:
frappe.msgprint(message, title="Success", indicator="green")
frappe.msgprint(message, title="Success", indicator="green", alert=alert)

def make_asset(self, row, is_grouped_asset=False):
if not row.asset_location:
Expand Down
27 changes: 27 additions & 0 deletions erpnext/stock/doctype/item/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ def test_get_item_details(self):
for key, value in to_check.items():
self.assertEqual(value, details.get(key), key)

def test_get_asset_item_details(self):
from erpnext.assets.doctype.asset.test_asset import create_asset_category, create_fixed_asset_item

create_asset_category(0)
create_fixed_asset_item()

details = get_item_details(
{
"item_code": "Macbook Pro",
"company": "_Test Company",
"currency": "INR",
"doctype": "Purchase Receipt",
}
)
self.assertEqual(details.get("expense_account"), "_Test Fixed Asset - _TC")

frappe.db.set_value("Asset Category", "Computers", "enable_cwip_accounting", "1")
details = get_item_details(
{
"item_code": "Macbook Pro",
"company": "_Test Company",
"currency": "INR",
"doctype": "Purchase Receipt",
}
)
self.assertEqual(details.get("expense_account"), "CWIP Account - _TC")

def test_item_tax_template(self):
expected_item_tax_template = [
{
Expand Down
22 changes: 2 additions & 20 deletions erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt


import frappe
from frappe import _, throw
from frappe.desk.notifications import clear_doctype_notifications
from frappe.model.mapper import get_mapped_doc
from frappe.query_builder.functions import CombineDatetime
from frappe.utils import cint, flt, get_datetime, getdate, nowdate
int, flt, get_datetime, getdate, nowdate
from pypika import functions as fn

import erpnext
Expand Down Expand Up @@ -670,18 +662,8 @@ def make_divisional_loss_gl_entry(item, outgoing_amount):
else self.get_company_default("stock_received_but_not_billed")
)
landed_cost_entries = get_item_account_wise_additional_cost(self.name)

if d.is_fixed_asset:
account_type = (
"capital_work_in_progress_account"
if is_cwip_accounting_enabled(d.asset_category)
else "fixed_asset_account"
)

stock_asset_account_name = get_asset_account(
account_type, asset_category=d.asset_category, company=self.company
)

stock_asset_account_name = d.expense_account
stock_value_diff = (
flt(d.base_net_amount) + flt(d.item_tax_amount) + flt(d.landed_cost_voucher_amount)
)
Expand Down
25 changes: 19 additions & 6 deletions erpnext/stock/get_item_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def get_item_details(args, doc=None, for_validate=False, overwrite_warehouse=Tru
args["bill_date"] = doc.get("bill_date")

out = get_basic_details(args, item, overwrite_warehouse)

get_item_tax_template(args, item, out)
out["item_tax_rate"] = get_item_tax_map(
args.company,
Expand Down Expand Up @@ -291,12 +290,26 @@ def get_basic_details(args, item, overwrite_warehouse=True):

expense_account = None

if args.get("doctype") == "Purchase Invoice" and item.is_fixed_asset:
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
if item.is_fixed_asset:
from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled

expense_account = get_asset_category_account(
fieldname="fixed_asset_account", item=args.item_code, company=args.company
)
if is_cwip_accounting_enabled(item.asset_category):
expense_account = get_asset_account(
"capital_work_in_progress_account",
asset_category=item.asset_category,
company=args.company,
)
elif args.get("doctype") in (
"Purchase Invoice",
"Purchase Receipt",
"Purchase Order",
"Material Request",
):
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account

expense_account = get_asset_category_account(
fieldname="fixed_asset_account", item=args.item_code, company=args.company
)

# Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
if not args.get("uom"):
Expand Down