fix: asset capitalization (backport #35832) (#35843)

* 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 fb823b53d1d9a631035016fa3b33003bf8ce297a)

# 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>
diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
index 01fcb11..6d55d77 100644
--- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
+++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
@@ -15,7 +15,6 @@
 	}
 
 	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();
@@ -129,10 +128,6 @@
 		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") {
@@ -247,26 +242,6 @@
 		}
 	}
 
-	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;
 
diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.json b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.json
index 01b35f6..04b0c4e 100644
--- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.json
+++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.json
@@ -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",
@@ -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'",
@@ -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
   },
   {
@@ -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"
@@ -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",
@@ -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",
@@ -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"
@@ -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",
diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py
index 6841c56..a883bec 100644
--- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py
+++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py
@@ -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
@@ -45,7 +42,6 @@
 	"target_has_batch_no",
 	"target_stock_uom",
 	"stock_uom",
-	"target_fixed_asset_account",
 	"fixed_asset_account",
 	"valuation_rate",
 ]
@@ -56,7 +52,6 @@
 		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()
@@ -71,11 +66,12 @@
 
 	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 = (
@@ -86,7 +82,7 @@
 		)
 		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
@@ -97,15 +93,6 @@
 			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())
@@ -157,9 +144,6 @@
 
 		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:
@@ -170,17 +154,6 @@
 
 		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:
@@ -386,7 +359,11 @@
 			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):
@@ -429,11 +406,14 @@
 	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(
@@ -519,40 +499,46 @@
 					)
 				)
 
-	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)
+		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()
 
-				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)
+		self.target_asset = asset_doc.name
 
-	def get_asset(self, item):
-		asset = frappe.get_doc("Asset", item.asset)
-		self.check_finance_books(item, asset)
-		return asset
+		self.target_fixed_asset_account = get_asset_category_account(
+			"fixed_asset_account", item=self.target_item_code, company=asset_doc.company
+		)
+
+		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:
@@ -603,33 +589,6 @@
 
 
 @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):
 		args = json.loads(args)
diff --git a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py
index 5345d0e..6e0a685 100644
--- a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py
+++ b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py
@@ -47,13 +47,6 @@
 
 		total_amount = 103000
 
-		# Create assets
-		target_asset = create_asset(
-			asset_name="Asset Capitalization Target Asset",
-			submit=1,
-			warehouse="Stores - TCP1",
-			company=company,
-		)
 		consumed_asset = create_asset(
 			asset_name="Asset Capitalization Consumable Asset",
 			asset_value=consumed_asset_value,
@@ -65,7 +58,8 @@
 		# Create and submit Asset Captitalization
 		asset_capitalization = create_asset_capitalization(
 			entry_type="Capitalization",
-			target_asset=target_asset.name,
+			target_item_code="Macbook Pro",
+			target_asset_location="Test Location",
 			stock_qty=stock_qty,
 			stock_rate=stock_rate,
 			consumed_asset=consumed_asset.name,
@@ -94,7 +88,7 @@
 		self.assertEqual(asset_capitalization.target_incoming_rate, total_amount)
 
 		# Test Target Asset values
-		target_asset.reload()
+		target_asset = frappe.get_doc("Asset", asset_capitalization.target_asset)
 		self.assertEqual(target_asset.gross_purchase_amount, total_amount)
 		self.assertEqual(target_asset.purchase_receipt_amount, total_amount)
 
@@ -142,13 +136,6 @@
 
 		total_amount = 103000
 
-		# Create assets
-		target_asset = create_asset(
-			asset_name="Asset Capitalization Target Asset",
-			submit=1,
-			warehouse="Stores - _TC",
-			company=company,
-		)
 		consumed_asset = create_asset(
 			asset_name="Asset Capitalization Consumable Asset",
 			asset_value=consumed_asset_value,
@@ -160,7 +147,8 @@
 		# Create and submit Asset Captitalization
 		asset_capitalization = create_asset_capitalization(
 			entry_type="Capitalization",
-			target_asset=target_asset.name,
+			target_item_code="Macbook Pro",
+			target_asset_location="Test Location",
 			stock_qty=stock_qty,
 			stock_rate=stock_rate,
 			consumed_asset=consumed_asset.name,
@@ -189,7 +177,7 @@
 		self.assertEqual(asset_capitalization.target_incoming_rate, total_amount)
 
 		# Test Target Asset values
-		target_asset.reload()
+		target_asset = frappe.get_doc("Asset", asset_capitalization.target_asset)
 		self.assertEqual(target_asset.gross_purchase_amount, total_amount)
 		self.assertEqual(target_asset.purchase_receipt_amount, total_amount)
 
@@ -364,6 +352,7 @@
 			"posting_time": args.posting_time or now.strftime("%H:%M:%S.%f"),
 			"target_item_code": target_item_code,
 			"target_asset": target_asset.name,
+			"target_asset_location": "Test Location",
 			"target_warehouse": target_warehouse,
 			"target_qty": flt(args.target_qty) or 1,
 			"target_batch_no": args.target_batch_no,