[Refactored] Asset Depreciation Ledger report based on GL entries (#15415)

* [Refactored] Asset Depreciation Ledger report is based on GL entries

* Provision to make manual JV from the asset if Calculate Depreciation is disabled
This commit is contained in:
rohitwaghchaure 2018-09-26 15:24:49 +05:30 committed by Nabin Hait
parent 0ff35a852a
commit 0cf0ebf08b
4 changed files with 116 additions and 35 deletions

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe.utils import flt
from frappe import _ from frappe import _
def execute(filters=None): def execute(filters=None):
@ -10,39 +11,66 @@ def execute(filters=None):
return columns, data return columns, data
def get_data(filters): def get_data(filters):
data = frappe.db.sql(""" data = []
select depreciation_accounts = frappe.db.sql_list(""" select name from tabAccount
a.name as asset, a.asset_category, a.status, where ifnull(account_type, '') = 'Depreciation' """)
a.depreciation_method, a.purchase_date, a.gross_purchase_amount,
ds.schedule_date as depreciation_date, ds.depreciation_amount, filters_data = [["company", "=", filters.get('company')],
ds.accumulated_depreciation_amount, ["posting_date", ">=", filters.get('from_date')],
(a.gross_purchase_amount - ds.accumulated_depreciation_amount) as amount_after_depreciation, ["posting_date", "<=", filters.get('to_date')],
ds.journal_entry as depreciation_entry ["against_voucher_type", "=", "Asset"],
from ["account", "in", depreciation_accounts]]
`tabAsset` a, `tabDepreciation Schedule` ds
where if filters.get("asset"):
a.name = ds.parent filters_data.append(["against_voucher", "=", filters.get("asset")])
and a.docstatus=1
and ifnull(ds.journal_entry, '') != '' if filters.get("asset_category"):
and ds.schedule_date between %(from_date)s and %(to_date)s assets = frappe.db.sql_list("""select name from tabAsset
and a.company = %(company)s where asset_category = %s and docstatus=1""", filters.get("asset_category"))
{conditions}
order by filters_data.append(["against_voucher", "in", assets])
a.name asc, ds.schedule_date asc
""".format(conditions=get_filter_conditions(filters)), filters, as_dict=1) gl_entries = frappe.get_all('GL Entry',
filters= filters_data,
fields = ["against_voucher", "debit_in_account_currency as debit", "voucher_no", "posting_date"],
order_by= "against_voucher, posting_date")
if not gl_entries:
return data
assets = [d.against_voucher for d in gl_entries]
assets_details = get_assets_details(assets)
for d in gl_entries:
asset_data = assets_details.get(d.against_voucher)
if not asset_data.get("accumulated_depreciation_amount"):
asset_data.accumulated_depreciation_amount = d.debit
else:
asset_data.accumulated_depreciation_amount += d.debit
row = frappe._dict(asset_data)
row.update({
"depreciation_amount": d.debit,
"depreciation_date": d.posting_date,
"amount_after_depreciation": (flt(row.gross_purchase_amount) -
flt(row.accumulated_depreciation_amount)),
"depreciation_entry": d.voucher_no
})
data.append(row)
return data return data
def get_filter_conditions(filters): def get_assets_details(assets):
conditions = "" assets_details = {}
if filters.get("asset"): fields = ["name as asset", "gross_purchase_amount",
conditions += " and a.name = %(asset)s" "asset_category", "status", "depreciation_method", "purchase_date"]
if filters.get("asset_category"): for d in frappe.get_all("Asset", fields = fields, filters = {'name': ('in', assets)}):
conditions += " and a.asset_category = %(asset_category)s" assets_details.setdefault(d.asset, d)
return conditions return assets_details
def get_columns(): def get_columns():
return [ return [

View File

@ -67,11 +67,33 @@ frappe.ui.form.on('Asset', {
frm.trigger("create_asset_maintenance"); frm.trigger("create_asset_maintenance");
}, __("Make")); }, __("Make"));
} }
if (!frm.doc.calculate_depreciation) {
frm.add_custom_button(__("Depreciation Entry"), function() {
frm.trigger("make_journal_entry");
}, __("Make"));
}
frm.page.set_inner_btn_group_as_primary(__("Make")); frm.page.set_inner_btn_group_as_primary(__("Make"));
frm.trigger("setup_chart"); frm.trigger("setup_chart");
} }
}, },
make_journal_entry: function(frm) {
frappe.call({
method: "erpnext.assets.doctype.asset.asset.make_journal_entry",
args: {
asset_name: frm.doc.name
},
callback: function(r) {
if (r.message) {
var doclist = frappe.model.sync(r.message);
frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
}
}
})
},
setup_chart: function(frm) { setup_chart: function(frm) {
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];

View File

@ -283,3 +283,34 @@ def get_item_details(item_code):
}) })
return ret return ret
@frappe.whitelist()
def make_journal_entry(asset_name):
asset = frappe.get_doc("Asset", asset_name)
fixed_asset_account, accumulated_depreciation_account, depreciation_expense_account = \
get_depreciation_accounts(asset)
depreciation_cost_center, depreciation_series = frappe.db.get_value("Company", asset.company,
["depreciation_cost_center", "series_for_depreciation_entry"])
depreciation_cost_center = asset.cost_center or depreciation_cost_center
je = frappe.new_doc("Journal Entry")
je.voucher_type = "Depreciation Entry"
je.naming_series = depreciation_series
je.company = asset.company
je.remark = "Depreciation Entry against asset {0}".format(asset_name)
je.append("accounts", {
"account": depreciation_expense_account,
"reference_type": "Asset",
"reference_name": asset.name,
"cost_center": depreciation_cost_center
})
je.append("accounts", {
"account": accumulated_depreciation_account,
"reference_type": "Asset",
"reference_name": asset.name
})
return je

View File

@ -5,11 +5,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe import _ from frappe import _
from frappe.utils import flt, today, getdate from frappe.utils import flt, today, getdate, cint
def post_depreciation_entries(date=None): def post_depreciation_entries(date=None):
# Return if automatic booking of asset depreciation is disabled # Return if automatic booking of asset depreciation is disabled
if not frappe.db.get_value("Accounts Settings", None, "book_asset_depreciation_entry_automatically"): if not cint(frappe.db.get_value("Accounts Settings", None, "book_asset_depreciation_entry_automatically")):
return return
if not date: if not date: