mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-20 21:49:18 +00:00
refactor: move fn to fetch advance taxes to utils & use qb
This commit is contained in:
@@ -12,6 +12,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|||||||
get_accounting_dimensions,
|
get_accounting_dimensions,
|
||||||
)
|
)
|
||||||
from erpnext.accounts.report.utils import (
|
from erpnext.accounts.report.utils import (
|
||||||
|
get_advance_taxes_and_charges,
|
||||||
get_conditions,
|
get_conditions,
|
||||||
get_journal_entries,
|
get_journal_entries,
|
||||||
get_party_details,
|
get_party_details,
|
||||||
@@ -235,7 +236,7 @@ def get_invoices(filters, additional_query_columns):
|
|||||||
if filters.get("supplier"):
|
if filters.get("supplier"):
|
||||||
query = query.where(pi.supplier == filters.supplier)
|
query = query.where(pi.supplier == filters.supplier)
|
||||||
query = get_conditions(filters, query, [pi, invoice_item], accounting_dimensions)
|
query = get_conditions(filters, query, [pi, invoice_item], accounting_dimensions)
|
||||||
invoices = query.run(as_dict=True, debug=True)
|
invoices = query.run(as_dict=True)
|
||||||
return invoices
|
return invoices
|
||||||
|
|
||||||
|
|
||||||
@@ -312,20 +313,7 @@ def get_invoice_tax_map(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if include_payments:
|
if include_payments:
|
||||||
advance_tax_details = frappe.db.sql(
|
tax_details += get_advance_taxes_and_charges(invoice_list)
|
||||||
"""
|
|
||||||
select parent, account_head, case add_deduct_tax when "Add" then sum(base_tax_amount)
|
|
||||||
else sum(base_tax_amount) * -1 end as tax_amount
|
|
||||||
from `tabAdvance Taxes and Charges`
|
|
||||||
where parent in (%s) and charge_type in ('On Paid Amount', 'Actual')
|
|
||||||
and base_tax_amount != 0
|
|
||||||
group by parent, account_head, add_deduct_tax
|
|
||||||
"""
|
|
||||||
% ", ".join(["%s"] * len(invoice_list)),
|
|
||||||
tuple(inv.name for inv in invoice_list),
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
tax_details += advance_tax_details
|
|
||||||
|
|
||||||
invoice_tax_map = {}
|
invoice_tax_map = {}
|
||||||
for d in tax_details:
|
for d in tax_details:
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|||||||
get_accounting_dimensions,
|
get_accounting_dimensions,
|
||||||
)
|
)
|
||||||
from erpnext.accounts.report.utils import (
|
from erpnext.accounts.report.utils import (
|
||||||
|
get_advance_taxes_and_charges,
|
||||||
get_conditions,
|
get_conditions,
|
||||||
get_journal_entries,
|
get_journal_entries,
|
||||||
get_party_details,
|
get_party_details,
|
||||||
@@ -42,6 +43,7 @@ def _execute(filters, additional_table_columns=None):
|
|||||||
invoice_list, additional_table_columns, include_payments
|
invoice_list, additional_table_columns, include_payments
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print("Accounts", tax_accounts)
|
||||||
if not invoice_list:
|
if not invoice_list:
|
||||||
msgprint(_("No record found"))
|
msgprint(_("No record found"))
|
||||||
return columns, invoice_list
|
return columns, invoice_list
|
||||||
@@ -120,6 +122,7 @@ def _execute(filters, additional_table_columns=None):
|
|||||||
or 2
|
or 2
|
||||||
)
|
)
|
||||||
tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc), tax_amount_precision)
|
tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc), tax_amount_precision)
|
||||||
|
print(tax_amount)
|
||||||
total_tax += tax_amount
|
total_tax += tax_amount
|
||||||
row.update({frappe.scrub(tax_acc): tax_amount})
|
row.update({frappe.scrub(tax_acc): tax_amount})
|
||||||
|
|
||||||
@@ -399,7 +402,7 @@ def get_invoices(filters, additional_query_columns):
|
|||||||
if filters.get("customer"):
|
if filters.get("customer"):
|
||||||
query = query.where(si.customer == filters.customer)
|
query = query.where(si.customer == filters.customer)
|
||||||
query = get_conditions(filters, query, [si, invoice_item, invoice_payment], accounting_dimensions)
|
query = get_conditions(filters, query, [si, invoice_item, invoice_payment], accounting_dimensions)
|
||||||
invoices = query.run(as_dict=True, debug=True)
|
invoices = query.run(as_dict=True)
|
||||||
return invoices
|
return invoices
|
||||||
|
|
||||||
|
|
||||||
@@ -465,20 +468,7 @@ def get_invoice_tax_map(invoice_list, invoice_income_map, income_accounts, inclu
|
|||||||
)
|
)
|
||||||
|
|
||||||
if include_payments:
|
if include_payments:
|
||||||
advance_tax_details = frappe.db.sql(
|
tax_details += get_advance_taxes_and_charges(invoice_list)
|
||||||
"""
|
|
||||||
select parent, account_head, case add_deduct_tax when "Add" then sum(base_tax_amount)
|
|
||||||
else sum(base_tax_amount) * -1 end as tax_amount
|
|
||||||
from `tabAdvance Taxes and Charges`
|
|
||||||
where parent in (%s) and charge_type in ('On Paid Amount', 'Actual')
|
|
||||||
and base_tax_amount != 0
|
|
||||||
group by parent, account_head, add_deduct_tax
|
|
||||||
"""
|
|
||||||
% ", ".join(["%s"] * len(invoice_list)),
|
|
||||||
tuple(inv.name for inv in invoice_list),
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
tax_details += advance_tax_details
|
|
||||||
|
|
||||||
invoice_tax_map = {}
|
invoice_tax_map = {}
|
||||||
for d in tax_details:
|
for d in tax_details:
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.query_builder.custom import ConstantColumn
|
from frappe.query_builder.custom import ConstantColumn
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
|
from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
|
||||||
from pypika import Order
|
from pypika import Order
|
||||||
|
|
||||||
@@ -220,7 +221,7 @@ def get_taxes_query(invoice_list, doctype, parenttype):
|
|||||||
if doctype == "Purchase Taxes and Charges":
|
if doctype == "Purchase Taxes and Charges":
|
||||||
return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
|
return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
|
||||||
elif doctype == "Sales Taxes and Charges":
|
elif doctype == "Sales Taxes and Charges":
|
||||||
return query.where(taxes.charge_type.isin(["Total", "Valuation and Total"]))
|
return query
|
||||||
return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
|
return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
|
||||||
|
|
||||||
|
|
||||||
@@ -250,7 +251,7 @@ def get_journal_entries(filters, accounting_dimensions, args):
|
|||||||
.orderby(je.posting_date, je.name, order=Order.desc)
|
.orderby(je.posting_date, je.name, order=Order.desc)
|
||||||
)
|
)
|
||||||
query = get_conditions(filters, query, [je], accounting_dimensions, payments=True)
|
query = get_conditions(filters, query, [je], accounting_dimensions, payments=True)
|
||||||
journal_entries = query.run(as_dict=True, debug=True)
|
journal_entries = query.run(as_dict=True)
|
||||||
return journal_entries
|
return journal_entries
|
||||||
|
|
||||||
|
|
||||||
@@ -276,7 +277,7 @@ def get_payment_entries(filters, accounting_dimensions, args):
|
|||||||
.orderby(pe.posting_date, pe.name, order=Order.desc)
|
.orderby(pe.posting_date, pe.name, order=Order.desc)
|
||||||
)
|
)
|
||||||
query = get_conditions(filters, query, [pe], accounting_dimensions, payments=True)
|
query = get_conditions(filters, query, [pe], accounting_dimensions, payments=True)
|
||||||
payment_entries = query.run(as_dict=True, debug=True)
|
payment_entries = query.run(as_dict=True)
|
||||||
return payment_entries
|
return payment_entries
|
||||||
|
|
||||||
|
|
||||||
@@ -326,3 +327,25 @@ def get_conditions(filters, query, docs, accounting_dimensions, payments=False):
|
|||||||
fieldname = dimension.fieldname
|
fieldname = dimension.fieldname
|
||||||
query = query.where(parent_doc.fieldname.isin(filters.fieldname))
|
query = query.where(parent_doc.fieldname.isin(filters.fieldname))
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
|
def get_advance_taxes_and_charges(invoice_list):
|
||||||
|
adv_taxes = frappe.qb.DocType("Advance Taxes and Charges")
|
||||||
|
return (
|
||||||
|
frappe.qb.from_(adv_taxes)
|
||||||
|
.select(
|
||||||
|
adv_taxes.parent,
|
||||||
|
adv_taxes.account_head,
|
||||||
|
(
|
||||||
|
frappe.qb.terms.Case()
|
||||||
|
.when(adv_taxes.add_deduct_tax == "Add", Sum(adv_taxes.base_tax_amount))
|
||||||
|
.else_(Sum(adv_taxes.base_tax_amount) * -1)
|
||||||
|
).as_("tax_amount"),
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
(adv_taxes.parent.isin([inv.name for inv in invoice_list]))
|
||||||
|
& (adv_taxes.charge_type.isin(["On Paid Amount", "Actual"]))
|
||||||
|
& (adv_taxes.base_tax_amount != 0)
|
||||||
|
)
|
||||||
|
.groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax)
|
||||||
|
).run(as_dict=True, debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user