Skip to content

Commit

Permalink
fix: asset capitalization (backport #35832) (#35843)
Browse files Browse the repository at this point in the history
* fix: asset capitalization (#35832)

* fix: misc asset capitalisation fixes

* chore: add location in tests and remove unnecessary code

* chore: more fixes and removals

* chore: show company and fix tests

* chore: make target qty read only on capitalization

(cherry picked from commit fb823b5)

# Conflicts:
#	erpnext/assets/doctype/asset_capitalization/asset_capitalization.json
#	erpnext/assets/doctype/asset_capitalization/asset_capitalization.py

* chore: fixing conflicts

---------

Co-authored-by: Anand Baburajan <anandbaburajan@gmail.com>
  • Loading branch information
mergify[bot] and anandbaburajan authored Jun 22, 2023
1 parent 754eb6b commit e745312
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ erpnext.assets.AssetCapitalization = class AssetCapitalization extends erpnext.s
}

refresh() {
erpnext.hide_company();
this.show_general_ledger();
if ((this.frm.doc.stock_items && this.frm.doc.stock_items.length) || !this.frm.doc.target_is_fixed_asset) {
this.show_stock_ledger();
Expand Down Expand Up @@ -129,10 +128,6 @@ erpnext.assets.AssetCapitalization = class AssetCapitalization extends erpnext.s
return this.get_target_item_details();
}

target_asset() {
return this.get_target_asset_details();
}

item_code(doc, cdt, cdn) {
var row = frappe.get_doc(cdt, cdn);
if (cdt === "Asset Capitalization Stock Item") {
Expand Down Expand Up @@ -247,26 +242,6 @@ erpnext.assets.AssetCapitalization = class AssetCapitalization extends erpnext.s
}
}

get_target_asset_details() {
var me = this;

if (me.frm.doc.target_asset) {
return me.frm.call({
method: "erpnext.assets.doctype.asset_capitalization.asset_capitalization.get_target_asset_details",
child: me.frm.doc,
args: {
asset: me.frm.doc.target_asset,
company: me.frm.doc.company,
},
callback: function (r) {
if (!r.exc) {
me.frm.refresh_fields();
}
}
});
}
}

get_consumed_stock_item_details(row) {
var me = this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"naming_series",
"entry_type",
"target_item_code",
"target_asset",
"target_item_name",
"target_is_fixed_asset",
"target_has_batch_no",
"target_has_serial_no",
"column_break_9",
"target_asset",
"target_asset_name",
"target_asset_location",
"target_warehouse",
"target_qty",
"target_stock_uom",
Expand Down Expand Up @@ -85,14 +86,13 @@
"fieldtype": "Column Break"
},
{
"depends_on": "eval:doc.entry_type=='Capitalization'",
"fieldname": "target_asset",
"fieldtype": "Link",
"in_standard_filter": 1,
"label": "Target Asset",
"mandatory_depends_on": "eval:doc.entry_type=='Capitalization'",
"no_copy": 1,
"options": "Asset"
"options": "Asset",
"read_only": 1
},
{
"depends_on": "eval:doc.entry_type=='Capitalization'",
Expand All @@ -108,11 +108,11 @@
"fieldtype": "Column Break"
},
{
"fetch_from": "asset.company",
"fieldname": "company",
"fieldtype": "Link",
"label": "Company",
"options": "Company",
"remember_last_selected_value": 1,
"reqd": 1
},
{
Expand Down Expand Up @@ -158,7 +158,7 @@
"read_only": 1
},
{
"depends_on": "eval:doc.docstatus == 0 || (doc.stock_items && doc.stock_items.length)",
"depends_on": "eval:doc.entry_type=='Capitalization' && (doc.docstatus == 0 || (doc.stock_items && doc.stock_items.length))",
"fieldname": "section_break_16",
"fieldtype": "Section Break",
"label": "Consumed Stock Items"
Expand Down Expand Up @@ -189,7 +189,7 @@
"fieldname": "target_qty",
"fieldtype": "Float",
"label": "Target Qty",
"read_only_depends_on": "target_is_fixed_asset"
"read_only_depends_on": "eval:doc.entry_type=='Capitalization'"
},
{
"fetch_from": "target_item_code.stock_uom",
Expand Down Expand Up @@ -227,7 +227,7 @@
"depends_on": "eval:doc.docstatus == 0 || (doc.asset_items && doc.asset_items.length)",
"fieldname": "section_break_26",
"fieldtype": "Section Break",
"label": "Consumed Asset Items"
"label": "Consumed Assets"
},
{
"fieldname": "asset_items",
Expand Down Expand Up @@ -266,7 +266,7 @@
"options": "Finance Book"
},
{
"depends_on": "eval:doc.docstatus == 0 || (doc.service_items && doc.service_items.length)",
"depends_on": "eval:doc.entry_type=='Capitalization' && (doc.docstatus == 0 || (doc.service_items && doc.service_items.length))",
"fieldname": "service_expenses_section",
"fieldtype": "Section Break",
"label": "Service Expenses"
Expand Down Expand Up @@ -329,12 +329,20 @@
"label": "Target Fixed Asset Account",
"options": "Account",
"read_only": 1
},
{
"depends_on": "eval:doc.entry_type=='Capitalization'",
"fieldname": "target_asset_location",
"fieldtype": "Link",
"label": "Target Asset Location",
"mandatory_depends_on": "eval:doc.entry_type=='Capitalization'",
"options": "Location"
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-10-12 15:09:40.771332",
"modified": "2023-06-22 14:17:07.995120",
"modified_by": "Administrator",
"module": "Assets",
"name": "Asset Capitalization",
Expand Down
139 changes: 49 additions & 90 deletions erpnext/assets/doctype/asset_capitalization/asset_capitalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
reverse_depreciation_entry_made_after_disposal,
)
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
from erpnext.controllers.stock_controller import StockController
from erpnext.setup.doctype.brand.brand import get_brand_defaults
from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
Expand All @@ -45,7 +42,6 @@
"target_has_batch_no",
"target_stock_uom",
"stock_uom",
"target_fixed_asset_account",
"fixed_asset_account",
"valuation_rate",
]
Expand All @@ -56,7 +52,6 @@ def validate(self):
self.validate_posting_time()
self.set_missing_values(for_validate=True)
self.validate_target_item()
self.validate_target_asset()
self.validate_consumed_stock_item()
self.validate_consumed_asset_item()
self.validate_service_item()
Expand All @@ -71,11 +66,12 @@ def on_update(self):

def before_submit(self):
self.validate_source_mandatory()
if self.entry_type == "Capitalization":
self.create_target_asset()

def on_submit(self):
self.update_stock_ledger()
self.make_gl_entries()
self.update_target_asset()

def on_cancel(self):
self.ignore_linked_doctypes = (
Expand All @@ -86,7 +82,7 @@ def on_cancel(self):
)
self.update_stock_ledger()
self.make_gl_entries()
self.update_target_asset()
self.restore_consumed_asset_items()

def set_title(self):
self.title = self.target_asset_name or self.target_item_name or self.target_item_code
Expand All @@ -97,15 +93,6 @@ def set_missing_values(self, for_validate=False):
if self.meta.has_field(k) and (not self.get(k) or k in force_fields):
self.set(k, v)

# Remove asset if item not a fixed asset
if not self.target_is_fixed_asset:
self.target_asset = None

target_asset_details = get_target_asset_details(self.target_asset, self.company)
for k, v in target_asset_details.items():
if self.meta.has_field(k) and (not self.get(k) or k in force_fields):
self.set(k, v)

for d in self.stock_items:
args = self.as_dict()
args.update(d.as_dict())
Expand Down Expand Up @@ -157,9 +144,6 @@ def validate_target_item(self):

if not target_item.is_stock_item:
self.target_warehouse = None
if not target_item.is_fixed_asset:
self.target_asset = None
self.target_fixed_asset_account = None
if not target_item.has_batch_no:
self.target_batch_no = None
if not target_item.has_serial_no:
Expand All @@ -170,17 +154,6 @@ def validate_target_item(self):

self.validate_item(target_item)

def validate_target_asset(self):
if self.target_asset:
target_asset = self.get_asset_for_validation(self.target_asset)

if target_asset.item_code != self.target_item_code:
frappe.throw(
_("Asset {0} does not belong to Item {1}").format(self.target_asset, self.target_item_code)
)

self.validate_asset(target_asset)

def validate_consumed_stock_item(self):
for d in self.stock_items:
if d.item_code:
Expand Down Expand Up @@ -386,7 +359,11 @@ def get_gl_entries(
gl_entries, target_account, target_against, precision
)

if not self.stock_items and not self.service_items and self.are_all_asset_items_non_depreciable:
return []

self.get_gl_entries_for_target_item(gl_entries, target_against, precision)

return gl_entries

def get_target_account(self):
Expand Down Expand Up @@ -429,11 +406,14 @@ def get_gl_entries_for_consumed_stock_items(
def get_gl_entries_for_consumed_asset_items(
self, gl_entries, target_account, target_against, precision
):
self.are_all_asset_items_non_depreciable = True

# Consumed Assets
for item in self.asset_items:
asset = self.get_asset(item)
asset = frappe.get_doc("Asset", item.asset)

if asset.calculate_depreciation:
self.are_all_asset_items_non_depreciable = False
notes = _(
"This schedule was created when Asset {0} was consumed through Asset Capitalization {1}."
).format(
Expand Down Expand Up @@ -519,40 +499,46 @@ def get_gl_entries_for_target_item(self, gl_entries, target_against, precision):
)
)

def update_target_asset(self):
def create_target_asset(self):
total_target_asset_value = flt(self.total_value, self.precision("total_value"))
if self.docstatus == 1 and self.entry_type == "Capitalization":
asset_doc = frappe.get_doc("Asset", self.target_asset)
asset_doc.purchase_date = self.posting_date
asset_doc.gross_purchase_amount = total_target_asset_value
asset_doc.purchase_receipt_amount = total_target_asset_value
notes = _(
"This schedule was created when target Asset {0} was updated through Asset Capitalization {1}."
).format(
get_link_to_form(asset_doc.doctype, asset_doc.name), get_link_to_form(self.doctype, self.name)
)
make_new_active_asset_depr_schedules_and_cancel_current_ones(asset_doc, notes)
asset_doc.flags.ignore_validate_update_after_submit = True
asset_doc.save()
elif self.docstatus == 2:
for item in self.asset_items:
asset = self.get_asset(item)
asset.db_set("disposal_date", None)
self.set_consumed_asset_status(asset)

if asset.calculate_depreciation:
reverse_depreciation_entry_made_after_disposal(asset, self.posting_date)
notes = _(
"This schedule was created when Asset {0} was restored on Asset Capitalization {1}'s cancellation."
).format(
get_link_to_form(asset.doctype, asset.name), get_link_to_form(self.doctype, self.name)
)
reset_depreciation_schedule(asset, self.posting_date, notes)
asset_doc = frappe.new_doc("Asset")
asset_doc.company = self.company
asset_doc.item_code = self.target_item_code
asset_doc.is_existing_asset = 1
asset_doc.location = self.target_asset_location
asset_doc.available_for_use_date = self.posting_date
asset_doc.purchase_date = self.posting_date
asset_doc.gross_purchase_amount = total_target_asset_value
asset_doc.purchase_receipt_amount = total_target_asset_value
asset_doc.flags.ignore_validate = True
asset_doc.insert()

self.target_asset = asset_doc.name

self.target_fixed_asset_account = get_asset_category_account(
"fixed_asset_account", item=self.target_item_code, company=asset_doc.company
)

def get_asset(self, item):
asset = frappe.get_doc("Asset", item.asset)
self.check_finance_books(item, asset)
return asset
frappe.msgprint(
_(
"Asset {0} has been created. Please set the depreciation details if any and submit it."
).format(get_link_to_form("Asset", asset_doc.name))
)

def restore_consumed_asset_items(self):
for item in self.asset_items:
asset = frappe.get_doc("Asset", item.asset)
asset.db_set("disposal_date", None)
self.set_consumed_asset_status(asset)

if asset.calculate_depreciation:
reverse_depreciation_entry_made_after_disposal(asset, self.posting_date)
notes = _(
"This schedule was created when Asset {0} was restored on Asset Capitalization {1}'s cancellation."
).format(
get_link_to_form(asset.doctype, asset.name), get_link_to_form(self.doctype, self.name)
)
reset_depreciation_schedule(asset, self.posting_date, notes)

def set_consumed_asset_status(self, asset):
if self.docstatus == 1:
Expand Down Expand Up @@ -602,33 +588,6 @@ def get_target_item_details(item_code=None, company=None):
return out


@frappe.whitelist()
def get_target_asset_details(asset=None, company=None):
out = frappe._dict()

# Get Asset Details
asset_details = frappe._dict()
if asset:
asset_details = frappe.db.get_value("Asset", asset, ["asset_name", "item_code"], as_dict=1)
if not asset_details:
frappe.throw(_("Asset {0} does not exist").format(asset))

# Re-set item code from Asset
out.target_item_code = asset_details.item_code

# Set Asset Details
out.asset_name = asset_details.asset_name

if asset_details.item_code:
out.target_fixed_asset_account = get_asset_category_account(
"fixed_asset_account", item=asset_details.item_code, company=company
)
else:
out.target_fixed_asset_account = None

return out


@frappe.whitelist()
def get_consumed_stock_item_details(args):
if isinstance(args, str):
Expand Down
Loading

0 comments on commit e745312

Please sign in to comment.