diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index e166fa22e8..d6cf5d8b90 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -768,7 +768,14 @@ class SalesInvoice(SellingController): if item.is_fixed_asset: asset = frappe.get_doc("Asset", item.asset) - fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(asset, item.base_net_amount) + if (len(asset.finance_books) > 1 and not item.finance_book + and asset.finance_books[0].finance_book): + frappe.throw(_("Select finance book for the item {0} at row {1}") + .format(item.item_code, item.idx)) + + fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(asset, + item.base_net_amount, item.finance_book) + for gle in fixed_asset_gl_entries: gle["against"] = self.customer gl_entries.append(self.get_gl_dict(gle)) diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json index d7eb8ed0ee..465df277fd 100644 --- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json +++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json @@ -57,6 +57,7 @@ "income_account", "is_fixed_asset", "asset", + "finance_book", "col_break4", "expense_account", "deferred_revenue", @@ -770,11 +771,18 @@ { "fieldname": "dimension_col_break", "fieldtype": "Column Break" + }, + { + "depends_on": "asset", + "fieldname": "finance_book", + "fieldtype": "Link", + "label": "Finance Book", + "options": "Finance Book" } ], "idx": 1, "istable": 1, - "modified": "2019-05-25 22:05:59.971263", + "modified": "2019-06-28 17:30:12.156086", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice Item", diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 797075b887..61108ec4a3 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -36,7 +36,7 @@ def make_depreciation_entry(asset_name, date=None): fixed_asset_account, accumulated_depreciation_account, depreciation_expense_account = \ get_depreciation_accounts(asset) - depreciation_cost_center, depreciation_series = frappe.get_cached_value('Company', asset.company, + depreciation_cost_center, depreciation_series = frappe.get_cached_value('Company', asset.company, ["depreciation_cost_center", "series_for_depreciation_entry"]) depreciation_cost_center = asset.cost_center or depreciation_cost_center @@ -70,7 +70,7 @@ def make_depreciation_entry(asset_name, date=None): je.submit() d.db_set("journal_entry", je.name) - + idx = cint(d.finance_book_id) finance_books = asset.get('finance_books')[idx - 1] finance_books.value_after_depreciation -= d.depreciation_amount @@ -88,15 +88,15 @@ def get_depreciation_accounts(asset): fieldname = ['fixed_asset_account', 'accumulated_depreciation_account', 'depreciation_expense_account'], as_dict=1) - if accounts: + if accounts: fixed_asset_account = accounts.fixed_asset_account accumulated_depreciation_account = accounts.accumulated_depreciation_account depreciation_expense_account = accounts.depreciation_expense_account - + if not accumulated_depreciation_account or not depreciation_expense_account: - accounts = frappe.get_cached_value('Company', asset.company, + accounts = frappe.get_cached_value('Company', asset.company, ["accumulated_depreciation_account", "depreciation_expense_account"]) - + if not accumulated_depreciation_account: accumulated_depreciation_account = accounts[0] if not depreciation_expense_account: @@ -135,11 +135,11 @@ def scrap_asset(asset_name): je.flags.ignore_permissions = True je.submit() - + frappe.db.set_value("Asset", asset_name, "disposal_date", today()) frappe.db.set_value("Asset", asset_name, "journal_entry_for_scrap", je.name) asset.set_status("Scrapped") - + frappe.msgprint(_("Asset scrapped via Journal Entry {0}").format(je.name)) @frappe.whitelist() @@ -147,21 +147,29 @@ def restore_asset(asset_name): asset = frappe.get_doc("Asset", asset_name) je = asset.journal_entry_for_scrap - + asset.db_set("disposal_date", None) asset.db_set("journal_entry_for_scrap", None) - + frappe.get_doc("Journal Entry", je).cancel() asset.set_status() @frappe.whitelist() -def get_gl_entries_on_asset_disposal(asset, selling_amount=0): +def get_gl_entries_on_asset_disposal(asset, selling_amount=0, finance_book=None): fixed_asset_account, accumulated_depr_account, depr_expense_account = get_depreciation_accounts(asset) disposal_account, depreciation_cost_center = get_disposal_account_and_cost_center(asset.company) depreciation_cost_center = asset.cost_center or depreciation_cost_center - accumulated_depr_amount = flt(asset.gross_purchase_amount) - flt(asset.value_after_depreciation) + idx = 1 + if finance_book: + for d in asset.finance_books: + if d.finance_book == finance_book: + idx = d.idx + break + + value_after_depreciation = asset.finance_books[idx - 1].value_after_depreciation + accumulated_depr_amount = flt(asset.gross_purchase_amount) - flt(value_after_depreciation) gl_entries = [ { @@ -176,7 +184,7 @@ def get_gl_entries_on_asset_disposal(asset, selling_amount=0): } ] - profit_amount = flt(selling_amount) - flt(asset.value_after_depreciation) + profit_amount = flt(selling_amount) - flt(value_after_depreciation) if profit_amount: debit_or_credit = "debit" if profit_amount < 0 else "credit" gl_entries.append({ @@ -190,7 +198,7 @@ def get_gl_entries_on_asset_disposal(asset, selling_amount=0): @frappe.whitelist() def get_disposal_account_and_cost_center(company): - disposal_account, depreciation_cost_center = frappe.get_cached_value('Company', company, + disposal_account, depreciation_cost_center = frappe.get_cached_value('Company', company, ["disposal_account", "depreciation_cost_center"]) if not disposal_account: diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index ef85ffa1cb..fceccfbd1c 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -366,8 +366,9 @@ class TestAsset(unittest.TestCase): self.assertTrue(asset.journal_entry_for_scrap) expected_gle = ( - ("_Test Accumulated Depreciations - _TC", 100000.0, 0.0), - ("_Test Fixed Asset - _TC", 0.0, 100000.0) + ("_Test Accumulated Depreciations - _TC", 147.54, 0.0), + ("_Test Fixed Asset - _TC", 0.0, 100000.0), + ("_Test Gain/Loss on Asset Disposal - _TC", 99852.46, 0.0) ) gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry` @@ -411,9 +412,9 @@ class TestAsset(unittest.TestCase): self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Sold") expected_gle = ( - ("_Test Accumulated Depreciations - _TC", 100000.0, 0.0), + ("_Test Accumulated Depreciations - _TC", 23051.47, 0.0), ("_Test Fixed Asset - _TC", 0.0, 100000.0), - ("_Test Gain/Loss on Asset Disposal - _TC", 0, 25000.0), + ("_Test Gain/Loss on Asset Disposal - _TC", 51948.53, 0.0), ("Debtors - _TC", 25000.0, 0.0) )