Tax rate in Itemised sales/purchase register and cleanups (#10439)
This commit is contained in:
parent
0d9ab86496
commit
1f49f511aa
@ -5,6 +5,7 @@ from __future__ import unicode_literals
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import flt
|
from frappe.utils import flt
|
||||||
|
from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import get_tax_accounts
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
return _execute(filters)
|
return _execute(filters)
|
||||||
@ -12,12 +13,12 @@ def execute(filters=None):
|
|||||||
def _execute(filters=None, additional_table_columns=None, additional_query_columns=None):
|
def _execute(filters=None, additional_table_columns=None, additional_query_columns=None):
|
||||||
if not filters: filters = {}
|
if not filters: filters = {}
|
||||||
columns = get_columns(additional_table_columns)
|
columns = get_columns(additional_table_columns)
|
||||||
last_col = len(columns)
|
|
||||||
|
|
||||||
item_list = get_items(filters, additional_query_columns)
|
item_list = get_items(filters, additional_query_columns)
|
||||||
aii_account_map = get_aii_accounts()
|
aii_account_map = get_aii_accounts()
|
||||||
if item_list:
|
if item_list:
|
||||||
item_row_tax, tax_accounts = get_tax_accounts(item_list, columns)
|
itemised_tax, tax_columns = get_tax_accounts(item_list, columns,
|
||||||
|
tax_doctype="Purchase Taxes and Charges")
|
||||||
|
|
||||||
columns.append({
|
columns.append({
|
||||||
"fieldname": "currency",
|
"fieldname": "currency",
|
||||||
@ -26,6 +27,7 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
"width": 80
|
"width": 80
|
||||||
})
|
})
|
||||||
company_currency = frappe.db.get_value("Company", filters.company, "default_currency")
|
company_currency = frappe.db.get_value("Company", filters.company, "default_currency")
|
||||||
|
po_pr_map = get_purchase_receipts_against_purchase_order(item_list)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
for d in item_list:
|
for d in item_list:
|
||||||
@ -33,8 +35,7 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
if d.purchase_receipt:
|
if d.purchase_receipt:
|
||||||
purchase_receipt = d.purchase_receipt
|
purchase_receipt = d.purchase_receipt
|
||||||
elif d.po_detail:
|
elif d.po_detail:
|
||||||
purchase_receipt = ", ".join(frappe.db.sql_list("""select distinct parent
|
purchase_receipt = ", ".join(po_pr_map.get(d.po_detail, []))
|
||||||
from `tabPurchase Receipt Item` where docstatus=1 and purchase_order_item=%s""", d.po_detail))
|
|
||||||
|
|
||||||
expense_account = d.expense_account or aii_account_map.get(d.company)
|
expense_account = d.expense_account or aii_account_map.get(d.company)
|
||||||
row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.supplier,
|
row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.supplier,
|
||||||
@ -49,10 +50,12 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
purchase_receipt, expense_account, d.qty, d.base_net_rate, d.base_net_amount
|
purchase_receipt, expense_account, d.qty, d.base_net_rate, d.base_net_amount
|
||||||
]
|
]
|
||||||
|
|
||||||
for tax in tax_accounts:
|
total_tax = 0
|
||||||
row.append(item_row_tax.get(d.name, {}).get(tax, 0))
|
for tax in tax_columns:
|
||||||
|
item_tax = itemised_tax.get(d.name, {}).get(tax, {})
|
||||||
|
row += [item_tax.get("tax_rate", 0), item_tax.get("tax_amount", 0)]
|
||||||
|
total_tax += flt(item_tax.get("tax_amount"))
|
||||||
|
|
||||||
total_tax = sum(row[last_col:])
|
|
||||||
row += [total_tax, d.base_net_amount + total_tax, company_currency]
|
row += [total_tax, d.base_net_amount + total_tax, company_currency]
|
||||||
|
|
||||||
data.append(row)
|
data.append(row)
|
||||||
@ -116,53 +119,18 @@ def get_items(filters, additional_query_columns):
|
|||||||
def get_aii_accounts():
|
def get_aii_accounts():
|
||||||
return dict(frappe.db.sql("select name, stock_received_but_not_billed from tabCompany"))
|
return dict(frappe.db.sql("select name, stock_received_but_not_billed from tabCompany"))
|
||||||
|
|
||||||
def get_tax_accounts(item_list, columns):
|
def get_purchase_receipts_against_purchase_order(item_list):
|
||||||
import json
|
po_pr_map = frappe._dict()
|
||||||
item_row_tax = {}
|
po_item_rows = list(set([d.po_detail for d in item_list]))
|
||||||
tax_accounts = []
|
|
||||||
invoice_item_row = {}
|
|
||||||
item_row_map = {}
|
|
||||||
for d in item_list:
|
|
||||||
invoice_item_row.setdefault(d.parent, []).append(d)
|
|
||||||
item_row_map.setdefault(d.parent, {}).setdefault(d.item_code, []).append(d)
|
|
||||||
|
|
||||||
tax_details = frappe.db.sql("""
|
purchase_receipts = frappe.db.sql("""
|
||||||
select
|
select parent, purchase_order_item
|
||||||
parent, account_head, item_wise_tax_detail, charge_type, base_tax_amount_after_discount_amount
|
from `tabPurchase Receipt Item`
|
||||||
from `tabPurchase Taxes and Charges`
|
where docstatus=1 and purchase_order_item in (%s)
|
||||||
where parenttype = 'Purchase Invoice' and docstatus = 1
|
group by purchase_order_item, parent
|
||||||
and (account_head is not null and account_head != '')
|
""" % (', '.join(['%s']*len(po_item_rows))), tuple(po_item_rows), as_dict=1)
|
||||||
and category in ('Total', 'Valuation and Total')
|
|
||||||
and parent in (%s)
|
|
||||||
""" % ', '.join(['%s']*len(invoice_item_row)), tuple(invoice_item_row.keys()))
|
|
||||||
|
|
||||||
for parent, account_head, item_wise_tax_detail, charge_type, tax_amount in tax_details:
|
for pr in purchase_receipts:
|
||||||
if account_head not in tax_accounts:
|
po_pr_map.setdefault(pr.po_detail, []).append(pr.parent)
|
||||||
tax_accounts.append(account_head)
|
|
||||||
|
|
||||||
if item_wise_tax_detail:
|
return po_pr_map
|
||||||
try:
|
|
||||||
item_wise_tax_detail = json.loads(item_wise_tax_detail)
|
|
||||||
|
|
||||||
for item_code, tax_amount in item_wise_tax_detail.items():
|
|
||||||
tax_amount = flt(tax_amount[1]) if isinstance(tax_amount, list) else flt(tax_amount)
|
|
||||||
|
|
||||||
item_net_amount = sum([flt(d.base_net_amount)
|
|
||||||
for d in item_row_map.get(parent, {}).get(item_code, [])])
|
|
||||||
|
|
||||||
for d in item_row_map.get(parent, {}).get(item_code, []):
|
|
||||||
item_tax_amount = flt((tax_amount * d.base_net_amount) / item_net_amount) if item_net_amount else 0
|
|
||||||
item_row_tax.setdefault(d.name, {})[account_head] = item_tax_amount
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
elif charge_type == "Actual" and tax_amount:
|
|
||||||
for d in invoice_item_row.get(parent, []):
|
|
||||||
item_row_tax.setdefault(d.name, {})[account_head] = \
|
|
||||||
flt((tax_amount * d.base_net_amount) / d.base_net_total)
|
|
||||||
|
|
||||||
tax_accounts.sort()
|
|
||||||
columns += [account_head + ":Currency/currency:80" for account_head in tax_accounts]
|
|
||||||
columns += ["Total Tax:Currency/currency:80", "Total:Currency/currency:80"]
|
|
||||||
|
|
||||||
return item_row_tax, tax_accounts
|
|
||||||
@ -13,11 +13,10 @@ def execute(filters=None):
|
|||||||
def _execute(filters=None, additional_table_columns=None, additional_query_columns=None):
|
def _execute(filters=None, additional_table_columns=None, additional_query_columns=None):
|
||||||
if not filters: filters = {}
|
if not filters: filters = {}
|
||||||
columns = get_columns(additional_table_columns)
|
columns = get_columns(additional_table_columns)
|
||||||
last_col = len(columns)
|
|
||||||
|
|
||||||
item_list = get_items(filters, additional_query_columns)
|
item_list = get_items(filters, additional_query_columns)
|
||||||
if item_list:
|
if item_list:
|
||||||
item_row_tax, tax_accounts = get_tax_accounts(item_list, columns)
|
itemised_tax, tax_columns = get_tax_accounts(item_list, columns)
|
||||||
columns.append({
|
columns.append({
|
||||||
"fieldname": "currency",
|
"fieldname": "currency",
|
||||||
"label": _("Currency"),
|
"label": _("Currency"),
|
||||||
@ -26,6 +25,7 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
})
|
})
|
||||||
company_currency = frappe.db.get_value("Company", filters.get("company"), "default_currency")
|
company_currency = frappe.db.get_value("Company", filters.get("company"), "default_currency")
|
||||||
mode_of_payments = get_mode_of_payments(set([d.parent for d in item_list]))
|
mode_of_payments = get_mode_of_payments(set([d.parent for d in item_list]))
|
||||||
|
so_dn_map = get_delivery_notes_against_sales_order(item_list)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
for d in item_list:
|
for d in item_list:
|
||||||
@ -33,8 +33,8 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
if d.delivery_note:
|
if d.delivery_note:
|
||||||
delivery_note = d.delivery_note
|
delivery_note = d.delivery_note
|
||||||
elif d.so_detail:
|
elif d.so_detail:
|
||||||
delivery_note = ", ".join(frappe.db.sql_list("""select distinct parent
|
delivery_note = ", ".join(so_dn_map.get(d.so_detail, []))
|
||||||
from `tabDelivery Note Item` where docstatus=1 and so_detail=%s""", d.so_detail))
|
|
||||||
if not delivery_note and d.update_stock:
|
if not delivery_note and d.update_stock:
|
||||||
delivery_note = d.parent
|
delivery_note = d.parent
|
||||||
|
|
||||||
@ -50,10 +50,12 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
|
|||||||
delivery_note, d.income_account, d.cost_center, d.qty, d.base_net_rate, d.base_net_amount
|
delivery_note, d.income_account, d.cost_center, d.qty, d.base_net_rate, d.base_net_amount
|
||||||
]
|
]
|
||||||
|
|
||||||
for tax in tax_accounts:
|
total_tax = 0
|
||||||
row.append(item_row_tax.get(d.name, {}).get(tax, 0))
|
for tax in tax_columns:
|
||||||
|
item_tax = itemised_tax.get(d.name, {}).get(tax, {})
|
||||||
|
row += [item_tax.get("tax_rate", 0), item_tax.get("tax_amount", 0)]
|
||||||
|
total_tax += flt(item_tax.get("tax_amount"))
|
||||||
|
|
||||||
total_tax = sum(row[last_col:])
|
|
||||||
row += [total_tax, d.base_net_amount + total_tax, company_currency]
|
row += [total_tax, d.base_net_amount + total_tax, company_currency]
|
||||||
|
|
||||||
data.append(row)
|
data.append(row)
|
||||||
@ -120,54 +122,90 @@ def get_items(filters, additional_query_columns):
|
|||||||
order by si.posting_date desc, si_item.item_code desc
|
order by si.posting_date desc, si_item.item_code desc
|
||||||
""".format(additional_query_columns or '') % conditions, filters, as_dict=1)
|
""".format(additional_query_columns or '') % conditions, filters, as_dict=1)
|
||||||
|
|
||||||
def get_tax_accounts(item_list, columns):
|
def get_delivery_notes_against_sales_order(item_list):
|
||||||
|
so_dn_map = frappe._dict()
|
||||||
|
so_item_rows = list(set([d.so_detail for d in item_list]))
|
||||||
|
|
||||||
|
delivery_notes = frappe.db.sql("""
|
||||||
|
select parent, so_detail
|
||||||
|
from `tabDelivery Note Item`
|
||||||
|
where docstatus=1 and so_detail in (%s)
|
||||||
|
group by so_detail, parent
|
||||||
|
""" % (', '.join(['%s']*len(so_item_rows))), tuple(so_item_rows), as_dict=1)
|
||||||
|
|
||||||
|
for dn in delivery_notes:
|
||||||
|
so_dn_map.setdefault(dn.so_detail, []).append(dn.parent)
|
||||||
|
|
||||||
|
return so_dn_map
|
||||||
|
|
||||||
|
def get_tax_accounts(item_list, columns, tax_doctype="Sales Taxes and Charges"):
|
||||||
import json
|
import json
|
||||||
item_row_tax = {}
|
|
||||||
tax_accounts = []
|
|
||||||
invoice_item_row = {}
|
|
||||||
item_row_map = {}
|
item_row_map = {}
|
||||||
|
tax_columns = []
|
||||||
|
invoice_item_row = {}
|
||||||
|
itemised_tax = {}
|
||||||
for d in item_list:
|
for d in item_list:
|
||||||
invoice_item_row.setdefault(d.parent, []).append(d)
|
invoice_item_row.setdefault(d.parent, []).append(d)
|
||||||
item_row_map.setdefault(d.parent, {}).setdefault(d.item_code, []).append(d)
|
item_row_map.setdefault(d.parent, {}).setdefault(d.item_code, []).append(d)
|
||||||
|
|
||||||
tax_details = frappe.db.sql("""
|
tax_details = frappe.db.sql("""
|
||||||
select
|
select
|
||||||
parent, account_head, item_wise_tax_detail,
|
parent, description, item_wise_tax_detail,
|
||||||
charge_type, base_tax_amount_after_discount_amount
|
charge_type, base_tax_amount_after_discount_amount
|
||||||
from `tabSales Taxes and Charges`
|
from `tab%s`
|
||||||
where
|
where
|
||||||
parenttype = 'Sales Invoice' and docstatus = 1
|
parenttype = 'Sales Invoice' and docstatus = 1
|
||||||
and (account_head is not null and account_head != '')
|
and (description is not null and description != '')
|
||||||
and parent in (%s)
|
and parent in (%s)
|
||||||
""" % ', '.join(['%s']*len(invoice_item_row)), tuple(invoice_item_row.keys()))
|
order by description
|
||||||
|
""" % (tax_doctype, ', '.join(['%s']*len(invoice_item_row))), tuple(invoice_item_row.keys()))
|
||||||
|
|
||||||
for parent, account_head, item_wise_tax_detail, charge_type, tax_amount in tax_details:
|
for parent, description, item_wise_tax_detail, charge_type, tax_amount in tax_details:
|
||||||
if account_head not in tax_accounts:
|
if description not in tax_columns and tax_amount:
|
||||||
tax_accounts.append(account_head)
|
tax_columns.append(description)
|
||||||
|
|
||||||
if item_wise_tax_detail:
|
if item_wise_tax_detail:
|
||||||
try:
|
try:
|
||||||
item_wise_tax_detail = json.loads(item_wise_tax_detail)
|
item_wise_tax_detail = json.loads(item_wise_tax_detail)
|
||||||
|
|
||||||
for item_code, tax_amount in item_wise_tax_detail.items():
|
for item_code, tax_data in item_wise_tax_detail.items():
|
||||||
tax_amount = flt(tax_amount[1]) if isinstance(tax_amount, list) else flt(tax_amount)
|
itemised_tax.setdefault(item_code, frappe._dict())
|
||||||
|
|
||||||
|
if isinstance(tax_data, list):
|
||||||
|
tax_rate, tax_amount = tax_data
|
||||||
|
else:
|
||||||
|
tax_rate = tax_data
|
||||||
|
tax_amount = 0
|
||||||
|
|
||||||
|
if charge_type == "Actual" and not tax_rate:
|
||||||
|
tax_rate = "NA"
|
||||||
|
|
||||||
item_net_amount = sum([flt(d.base_net_amount)
|
item_net_amount = sum([flt(d.base_net_amount)
|
||||||
for d in item_row_map.get(parent, {}).get(item_code, [])])
|
for d in item_row_map.get(parent, {}).get(item_code, [])])
|
||||||
|
|
||||||
for d in item_row_map.get(parent, {}).get(item_code, []):
|
for d in item_row_map.get(parent, {}).get(item_code, []):
|
||||||
item_tax_amount = flt((tax_amount * d.base_net_amount) / item_net_amount) if item_net_amount else 0
|
item_tax_amount = flt((tax_amount * d.base_net_amount) / item_net_amount) \
|
||||||
item_row_tax.setdefault(d.name, {})[account_head] = item_tax_amount
|
if item_net_amount else 0
|
||||||
|
if item_tax_amount:
|
||||||
|
itemised_tax.setdefault(d.name, {})[description] = frappe._dict({
|
||||||
|
"tax_rate": tax_rate,
|
||||||
|
"tax_amount": item_tax_amount
|
||||||
|
})
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
elif charge_type == "Actual" and tax_amount:
|
elif charge_type == "Actual" and tax_amount:
|
||||||
for d in invoice_item_row.get(parent, []):
|
for d in invoice_item_row.get(parent, []):
|
||||||
item_row_tax.setdefault(d.name, {})[account_head] = \
|
itemised_tax.setdefault(d.name, {})[description] = frappe._dict({
|
||||||
flt((tax_amount * d.base_net_amount) / d.base_net_total)
|
"tax_rate": "NA",
|
||||||
|
"tax_amount": flt((tax_amount * d.base_net_amount) / d.base_net_total)
|
||||||
|
})
|
||||||
|
|
||||||
tax_accounts.sort()
|
tax_columns.sort()
|
||||||
columns += [account_head + ":Currency/currency:80" for account_head in tax_accounts]
|
for desc in tax_columns:
|
||||||
columns += ["Total Tax:Currency/currency:80", "Total:Currency/currency:80"]
|
columns.append(desc + " Rate:Data:80")
|
||||||
|
columns.append(desc + " Amount:Currency/currency:100")
|
||||||
|
|
||||||
return item_row_tax, tax_accounts
|
columns += ["Total Tax:Currency/currency:80", "Total:Currency/currency:100"]
|
||||||
|
|
||||||
|
return itemised_tax, tax_columns
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user