mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-28 01:14:46 +00:00
refactor: more functions in same file
This commit is contained in:
@@ -81,9 +81,11 @@ def get_asset_categories_for_grouped_by_category(filters):
|
|||||||
asset_capitalization_asset_item = frappe.qb.DocType("Asset Capitalization Asset Item")
|
asset_capitalization_asset_item = frappe.qb.DocType("Asset Capitalization Asset Item")
|
||||||
asset_capitalization = frappe.qb.DocType("Asset Capitalization")
|
asset_capitalization = frappe.qb.DocType("Asset Capitalization")
|
||||||
|
|
||||||
disposal_in_period = (asset.disposal_date.isnull()) | (asset.disposal_date >= filters.from_date) & (
|
disposal_in_period = (
|
||||||
asset.disposal_date >= filters.from_date
|
(IfNull(asset.disposal_date, 0) != 0)
|
||||||
) & (asset.disposal_date <= filters.to_date)
|
& (asset.disposal_date >= filters.from_date)
|
||||||
|
& (asset.disposal_date <= filters.to_date)
|
||||||
|
)
|
||||||
|
|
||||||
value_as_on_from_date = IfNull(
|
value_as_on_from_date = IfNull(
|
||||||
Sum(
|
Sum(
|
||||||
@@ -175,131 +177,232 @@ def get_asset_categories_for_grouped_by_category(filters):
|
|||||||
|
|
||||||
|
|
||||||
def get_assets_for_grouped_by_category(filters):
|
def get_assets_for_grouped_by_category(filters):
|
||||||
condition = ""
|
asset = frappe.qb.DocType("Asset")
|
||||||
if filters.get("asset_category"):
|
gl_entry = frappe.qb.DocType("GL Entry")
|
||||||
condition = f" and a.asset_category = '{filters.get('asset_category')}'"
|
asset_category_account = frappe.qb.DocType("Asset Category Account")
|
||||||
finance_book_filter = ""
|
company = frappe.qb.DocType("Company")
|
||||||
if filters.get("finance_book"):
|
asset_depreciation_schedule = frappe.qb.DocType("Asset Depreciation Schedule")
|
||||||
finance_book_filter += " and ifnull(gle.finance_book, '')=%(finance_book)s"
|
|
||||||
condition += " and exists (select 1 from `tabAsset Depreciation Schedule` ads where ads.asset = a.name and ads.finance_book = %(finance_book)s)"
|
|
||||||
|
|
||||||
# nosemgrep
|
assets_with_finance_book = None
|
||||||
return frappe.db.sql(
|
if filters.get("finance_book"):
|
||||||
f"""
|
assets_with_finance_book = (
|
||||||
SELECT results.asset_category,
|
frappe.qb.from_(asset_depreciation_schedule)
|
||||||
sum(results.accumulated_depreciation_as_on_from_date) as accumulated_depreciation_as_on_from_date,
|
.select(asset_depreciation_schedule.asset)
|
||||||
sum(results.depreciation_eliminated_via_reversal) as depreciation_eliminated_via_reversal,
|
.where(asset_depreciation_schedule.finance_book == filters.get("finance_book"))
|
||||||
sum(results.depreciation_eliminated_during_the_period) as depreciation_eliminated_during_the_period,
|
)
|
||||||
sum(results.depreciation_amount_during_the_period) as depreciation_amount_during_the_period
|
|
||||||
from (SELECT a.asset_category,
|
from_gl_entries_query = (
|
||||||
ifnull(sum(case when gle.posting_date < %(from_date)s and (ifnull(a.disposal_date, 0) = 0 or a.disposal_date >= %(from_date)s) then
|
frappe.qb.from_(gl_entry)
|
||||||
gle.debit
|
.join(asset)
|
||||||
else
|
.on(gl_entry.against_voucher == asset.name)
|
||||||
0
|
.join(asset_category_account)
|
||||||
end), 0) as accumulated_depreciation_as_on_from_date,
|
.on(
|
||||||
ifnull(sum(case when gle.posting_date <= %(to_date)s and ifnull(a.disposal_date, 0) = 0 then
|
(asset_category_account.parent == asset.asset_category)
|
||||||
gle.credit
|
& (asset_category_account.company_name == filters.company)
|
||||||
else
|
)
|
||||||
0
|
.join(company)
|
||||||
end), 0) as depreciation_eliminated_via_reversal,
|
.on(company.name == filters.company)
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0 and a.disposal_date >= %(from_date)s
|
.select(
|
||||||
and a.disposal_date <= %(to_date)s and gle.posting_date <= a.disposal_date then
|
asset.asset_category,
|
||||||
gle.debit
|
IfNull(
|
||||||
else
|
Sum(
|
||||||
0
|
frappe.qb.terms.Case()
|
||||||
end), 0) as depreciation_eliminated_during_the_period,
|
.when(
|
||||||
ifnull(sum(case when gle.posting_date >= %(from_date)s and gle.posting_date <= %(to_date)s
|
(gl_entry.posting_date < filters.from_date)
|
||||||
and (ifnull(a.disposal_date, 0) = 0 or gle.posting_date <= a.disposal_date) then
|
& (
|
||||||
gle.debit
|
(IfNull(asset.disposal_date, 0) == 0) | (asset.disposal_date >= filters.from_date)
|
||||||
else
|
),
|
||||||
0
|
gl_entry.debit,
|
||||||
end), 0) as depreciation_amount_during_the_period
|
)
|
||||||
from `tabGL Entry` gle
|
.else_(0)
|
||||||
join `tabAsset` a on
|
),
|
||||||
gle.against_voucher = a.name
|
0,
|
||||||
join `tabAsset Category Account` aca on
|
).as_("accumulated_depreciation_as_on_from_date"),
|
||||||
aca.parent = a.asset_category and aca.company_name = %(company)s
|
IfNull(
|
||||||
join `tabCompany` company on
|
Sum(
|
||||||
company.name = %(company)s
|
frappe.qb.terms.Case()
|
||||||
where
|
.when(
|
||||||
a.docstatus=1
|
(gl_entry.posting_date <= filters.to_date) & (IfNull(asset.disposal_date, 0) == 0),
|
||||||
and a.company=%(company)s
|
gl_entry.credit,
|
||||||
and a.purchase_date <= %(to_date)s
|
)
|
||||||
and gle.is_cancelled = 0
|
.else_(0)
|
||||||
and gle.account = ifnull(aca.depreciation_expense_account, company.depreciation_expense_account)
|
),
|
||||||
{condition} {finance_book_filter}
|
0,
|
||||||
group by a.asset_category
|
).as_("depreciation_eliminated_via_reversal"),
|
||||||
union
|
IfNull(
|
||||||
SELECT a.asset_category,
|
Sum(
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0 and a.disposal_date < %(from_date)s then
|
frappe.qb.terms.Case()
|
||||||
0
|
.when(
|
||||||
else
|
(IfNull(asset.disposal_date, 0) != 0)
|
||||||
a.opening_accumulated_depreciation
|
& (asset.disposal_date >= filters.from_date)
|
||||||
end), 0) as accumulated_depreciation_as_on_from_date,
|
& (asset.disposal_date <= filters.to_date)
|
||||||
0 as depreciation_eliminated_via_reversal,
|
& (gl_entry.posting_date <= asset.disposal_date),
|
||||||
ifnull(sum(case when a.disposal_date >= %(from_date)s and a.disposal_date <= %(to_date)s then
|
gl_entry.debit,
|
||||||
a.opening_accumulated_depreciation
|
)
|
||||||
else
|
.else_(0)
|
||||||
0
|
),
|
||||||
end), 0) as depreciation_eliminated_during_the_period,
|
0,
|
||||||
0 as depreciation_amount_during_the_period
|
).as_("depreciation_eliminated_during_the_period"),
|
||||||
from `tabAsset` a
|
IfNull(
|
||||||
where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s {condition}
|
Sum(
|
||||||
group by a.asset_category) as results
|
frappe.qb.terms.Case()
|
||||||
group by results.asset_category
|
.when(
|
||||||
""",
|
(gl_entry.posting_date >= filters.from_date)
|
||||||
{
|
& (gl_entry.posting_date <= filters.to_date)
|
||||||
"to_date": filters.to_date,
|
& (
|
||||||
"from_date": filters.from_date,
|
(IfNull(asset.disposal_date, 0) == 0)
|
||||||
"company": filters.company,
|
| (gl_entry.posting_date <= asset.disposal_date)
|
||||||
"finance_book": filters.get("finance_book", ""),
|
),
|
||||||
},
|
gl_entry.debit,
|
||||||
as_dict=1,
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("depreciation_amount_during_the_period"),
|
||||||
|
)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.where(gl_entry.is_cancelled == 0)
|
||||||
|
.where(
|
||||||
|
gl_entry.account
|
||||||
|
== IfNull(
|
||||||
|
asset_category_account.depreciation_expense_account,
|
||||||
|
company.depreciation_expense_account,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.groupby(asset.asset_category)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from_opening_depreciation_query = (
|
||||||
|
frappe.qb.from_(asset)
|
||||||
|
.select(
|
||||||
|
asset.asset_category,
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(IfNull(asset.disposal_date, 0) != 0) & (asset.disposal_date < filters.from_date),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
.else_(asset.opening_accumulated_depreciation)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("accumulated_depreciation_as_on_from_date"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(asset.disposal_date >= filters.from_date) & (asset.disposal_date <= filters.to_date),
|
||||||
|
asset.opening_accumulated_depreciation,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("depreciation_eliminated_during_the_period"),
|
||||||
|
)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.groupby(asset.asset_category)
|
||||||
|
)
|
||||||
|
|
||||||
|
if filters.get("asset_category"):
|
||||||
|
from_gl_entries_query = from_gl_entries_query.where(
|
||||||
|
asset.asset_category == filters.get("asset_category")
|
||||||
|
)
|
||||||
|
from_opening_depreciation_query = from_opening_depreciation_query.where(
|
||||||
|
asset.asset_category == filters.get("asset_category")
|
||||||
|
)
|
||||||
|
|
||||||
|
if assets_with_finance_book is not None:
|
||||||
|
from_gl_entries_query = from_gl_entries_query.where(
|
||||||
|
IfNull(gl_entry.finance_book, "") == filters.get("finance_book")
|
||||||
|
).where(asset.name.isin(assets_with_finance_book))
|
||||||
|
from_opening_depreciation_query = from_opening_depreciation_query.where(
|
||||||
|
asset.name.isin(assets_with_finance_book)
|
||||||
|
)
|
||||||
|
|
||||||
|
combined = {}
|
||||||
|
|
||||||
|
for row in from_gl_entries_query.run(as_dict=True):
|
||||||
|
combined[row.asset_category] = {
|
||||||
|
"asset_category": row.asset_category,
|
||||||
|
"accumulated_depreciation_as_on_from_date": flt(row.accumulated_depreciation_as_on_from_date),
|
||||||
|
"depreciation_eliminated_via_reversal": flt(row.depreciation_eliminated_via_reversal),
|
||||||
|
"depreciation_eliminated_during_the_period": flt(row.depreciation_eliminated_during_the_period),
|
||||||
|
"depreciation_amount_during_the_period": flt(row.depreciation_amount_during_the_period),
|
||||||
|
}
|
||||||
|
|
||||||
|
for row in from_opening_depreciation_query.run(as_dict=True):
|
||||||
|
if row.asset_category not in combined:
|
||||||
|
combined[row.asset_category] = {
|
||||||
|
"asset_category": row.asset_category,
|
||||||
|
"accumulated_depreciation_as_on_from_date": 0.0,
|
||||||
|
"depreciation_eliminated_via_reversal": 0.0,
|
||||||
|
"depreciation_eliminated_during_the_period": 0.0,
|
||||||
|
"depreciation_amount_during_the_period": 0.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
combined[row.asset_category]["accumulated_depreciation_as_on_from_date"] += flt(
|
||||||
|
row.accumulated_depreciation_as_on_from_date
|
||||||
|
)
|
||||||
|
combined[row.asset_category]["depreciation_eliminated_during_the_period"] += flt(
|
||||||
|
row.depreciation_eliminated_during_the_period
|
||||||
|
)
|
||||||
|
|
||||||
|
return list(combined.values())
|
||||||
|
|
||||||
|
|
||||||
def get_asset_value_adjustment_map_by_category(filters):
|
def get_asset_value_adjustment_map_by_category(filters):
|
||||||
asset_value_adjustments = frappe.db.sql(
|
asset = frappe.qb.DocType("Asset")
|
||||||
"""
|
gl_entry = frappe.qb.DocType("GL Entry")
|
||||||
SELECT
|
asset_category_account = frappe.qb.DocType("Asset Category Account")
|
||||||
a.asset_category AS asset_category,
|
|
||||||
IFNULL(
|
|
||||||
SUM(
|
|
||||||
CASE
|
|
||||||
WHEN gle.posting_date < %(from_date)s
|
|
||||||
AND (a.disposal_date IS NULL OR a.disposal_date >= %(from_date)s)
|
|
||||||
THEN gle.debit - gle.credit
|
|
||||||
ELSE 0
|
|
||||||
END
|
|
||||||
),
|
|
||||||
0) AS value_adjustment_before_from_date,
|
|
||||||
IFNULL(
|
|
||||||
SUM(
|
|
||||||
CASE
|
|
||||||
WHEN gle.posting_date <= %(to_date)s
|
|
||||||
AND (a.disposal_date IS NULL OR a.disposal_date >= %(to_date)s)
|
|
||||||
THEN gle.debit - gle.credit
|
|
||||||
ELSE 0
|
|
||||||
END
|
|
||||||
),
|
|
||||||
0) AS value_adjustment_till_to_date
|
|
||||||
|
|
||||||
FROM `tabGL Entry` gle
|
asset_value_adjustments = (
|
||||||
JOIN `tabAsset` a ON gle.against_voucher = a.name
|
frappe.qb.from_(gl_entry)
|
||||||
JOIN `tabAsset Category Account` aca
|
.join(asset)
|
||||||
ON aca.parent = a.asset_category
|
.on(gl_entry.against_voucher == asset.name)
|
||||||
AND aca.company_name = %(company)s
|
.join(asset_category_account)
|
||||||
WHERE gle.is_cancelled = 0
|
.on(
|
||||||
AND a.docstatus = 1
|
(asset_category_account.parent == asset.asset_category)
|
||||||
AND a.company = %(company)s
|
& (asset_category_account.company_name == filters.company)
|
||||||
AND a.purchase_date <= %(to_date)s
|
)
|
||||||
AND gle.account = aca.fixed_asset_account
|
.select(
|
||||||
AND gle.is_opening = 'No'
|
asset.asset_category.as_("asset_category"),
|
||||||
GROUP BY a.asset_category
|
IfNull(
|
||||||
""",
|
Sum(
|
||||||
{"from_date": filters.from_date, "to_date": filters.to_date, "company": filters.company},
|
frappe.qb.terms.Case()
|
||||||
as_dict=1,
|
.when(
|
||||||
)
|
(gl_entry.posting_date < filters.from_date)
|
||||||
|
& (asset.disposal_date.isnull() | (asset.disposal_date >= filters.from_date)),
|
||||||
|
gl_entry.debit - gl_entry.credit,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_adjustment_before_from_date"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(gl_entry.posting_date <= filters.to_date)
|
||||||
|
& (asset.disposal_date.isnull() | (asset.disposal_date >= filters.to_date)),
|
||||||
|
gl_entry.debit - gl_entry.credit,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_adjustment_till_to_date"),
|
||||||
|
)
|
||||||
|
.where(gl_entry.is_cancelled == 0)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.where(gl_entry.account == asset_category_account.fixed_asset_account)
|
||||||
|
.where(gl_entry.is_opening == "No")
|
||||||
|
.groupby(asset.asset_category)
|
||||||
|
).run(as_dict=True)
|
||||||
|
|
||||||
category_value_adjustment_map = {}
|
category_value_adjustment_map = {}
|
||||||
|
|
||||||
@@ -367,210 +470,327 @@ def get_group_by_asset_data(filters):
|
|||||||
|
|
||||||
|
|
||||||
def get_asset_details_for_grouped_by_category(filters):
|
def get_asset_details_for_grouped_by_category(filters):
|
||||||
condition = ""
|
asset = frappe.qb.DocType("Asset")
|
||||||
if filters.get("asset"):
|
asset_depreciation_schedule = frappe.qb.DocType("Asset Depreciation Schedule")
|
||||||
condition += " and a.name = %(asset)s"
|
asset_capitalization_asset_item = frappe.qb.DocType("Asset Capitalization Asset Item")
|
||||||
if filters.get("finance_book"):
|
asset_capitalization = frappe.qb.DocType("Asset Capitalization")
|
||||||
condition += " and exists (select 1 from `tabAsset Depreciation Schedule` ads where ads.asset = a.name and ads.finance_book = %(finance_book)s)"
|
|
||||||
|
|
||||||
# nosemgrep
|
disposal_in_period = (
|
||||||
return frappe.db.sql(
|
(IfNull(asset.disposal_date, 0) != 0)
|
||||||
f"""
|
& (asset.disposal_date >= filters.from_date)
|
||||||
SELECT a.name, a.asset_name,
|
& (asset.disposal_date <= filters.to_date)
|
||||||
ifnull(sum(case when a.purchase_date < %(from_date)s then
|
|
||||||
case when ifnull(a.disposal_date, 0) = 0 or a.disposal_date >= %(from_date)s then
|
|
||||||
a.net_purchase_amount
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end), 0) as value_as_on_from_date,
|
|
||||||
ifnull(sum(case when a.purchase_date >= %(from_date)s then
|
|
||||||
a.net_purchase_amount
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end), 0) as value_of_new_purchase,
|
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0
|
|
||||||
and a.disposal_date >= %(from_date)s
|
|
||||||
and a.disposal_date <= %(to_date)s then
|
|
||||||
case when a.status = "Sold" then
|
|
||||||
a.net_purchase_amount
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end), 0) as value_of_sold_asset,
|
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0
|
|
||||||
and a.disposal_date >= %(from_date)s
|
|
||||||
and a.disposal_date <= %(to_date)s then
|
|
||||||
case when a.status = "Scrapped" then
|
|
||||||
a.net_purchase_amount
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end), 0) as value_of_scrapped_asset,
|
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0
|
|
||||||
and a.disposal_date >= %(from_date)s
|
|
||||||
and a.disposal_date <= %(to_date)s then
|
|
||||||
case when a.status = "Capitalized" then
|
|
||||||
a.net_purchase_amount
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end), 0) as value_of_capitalized_asset
|
|
||||||
from `tabAsset` a
|
|
||||||
where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s {condition}
|
|
||||||
and not exists(
|
|
||||||
select 1 from `tabAsset Capitalization Asset Item` acai join `tabAsset Capitalization` ac on acai.parent=ac.name
|
|
||||||
where acai.asset = a.name
|
|
||||||
and ac.posting_date < %(from_date)s
|
|
||||||
and ac.docstatus=1
|
|
||||||
)
|
|
||||||
group by a.name
|
|
||||||
""",
|
|
||||||
{
|
|
||||||
"to_date": filters.to_date,
|
|
||||||
"from_date": filters.from_date,
|
|
||||||
"company": filters.company,
|
|
||||||
"asset": filters.get("asset"),
|
|
||||||
"finance_book": filters.get("finance_book"),
|
|
||||||
},
|
|
||||||
as_dict=1,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
capitalized_before_from_date = (
|
||||||
|
frappe.qb.from_(asset_capitalization_asset_item)
|
||||||
|
.join(asset_capitalization)
|
||||||
|
.on(asset_capitalization_asset_item.parent == asset_capitalization.name)
|
||||||
|
.select(asset_capitalization_asset_item.asset)
|
||||||
|
.where(asset_capitalization.posting_date < filters.from_date)
|
||||||
|
.where(asset_capitalization.docstatus == 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
query = (
|
||||||
|
frappe.qb.from_(asset)
|
||||||
|
.select(
|
||||||
|
asset.name,
|
||||||
|
asset.asset_name,
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(asset.purchase_date < filters.from_date)
|
||||||
|
& (
|
||||||
|
(IfNull(asset.disposal_date, 0) == 0) | (asset.disposal_date >= filters.from_date)
|
||||||
|
),
|
||||||
|
asset.net_purchase_amount,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_as_on_from_date"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(asset.purchase_date >= filters.from_date, asset.net_purchase_amount)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_of_new_purchase"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(disposal_in_period & (asset.status == "Sold"), asset.net_purchase_amount)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_of_sold_asset"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(disposal_in_period & (asset.status == "Scrapped"), asset.net_purchase_amount)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_of_scrapped_asset"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
disposal_in_period & (asset.status == "Capitalized"),
|
||||||
|
asset.net_purchase_amount,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_of_capitalized_asset"),
|
||||||
|
)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.where(asset.name.notin(capitalized_before_from_date))
|
||||||
|
.groupby(asset.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
if filters.get("asset"):
|
||||||
|
query = query.where(asset.name == filters.get("asset"))
|
||||||
|
|
||||||
|
if filters.get("finance_book"):
|
||||||
|
assets_with_finance_book = (
|
||||||
|
frappe.qb.from_(asset_depreciation_schedule)
|
||||||
|
.select(asset_depreciation_schedule.asset)
|
||||||
|
.where(asset_depreciation_schedule.finance_book == filters.get("finance_book"))
|
||||||
|
)
|
||||||
|
query = query.where(asset.name.isin(assets_with_finance_book))
|
||||||
|
|
||||||
|
return query.run(as_dict=True)
|
||||||
|
|
||||||
|
|
||||||
def get_assets_for_grouped_by_asset(filters):
|
def get_assets_for_grouped_by_asset(filters):
|
||||||
condition = ""
|
asset = frappe.qb.DocType("Asset")
|
||||||
if filters.get("asset"):
|
gl_entry = frappe.qb.DocType("GL Entry")
|
||||||
condition = f" and a.name = '{filters.get('asset')}'"
|
asset_category_account = frappe.qb.DocType("Asset Category Account")
|
||||||
finance_book_filter = ""
|
company = frappe.qb.DocType("Company")
|
||||||
if filters.get("finance_book"):
|
asset_depreciation_schedule = frappe.qb.DocType("Asset Depreciation Schedule")
|
||||||
finance_book_filter += " and ifnull(gle.finance_book, '')=%(finance_book)s"
|
|
||||||
condition += " and exists (select 1 from `tabAsset Depreciation Schedule` ads where ads.asset = a.name and ads.finance_book = %(finance_book)s)"
|
|
||||||
|
|
||||||
# nosemgrep
|
assets_with_finance_book = None
|
||||||
return frappe.db.sql(
|
if filters.get("finance_book"):
|
||||||
f"""
|
assets_with_finance_book = (
|
||||||
SELECT results.name as asset,
|
frappe.qb.from_(asset_depreciation_schedule)
|
||||||
sum(results.accumulated_depreciation_as_on_from_date) as accumulated_depreciation_as_on_from_date,
|
.select(asset_depreciation_schedule.asset)
|
||||||
sum(results.depreciation_eliminated_via_reversal) as depreciation_eliminated_via_reversal,
|
.where(asset_depreciation_schedule.finance_book == filters.get("finance_book"))
|
||||||
sum(results.depreciation_eliminated_during_the_period) as depreciation_eliminated_during_the_period,
|
)
|
||||||
sum(results.depreciation_amount_during_the_period) as depreciation_amount_during_the_period
|
|
||||||
from (SELECT a.name as name,
|
from_gl_entries_query = (
|
||||||
ifnull(sum(case when gle.posting_date < %(from_date)s and (ifnull(a.disposal_date, 0) = 0 or a.disposal_date >= %(from_date)s) then
|
frappe.qb.from_(gl_entry)
|
||||||
gle.debit
|
.join(asset)
|
||||||
else
|
.on(gl_entry.against_voucher == asset.name)
|
||||||
0
|
.join(asset_category_account)
|
||||||
end), 0) as accumulated_depreciation_as_on_from_date,
|
.on(
|
||||||
ifnull(sum(case when gle.posting_date <= %(to_date)s and ifnull(a.disposal_date, 0) = 0 then
|
(asset_category_account.parent == asset.asset_category)
|
||||||
gle.credit
|
& (asset_category_account.company_name == filters.company)
|
||||||
else
|
)
|
||||||
0
|
.join(company)
|
||||||
end), 0) as depreciation_eliminated_via_reversal,
|
.on(company.name == filters.company)
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0 and a.disposal_date >= %(from_date)s
|
.select(
|
||||||
and a.disposal_date <= %(to_date)s and gle.posting_date <= a.disposal_date then
|
asset.name.as_("asset"),
|
||||||
gle.debit
|
IfNull(
|
||||||
else
|
Sum(
|
||||||
0
|
frappe.qb.terms.Case()
|
||||||
end), 0) as depreciation_eliminated_during_the_period,
|
.when(
|
||||||
ifnull(sum(case when gle.posting_date >= %(from_date)s and gle.posting_date <= %(to_date)s
|
(gl_entry.posting_date < filters.from_date)
|
||||||
and (ifnull(a.disposal_date, 0) = 0 or gle.posting_date <= a.disposal_date) then
|
& (
|
||||||
gle.debit
|
(IfNull(asset.disposal_date, 0) == 0) | (asset.disposal_date >= filters.from_date)
|
||||||
else
|
),
|
||||||
0
|
gl_entry.debit,
|
||||||
end), 0) as depreciation_amount_during_the_period
|
)
|
||||||
from `tabGL Entry` gle
|
.else_(0)
|
||||||
join `tabAsset` a on
|
),
|
||||||
gle.against_voucher = a.name
|
0,
|
||||||
join `tabAsset Category Account` aca on
|
).as_("accumulated_depreciation_as_on_from_date"),
|
||||||
aca.parent = a.asset_category and aca.company_name = %(company)s
|
IfNull(
|
||||||
join `tabCompany` company on
|
Sum(
|
||||||
company.name = %(company)s
|
frappe.qb.terms.Case()
|
||||||
where
|
.when(
|
||||||
a.docstatus=1
|
(gl_entry.posting_date <= filters.to_date) & (IfNull(asset.disposal_date, 0) == 0),
|
||||||
and a.company=%(company)s
|
gl_entry.credit,
|
||||||
and a.purchase_date <= %(to_date)s
|
)
|
||||||
and gle.is_cancelled = 0
|
.else_(0)
|
||||||
and gle.account = ifnull(aca.depreciation_expense_account, company.depreciation_expense_account)
|
),
|
||||||
{finance_book_filter} {condition}
|
0,
|
||||||
group by a.name
|
).as_("depreciation_eliminated_via_reversal"),
|
||||||
union
|
IfNull(
|
||||||
SELECT a.name as name,
|
Sum(
|
||||||
ifnull(sum(case when ifnull(a.disposal_date, 0) != 0 and a.disposal_date < %(from_date)s then
|
frappe.qb.terms.Case()
|
||||||
0
|
.when(
|
||||||
else
|
(IfNull(asset.disposal_date, 0) != 0)
|
||||||
a.opening_accumulated_depreciation
|
& (asset.disposal_date >= filters.from_date)
|
||||||
end), 0) as accumulated_depreciation_as_on_from_date,
|
& (asset.disposal_date <= filters.to_date)
|
||||||
0 as depreciation_as_on_from_date_credit,
|
& (gl_entry.posting_date <= asset.disposal_date),
|
||||||
ifnull(sum(case when a.disposal_date >= %(from_date)s and a.disposal_date <= %(to_date)s then
|
gl_entry.debit,
|
||||||
a.opening_accumulated_depreciation
|
)
|
||||||
else
|
.else_(0)
|
||||||
0
|
),
|
||||||
end), 0) as depreciation_eliminated_during_the_period,
|
0,
|
||||||
0 as depreciation_amount_during_the_period
|
).as_("depreciation_eliminated_during_the_period"),
|
||||||
from `tabAsset` a
|
IfNull(
|
||||||
where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s {condition}
|
Sum(
|
||||||
group by a.name) as results
|
frappe.qb.terms.Case()
|
||||||
group by results.name
|
.when(
|
||||||
""",
|
(gl_entry.posting_date >= filters.from_date)
|
||||||
{
|
& (gl_entry.posting_date <= filters.to_date)
|
||||||
"to_date": filters.to_date,
|
& (
|
||||||
"from_date": filters.from_date,
|
(IfNull(asset.disposal_date, 0) == 0)
|
||||||
"company": filters.company,
|
| (gl_entry.posting_date <= asset.disposal_date)
|
||||||
"finance_book": filters.get("finance_book", ""),
|
),
|
||||||
},
|
gl_entry.debit,
|
||||||
as_dict=1,
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("depreciation_amount_during_the_period"),
|
||||||
|
)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.where(gl_entry.is_cancelled == 0)
|
||||||
|
.where(
|
||||||
|
gl_entry.account
|
||||||
|
== IfNull(
|
||||||
|
asset_category_account.depreciation_expense_account,
|
||||||
|
company.depreciation_expense_account,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.groupby(asset.name)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from_opening_depreciation_query = (
|
||||||
|
frappe.qb.from_(asset)
|
||||||
|
.select(
|
||||||
|
asset.name.as_("asset"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(IfNull(asset.disposal_date, 0) != 0) & (asset.disposal_date < filters.from_date),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
.else_(asset.opening_accumulated_depreciation)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("accumulated_depreciation_as_on_from_date"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(asset.disposal_date >= filters.from_date) & (asset.disposal_date <= filters.to_date),
|
||||||
|
asset.opening_accumulated_depreciation,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("depreciation_eliminated_during_the_period"),
|
||||||
|
)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.groupby(asset.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
if filters.get("asset"):
|
||||||
|
from_gl_entries_query = from_gl_entries_query.where(asset.name == filters.get("asset"))
|
||||||
|
from_opening_depreciation_query = from_opening_depreciation_query.where(
|
||||||
|
asset.name == filters.get("asset")
|
||||||
|
)
|
||||||
|
|
||||||
|
if assets_with_finance_book is not None:
|
||||||
|
from_gl_entries_query = from_gl_entries_query.where(
|
||||||
|
IfNull(gl_entry.finance_book, "") == filters.get("finance_book")
|
||||||
|
).where(asset.name.isin(assets_with_finance_book))
|
||||||
|
from_opening_depreciation_query = from_opening_depreciation_query.where(
|
||||||
|
asset.name.isin(assets_with_finance_book)
|
||||||
|
)
|
||||||
|
|
||||||
|
combined = {}
|
||||||
|
|
||||||
|
for row in from_gl_entries_query.run(as_dict=True):
|
||||||
|
combined[row.asset] = {
|
||||||
|
"asset": row.asset,
|
||||||
|
"accumulated_depreciation_as_on_from_date": flt(row.accumulated_depreciation_as_on_from_date),
|
||||||
|
"depreciation_eliminated_via_reversal": flt(row.depreciation_eliminated_via_reversal),
|
||||||
|
"depreciation_eliminated_during_the_period": flt(row.depreciation_eliminated_during_the_period),
|
||||||
|
"depreciation_amount_during_the_period": flt(row.depreciation_amount_during_the_period),
|
||||||
|
}
|
||||||
|
|
||||||
|
for row in from_opening_depreciation_query.run(as_dict=True):
|
||||||
|
if row.asset not in combined:
|
||||||
|
combined[row.asset] = {
|
||||||
|
"asset": row.asset,
|
||||||
|
"accumulated_depreciation_as_on_from_date": 0.0,
|
||||||
|
"depreciation_eliminated_via_reversal": 0.0,
|
||||||
|
"depreciation_eliminated_during_the_period": 0.0,
|
||||||
|
"depreciation_amount_during_the_period": 0.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
combined[row.asset]["accumulated_depreciation_as_on_from_date"] += flt(
|
||||||
|
row.accumulated_depreciation_as_on_from_date
|
||||||
|
)
|
||||||
|
combined[row.asset]["depreciation_eliminated_during_the_period"] += flt(
|
||||||
|
row.depreciation_eliminated_during_the_period
|
||||||
|
)
|
||||||
|
|
||||||
|
return list(combined.values())
|
||||||
|
|
||||||
|
|
||||||
def get_asset_value_adjustment_map(filters):
|
def get_asset_value_adjustment_map(filters):
|
||||||
asset_with_value_adjustments = frappe.db.sql(
|
asset = frappe.qb.DocType("Asset")
|
||||||
"""
|
gl_entry = frappe.qb.DocType("GL Entry")
|
||||||
SELECT
|
asset_category_account = frappe.qb.DocType("Asset Category Account")
|
||||||
a.name AS asset,
|
|
||||||
IFNULL(
|
|
||||||
SUM(
|
|
||||||
CASE
|
|
||||||
WHEN gle.posting_date < %(from_date)s
|
|
||||||
AND (a.disposal_date IS NULL OR a.disposal_date >= %(from_date)s)
|
|
||||||
THEN gle.debit - gle.credit
|
|
||||||
ELSE 0
|
|
||||||
END
|
|
||||||
),
|
|
||||||
0) AS value_adjustment_before_from_date,
|
|
||||||
IFNULL(
|
|
||||||
SUM(
|
|
||||||
CASE
|
|
||||||
WHEN gle.posting_date <= %(to_date)s
|
|
||||||
AND (a.disposal_date IS NULL OR a.disposal_date >= %(to_date)s)
|
|
||||||
THEN gle.debit - gle.credit
|
|
||||||
ELSE 0
|
|
||||||
END
|
|
||||||
),
|
|
||||||
0) AS value_adjustment_till_to_date
|
|
||||||
|
|
||||||
FROM `tabGL Entry` gle
|
asset_with_value_adjustments = (
|
||||||
JOIN `tabAsset` a ON gle.against_voucher = a.name
|
frappe.qb.from_(gl_entry)
|
||||||
JOIN `tabAsset Category Account` aca
|
.join(asset)
|
||||||
ON aca.parent = a.asset_category
|
.on(gl_entry.against_voucher == asset.name)
|
||||||
AND aca.company_name = %(company)s
|
.join(asset_category_account)
|
||||||
WHERE gle.is_cancelled = 0
|
.on(
|
||||||
AND a.docstatus = 1
|
(asset_category_account.parent == asset.asset_category)
|
||||||
AND a.company = %(company)s
|
& (asset_category_account.company_name == filters.company)
|
||||||
AND a.purchase_date <= %(to_date)s
|
)
|
||||||
AND gle.account = aca.fixed_asset_account
|
.select(
|
||||||
AND gle.is_opening = 'No'
|
asset.name.as_("asset"),
|
||||||
GROUP BY a.name
|
IfNull(
|
||||||
""",
|
Sum(
|
||||||
{"from_date": filters.from_date, "to_date": filters.to_date, "company": filters.company},
|
frappe.qb.terms.Case()
|
||||||
as_dict=1,
|
.when(
|
||||||
)
|
(gl_entry.posting_date < filters.from_date)
|
||||||
|
& (asset.disposal_date.isnull() | (asset.disposal_date >= filters.from_date)),
|
||||||
|
gl_entry.debit - gl_entry.credit,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_adjustment_before_from_date"),
|
||||||
|
IfNull(
|
||||||
|
Sum(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(
|
||||||
|
(gl_entry.posting_date <= filters.to_date)
|
||||||
|
& (asset.disposal_date.isnull() | (asset.disposal_date >= filters.to_date)),
|
||||||
|
gl_entry.debit - gl_entry.credit,
|
||||||
|
)
|
||||||
|
.else_(0)
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
).as_("value_adjustment_till_to_date"),
|
||||||
|
)
|
||||||
|
.where(gl_entry.is_cancelled == 0)
|
||||||
|
.where(asset.docstatus == 1)
|
||||||
|
.where(asset.company == filters.company)
|
||||||
|
.where(asset.purchase_date <= filters.to_date)
|
||||||
|
.where(gl_entry.account == asset_category_account.fixed_asset_account)
|
||||||
|
.where(gl_entry.is_opening == "No")
|
||||||
|
.groupby(asset.name)
|
||||||
|
).run(as_dict=True)
|
||||||
|
|
||||||
asset_value_adjustment_map = {}
|
asset_value_adjustment_map = {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user