refactor(UAE VAT 201): Use frappe api instead sql

This commit is contained in:
Mohammad Hasnain 2020-10-28 22:54:56 +05:30
parent 6e9e6d2491
commit 2c00549dfe

View File

@ -146,11 +146,14 @@ def append_data(data, no, legend, amount, vat_amount):
def get_total_emiratewise(filters): def get_total_emiratewise(filters):
"""Returns Emiratewise Amount and Taxes.""" """Returns Emiratewise Amount and Taxes."""
return frappe.db.sql(f""" query_filters = get_filters(filters)
select vat_emirate as emirate, sum(total), sum(total_taxes_and_charges) from `tabSales Invoice` query_filters['docstatus'] = ['=', 1]
where docstatus = 1 {get_conditions(filters)} return frappe.db.get_list('Sales Invoice',
group by `tabSales Invoice`.vat_emirate; filters = query_filters,
""", filters) fields = ['vat_emirate as emirate','sum(total)', 'sum(total_taxes_and_charges)'],
group_by='vat_emirate',
as_list=True
)
def get_emirates(): def get_emirates():
"""Returns a List of emirates in the order that they are to be displayed.""" """Returns a List of emirates in the order that they are to be displayed."""
@ -174,28 +177,40 @@ def get_conditions(filters):
conditions += opts[1] conditions += opts[1]
return conditions return conditions
def get_filters(filters):
"""The conditions to be used to filter data to calculate the total sale."""
query_filters = {}
if filters.get("company"):
query_filters["company"] = ['=', filters['company']]
if filters.get("from_date"):
query_filters["posting_date"] = ['>=', filters['from_date']]
if filters.get("from_date"):
query_filters["posting_date"] = ['<=', filters['to_date']]
return query_filters
def get_reverse_charge_total(filters): def get_reverse_charge_total(filters):
"""Returns the sum of the total of each Purchase invoice made.""" """Returns the sum of the total of each Purchase invoice made."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['reverse_charge'] = ['=', 'Y']
select sum(total) from query_filters['docstatus'] = ['=', 1]
`tabPurchase Invoice` return frappe.db.get_list('Purchase Invoice',
where filters = query_filters,
reverse_charge = "Y" fields = ['sum(total)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_reverse_charge_tax(filters): def get_reverse_charge_tax(filters):
"""Returns the sum of the tax of each Purchase invoice made.""" """Returns the sum of the tax of each Purchase invoice made."""
conditions = get_conditions_join(filters) conditions = get_conditions_join(filters)
return frappe.db.sql(""" return frappe.db.sql("""
select sum(debit) from select sum(debit) from
`tabPurchase Invoice` inner join `tabGL Entry` `tabPurchase Invoice` p inner join `tabGL Entry` gl
on `tabGL Entry`.voucher_no = `tabPurchase Invoice`.name on gl.voucher_no = p.name
where where
`tabPurchase Invoice`.reverse_charge = "Y" p.reverse_charge = "Y"
and `tabPurchase Invoice`.docstatus = 1 and p.docstatus = 1
and `tabGL Entry`.docstatus = 1 and gl.docstatus = 1
and account in (select account from `tabUAE VAT Account` where parent=%(company)s) and account in (select account from `tabUAE VAT Account` where parent=%(company)s)
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
@ -212,75 +227,80 @@ def get_conditions_join(filters):
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."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['reverse_charge'] = ['=', 'Y']
select sum(total) from query_filters['recoverable_reverse_charge'] = ['>', '0']
`tabPurchase Invoice` query_filters['docstatus'] = ['=', 1]
where return frappe.db.get_list('Purchase Invoice',
reverse_charge = "Y" filters = query_filters,
and recoverable_reverse_charge > 0 fields = ['sum(total)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_reverse_charge_recoverable_tax(filters): def get_reverse_charge_recoverable_tax(filters):
"""Returns the sum of the tax of each Purchase invoice made.""" """Returns the sum of the tax of each Purchase invoice made."""
conditions = get_conditions_join(filters) conditions = get_conditions_join(filters)
return frappe.db.sql(""" return frappe.db.sql("""
select sum(debit * `tabPurchase Invoice`.recoverable_reverse_charge / 100) from select sum(debit * p.recoverable_reverse_charge / 100) from
`tabPurchase Invoice` inner join `tabGL Entry` `tabPurchase Invoice` p inner join `tabGL Entry` gl
on `tabGL Entry`.voucher_no = `tabPurchase Invoice`.name on gl.voucher_no = p.name
where where
`tabPurchase Invoice`.reverse_charge = "Y" p.reverse_charge = "Y"
and `tabPurchase Invoice`.docstatus = 1 and p.docstatus = 1
and `tabPurchase Invoice`.recoverable_reverse_charge > 0 and p.recoverable_reverse_charge > 0
and `tabGL Entry`.docstatus = 1 and gl.docstatus = 1
and account in (select account from `tabUAE VAT Account` where parent=%(company)s) and account in (select account from `tabUAE VAT Account` where parent=%(company)s)
{where_conditions} ; {where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0 """.format(where_conditions=conditions), filters)[0][0] or 0
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."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['recoverable_standard_rated_expenses'] = ['>', 0]
select sum(total) from query_filters['docstatus'] = ['=', 1]
`tabPurchase Invoice` return frappe.db.get_list('Purchase Invoice',
where filters = query_filters,
recoverable_standard_rated_expenses > 0 fields = ['sum(total)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_standard_rated_expenses_tax(filters): def get_standard_rated_expenses_tax(filters):
"""Returns the sum of the tax of each Purchase invoice made.""" """Returns the sum of the tax of each Purchase invoice made."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['recoverable_standard_rated_expenses'] = ['>', 0]
select sum(recoverable_standard_rated_expenses) from query_filters['docstatus'] = ['=', 1]
`tabPurchase Invoice` return frappe.db.get_list('Purchase Invoice',
where filters = query_filters,
recoverable_standard_rated_expenses > 0 fields = ['sum(recoverable_standard_rated_expenses)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_tourist_tax_return_total(filters): def get_tourist_tax_return_total(filters):
"""Returns the sum of the total of each Sales invoice with non zero tourist_tax_return.""" """Returns the sum of the total of each Sales invoice with non zero tourist_tax_return."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['tourist_tax_return'] = ['>', 0]
select sum(total) from query_filters['docstatus'] = ['=', 1]
`tabSales Invoice` return frappe.db.get_list('Sales Invoice',
where filters = query_filters,
tourist_tax_return > 0 fields = ['sum(total)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_tourist_tax_return_tax(filters): def get_tourist_tax_return_tax(filters):
"""Returns the sum of the tax of each Sales invoice with non zero tourist_tax_return.""" """Returns the sum of the tax of each Sales invoice with non zero tourist_tax_return."""
conditions = get_conditions(filters) query_filters = get_filters(filters)
return frappe.db.sql(""" query_filters['tourist_tax_return'] = ['>', 0]
select sum(tourist_tax_return) from query_filters['docstatus'] = ['=', 1]
`tabSales Invoice` return frappe.db.get_list('Sales Invoice',
where filters = query_filters,
tourist_tax_return > 0 fields = ['sum(tourist_tax_return)'],
and docstatus = 1 {where_conditions} ; as_list=True,
""".format(where_conditions=conditions), filters)[0][0] or 0 limit = 1
)[0][0] or 0
def get_zero_rated_total(filters): def get_zero_rated_total(filters):
"""Returns the sum of each Sales Invoice Item Amount which is zero rated.""" """Returns the sum of each Sales Invoice Item Amount which is zero rated."""