refactor(UAE VAT 201): replace cartesian product to inner joins

This commit is contained in:
Mohammad Hasnain 2020-10-28 23:33:56 +05:30
parent 2c00549dfe
commit 08846f6b11

View File

@ -9,7 +9,6 @@ def execute(filters=None):
columns = get_columns() columns = get_columns()
data, emirates, amounts_by_emirate = get_data(filters) data, emirates, amounts_by_emirate = get_data(filters)
chart = get_chart(emirates, amounts_by_emirate) chart = get_chart(emirates, amounts_by_emirate)
return columns, data, None, chart return columns, data, None, chart
def get_columns(): def get_columns():
@ -50,7 +49,6 @@ def get_data(filters = None):
append_vat_on_expenses(data, filters) append_vat_on_expenses(data, filters)
return data, emirates, amounts_by_emirate return data, emirates, amounts_by_emirate
def get_chart(emirates, amounts_by_emirate): def get_chart(emirates, amounts_by_emirate):
"""Returns chart data.""" """Returns chart data."""
labels = [] labels = []
@ -167,16 +165,6 @@ def get_emirates():
'Fujairah' 'Fujairah'
] ]
def get_conditions(filters):
"""The conditions to be used to filter data to calculate the total sale."""
conditions = ""
for opts in (("company", " and company=%(company)s"),
("from_date", " and posting_date>=%(from_date)s"),
("to_date", " and posting_date<=%(to_date)s")):
if filters.get(opts[0]):
conditions += opts[1]
return conditions
def get_filters(filters): def get_filters(filters):
"""The conditions to be used to filter data to calculate the total sale.""" """The conditions to be used to filter data to calculate the total sale."""
query_filters = {} query_filters = {}
@ -215,16 +203,6 @@ def get_reverse_charge_tax(filters):
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
def get_conditions_join(filters):
"""The conditions to be used to filter data to calculate the total vat."""
conditions = ""
for opts in (("company", " and `tabPurchase Invoice`.company=%(company)s"),
("from_date", " and `tabPurchase Invoice`.posting_date>=%(from_date)s"),
("to_date", " and `tabPurchase Invoice`.posting_date<=%(to_date)s")):
if filters.get(opts[0]):
conditions += opts[1]
return conditions
def get_reverse_charge_recoverable_total(filters): def get_reverse_charge_recoverable_total(filters):
"""Returns the sum of the total of each Purchase invoice made with recoverable reverse charge.""" """Returns the sum of the total of each Purchase invoice made with recoverable reverse charge."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
@ -254,6 +232,16 @@ def get_reverse_charge_recoverable_tax(filters):
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
def get_conditions_join(filters):
"""The conditions to be used to filter data to calculate the total vat."""
conditions = ""
for opts in (("company", " and `tabPurchase Invoice`.company=%(company)s"),
("from_date", " and `tabPurchase Invoice`.posting_date>=%(from_date)s"),
("to_date", " and `tabPurchase Invoice`.posting_date<=%(to_date)s")):
if filters.get(opts[0]):
conditions += opts[1]
return conditions
def get_standard_rated_expenses_total(filters): def get_standard_rated_expenses_total(filters):
"""Returns the sum of the total of each Purchase invoice made with recoverable reverse charge.""" """Returns the sum of the total of each Purchase invoice made with recoverable reverse charge."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
@ -307,8 +295,9 @@ def get_zero_rated_total(filters):
conditions = get_conditions(filters) conditions = get_conditions(filters)
return frappe.db.sql(""" return frappe.db.sql("""
select sum(i.base_amount) as total from select sum(i.base_amount) as total from
`tabSales Invoice Item` i, `tabSales Invoice` s `tabSales Invoice Item` i inner join `tabSales Invoice` s
where s.docstatus = 1 and i.parent = s.name and i.is_zero_rated = 1 on i.parent = s.name
where s.docstatus = 1 and i.is_zero_rated = 1
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
@ -317,7 +306,18 @@ def get_exempt_total(filters):
conditions = get_conditions(filters) conditions = get_conditions(filters)
return frappe.db.sql(""" return frappe.db.sql("""
select sum(i.base_amount) as total from select sum(i.base_amount) as total from
`tabSales Invoice Item` i, `tabSales Invoice` s `tabSales Invoice Item` i inner join `tabSales Invoice` s
where s.docstatus = 1 and i.parent = s.name and i.is_exempt = 1 on i.parent = s.name
where s.docstatus = 1 and i.is_exempt = 1
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
def get_conditions(filters):
"""The conditions to be used to filter data to calculate the total sale."""
conditions = ""
for opts in (("company", " and company=%(company)s"),
("from_date", " and posting_date>=%(from_date)s"),
("to_date", " and posting_date<=%(to_date)s")):
if filters.get(opts[0]):
conditions += opts[1]
return conditions