From a8fc32dc3256963ef72fbf1681e53b9a357dce23 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Tue, 4 Jun 2024 11:44:39 +0530 Subject: [PATCH] fix: incorrect accumulated depr amount in asset depr ledger (#41654) * fix: incorrect accumulated depr amount in asset depr ledger * refactor: depreciation amount retrieval with query builder * style: apply formatting changes from pre-commit --- .../asset_depreciation_ledger.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/report/asset_depreciation_ledger/asset_depreciation_ledger.py b/erpnext/accounts/report/asset_depreciation_ledger/asset_depreciation_ledger.py index d285f28d8e3..e1545bdcd87 100644 --- a/erpnext/accounts/report/asset_depreciation_ledger/asset_depreciation_ledger.py +++ b/erpnext/accounts/report/asset_depreciation_ledger/asset_depreciation_ledger.py @@ -4,6 +4,7 @@ import frappe from frappe import _ +from frappe.query_builder import DocType from frappe.utils import cstr, flt @@ -75,11 +76,24 @@ def get_data(filters): asset_data = assets_details.get(d.against_voucher) if asset_data: if not asset_data.get("accumulated_depreciation_amount"): - asset_data.accumulated_depreciation_amount = d.debit + asset_data.get( - "opening_accumulated_depreciation" - ) + AssetDepreciationSchedule = DocType("Asset Depreciation Schedule") + DepreciationSchedule = DocType("Depreciation Schedule") + query = ( + frappe.qb.from_(DepreciationSchedule) + .join(AssetDepreciationSchedule) + .on(DepreciationSchedule.parent == AssetDepreciationSchedule.name) + .select(DepreciationSchedule.accumulated_depreciation_amount) + .where( + (AssetDepreciationSchedule.asset == d.against_voucher) + & (DepreciationSchedule.parenttype == "Asset Depreciation Schedule") + & (DepreciationSchedule.schedule_date == d.posting_date) + ) + ).run(as_dict=True) + asset_data.accumulated_depreciation_amount = query[0]["accumulated_depreciation_amount"] + else: asset_data.accumulated_depreciation_amount += d.debit + asset_data.opening_accumulated_depreciation = asset_data.accumulated_depreciation_amount - d.debit row = frappe._dict(asset_data) row.update(