fix: manual depr entry not updating asset value [v14] (#33788)
* fix: get value_after_depreciation functions and manual depr entry not updating asset value * fix: reflect manual depr entry in chart and fixed asset register * fix: add linked JEs in asset connections * chore: add test * chore: add patch and fix some things * chore: get depreciation_expense_account properly * chore: fix patch * chore: fix patch * chore: fix patch again * chore: rename var in patch * fix: filter assets with finance books properly * chore: refactor get_value_after_depreciation * chore: use dict format for filters in assets_linked_to_fb * chore: refactor update_asset_value * chore: sort manual depr entries
This commit is contained in:
@@ -81,6 +81,7 @@ class JournalEntry(AccountsController):
|
|||||||
self.check_credit_limit()
|
self.check_credit_limit()
|
||||||
self.make_gl_entries()
|
self.make_gl_entries()
|
||||||
self.update_advance_paid()
|
self.update_advance_paid()
|
||||||
|
self.update_asset_value()
|
||||||
self.update_inter_company_jv()
|
self.update_inter_company_jv()
|
||||||
self.update_invoice_discounting()
|
self.update_invoice_discounting()
|
||||||
|
|
||||||
@@ -225,6 +226,34 @@ class JournalEntry(AccountsController):
|
|||||||
for d in to_remove:
|
for d in to_remove:
|
||||||
self.remove(d)
|
self.remove(d)
|
||||||
|
|
||||||
|
def update_asset_value(self):
|
||||||
|
if self.voucher_type != "Depreciation Entry":
|
||||||
|
return
|
||||||
|
|
||||||
|
processed_assets = []
|
||||||
|
|
||||||
|
for d in self.get("accounts"):
|
||||||
|
if (
|
||||||
|
d.reference_type == "Asset" and d.reference_name and d.reference_name not in processed_assets
|
||||||
|
):
|
||||||
|
processed_assets.append(d.reference_name)
|
||||||
|
|
||||||
|
asset = frappe.db.get_value(
|
||||||
|
"Asset", d.reference_name, ["calculate_depreciation", "value_after_depreciation"], as_dict=1
|
||||||
|
)
|
||||||
|
|
||||||
|
if asset.calculate_depreciation:
|
||||||
|
continue
|
||||||
|
|
||||||
|
depr_value = d.debit or d.credit
|
||||||
|
|
||||||
|
frappe.db.set_value(
|
||||||
|
"Asset",
|
||||||
|
d.reference_name,
|
||||||
|
"value_after_depreciation",
|
||||||
|
asset.value_after_depreciation - depr_value,
|
||||||
|
)
|
||||||
|
|
||||||
def update_inter_company_jv(self):
|
def update_inter_company_jv(self):
|
||||||
if (
|
if (
|
||||||
self.voucher_type == "Inter Company Journal Entry"
|
self.voucher_type == "Inter Company Journal Entry"
|
||||||
@@ -283,9 +312,20 @@ class JournalEntry(AccountsController):
|
|||||||
d.db_update()
|
d.db_update()
|
||||||
|
|
||||||
def unlink_asset_reference(self):
|
def unlink_asset_reference(self):
|
||||||
|
if self.voucher_type != "Depreciation Entry":
|
||||||
|
return
|
||||||
|
|
||||||
|
processed_assets = []
|
||||||
|
|
||||||
for d in self.get("accounts"):
|
for d in self.get("accounts"):
|
||||||
if d.reference_type == "Asset" and d.reference_name:
|
if (
|
||||||
|
d.reference_type == "Asset" and d.reference_name and d.reference_name not in processed_assets
|
||||||
|
):
|
||||||
|
processed_assets.append(d.reference_name)
|
||||||
|
|
||||||
asset = frappe.get_doc("Asset", d.reference_name)
|
asset = frappe.get_doc("Asset", d.reference_name)
|
||||||
|
|
||||||
|
if asset.calculate_depreciation:
|
||||||
for s in asset.get("schedules"):
|
for s in asset.get("schedules"):
|
||||||
if s.journal_entry == self.name:
|
if s.journal_entry == self.name:
|
||||||
s.db_set("journal_entry", None)
|
s.db_set("journal_entry", None)
|
||||||
@@ -296,6 +336,15 @@ class JournalEntry(AccountsController):
|
|||||||
finance_books.db_update()
|
finance_books.db_update()
|
||||||
|
|
||||||
asset.set_status()
|
asset.set_status()
|
||||||
|
else:
|
||||||
|
depr_value = d.debit or d.credit
|
||||||
|
|
||||||
|
frappe.db.set_value(
|
||||||
|
"Asset",
|
||||||
|
d.reference_name,
|
||||||
|
"value_after_depreciation",
|
||||||
|
asset.value_after_depreciation + depr_value,
|
||||||
|
)
|
||||||
|
|
||||||
def unlink_inter_company_jv(self):
|
def unlink_inter_company_jv(self):
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -205,7 +205,7 @@ frappe.ui.form.on('Asset', {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
setup_chart: function(frm) {
|
setup_chart: async function(frm) {
|
||||||
if(frm.doc.finance_books.length > 1) {
|
if(frm.doc.finance_books.length > 1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -223,6 +223,7 @@ frappe.ui.form.on('Asset', {
|
|||||||
flt(frm.doc.opening_accumulated_depreciation));
|
flt(frm.doc.opening_accumulated_depreciation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(frm.doc.calculate_depreciation) {
|
||||||
$.each(frm.doc.schedules || [], function(i, v) {
|
$.each(frm.doc.schedules || [], function(i, v) {
|
||||||
x_intervals.push(v.schedule_date);
|
x_intervals.push(v.schedule_date);
|
||||||
var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount);
|
var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount);
|
||||||
@@ -237,6 +238,19 @@ frappe.ui.form.on('Asset', {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
let depr_entries = (await frappe.call({
|
||||||
|
method: "get_manual_depreciation_entries",
|
||||||
|
doc: frm.doc,
|
||||||
|
})).message;
|
||||||
|
|
||||||
|
$.each(depr_entries || [], function(i, v) {
|
||||||
|
x_intervals.push(v.posting_date);
|
||||||
|
last_depreciation_date = v.posting_date;
|
||||||
|
let last_asset_value = asset_values[asset_values.length - 1]
|
||||||
|
asset_values.push(last_asset_value - v.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if(in_list(["Scrapped", "Sold"], frm.doc.status)) {
|
if(in_list(["Scrapped", "Sold"], frm.doc.status)) {
|
||||||
x_intervals.push(frm.doc.disposal_date);
|
x_intervals.push(frm.doc.disposal_date);
|
||||||
|
|||||||
@@ -519,9 +519,15 @@
|
|||||||
"group": "Value",
|
"group": "Value",
|
||||||
"link_doctype": "Asset Value Adjustment",
|
"link_doctype": "Asset Value Adjustment",
|
||||||
"link_fieldname": "asset"
|
"link_fieldname": "asset"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"group": "Journal Entry",
|
||||||
|
"link_doctype": "Journal Entry",
|
||||||
|
"link_fieldname": "reference_name",
|
||||||
|
"table_fieldname": "accounts"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"modified": "2023-01-16 23:35:37.423100",
|
"modified": "2023-01-25 17:45:48.649543",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Assets",
|
"module": "Assets",
|
||||||
"name": "Asset",
|
"name": "Asset",
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ class Asset(AccountsController):
|
|||||||
def _make_depreciation_schedule(self, finance_book, start, date_of_disposal):
|
def _make_depreciation_schedule(self, finance_book, start, date_of_disposal):
|
||||||
self.validate_asset_finance_books(finance_book)
|
self.validate_asset_finance_books(finance_book)
|
||||||
|
|
||||||
value_after_depreciation = self._get_value_after_depreciation(finance_book)
|
value_after_depreciation = self._get_value_after_depreciation_for_making_schedule(finance_book)
|
||||||
finance_book.value_after_depreciation = value_after_depreciation
|
finance_book.value_after_depreciation = value_after_depreciation
|
||||||
|
|
||||||
number_of_pending_depreciations = cint(finance_book.total_number_of_depreciations) - cint(
|
number_of_pending_depreciations = cint(finance_book.total_number_of_depreciations) - cint(
|
||||||
@@ -377,8 +377,7 @@ class Asset(AccountsController):
|
|||||||
for idx, s in enumerate(self.schedules, 1):
|
for idx, s in enumerate(self.schedules, 1):
|
||||||
s.idx = idx
|
s.idx = idx
|
||||||
|
|
||||||
def _get_value_after_depreciation(self, finance_book):
|
def _get_value_after_depreciation_for_making_schedule(self, finance_book):
|
||||||
# value_after_depreciation - current Asset value
|
|
||||||
if self.docstatus == 1 and finance_book.value_after_depreciation:
|
if self.docstatus == 1 and finance_book.value_after_depreciation:
|
||||||
value_after_depreciation = flt(finance_book.value_after_depreciation)
|
value_after_depreciation = flt(finance_book.value_after_depreciation)
|
||||||
else:
|
else:
|
||||||
@@ -567,7 +566,9 @@ class Asset(AccountsController):
|
|||||||
|
|
||||||
if int(d.finance_book_id) not in finance_books:
|
if int(d.finance_book_id) not in finance_books:
|
||||||
accumulated_depreciation = flt(self.opening_accumulated_depreciation)
|
accumulated_depreciation = flt(self.opening_accumulated_depreciation)
|
||||||
value_after_depreciation = flt(self.get_value_after_depreciation(d.finance_book_id))
|
value_after_depreciation = flt(
|
||||||
|
self.get("finance_books")[cint(d.finance_book_id) - 1].value_after_depreciation
|
||||||
|
)
|
||||||
finance_books.append(int(d.finance_book_id))
|
finance_books.append(int(d.finance_book_id))
|
||||||
|
|
||||||
depreciation_amount = flt(d.depreciation_amount, d.precision("depreciation_amount"))
|
depreciation_amount = flt(d.depreciation_amount, d.precision("depreciation_amount"))
|
||||||
@@ -592,9 +593,6 @@ class Asset(AccountsController):
|
|||||||
accumulated_depreciation, d.precision("accumulated_depreciation_amount")
|
accumulated_depreciation, d.precision("accumulated_depreciation_amount")
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_value_after_depreciation(self, idx):
|
|
||||||
return flt(self.get("finance_books")[cint(idx) - 1].value_after_depreciation)
|
|
||||||
|
|
||||||
def validate_expected_value_after_useful_life(self):
|
def validate_expected_value_after_useful_life(self):
|
||||||
for row in self.get("finance_books"):
|
for row in self.get("finance_books"):
|
||||||
accumulated_depreciation_after_full_schedule = [
|
accumulated_depreciation_after_full_schedule = [
|
||||||
@@ -649,10 +647,15 @@ class Asset(AccountsController):
|
|||||||
movement.cancel()
|
movement.cancel()
|
||||||
|
|
||||||
def delete_depreciation_entries(self):
|
def delete_depreciation_entries(self):
|
||||||
|
if self.calculate_depreciation:
|
||||||
for d in self.get("schedules"):
|
for d in self.get("schedules"):
|
||||||
if d.journal_entry:
|
if d.journal_entry:
|
||||||
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
|
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
|
||||||
d.db_set("journal_entry", None)
|
else:
|
||||||
|
depr_entries = self.get_manual_depreciation_entries()
|
||||||
|
|
||||||
|
for depr_entry in depr_entries or []:
|
||||||
|
frappe.get_doc("Journal Entry", depr_entry.name).cancel()
|
||||||
|
|
||||||
self.db_set(
|
self.db_set(
|
||||||
"value_after_depreciation",
|
"value_after_depreciation",
|
||||||
@@ -688,6 +691,17 @@ class Asset(AccountsController):
|
|||||||
status = "Cancelled"
|
status = "Cancelled"
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
def get_value_after_depreciation(self, finance_book=None):
|
||||||
|
if not self.calculate_depreciation:
|
||||||
|
return self.value_after_depreciation
|
||||||
|
|
||||||
|
if not finance_book:
|
||||||
|
return self.get("finance_books")[0].value_after_depreciation
|
||||||
|
|
||||||
|
for row in self.get("finance_books"):
|
||||||
|
if finance_book == row.finance_book:
|
||||||
|
return row.value_after_depreciation
|
||||||
|
|
||||||
def get_default_finance_book_idx(self):
|
def get_default_finance_book_idx(self):
|
||||||
if not self.get("default_finance_book") and self.company:
|
if not self.get("default_finance_book") and self.company:
|
||||||
self.default_finance_book = erpnext.get_default_finance_book(self.company)
|
self.default_finance_book = erpnext.get_default_finance_book(self.company)
|
||||||
@@ -813,6 +827,24 @@ class Asset(AccountsController):
|
|||||||
make_gl_entries(gl_entries)
|
make_gl_entries(gl_entries)
|
||||||
self.db_set("booked_fixed_asset", 1)
|
self.db_set("booked_fixed_asset", 1)
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def get_manual_depreciation_entries(self):
|
||||||
|
(_, _, depreciation_expense_account) = get_depreciation_accounts(self)
|
||||||
|
|
||||||
|
gle = frappe.qb.DocType("GL Entry")
|
||||||
|
|
||||||
|
records = (
|
||||||
|
frappe.qb.from_(gle)
|
||||||
|
.select(gle.voucher_no.as_("name"), gle.debit.as_("value"), gle.posting_date)
|
||||||
|
.where(gle.against_voucher == self.name)
|
||||||
|
.where(gle.account == depreciation_expense_account)
|
||||||
|
.where(gle.debit != 0)
|
||||||
|
.where(gle.is_cancelled == 0)
|
||||||
|
.orderby(gle.posting_date)
|
||||||
|
).run(as_dict=True)
|
||||||
|
|
||||||
|
return records
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_depreciation_rate(self, args, on_validate=False):
|
def get_depreciation_rate(self, args, on_validate=False):
|
||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
@@ -857,7 +889,6 @@ def update_maintenance_status():
|
|||||||
|
|
||||||
|
|
||||||
def make_post_gl_entry():
|
def make_post_gl_entry():
|
||||||
|
|
||||||
asset_categories = frappe.db.get_all("Asset Category", fields=["name", "enable_cwip_accounting"])
|
asset_categories = frappe.db.get_all("Asset Category", fields=["name", "enable_cwip_accounting"])
|
||||||
|
|
||||||
for asset_category in asset_categories:
|
for asset_category in asset_categories:
|
||||||
@@ -1010,7 +1041,7 @@ def make_journal_entry(asset_name):
|
|||||||
depreciation_expense_account,
|
depreciation_expense_account,
|
||||||
) = get_depreciation_accounts(asset)
|
) = get_depreciation_accounts(asset)
|
||||||
|
|
||||||
depreciation_cost_center, depreciation_series = frappe.db.get_value(
|
depreciation_cost_center, depreciation_series = frappe.get_cached_value(
|
||||||
"Company", asset.company, ["depreciation_cost_center", "series_for_depreciation_entry"]
|
"Company", asset.company, ["depreciation_cost_center", "series_for_depreciation_entry"]
|
||||||
)
|
)
|
||||||
depreciation_cost_center = asset.cost_center or depreciation_cost_center
|
depreciation_cost_center = asset.cost_center or depreciation_cost_center
|
||||||
@@ -1075,6 +1106,13 @@ def is_cwip_accounting_enabled(asset_category):
|
|||||||
return cint(frappe.db.get_value("Asset Category", asset_category, "enable_cwip_accounting"))
|
return cint(frappe.db.get_value("Asset Category", asset_category, "enable_cwip_accounting"))
|
||||||
|
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def get_asset_value_after_depreciation(asset_name, finance_book=None):
|
||||||
|
asset = frappe.get_doc("Asset", asset_name)
|
||||||
|
|
||||||
|
return asset.get_value_after_depreciation(finance_book)
|
||||||
|
|
||||||
|
|
||||||
def get_total_days(date, frequency):
|
def get_total_days(date, frequency):
|
||||||
period_start_date = add_months(date, cint(frequency) * -1)
|
period_start_date = add_months(date, cint(frequency) * -1)
|
||||||
|
|
||||||
|
|||||||
@@ -494,18 +494,8 @@ def get_asset_details(asset, finance_book=None):
|
|||||||
disposal_account, depreciation_cost_center = get_disposal_account_and_cost_center(asset.company)
|
disposal_account, depreciation_cost_center = get_disposal_account_and_cost_center(asset.company)
|
||||||
depreciation_cost_center = asset.cost_center or depreciation_cost_center
|
depreciation_cost_center = asset.cost_center or depreciation_cost_center
|
||||||
|
|
||||||
idx = 1
|
value_after_depreciation = asset.get_value_after_depreciation(finance_book)
|
||||||
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
|
|
||||||
if asset.calculate_depreciation
|
|
||||||
else asset.value_after_depreciation
|
|
||||||
)
|
|
||||||
accumulated_depr_amount = flt(asset.gross_purchase_amount) - flt(value_after_depreciation)
|
accumulated_depr_amount = flt(asset.gross_purchase_amount) - flt(value_after_depreciation)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from frappe.utils import (
|
|||||||
nowdate,
|
nowdate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry
|
||||||
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
|
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
|
||||||
from erpnext.assets.doctype.asset.asset import (
|
from erpnext.assets.doctype.asset.asset import (
|
||||||
make_sales_invoice,
|
make_sales_invoice,
|
||||||
@@ -1442,6 +1443,36 @@ class TestDepreciationBasics(AssetSetup):
|
|||||||
for i, schedule in enumerate(asset.schedules):
|
for i, schedule in enumerate(asset.schedules):
|
||||||
self.assertEqual(getdate(expected_dates[i]), getdate(schedule.schedule_date))
|
self.assertEqual(getdate(expected_dates[i]), getdate(schedule.schedule_date))
|
||||||
|
|
||||||
|
def test_manual_depreciation_for_existing_asset(self):
|
||||||
|
asset = create_asset(
|
||||||
|
item_code="Macbook Pro",
|
||||||
|
is_existing_asset=1,
|
||||||
|
purchase_date="2020-01-30",
|
||||||
|
available_for_use_date="2020-01-30",
|
||||||
|
submit=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(asset.status, "Submitted")
|
||||||
|
self.assertEqual(asset.get("value_after_depreciation"), 100000)
|
||||||
|
|
||||||
|
jv = make_journal_entry(
|
||||||
|
"_Test Depreciations - _TC", "_Test Accumulated Depreciations - _TC", 100, save=False
|
||||||
|
)
|
||||||
|
for d in jv.accounts:
|
||||||
|
d.reference_type = "Asset"
|
||||||
|
d.reference_name = asset.name
|
||||||
|
jv.voucher_type = "Depreciation Entry"
|
||||||
|
jv.insert()
|
||||||
|
jv.submit()
|
||||||
|
|
||||||
|
asset.reload()
|
||||||
|
self.assertEqual(asset.get("value_after_depreciation"), 99900)
|
||||||
|
|
||||||
|
jv.cancel()
|
||||||
|
|
||||||
|
asset.reload()
|
||||||
|
self.assertEqual(asset.get("value_after_depreciation"), 100000)
|
||||||
|
|
||||||
|
|
||||||
def create_asset_data():
|
def create_asset_data():
|
||||||
if not frappe.db.exists("Asset Category", "Computers"):
|
if not frappe.db.exists("Asset Category", "Computers"):
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from frappe.utils import cint, flt
|
|||||||
from six import string_types
|
from six import string_types
|
||||||
|
|
||||||
import erpnext
|
import erpnext
|
||||||
|
from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation
|
||||||
from erpnext.assets.doctype.asset.depreciation import (
|
from erpnext.assets.doctype.asset.depreciation import (
|
||||||
depreciate_asset,
|
depreciate_asset,
|
||||||
get_gl_entries_on_asset_disposal,
|
get_gl_entries_on_asset_disposal,
|
||||||
@@ -19,9 +20,6 @@ from erpnext.assets.doctype.asset.depreciation import (
|
|||||||
reverse_depreciation_entry_made_after_disposal,
|
reverse_depreciation_entry_made_after_disposal,
|
||||||
)
|
)
|
||||||
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
|
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
|
||||||
from erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment import (
|
|
||||||
get_current_asset_value,
|
|
||||||
)
|
|
||||||
from erpnext.controllers.stock_controller import StockController
|
from erpnext.controllers.stock_controller import StockController
|
||||||
from erpnext.setup.doctype.brand.brand import get_brand_defaults
|
from erpnext.setup.doctype.brand.brand import get_brand_defaults
|
||||||
from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
|
from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
|
||||||
@@ -259,7 +257,9 @@ class AssetCapitalization(StockController):
|
|||||||
for d in self.get("asset_items"):
|
for d in self.get("asset_items"):
|
||||||
if d.asset:
|
if d.asset:
|
||||||
finance_book = d.get("finance_book") or self.get("finance_book")
|
finance_book = d.get("finance_book") or self.get("finance_book")
|
||||||
d.current_asset_value = flt(get_current_asset_value(d.asset, finance_book=finance_book))
|
d.current_asset_value = flt(
|
||||||
|
get_asset_value_after_depreciation(d.asset, finance_book=finance_book)
|
||||||
|
)
|
||||||
d.asset_value = get_value_after_depreciation_on_disposal_date(
|
d.asset_value = get_value_after_depreciation_on_disposal_date(
|
||||||
d.asset, self.posting_date, finance_book=finance_book
|
d.asset, self.posting_date, finance_book=finance_book
|
||||||
)
|
)
|
||||||
@@ -696,7 +696,7 @@ def get_consumed_asset_details(args):
|
|||||||
|
|
||||||
if args.asset:
|
if args.asset:
|
||||||
out.current_asset_value = flt(
|
out.current_asset_value = flt(
|
||||||
get_current_asset_value(args.asset, finance_book=args.finance_book)
|
get_asset_value_after_depreciation(args.asset, finance_book=args.finance_book)
|
||||||
)
|
)
|
||||||
out.asset_value = get_value_after_depreciation_on_disposal_date(
|
out.asset_value = get_value_after_depreciation_on_disposal_date(
|
||||||
args.asset, args.posting_date, finance_book=args.finance_book
|
args.asset, args.posting_date, finance_book=args.finance_book
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import unittest
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.utils import flt, nowdate
|
from frappe.utils import flt, nowdate
|
||||||
|
|
||||||
from erpnext.assets.doctype.asset.asset import get_asset_account
|
from erpnext.assets.doctype.asset.asset import (
|
||||||
|
get_asset_account,
|
||||||
|
get_asset_value_after_depreciation,
|
||||||
|
)
|
||||||
from erpnext.assets.doctype.asset.test_asset import (
|
from erpnext.assets.doctype.asset.test_asset import (
|
||||||
create_asset,
|
create_asset,
|
||||||
create_asset_data,
|
create_asset_data,
|
||||||
@@ -106,20 +109,20 @@ class TestAssetRepair(unittest.TestCase):
|
|||||||
|
|
||||||
def test_increase_in_asset_value_due_to_stock_consumption(self):
|
def test_increase_in_asset_value_due_to_stock_consumption(self):
|
||||||
asset = create_asset(calculate_depreciation=1, submit=1)
|
asset = create_asset(calculate_depreciation=1, submit=1)
|
||||||
initial_asset_value = get_asset_value(asset)
|
initial_asset_value = get_asset_value_after_depreciation(asset.name)
|
||||||
asset_repair = create_asset_repair(asset=asset, stock_consumption=1, submit=1)
|
asset_repair = create_asset_repair(asset=asset, stock_consumption=1, submit=1)
|
||||||
asset.reload()
|
asset.reload()
|
||||||
|
|
||||||
increase_in_asset_value = get_asset_value(asset) - initial_asset_value
|
increase_in_asset_value = get_asset_value_after_depreciation(asset.name) - initial_asset_value
|
||||||
self.assertEqual(asset_repair.stock_items[0].total_value, increase_in_asset_value)
|
self.assertEqual(asset_repair.stock_items[0].total_value, increase_in_asset_value)
|
||||||
|
|
||||||
def test_increase_in_asset_value_due_to_repair_cost_capitalisation(self):
|
def test_increase_in_asset_value_due_to_repair_cost_capitalisation(self):
|
||||||
asset = create_asset(calculate_depreciation=1, submit=1)
|
asset = create_asset(calculate_depreciation=1, submit=1)
|
||||||
initial_asset_value = get_asset_value(asset)
|
initial_asset_value = get_asset_value_after_depreciation(asset.name)
|
||||||
asset_repair = create_asset_repair(asset=asset, capitalize_repair_cost=1, submit=1)
|
asset_repair = create_asset_repair(asset=asset, capitalize_repair_cost=1, submit=1)
|
||||||
asset.reload()
|
asset.reload()
|
||||||
|
|
||||||
increase_in_asset_value = get_asset_value(asset) - initial_asset_value
|
increase_in_asset_value = get_asset_value_after_depreciation(asset.name) - initial_asset_value
|
||||||
self.assertEqual(asset_repair.repair_cost, increase_in_asset_value)
|
self.assertEqual(asset_repair.repair_cost, increase_in_asset_value)
|
||||||
|
|
||||||
def test_purchase_invoice(self):
|
def test_purchase_invoice(self):
|
||||||
@@ -244,10 +247,6 @@ class TestAssetRepair(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_asset_value(asset):
|
|
||||||
return asset.finance_books[0].value_after_depreciation
|
|
||||||
|
|
||||||
|
|
||||||
def num_of_depreciations(asset):
|
def num_of_depreciations(asset):
|
||||||
return asset.finance_books[0].total_number_of_depreciations
|
return asset.finance_books[0].total_number_of_depreciations
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ frappe.ui.form.on('Asset Value Adjustment', {
|
|||||||
set_current_asset_value: function(frm) {
|
set_current_asset_value: function(frm) {
|
||||||
if (frm.doc.asset) {
|
if (frm.doc.asset) {
|
||||||
frm.call({
|
frm.call({
|
||||||
method: "erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment.get_current_asset_value",
|
method: "erpnext.assets.doctype.asset.asset.get_asset_value_after_depreciation",
|
||||||
args: {
|
args: {
|
||||||
asset: frm.doc.asset,
|
asset: frm.doc.asset,
|
||||||
finance_book: frm.doc.finance_book
|
finance_book: frm.doc.finance_book
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ from frappe.utils import cint, date_diff, flt, formatdate, getdate
|
|||||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
||||||
get_checks_for_pl_and_bs_accounts,
|
get_checks_for_pl_and_bs_accounts,
|
||||||
)
|
)
|
||||||
from erpnext.assets.doctype.asset.asset import get_depreciation_amount
|
from erpnext.assets.doctype.asset.asset import (
|
||||||
|
get_asset_value_after_depreciation,
|
||||||
|
get_depreciation_amount,
|
||||||
|
)
|
||||||
from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts
|
from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts
|
||||||
|
|
||||||
|
|
||||||
@@ -42,7 +45,7 @@ class AssetValueAdjustment(Document):
|
|||||||
|
|
||||||
def set_current_asset_value(self):
|
def set_current_asset_value(self):
|
||||||
if not self.current_asset_value and self.asset:
|
if not self.current_asset_value and self.asset:
|
||||||
self.current_asset_value = get_current_asset_value(self.asset, self.finance_book)
|
self.current_asset_value = get_asset_value_after_depreciation(self.asset, self.finance_book)
|
||||||
|
|
||||||
def make_depreciation_entry(self):
|
def make_depreciation_entry(self):
|
||||||
asset = frappe.get_doc("Asset", self.asset)
|
asset = frappe.get_doc("Asset", self.asset)
|
||||||
@@ -142,12 +145,3 @@ class AssetValueAdjustment(Document):
|
|||||||
for asset_data in asset.schedules:
|
for asset_data in asset.schedules:
|
||||||
if not asset_data.journal_entry:
|
if not asset_data.journal_entry:
|
||||||
asset_data.db_update()
|
asset_data.db_update()
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def get_current_asset_value(asset, finance_book=None):
|
|
||||||
cond = {"parent": asset, "parenttype": "Asset"}
|
|
||||||
if finance_book:
|
|
||||||
cond.update({"finance_book": finance_book})
|
|
||||||
|
|
||||||
return frappe.db.get_value("Asset Finance Book", cond, "value_after_depreciation")
|
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ import unittest
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.utils import add_days, get_last_day, nowdate
|
from frappe.utils import add_days, get_last_day, nowdate
|
||||||
|
|
||||||
|
from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation
|
||||||
from erpnext.assets.doctype.asset.test_asset import create_asset_data
|
from erpnext.assets.doctype.asset.test_asset import create_asset_data
|
||||||
from erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment import (
|
|
||||||
get_current_asset_value,
|
|
||||||
)
|
|
||||||
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
|
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +41,7 @@ class TestAssetValueAdjustment(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
asset_doc.submit()
|
asset_doc.submit()
|
||||||
|
|
||||||
current_value = get_current_asset_value(asset_doc.name)
|
current_value = get_asset_value_after_depreciation(asset_doc.name)
|
||||||
self.assertEqual(current_value, 100000.0)
|
self.assertEqual(current_value, 100000.0)
|
||||||
|
|
||||||
def test_asset_depreciation_value_adjustment(self):
|
def test_asset_depreciation_value_adjustment(self):
|
||||||
@@ -73,7 +71,7 @@ class TestAssetValueAdjustment(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
asset_doc.submit()
|
asset_doc.submit()
|
||||||
|
|
||||||
current_value = get_current_asset_value(asset_doc.name)
|
current_value = get_asset_value_after_depreciation(asset_doc.name)
|
||||||
adj_doc = make_asset_value_adjustment(
|
adj_doc = make_asset_value_adjustment(
|
||||||
asset=asset_doc.name, current_asset_value=current_value, new_asset_value=50000.0
|
asset=asset_doc.name, current_asset_value=current_value, new_asset_value=50000.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,13 +4,16 @@
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import cstr, flt, formatdate, getdate
|
from frappe.query_builder.functions import Sum
|
||||||
|
from frappe.utils import cstr, formatdate, getdate
|
||||||
|
|
||||||
from erpnext.accounts.report.financial_statements import (
|
from erpnext.accounts.report.financial_statements import (
|
||||||
get_fiscal_year_data,
|
get_fiscal_year_data,
|
||||||
get_period_list,
|
get_period_list,
|
||||||
validate_fiscal_year,
|
validate_fiscal_year,
|
||||||
)
|
)
|
||||||
|
from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation
|
||||||
|
from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts
|
||||||
|
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
@@ -85,6 +88,7 @@ def get_data(filters):
|
|||||||
"asset_name",
|
"asset_name",
|
||||||
"status",
|
"status",
|
||||||
"department",
|
"department",
|
||||||
|
"company",
|
||||||
"cost_center",
|
"cost_center",
|
||||||
"calculate_depreciation",
|
"calculate_depreciation",
|
||||||
"purchase_receipt",
|
"purchase_receipt",
|
||||||
@@ -98,8 +102,25 @@ def get_data(filters):
|
|||||||
]
|
]
|
||||||
assets_record = frappe.db.get_all("Asset", filters=conditions, fields=fields)
|
assets_record = frappe.db.get_all("Asset", filters=conditions, fields=fields)
|
||||||
|
|
||||||
|
finance_book_filter = ("is", "not set")
|
||||||
|
if filters.finance_book:
|
||||||
|
finance_book_filter = ("=", filters.finance_book)
|
||||||
|
|
||||||
|
assets_linked_to_fb = frappe.db.get_all(
|
||||||
|
doctype="Asset Finance Book",
|
||||||
|
filters={"finance_book": finance_book_filter},
|
||||||
|
pluck="parent",
|
||||||
|
)
|
||||||
|
|
||||||
for asset in assets_record:
|
for asset in assets_record:
|
||||||
asset_value = get_asset_value(asset, filters.finance_book)
|
if filters.finance_book:
|
||||||
|
if asset.asset_id not in assets_linked_to_fb:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if asset.calculate_depreciation and asset.asset_id not in assets_linked_to_fb:
|
||||||
|
continue
|
||||||
|
|
||||||
|
asset_value = get_asset_value_after_depreciation(asset.asset_id, filters.finance_book)
|
||||||
row = {
|
row = {
|
||||||
"asset_id": asset.asset_id,
|
"asset_id": asset.asset_id,
|
||||||
"asset_name": asset.asset_name,
|
"asset_name": asset.asset_name,
|
||||||
@@ -110,7 +131,7 @@ def get_data(filters):
|
|||||||
or pi_supplier_map.get(asset.purchase_invoice),
|
or pi_supplier_map.get(asset.purchase_invoice),
|
||||||
"gross_purchase_amount": asset.gross_purchase_amount,
|
"gross_purchase_amount": asset.gross_purchase_amount,
|
||||||
"opening_accumulated_depreciation": asset.opening_accumulated_depreciation,
|
"opening_accumulated_depreciation": asset.opening_accumulated_depreciation,
|
||||||
"depreciated_amount": depreciation_amount_map.get(asset.asset_id) or 0.0,
|
"depreciated_amount": get_depreciation_amount_of_asset(asset, depreciation_amount_map, filters),
|
||||||
"available_for_use_date": asset.available_for_use_date,
|
"available_for_use_date": asset.available_for_use_date,
|
||||||
"location": asset.location,
|
"location": asset.location,
|
||||||
"asset_category": asset.asset_category,
|
"asset_category": asset.asset_category,
|
||||||
@@ -122,21 +143,6 @@ def get_data(filters):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_asset_value(asset, finance_book=None):
|
|
||||||
if not asset.calculate_depreciation:
|
|
||||||
return flt(asset.gross_purchase_amount) - flt(asset.opening_accumulated_depreciation)
|
|
||||||
|
|
||||||
finance_book_filter = ["finance_book", "is", "not set"]
|
|
||||||
if finance_book:
|
|
||||||
finance_book_filter = ["finance_book", "=", finance_book]
|
|
||||||
|
|
||||||
return frappe.db.get_value(
|
|
||||||
doctype="Asset Finance Book",
|
|
||||||
filters=[["parent", "=", asset.asset_id], finance_book_filter],
|
|
||||||
fieldname="value_after_depreciation",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def prepare_chart_data(data, filters):
|
def prepare_chart_data(data, filters):
|
||||||
labels_values_map = {}
|
labels_values_map = {}
|
||||||
date_field = frappe.scrub(filters.date_based_on)
|
date_field = frappe.scrub(filters.date_based_on)
|
||||||
@@ -182,6 +188,15 @@ def prepare_chart_data(data, filters):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_depreciation_amount_of_asset(asset, depreciation_amount_map, filters):
|
||||||
|
if asset.calculate_depreciation:
|
||||||
|
depr_amount = depreciation_amount_map.get(asset.asset_id) or 0.0
|
||||||
|
else:
|
||||||
|
depr_amount = get_manual_depreciation_amount_of_asset(asset, filters)
|
||||||
|
|
||||||
|
return depr_amount
|
||||||
|
|
||||||
|
|
||||||
def get_finance_book_value_map(filters):
|
def get_finance_book_value_map(filters):
|
||||||
date = filters.to_date if filters.filter_based_on == "Date Range" else filters.year_end_date
|
date = filters.to_date if filters.filter_based_on == "Date Range" else filters.year_end_date
|
||||||
|
|
||||||
@@ -201,6 +216,31 @@ def get_finance_book_value_map(filters):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_manual_depreciation_amount_of_asset(asset, filters):
|
||||||
|
date = filters.to_date if filters.filter_based_on == "Date Range" else filters.year_end_date
|
||||||
|
|
||||||
|
(_, _, depreciation_expense_account) = get_depreciation_accounts(asset)
|
||||||
|
|
||||||
|
gle = frappe.qb.DocType("GL Entry")
|
||||||
|
|
||||||
|
result = (
|
||||||
|
frappe.qb.from_(gle)
|
||||||
|
.select(Sum(gle.debit))
|
||||||
|
.where(gle.against_voucher == asset.asset_id)
|
||||||
|
.where(gle.account == depreciation_expense_account)
|
||||||
|
.where(gle.debit != 0)
|
||||||
|
.where(gle.is_cancelled == 0)
|
||||||
|
.where(gle.posting_date <= date)
|
||||||
|
).run()
|
||||||
|
|
||||||
|
if result and result[0] and result[0][0]:
|
||||||
|
depr_amount = result[0][0]
|
||||||
|
else:
|
||||||
|
depr_amount = 0
|
||||||
|
|
||||||
|
return depr_amount
|
||||||
|
|
||||||
|
|
||||||
def get_purchase_receipt_supplier_map():
|
def get_purchase_receipt_supplier_map():
|
||||||
return frappe._dict(
|
return frappe._dict(
|
||||||
frappe.db.sql(
|
frappe.db.sql(
|
||||||
|
|||||||
@@ -325,3 +325,4 @@ erpnext.patches.v14_0.setup_clear_repost_logs
|
|||||||
erpnext.patches.v14_0.create_accounting_dimensions_for_payment_request
|
erpnext.patches.v14_0.create_accounting_dimensions_for_payment_request
|
||||||
erpnext.patches.v14_0.update_entry_type_for_journal_entry
|
erpnext.patches.v14_0.update_entry_type_for_journal_entry
|
||||||
erpnext.patches.v14_0.change_autoname_for_tax_withheld_vouchers
|
erpnext.patches.v14_0.change_autoname_for_tax_withheld_vouchers
|
||||||
|
erpnext.patches.v14_0.update_asset_value_for_manual_depr_entries
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import frappe
|
||||||
|
from frappe.query_builder.functions import IfNull, Sum
|
||||||
|
|
||||||
|
|
||||||
|
def execute():
|
||||||
|
asset = frappe.qb.DocType("Asset")
|
||||||
|
gle = frappe.qb.DocType("GL Entry")
|
||||||
|
aca = frappe.qb.DocType("Asset Category Account")
|
||||||
|
company = frappe.qb.DocType("Company")
|
||||||
|
|
||||||
|
asset_total_depr_value_map = (
|
||||||
|
frappe.qb.from_(gle)
|
||||||
|
.join(asset)
|
||||||
|
.on(gle.against_voucher == asset.name)
|
||||||
|
.join(aca)
|
||||||
|
.on((aca.parent == asset.asset_category) & (aca.company_name == asset.company))
|
||||||
|
.join(company)
|
||||||
|
.on(company.name == asset.company)
|
||||||
|
.select(Sum(gle.debit).as_("value"), asset.name.as_("asset_name"))
|
||||||
|
.where(
|
||||||
|
gle.account == IfNull(aca.depreciation_expense_account, company.depreciation_expense_account)
|
||||||
|
)
|
||||||
|
.where(gle.debit != 0)
|
||||||
|
.where(gle.is_cancelled == 0)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.calculate_depreciation == 0)
|
||||||
|
.groupby(asset.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
frappe.qb.update(asset).join(asset_total_depr_value_map).on(
|
||||||
|
asset_total_depr_value_map.asset_name == asset.name
|
||||||
|
).set(
|
||||||
|
asset.value_after_depreciation, asset.value_after_depreciation - asset_total_depr_value_map.value
|
||||||
|
).where(
|
||||||
|
asset.docstatus == 1
|
||||||
|
).where(
|
||||||
|
asset.calculate_depreciation == 0
|
||||||
|
).run()
|
||||||
Reference in New Issue
Block a user