From bb877f4a6bf15adab69a71223307aab3427db52f Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:37:59 +0530 Subject: [PATCH] fix: disable primary action button only when there are no active capitalization --- erpnext/assets/doctype/asset/asset.js | 18 ++++++++++++++---- erpnext/assets/doctype/asset/asset.json | 3 +-- erpnext/assets/doctype/asset/asset.py | 8 ++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.js b/erpnext/assets/doctype/asset/asset.js index 07eb2b2b4d69..eea44697d638 100644 --- a/erpnext/assets/doctype/asset/asset.js +++ b/erpnext/assets/doctype/asset/asset.js @@ -188,11 +188,21 @@ frappe.ui.form.on("Asset", { frm.toggle_reqd("finance_books", frm.doc.calculate_depreciation); if (frm.doc.is_composite_asset) { - $(".primary-action").prop("hidden", true); - $(".form-message").text("Capitalize this asset to confirm"); + frappe.call({ + method: "erpnext.assets.doctype.asset.asset.has_active_capitalization", + args: { + asset: frm.doc.name, + }, + callback: function (r) { + if (!r.message) { + $(".primary-action").prop("hidden", true); + $(".form-message").text("Capitalize this asset to confirm"); - frm.add_custom_button(__("Capitalize Asset"), function () { - frm.trigger("create_asset_capitalization"); + frm.add_custom_button(__("Capitalize Asset"), function () { + frm.trigger("create_asset_capitalization"); + }); + } + }, }); } } diff --git a/erpnext/assets/doctype/asset/asset.json b/erpnext/assets/doctype/asset/asset.json index 2331e7d3e38d..07228c5f2551 100644 --- a/erpnext/assets/doctype/asset/asset.json +++ b/erpnext/assets/doctype/asset/asset.json @@ -221,7 +221,6 @@ "read_only": 1 }, { - "depends_on": "eval:!doc.is_composite_asset", "fieldname": "gross_purchase_amount", "fieldtype": "Currency", "label": "Gross Purchase Amount", @@ -580,7 +579,7 @@ "link_fieldname": "target_asset" } ], - "modified": "2024-07-07 22:27:14.733839", + "modified": "2024-08-01 16:39:09.340973", "modified_by": "Administrator", "module": "Assets", "name": "Asset", diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 882527b26114..9bd2f98b8311 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -1036,6 +1036,14 @@ def get_asset_value_after_depreciation(asset_name, finance_book=None): return asset.get_value_after_depreciation(finance_book) +@frappe.whitelist() +def has_active_capitalization(asset): + active_capitalizations = frappe.db.count( + "Asset Capitalization", filters={"target_asset": asset, "docstatus": 1} + ) + return active_capitalizations > 0 + + @frappe.whitelist() def split_asset(asset_name, split_qty): asset = frappe.get_doc("Asset", asset_name)