Skip to content

Commit

Permalink
Merge pull request frappe#32933 from AnandBaburajan/asset_depreciatio…
Browse files Browse the repository at this point in the history
…n_schedule

feat: separating depreciation schedule from assets into a new doc
  • Loading branch information
deepeshgarg007 authored Jan 9, 2023
2 parents fa4af2a + 3ae47ab commit 57a6c37
Show file tree
Hide file tree
Showing 24 changed files with 1,484 additions and 890 deletions.
22 changes: 13 additions & 9 deletions erpnext/accounts/doctype/journal_entry/journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import frappe
from frappe import _, msgprint, scrub
from frappe.utils import cint, cstr, flt, fmt_money, formatdate, get_link_to_form, nowdate
from frappe.utils import cstr, flt, fmt_money, formatdate, get_link_to_form, nowdate

import erpnext
from erpnext.accounts.deferred_revenue import get_deferred_booking_accounts
Expand All @@ -23,6 +23,9 @@
get_stock_accounts,
get_stock_and_account_balance,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_depr_schedule,
)
from erpnext.controllers.accounts_controller import AccountsController


Expand Down Expand Up @@ -283,16 +286,17 @@ def unlink_asset_reference(self):
for d in self.get("accounts"):
if d.reference_type == "Asset" and 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)
for row in asset.get("finance_books"):
depr_schedule = get_depr_schedule(asset.name, "Active", row.finance_book)

for s in depr_schedule or []:
if s.journal_entry == self.name:
s.db_set("journal_entry", None)

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()
row.value_after_depreciation += s.depreciation_amount
row.db_update()

asset.set_status()
asset.set_status()

def unlink_inter_company_jv(self):
if (
Expand Down
17 changes: 15 additions & 2 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,24 @@ def make_item_gl_entries(self, gl_entries):
if asset.calculate_depreciation:
posting_date = frappe.db.get_value("Sales Invoice", self.return_against, "posting_date")
reverse_depreciation_entry_made_after_disposal(asset, posting_date)
reset_depreciation_schedule(asset, self.posting_date)
notes = _(
"This schedule was created when Asset {0} was returned after being sold through Sales Invoice {1}."
).format(
get_link_to_form(asset.doctype, asset.name),
get_link_to_form(self.doctype, self.get("name")),
)
reset_depreciation_schedule(asset, self.posting_date, notes)
asset.reload()

else:
if asset.calculate_depreciation:
depreciate_asset(asset, self.posting_date)
notes = _(
"This schedule was created when Asset {0} was sold through Sales Invoice {1}."
).format(
get_link_to_form(asset.doctype, asset.name),
get_link_to_form(self.doctype, self.get("name")),
)
depreciate_asset(asset, self.posting_date, notes)
asset.reload()

fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(
Expand Down
9 changes: 6 additions & 3 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from erpnext.accounts.utils import PaymentEntryUnlinkError
from erpnext.assets.doctype.asset.depreciation import post_depreciation_entries
from erpnext.assets.doctype.asset.test_asset import create_asset, create_asset_data
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_depr_schedule,
)
from erpnext.controllers.accounts_controller import update_invoice_status
from erpnext.controllers.taxes_and_totals import get_itemised_tax_breakup_data
from erpnext.exceptions import InvalidAccountCurrency, InvalidCurrency
Expand Down Expand Up @@ -2774,7 +2777,7 @@ def test_asset_depreciation_on_sale_with_pro_rata(self):
["2021-09-30", 5041.1, 26407.22],
]

for i, schedule in enumerate(asset.schedules):
for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
self.assertEqual(expected_values[i][2], schedule.accumulated_depreciation_amount)
Expand Down Expand Up @@ -2805,7 +2808,7 @@ def test_asset_depreciation_on_sale_without_pro_rata(self):

expected_values = [["2020-12-31", 30000, 30000], ["2021-12-31", 30000, 60000]]

for i, schedule in enumerate(asset.schedules):
for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
self.assertEqual(expected_values[i][2], schedule.accumulated_depreciation_amount)
Expand Down Expand Up @@ -2834,7 +2837,7 @@ def test_depreciation_on_return_of_sold_asset(self):
["2025-06-06", 18633.88, 100000.0, False],
]

