chore: refactor chart and manual set_accumulated_depreciation logic

This commit is contained in:
anandbaburajan 2022-12-04 22:45:48 +05:30
parent ca8c827492
commit 83ed93fbb6
4 changed files with 42 additions and 19 deletions

View File

@ -187,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 x_intervals = [frm.doc.purchase_date];
var asset_values = [frm.doc.gross_purchase_amount]; var asset_values = [frm.doc.gross_purchase_amount];
var last_depreciation_date = frm.doc.purchase_date; var last_depreciation_date = frm.doc.purchase_date;
@ -201,7 +205,19 @@ frappe.ui.form.on('Asset', {
flt(frm.doc.opening_accumulated_depreciation)); 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_from_asset_depr_schedule_of_asset",
{
asset_name: frm.doc.name,
finance_book: frm.doc.finance_books[0].finance_book || null
}
)).message;
}
$.each(depr_schedule || [], 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);
if(v.journal_entry) { if(v.journal_entry) {
@ -265,10 +281,6 @@ frappe.ui.form.on('Asset', {
// frm.toggle_reqd("next_depreciation_date", (!frm.doc.is_existing_asset && frm.doc.calculate_depreciation)); // 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) { make_schedules_editable: function(frm) {
if (frm.doc.finance_books) { if (frm.doc.finance_books) {
var is_editable = frm.doc.finance_books.filter(d => d.depreciation_method == "Manual").length > 0 var is_editable = frm.doc.finance_books.filter(d => d.depreciation_method == "Manual").length > 0
@ -511,17 +523,6 @@ frappe.ui.form.on('Asset Finance Book', {
} }
}); });
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) { erpnext.asset.scrap_asset = function(frm) {
frappe.confirm(__("Do you really want to scrap this asset?"), function () { frappe.confirm(__("Do you really want to scrap this asset?"), function () {
frappe.call({ frappe.call({

View File

@ -1,5 +1,6 @@
// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors // Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt // For license information, please see license.txt
frappe.provide("erpnext.asset");
frappe.ui.form.on('Depreciation Schedule', { frappe.ui.form.on('Depreciation Schedule', {
make_depreciation_entry: function(frm, cdt, cdn) { make_depreciation_entry: function(frm, cdt, cdn) {
@ -22,4 +23,15 @@ frappe.ui.form.on('Depreciation Schedule', {
depreciation_amount: function(frm, cdt, cdn) { depreciation_amount: function(frm, cdt, cdn) {
erpnext.asset.set_accumulated_depreciation(frm); 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);
})
};

View File

@ -10,6 +10,7 @@
"asset", "asset",
"naming_series", "naming_series",
"column_break_2", "column_break_2",
"opening_accumulated_depreciation",
"finance_book", "finance_book",
"finance_book_id", "finance_book_id",
"depreciation_details_section", "depreciation_details_section",
@ -151,6 +152,14 @@
"label": "Finance Book Id", "label": "Finance Book Id",
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
},
{
"depends_on": "opening_accumulated_depreciation",
"fieldname": "opening_accumulated_depreciation",
"fieldtype": "Currency",
"label": "Opening Accumulated Depreciation",
"read_only": 1,
"options": "Company:company:default_currency"
} }
], ],
"in_create": 1, "in_create": 1,

View File

@ -150,6 +150,7 @@ def get_asset_depr_schedule_name(asset_name, finance_book):
) )
@frappe.whitelist()
def get_depr_schedule_from_asset_depr_schedule_of_asset(asset_name, finance_book): def get_depr_schedule_from_asset_depr_schedule_of_asset(asset_name, finance_book):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book) asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book)