fix: manual depr entry not updating asset value [v13] (#33890)
fix: asset value for manual depr entries
This commit is contained in:
@@ -87,6 +87,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_expense_claim()
|
self.update_expense_claim()
|
||||||
self.update_inter_company_jv()
|
self.update_inter_company_jv()
|
||||||
self.update_invoice_discounting()
|
self.update_invoice_discounting()
|
||||||
@@ -235,6 +236,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"
|
||||||
@@ -293,19 +322,39 @@ 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)
|
||||||
for s in asset.get("schedules"):
|
|
||||||
if s.journal_entry == self.name:
|
|
||||||
s.db_set("journal_entry", None)
|
|
||||||
|
|
||||||
idx = cint(s.finance_book_id) or 1
|
if asset.calculate_depreciation:
|
||||||
finance_books = asset.get("finance_books")[idx - 1]
|
for s in asset.get("schedules"):
|
||||||
finance_books.value_after_depreciation += s.depreciation_amount
|
if s.journal_entry == self.name:
|
||||||
finance_books.db_update()
|
s.db_set("journal_entry", None)
|
||||||
|
|
||||||
asset.set_status()
|
idx = cint(s.finance_book_id) or 1
|
||||||
|
finance_books = asset.get("finance_books")[idx - 1]
|
||||||
|
finance_books.value_after_depreciation += s.depreciation_amount
|
||||||
|
finance_books.db_update()
|
||||||
|
|
||||||
|
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 (
|
||||||
|
|||||||
@@ -201,7 +201,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
|
||||||
}
|
}
|
||||||
@@ -219,20 +219,34 @@ frappe.ui.form.on('Asset', {
|
|||||||
flt(frm.doc.opening_accumulated_depreciation));
|
flt(frm.doc.opening_accumulated_depreciation));
|
||||||
}
|
}
|
||||||
|
|
||||||
$.each(frm.doc.schedules || [], function(i, v) {
|
if(frm.doc.calculate_depreciation) {
|
||||||
x_intervals.push(v.schedule_date);
|
$.each(frm.doc.schedules || [], function(i, v) {
|
||||||
var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount);
|
x_intervals.push(v.schedule_date);
|
||||||
if(v.journal_entry) {
|
var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount);
|
||||||
last_depreciation_date = v.schedule_date;
|
if(v.journal_entry) {
|
||||||
asset_values.push(asset_value);
|
last_depreciation_date = v.schedule_date;
|
||||||
} else {
|
asset_values.push(asset_value);
|
||||||
if (in_list(["Scrapped", "Sold"], frm.doc.status)) {
|
|
||||||
asset_values.push(null);
|
|
||||||
} else {
|
} else {
|
||||||
asset_values.push(asset_value)
|
if (in_list(["Scrapped", "Sold"], frm.doc.status)) {
|
||||||
|
asset_values.push(null);
|
||||||
|
} else {
|
||||||
|
asset_values.push(asset_value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
} 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);
|
||||||
|
|||||||
@@ -504,9 +504,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-17 00:28:37.789345",
|
"modified": "2023-01-31 01:03:09.467817",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Assets",
|
"module": "Assets",
|
||||||
"name": "Asset",
|
"name": "Asset",
|
||||||
|
|||||||
@@ -556,7 +556,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"))
|
||||||
@@ -581,9 +583,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 = [
|
||||||
@@ -638,15 +637,20 @@ class Asset(AccountsController):
|
|||||||
movement.cancel()
|
movement.cancel()
|
||||||
|
|
||||||
def delete_depreciation_entries(self):
|
def delete_depreciation_entries(self):
|
||||||
for d in self.get("schedules"):
|
if self.calculate_depreciation:
|
||||||
if d.journal_entry:
|
for d in self.get("schedules"):
|
||||||
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
|
if d.journal_entry:
|
||||||
d.db_set("journal_entry", None)
|
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
|
||||||
|
else:
|
||||||
|
depr_entries = self.get_manual_depreciation_entries()
|
||||||
|
|
||||||
self.db_set(
|
for depr_entry in depr_entries or []:
|
||||||
"value_after_depreciation",
|
frappe.get_doc("Journal Entry", depr_entry.name).cancel()
|
||||||
(flt(self.gross_purchase_amount) - flt(self.opening_accumulated_depreciation)),
|
|
||||||
)
|
self.db_set(
|
||||||
|
"value_after_depreciation",
|
||||||
|
(flt(self.gross_purchase_amount) - flt(self.opening_accumulated_depreciation)),
|
||||||
|
)
|
||||||
|
|
||||||
def set_status(self, status=None):
|
def set_status(self, status=None):
|
||||||
"""Get and update status"""
|
"""Get and update status"""
|
||||||
@@ -677,6 +681,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)
|
||||||
@@ -686,6 +701,24 @@ class Asset(AccountsController):
|
|||||||
if d.finance_book == self.default_finance_book:
|
if d.finance_book == self.default_finance_book:
|
||||||
return cint(d.idx) - 1
|
return cint(d.idx) - 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
|
||||||
|
|
||||||
def validate_make_gl_entry(self):
|
def validate_make_gl_entry(self):
|
||||||
purchase_document = self.get_purchase_document()
|
purchase_document = self.get_purchase_document()
|
||||||
if not purchase_document:
|
if not purchase_document:
|
||||||
@@ -850,7 +883,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:
|
||||||
@@ -1003,7 +1035,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
|
||||||
@@ -1070,6 +1102,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)
|
||||||
|
|
||||||
|
|||||||
@@ -469,18 +469,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 make_sales_invoice, update_maintenance_status
|
from erpnext.assets.doctype.asset.asset import make_sales_invoice, update_maintenance_status
|
||||||
from erpnext.assets.doctype.asset.depreciation import (
|
from erpnext.assets.doctype.asset.depreciation import (
|
||||||
@@ -1410,6 +1411,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"):
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ 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_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,
|
||||||
@@ -105,20 +106,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):
|
||||||
@@ -143,10 +144,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
|
||||||
from erpnext.regional.india.utils import (
|
from erpnext.regional.india.utils import (
|
||||||
get_depreciation_amount as get_depreciation_amount_for_india,
|
get_depreciation_amount as get_depreciation_amount_for_india,
|
||||||
@@ -45,7 +48,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)
|
||||||
@@ -148,12 +151,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(
|
||||||
|
|||||||
@@ -375,3 +375,4 @@ erpnext.patches.v13_0.show_hr_payroll_deprecation_warning
|
|||||||
erpnext.patches.v13_0.create_accounting_dimensions_for_asset_repair
|
erpnext.patches.v13_0.create_accounting_dimensions_for_asset_repair
|
||||||
execute:frappe.db.set_value("Naming Series", "Naming Series", {"select_doc_for_series": "", "set_options": "", "prefix": "", "current_value": 0, "user_must_always_select": 0})
|
execute:frappe.db.set_value("Naming Series", "Naming Series", {"select_doc_for_series": "", "set_options": "", "prefix": "", "current_value": 0, "user_must_always_select": 0})
|
||||||
erpnext.patches.v13_0.update_schedule_type_in_loans
|
erpnext.patches.v13_0.update_schedule_type_in_loans
|
||||||
|
erpnext.patches.v13_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