for i, schedule in enumerate(asset.schedules):
for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
self.assertEqual(expected_values[i][2], schedule.accumulated_depreciation_amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def get_assets(filters):
else
0
end), 0) as depreciation_amount_during_the_period
from `tabAsset` a, `tabDepreciation Schedule` ds
where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s and a.name = ds.parent and ifnull(ds.journal_entry, '') != ''
from `tabAsset` a, `tabAsset Depreciation Schedule` ads, `tabDepreciation Schedule` ds
where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s and ads.asset = a.name and ads.docstatus=1 and ads.name = ds.parent and ifnull(ds.journal_entry, '') != ''
group by a.asset_category
union
SELECT a.asset_category,
Expand Down
73 changes: 19 additions & 54 deletions erpnext/assets/doctype/asset/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ frappe.ui.form.on('Asset', {
refresh: function(frm) {
frappe.ui.form.trigger("Asset", "is_existing_asset");
frm.toggle_display("next_depreciation_date", frm.doc.docstatus < 1);
frm.events.make_schedules_editable(frm);

if (frm.doc.docstatus==1) {
if (in_list(["Submitted", "Partially Depreciated", "Fully Depreciated"], frm.doc.status)) {
Expand Down Expand Up @@ -188,7 +187,11 @@ frappe.ui.form.on('Asset', {
})
},

setup_chart: function(frm) {
setup_chart: async function(frm) {
if(frm.doc.finance_books.length > 1) {
return
}

var x_intervals = [frm.doc.purchase_date];
var asset_values = [frm.doc.gross_purchase_amount];
var last_depreciation_date = frm.doc.purchase_date;
Expand All @@ -202,7 +205,20 @@ frappe.ui.form.on('Asset', {
flt(frm.doc.opening_accumulated_depreciation));
}

$.each(frm.doc.schedules || [], function(i, v) {
let depr_schedule = [];

if (frm.doc.finance_books.length == 1) {
depr_schedule = (await frappe.call(
"erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_depr_schedule",
{
asset_name: frm.doc.name,
status: frm.doc.docstatus ? "Active" : "Draft",
finance_book: frm.doc.finance_books[0].finance_book || null
}
)).message;
}

$.each(depr_schedule || [], function(i, v) {
x_intervals.push(v.schedule_date);
var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount);
if(v.journal_entry) {
Expand Down Expand Up @@ -266,21 +282,6 @@ frappe.ui.form.on('Asset', {
// frm.toggle_reqd("next_depreciation_date", (!frm.doc.is_existing_asset && frm.doc.calculate_depreciation));
},

opening_accumulated_depreciation: function(frm) {
erpnext.asset.set_accumulated_depreciation(frm);
},

make_schedules_editable: function(frm) {
if (frm.doc.finance_books) {
var is_editable = frm.doc.finance_books.filter(d => d.depreciation_method == "Manual").length > 0
? true : false;

frm.toggle_enable("schedules", is_editable);
frm.fields_dict["schedules"].grid.toggle_enable("schedule_date", is_editable);
frm.fields_dict["schedules"].grid.toggle_enable("depreciation_amount", is_editable);
}
},

make_sales_invoice: function(frm) {
frappe.call({
args: {
Expand Down Expand Up @@ -476,7 +477,6 @@ frappe.ui.form.on('Asset Finance Book', {
depreciation_method: function(frm, cdt, cdn) {
const row = locals[cdt][cdn];
frm.events.set_depreciation_rate(frm, row);
frm.events.make_schedules_editable(frm);
},

expected_value_after_useful_life: function(frm, cdt, cdn) {
Expand Down Expand Up @@ -512,41 +512,6 @@ frappe.ui.form.on('Asset Finance Book', {
}
});

frappe.ui.form.on('Depreciation Schedule', {
make_depreciation_entry: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
if (!row.journal_entry) {
frappe.call({
method: "erpnext.assets.doctype.asset.depreciation.make_depreciation_entry",
args: {
"asset_name": frm.doc.name,
"date": row.schedule_date
},
callback: function(r) {
frappe.model.sync(r.message);
frm.refresh();
}
})
}
},

depreciation_amount: function(frm, cdt, cdn) {
erpnext.asset.set_accumulated_depreciation(frm);
}

})

erpnext.asset.set_accumulated_depreciation = function(frm) {
if(frm.doc.depreciation_method != "Manual") return;

var accumulated_depreciation = flt(frm.doc.opening_accumulated_depreciation);
$.each(frm.doc.schedules || [], function(i, row) {
accumulated_depreciation += flt(row.depreciation_amount);
frappe.model.set_value(row.doctype, row.name,
"accumulated_depreciation_amount", accumulated_depreciation);
})
};

erpnext.asset.scrap_asset = function(frm) {
frappe.confirm(__("Do you really want to scrap this asset?"), function () {
frappe.call({
Expand Down
22 changes: 6 additions & 16 deletions erpnext/assets/doctype/asset/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
"column_break_24",
"frequency_of_depreciation",
"next_depreciation_date",
"section_break_14",
"schedules",
"insurance_details",
"policy_number",
"insurer",
Expand Down Expand Up @@ -307,19 +305,6 @@
"label": "Next Depreciation Date",
"no_copy": 1
},
{
"depends_on": "calculate_depreciation",
"fieldname": "section_break_14",
"fieldtype": "Section Break",
"label": "Depreciation Schedule"
},
{
"fieldname": "schedules",
"fieldtype": "Table",
"label": "Depreciation Schedule",
"no_copy": 1,
"options": "Depreciation Schedule"
},
{
"collapsible": 1,
"fieldname": "insurance_details",
Expand Down Expand Up @@ -508,9 +493,14 @@
"group": "Value",
"link_doctype": "Asset Value Adjustment",
"link_fieldname": "asset"
},
{
"group": "Depreciation",
"link_doctype": "Asset Depreciation Schedule",
"link_fieldname": "asset"
}
],
"modified": "2022-07-20 10:15:12.887372",
"modified": "2022-11-25 12:47:19.689702",
"modified_by": "Administrator",
"module": "Assets",
"name": "Asset",
Expand Down
Loading

0 comments on commit 57a6c37

Please sign in to comment.