mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-25 16:04:46 +00:00
Merge pull request #30320 from frappe/mergify/bp/version-13-hotfix/pr-30287
fix: custom cash flow mapper doesn't show any data (backport #30287)
This commit is contained in:
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import add_to_date
|
from frappe.query_builder.functions import Sum
|
||||||
|
from frappe.utils import add_to_date, get_date_str
|
||||||
|
|
||||||
from erpnext.accounts.report.financial_statements import get_columns, get_data, get_period_list
|
from erpnext.accounts.report.financial_statements import get_columns, get_data, get_period_list
|
||||||
from erpnext.accounts.report.profit_and_loss_statement.profit_and_loss_statement import (
|
from erpnext.accounts.report.profit_and_loss_statement.profit_and_loss_statement import (
|
||||||
@@ -28,15 +29,22 @@ def get_mappers_from_db():
|
|||||||
|
|
||||||
|
|
||||||
def get_accounts_in_mappers(mapping_names):
|
def get_accounts_in_mappers(mapping_names):
|
||||||
return frappe.db.sql('''
|
cfm = frappe.qb.DocType('Cash Flow Mapping')
|
||||||
select cfma.name, cfm.label, cfm.is_working_capital, cfm.is_income_tax_liability,
|
cfma = frappe.qb.DocType('Cash Flow Mapping Accounts')
|
||||||
cfm.is_income_tax_expense, cfm.is_finance_cost, cfm.is_finance_cost_adjustment
|
result = (
|
||||||
from `tabCash Flow Mapping Accounts` cfma
|
frappe.qb
|
||||||
join `tabCash Flow Mapping` cfm on cfma.parent=cfm.name
|
.select(
|
||||||
where cfma.parent in (%s)
|
cfma.name, cfm.label, cfm.is_working_capital,
|
||||||
order by cfm.is_working_capital
|
cfm.is_income_tax_liability, cfm.is_income_tax_expense,
|
||||||
''', (', '.join('"%s"' % d for d in mapping_names)))
|
cfm.is_finance_cost, cfm.is_finance_cost_adjustment, cfma.account
|
||||||
|
)
|
||||||
|
.from_(cfm)
|
||||||
|
.join(cfma)
|
||||||
|
.on(cfm.name == cfma.parent)
|
||||||
|
.where(cfma.parent.isin(mapping_names))
|
||||||
|
).run()
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def setup_mappers(mappers):
|
def setup_mappers(mappers):
|
||||||
cash_flow_accounts = []
|
cash_flow_accounts = []
|
||||||
@@ -57,31 +65,31 @@ def setup_mappers(mappers):
|
|||||||
|
|
||||||
account_types = [
|
account_types = [
|
||||||
dict(
|
dict(
|
||||||
name=account[0], label=account[1], is_working_capital=account[2],
|
name=account[0], account_name=account[7], label=account[1], is_working_capital=account[2],
|
||||||
is_income_tax_liability=account[3], is_income_tax_expense=account[4]
|
is_income_tax_liability=account[3], is_income_tax_expense=account[4]
|
||||||
) for account in accounts if not account[3]]
|
) for account in accounts if not account[3]]
|
||||||
|
|
||||||
finance_costs_adjustments = [
|
finance_costs_adjustments = [
|
||||||
dict(
|
dict(
|
||||||
name=account[0], label=account[1], is_finance_cost=account[5],
|
name=account[0], account_name=account[7], label=account[1], is_finance_cost=account[5],
|
||||||
is_finance_cost_adjustment=account[6]
|
is_finance_cost_adjustment=account[6]
|
||||||
) for account in accounts if account[6]]
|
) for account in accounts if account[6]]
|
||||||
|
|
||||||
tax_liabilities = [
|
tax_liabilities = [
|
||||||
dict(
|
dict(
|
||||||
name=account[0], label=account[1], is_income_tax_liability=account[3],
|
name=account[0], account_name=account[7], label=account[1], is_income_tax_liability=account[3],
|
||||||
is_income_tax_expense=account[4]
|
is_income_tax_expense=account[4]
|
||||||
) for account in accounts if account[3]]
|
) for account in accounts if account[3]]
|
||||||
|
|
||||||
tax_expenses = [
|
tax_expenses = [
|
||||||
dict(
|
dict(
|
||||||
name=account[0], label=account[1], is_income_tax_liability=account[3],
|
name=account[0], account_name=account[7], label=account[1], is_income_tax_liability=account[3],
|
||||||
is_income_tax_expense=account[4]
|
is_income_tax_expense=account[4]
|
||||||
) for account in accounts if account[4]]
|
) for account in accounts if account[4]]
|
||||||
|
|
||||||
finance_costs = [
|
finance_costs = [
|
||||||
dict(
|
dict(
|
||||||
name=account[0], label=account[1], is_finance_cost=account[5])
|
name=account[0], account_name=account[7], label=account[1], is_finance_cost=account[5])
|
||||||
for account in accounts if account[5]]
|
for account in accounts if account[5]]
|
||||||
|
|
||||||
account_types_labels = sorted(
|
account_types_labels = sorted(
|
||||||
@@ -124,27 +132,27 @@ def setup_mappers(mappers):
|
|||||||
)
|
)
|
||||||
|
|
||||||
for label in account_types_labels:
|
for label in account_types_labels:
|
||||||
names = [d['name'] for d in account_types if d['label'] == label[0]]
|
names = [d['account_name'] for d in account_types if d['label'] == label[0]]
|
||||||
m = dict(label=label[0], names=names, is_working_capital=label[1])
|
m = dict(label=label[0], names=names, is_working_capital=label[1])
|
||||||
mapping['account_types'].append(m)
|
mapping['account_types'].append(m)
|
||||||
|
|
||||||
for label in fc_adjustment_labels:
|
for label in fc_adjustment_labels:
|
||||||
names = [d['name'] for d in finance_costs_adjustments if d['label'] == label[0]]
|
names = [d['account_name'] for d in finance_costs_adjustments if d['label'] == label[0]]
|
||||||
m = dict(label=label[0], names=names)
|
m = dict(label=label[0], names=names)
|
||||||
mapping['finance_costs_adjustments'].append(m)
|
mapping['finance_costs_adjustments'].append(m)
|
||||||
|
|
||||||
for label in unique_liability_labels:
|
for label in unique_liability_labels:
|
||||||
names = [d['name'] for d in tax_liabilities if d['label'] == label[0]]
|
names = [d['account_name'] for d in tax_liabilities if d['label'] == label[0]]
|
||||||
m = dict(label=label[0], names=names, tax_liability=label[1], tax_expense=label[2])
|
m = dict(label=label[0], names=names, tax_liability=label[1], tax_expense=label[2])
|
||||||
mapping['tax_liabilities'].append(m)
|
mapping['tax_liabilities'].append(m)
|
||||||
|
|
||||||
for label in unique_expense_labels:
|
for label in unique_expense_labels:
|
||||||
names = [d['name'] for d in tax_expenses if d['label'] == label[0]]
|
names = [d['account_name'] for d in tax_expenses if d['label'] == label[0]]
|
||||||
m = dict(label=label[0], names=names, tax_liability=label[1], tax_expense=label[2])
|
m = dict(label=label[0], names=names, tax_liability=label[1], tax_expense=label[2])
|
||||||
mapping['tax_expenses'].append(m)
|
mapping['tax_expenses'].append(m)
|
||||||
|
|
||||||
for label in unique_finance_costs_labels:
|
for label in unique_finance_costs_labels:
|
||||||
names = [d['name'] for d in finance_costs if d['label'] == label[0]]
|
names = [d['account_name'] for d in finance_costs if d['label'] == label[0]]
|
||||||
m = dict(label=label[0], names=names, is_finance_cost=label[1])
|
m = dict(label=label[0], names=names, is_finance_cost=label[1])
|
||||||
mapping['finance_costs'].append(m)
|
mapping['finance_costs'].append(m)
|
||||||
|
|
||||||
@@ -371,14 +379,30 @@ def execute(filters=None):
|
|||||||
|
|
||||||
|
|
||||||
def _get_account_type_based_data(filters, account_names, period_list, accumulated_values, opening_balances=0):
|
def _get_account_type_based_data(filters, account_names, period_list, accumulated_values, opening_balances=0):
|
||||||
|
if not account_names or not account_names[0] or not type(account_names[0]) == str:
|
||||||
|
# only proceed if account_names is a list of account names
|
||||||
|
return {}
|
||||||
|
|
||||||
from erpnext.accounts.report.cash_flow.cash_flow import get_start_date
|
from erpnext.accounts.report.cash_flow.cash_flow import get_start_date
|
||||||
|
|
||||||
company = filters.company
|
company = filters.company
|
||||||
data = {}
|
data = {}
|
||||||
total = 0
|
total = 0
|
||||||
|
GLEntry = frappe.qb.DocType('GL Entry')
|
||||||
|
Account = frappe.qb.DocType('Account')
|
||||||
|
|
||||||
for period in period_list:
|
for period in period_list:
|
||||||
start_date = get_start_date(period, accumulated_values, company)
|
start_date = get_start_date(period, accumulated_values, company)
|
||||||
accounts = ', '.join('"%s"' % d for d in account_names)
|
|
||||||
|
account_subquery = (
|
||||||
|
frappe.qb.from_(Account)
|
||||||
|
.where(
|
||||||
|
(Account.name.isin(account_names)) |
|
||||||
|
(Account.parent_account.isin(account_names))
|
||||||
|
)
|
||||||
|
.select(Account.name)
|
||||||
|
.as_("account_subquery")
|
||||||
|
)
|
||||||
|
|
||||||
if opening_balances:
|
if opening_balances:
|
||||||
date_info = dict(date=start_date)
|
date_info = dict(date=start_date)
|
||||||
@@ -395,32 +419,31 @@ def _get_account_type_based_data(filters, account_names, period_list, accumulate
|
|||||||
else:
|
else:
|
||||||
start, end = add_to_date(**date_info), add_to_date(**date_info)
|
start, end = add_to_date(**date_info), add_to_date(**date_info)
|
||||||
|
|
||||||
gl_sum = frappe.db.sql_list("""
|
start, end = get_date_str(start), get_date_str(end)
|
||||||
select sum(credit) - sum(debit)
|
|
||||||
from `tabGL Entry`
|
|
||||||
where company=%s and posting_date >= %s and posting_date <= %s
|
|
||||||
and voucher_type != 'Period Closing Voucher'
|
|
||||||
and account in ( SELECT name FROM tabAccount WHERE name IN (%s)
|
|
||||||
OR parent_account IN (%s))
|
|
||||||
""", (company, start, end, accounts, accounts))
|
|
||||||
else:
|
|
||||||
gl_sum = frappe.db.sql_list("""
|
|
||||||
select sum(credit) - sum(debit)
|
|
||||||
from `tabGL Entry`
|
|
||||||
where company=%s and posting_date >= %s and posting_date <= %s
|
|
||||||
and voucher_type != 'Period Closing Voucher'
|
|
||||||
and account in ( SELECT name FROM tabAccount WHERE name IN (%s)
|
|
||||||
OR parent_account IN (%s))
|
|
||||||
""", (company, start_date if accumulated_values else period['from_date'],
|
|
||||||
period['to_date'], accounts, accounts))
|
|
||||||
|
|
||||||
if gl_sum and gl_sum[0]:
|
|
||||||
amount = gl_sum[0]
|
|
||||||
else:
|
else:
|
||||||
amount = 0
|
start, end = start_date if accumulated_values else period['from_date'], period['to_date']
|
||||||
|
start, end = get_date_str(start), get_date_str(end)
|
||||||
|
|
||||||
total += amount
|
result = (
|
||||||
data.setdefault(period["key"], amount)
|
frappe.qb.from_(GLEntry)
|
||||||
|
.select(Sum(GLEntry.credit) - Sum(GLEntry.debit))
|
||||||
|
.where(
|
||||||
|
(GLEntry.company == company) &
|
||||||
|
(GLEntry.posting_date >= start) &
|
||||||
|
(GLEntry.posting_date <= end) &
|
||||||
|
(GLEntry.voucher_type != 'Period Closing Voucher') &
|
||||||
|
(GLEntry.account.isin(account_subquery))
|
||||||
|
)
|
||||||
|
).run()
|
||||||
|
|
||||||
|
if result and result[0]:
|
||||||
|
gl_sum = result[0][0]
|
||||||
|
else:
|
||||||
|
gl_sum = 0
|
||||||
|
|
||||||
|
total += gl_sum
|
||||||
|
data.setdefault(period["key"], gl_sum)
|
||||||
|
|
||||||
data["total"] = total
|
data["total"] = total
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user