Merge branch 'develop' into fix_asset_sold_status
diff --git a/erpnext/assets/doctype/asset/asset.json b/erpnext/assets/doctype/asset/asset.json
index 991df4e..f0505ff 100644
--- a/erpnext/assets/doctype/asset/asset.json
+++ b/erpnext/assets/doctype/asset/asset.json
@@ -388,7 +388,7 @@
    "in_standard_filter": 1,
    "label": "Status",
    "no_copy": 1,
-   "options": "Draft\nSubmitted\nPartially Depreciated\nFully Depreciated\nSold\nScrapped\nIn Maintenance\nOut of Order\nIssue\nReceipt",
+   "options": "Draft\nSubmitted\nPartially Depreciated\nFully Depreciated\nSold\nScrapped\nIn Maintenance\nOut of Order\nIssue\nReceipt\nCapitalized\nDecapitalized",
    "read_only": 1
   },
   {
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index 8ac7ed6..87d4c16 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -654,13 +654,27 @@
 		self.db_set("status", status)
 
 	def get_status(self):
-		"""Returns status based on whether it is draft, submitted, scrapped or depreciated"""
+		"""Returns status based on whether it is draft, submitted, sold, scrapped or depreciated"""
 		if self.docstatus == 0:
 			status = "Draft"
 		elif self.docstatus == 1:
 			status = "Submitted"
 
-			if self.journal_entry_for_scrap:
+			item = frappe.qb.DocType("Sales Invoice Item").as_("item")
+			si = frappe.qb.DocType("Sales Invoice").as_("si")
+
+			is_asset_sold = (
+				frappe.qb.from_(item)
+				.select(item.parent)
+				.inner_join(si)
+				.on(item.parent == si.name)
+				.where(item.asset == self.name)
+				.where(si.docstatus == 1)
+			).run()
+
+			if is_asset_sold:
+				status = "Sold"
+			elif self.journal_entry_for_scrap:
 				status = "Scrapped"
 			elif self.finance_books:
 				idx = self.get_default_finance_book_idx() or 0
diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py
index f72b524..ccd19ad 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -16,7 +16,11 @@
 )
 
 from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
-from erpnext.assets.doctype.asset.asset import make_sales_invoice, split_asset
+from erpnext.assets.doctype.asset.asset import (
+	make_sales_invoice,
+	split_asset,
+	update_maintenance_status,
+)
 from erpnext.assets.doctype.asset.depreciation import (
 	post_depreciation_entries,
 	restore_asset,
@@ -300,6 +304,34 @@
 		si.cancel()
 		self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Partially Depreciated")
 
+	def test_asset_with_maintenance_required_status_after_sale(self):
+		asset = create_asset(
+			calculate_depreciation=1,
+			available_for_use_date="2020-06-06",
+			purchase_date="2020-01-01",
+			expected_value_after_useful_life=10000,
+			total_number_of_depreciations=3,
+			frequency_of_depreciation=10,
+			maintenance_required=1,
+			depreciation_start_date="2020-12-31",
+			submit=1,
+		)
+
+		post_depreciation_entries(date="2021-01-01")
+
+		si = make_sales_invoice(asset=asset.name, item_code="Macbook Pro", company="_Test Company")
+		si.customer = "_Test Customer"
+		si.due_date = nowdate()
+		si.get("items")[0].rate = 25000
+		si.insert()
+		si.submit()
+
+		self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Sold")
+
+		update_maintenance_status()
+
+		self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Sold")
+
 	def test_asset_splitting(self):
 		asset = create_asset(
 			calculate_depreciation=1,
@@ -1418,6 +1450,7 @@
 			"number_of_depreciations_booked": args.number_of_depreciations_booked or 0,
 			"gross_purchase_amount": args.gross_purchase_amount or 100000,
 			"purchase_receipt_amount": args.purchase_receipt_amount or 100000,
+			"maintenance_required": args.maintenance_required or 0,
 			"warehouse": args.warehouse or "_Test Warehouse - _TC",
 			"available_for_use_date": args.available_for_use_date or "2020-06-06",
 			"location": args.location or "Test Location",