fix: show tax withholding category details for customers

This commit is contained in:
Gursheen Anand 2023-07-19 11:51:45 +05:30
parent 07d2b896c1
commit dd37f6cbd6
2 changed files with 84 additions and 38 deletions

View File

@ -12,10 +12,23 @@ frappe.query_reports["TDS Payable Monthly"] = {
"default": frappe.defaults.get_default('company') "default": frappe.defaults.get_default('company')
}, },
{ {
"fieldname":"supplier", "fieldname":"party_type",
"label": __("Supplier"), "label": __("Party Type"),
"fieldtype": "Link", "fieldtype": "Select",
"options": "Supplier", "options": ["Supplier", "Customer"]
},
{
"fieldname":"party",
"label": __("Party"),
"fieldtype": "Dynamic Link",
"get_options": function() {
var party_type = frappe.query_report.get_filter_value('party_type');
var party = frappe.query_report.get_filter_value('party');
if(party && !party_type) {
frappe.throw(__("Please select Party Type first"));
}
return party_type;
}
}, },
{ {
"fieldname":"from_date", "fieldname":"from_date",

View File

@ -33,77 +33,95 @@ def validate_filters(filters):
def get_result( def get_result(
filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, invoice_net_total_map filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, invoice_net_total_map
): ):
supplier_map = get_supplier_pan_map() party_map = get_party_pan_map()
tax_rate_map = get_tax_rate_map(filters) tax_rate_map = get_tax_rate_map(filters)
gle_map = get_gle_map(tds_docs) gle_map = get_gle_map(tds_docs)
out = [] out = []
for name, details in gle_map.items(): for name, details in gle_map.items():
tds_deducted, total_amount_credited = 0, 0 tax_deducted, total_amount_credited = 0, 0
tax_withholding_category = tax_category_map.get(name) tax_withholding_category = tax_category_map.get(name)
rate = tax_rate_map.get(tax_withholding_category) rate = tax_rate_map.get(tax_withholding_category)
for entry in details: for entry in details:
supplier = entry.party or entry.against party = entry.party or entry.against
posting_date = entry.posting_date posting_date = entry.posting_date
voucher_type = entry.voucher_type voucher_type = entry.voucher_type
if voucher_type == "Journal Entry": if voucher_type == "Journal Entry":
suppliers = journal_entry_party_map.get(name) party_list = journal_entry_party_map.get(name)
if suppliers: if party_list:
supplier = suppliers[0] party = party_list[0]
if not tax_withholding_category: if not tax_withholding_category:
tax_withholding_category = supplier_map.get(supplier, {}).get("tax_withholding_category") tax_withholding_category = party_map.get(party, {}).get("tax_withholding_category")
rate = tax_rate_map.get(tax_withholding_category) rate = tax_rate_map.get(tax_withholding_category)
if entry.account in tds_accounts: if entry.account in tds_accounts:
tds_deducted += entry.credit - entry.debit tax_deducted += entry.credit - entry.debit
if invoice_net_total_map.get(name): if invoice_net_total_map.get(name):
total_amount_credited = invoice_net_total_map.get(name) total_amount_credited = invoice_net_total_map.get(name)
else: else:
total_amount_credited += entry.credit total_amount_credited += entry.credit
if tds_deducted: if tax_deducted:
if party_map.get(party, {}).get("party_type") == "Supplier":
party_name = "supplier_name"
party_type = "supplier_type"
table_name = "Supplier"
else:
party_name = "customer_name"
party_type = "customer_type"
table_name = "Customer"
row = { row = {
"pan" "pan"
if frappe.db.has_column("Supplier", "pan") if frappe.db.has_column(table_name, "pan")
else "tax_id": supplier_map.get(supplier, {}).get("pan"), else "tax_id": party_map.get(party, {}).get("pan"),
"supplier": supplier_map.get(supplier, {}).get("name"), "party": party_map.get(party, {}).get("name"),
} }
if filters.naming_series == "Naming Series": if filters.naming_series == "Naming Series":
row.update({"supplier_name": supplier_map.get(supplier, {}).get("supplier_name")}) row.update({"party_name": party_map.get(party, {}).get(party_name)})
row.update( row.update(
{ {
"section_code": tax_withholding_category, "section_code": tax_withholding_category,
"entity_type": supplier_map.get(supplier, {}).get("supplier_type"), "entity_type": party_map.get(party, {}).get(party_type),
"tds_rate": rate, "rate": rate,
"total_amount_credited": total_amount_credited, "total_amount_credited": total_amount_credited,
"tds_deducted": tds_deducted, "tax_deducted": tax_deducted,
"transaction_date": posting_date, "transaction_date": posting_date,
"transaction_type": voucher_type, "transaction_type": voucher_type,
"ref_no": name, "ref_no": name,
"party_type": party_map.get(party, {}).get("party_type"),
} }
) )
out.append(row) out.append(row)
return out return out
def get_supplier_pan_map(): def get_party_pan_map():
supplier_map = frappe._dict() party_map = frappe._dict()
suppliers = frappe.db.get_all( fields = ["name", "supplier_type", "supplier_name", "tax_withholding_category"]
"Supplier", fields=["name", "pan", "supplier_type", "supplier_name", "tax_withholding_category"] if frappe.db.has_column("Supplier", "pan"):
) fields.append("pan")
suppliers = frappe.db.get_all("Supplier", fields=fields)
fields = ["name", "customer_type", "customer_name", "tax_withholding_category"]
if frappe.db.has_column("Customer", "pan"):
fields.append("pan")
customers = frappe.db.get_all("Customer", fields=fields)
for d in suppliers: for d in suppliers:
supplier_map[d.name] = d d.party_type = "Supplier"
party_map[d.name] = d
for d in customers:
d.party_type = "Customer"
party_map[d.name] = d
return supplier_map return party_map
def get_gle_map(documents): def get_gle_map(documents):
@ -131,17 +149,22 @@ def get_columns(filters):
columns = [ columns = [
{"label": _(frappe.unscrub(pan)), "fieldname": pan, "fieldtype": "Data", "width": 90}, {"label": _(frappe.unscrub(pan)), "fieldname": pan, "fieldtype": "Data", "width": 90},
{ {
"label": _("Supplier"), "label": _("Party Type"),
"options": "Supplier", "fieldname": "party_type",
"fieldname": "supplier", "width": 180,
"fieldtype": "Link", },
{
"label": _("Party"),
"fieldname": "party",
"fieldtype": "Dynamic Link",
"options": "party_type",
"width": 180, "width": 180,
}, },
] ]
if filters.naming_series == "Naming Series": if filters.naming_series == "Naming Series":
columns.append( columns.append(
{"label": _("Supplier Name"), "fieldname": "supplier_name", "fieldtype": "Data", "width": 180} {"label": _("Party Name"), "fieldname": "party_name", "fieldtype": "Data", "width": 180}
) )
columns.extend( columns.extend(
@ -154,7 +177,7 @@ def get_columns(filters):
"width": 180, "width": 180,
}, },
{"label": _("Entity Type"), "fieldname": "entity_type", "fieldtype": "Data", "width": 180}, {"label": _("Entity Type"), "fieldname": "entity_type", "fieldtype": "Data", "width": 180},
{"label": _("TDS Rate %"), "fieldname": "tds_rate", "fieldtype": "Percent", "width": 90}, {"label": _("Rate %"), "fieldname": "rate", "fieldtype": "Percent", "width": 90},
{ {
"label": _("Total Amount Credited"), "label": _("Total Amount Credited"),
"fieldname": "total_amount_credited", "fieldname": "total_amount_credited",
@ -162,8 +185,8 @@ def get_columns(filters):
"width": 90, "width": 90,
}, },
{ {
"label": _("Amount of TDS Deducted"), "label": _("Amount of Tax Deducted"),
"fieldname": "tds_deducted", "fieldname": "tax_deducted",
"fieldtype": "Float", "fieldtype": "Float",
"width": 90, "width": 90,
}, },
@ -190,6 +213,7 @@ def get_columns(filters):
def get_tds_docs(filters): def get_tds_docs(filters):
tds_documents = [] tds_documents = []
purchase_invoices = [] purchase_invoices = []
sales_invoices = []
payment_entries = [] payment_entries = []
journal_entries = [] journal_entries = []
tax_category_map = frappe._dict() tax_category_map = frappe._dict()
@ -209,10 +233,10 @@ def get_tds_docs(filters):
"against": ("not in", bank_accounts), "against": ("not in", bank_accounts),
} }
if filters.get("supplier"): if filters.get("party"):
del query_filters["account"] del query_filters["account"]
del query_filters["against"] del query_filters["against"]
or_filters = {"against": filters.get("supplier"), "party": filters.get("supplier")} or_filters = {"against": filters.get("party"), "party": filters.get("party")}
tds_docs = frappe.get_all( tds_docs = frappe.get_all(
"GL Entry", "GL Entry",
@ -224,6 +248,8 @@ def get_tds_docs(filters):
for d in tds_docs: for d in tds_docs:
if d.voucher_type == "Purchase Invoice": if d.voucher_type == "Purchase Invoice":
purchase_invoices.append(d.voucher_no) purchase_invoices.append(d.voucher_no)
if d.voucher_type == "Sales Invoice":
sales_invoices.append(d.voucher_no)
elif d.voucher_type == "Payment Entry": elif d.voucher_type == "Payment Entry":
payment_entries.append(d.voucher_no) payment_entries.append(d.voucher_no)
elif d.voucher_type == "Journal Entry": elif d.voucher_type == "Journal Entry":
@ -234,6 +260,9 @@ def get_tds_docs(filters):
if purchase_invoices: if purchase_invoices:
get_doc_info(purchase_invoices, "Purchase Invoice", tax_category_map, invoice_net_total_map) get_doc_info(purchase_invoices, "Purchase Invoice", tax_category_map, invoice_net_total_map)
if sales_invoices:
get_doc_info(sales_invoices, "Sales Invoice", tax_category_map, invoice_net_total_map)
if payment_entries: if payment_entries:
get_doc_info(payment_entries, "Payment Entry", tax_category_map) get_doc_info(payment_entries, "Payment Entry", tax_category_map)
@ -267,6 +296,8 @@ def get_journal_entry_party_map(journal_entries):
def get_doc_info(vouchers, doctype, tax_category_map, invoice_net_total_map=None): def get_doc_info(vouchers, doctype, tax_category_map, invoice_net_total_map=None):
if doctype == "Purchase Invoice": if doctype == "Purchase Invoice":
fields = ["name", "tax_withholding_category", "base_tax_withholding_net_total"] fields = ["name", "tax_withholding_category", "base_tax_withholding_net_total"]
if doctype == "Sales Invoice":
fields = ["name", "base_net_total"]
else: else:
fields = ["name", "tax_withholding_category"] fields = ["name", "tax_withholding_category"]
@ -276,6 +307,8 @@ def get_doc_info(vouchers, doctype, tax_category_map, invoice_net_total_map=None
tax_category_map.update({entry.name: entry.tax_withholding_category}) tax_category_map.update({entry.name: entry.tax_withholding_category})
if doctype == "Purchase Invoice": if doctype == "Purchase Invoice":
invoice_net_total_map.update({entry.name: entry.base_tax_withholding_net_total}) invoice_net_total_map.update({entry.name: entry.base_tax_withholding_net_total})
if doctype == "Sales Invoice":
invoice_net_total_map.update({entry.name: entry.base_net_total})
def get_tax_rate_map(filters): def get_tax_rate_map(filters):