Refactor(UAE VAT 201): Fix fomatiing

This commit is contained in:
Mohammad Hasnain 2020-11-02 12:47:10 +05:30
parent c53237f5e0
commit b0eca23485
3 changed files with 201 additions and 232 deletions

View File

@ -8,8 +8,8 @@ from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sal
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
from erpnext.stock.doctype.warehouse.test_warehouse import get_warehouse_account from erpnext.stock.doctype.warehouse.test_warehouse import get_warehouse_account
from erpnext.regional.report.uae_vat_201.uae_vat_201 import ( from erpnext.regional.report.uae_vat_201.uae_vat_201 import (
get_total_emiratewise, get_total_emiratewise,
get_tourist_tax_return_total, get_tourist_tax_return_total,
get_tourist_tax_return_tax, get_tourist_tax_return_tax,
get_zero_rated_total, get_zero_rated_total,
get_exempt_total, get_exempt_total,
@ -26,7 +26,6 @@ class TestUaeVat201(TestCase):
frappe.db.sql("delete from `tabSales Invoice` where company='_Test Company UAE VAT'") frappe.db.sql("delete from `tabSales Invoice` where company='_Test Company UAE VAT'")
frappe.db.sql("delete from `tabPurchase Invoice` where company='_Test Company UAE VAT'") frappe.db.sql("delete from `tabPurchase Invoice` where company='_Test Company UAE VAT'")
make_company("_Test Company UAE VAT", "_TCUV") make_company("_Test Company UAE VAT", "_TCUV")
set_vat_accounts() set_vat_accounts()
@ -79,7 +78,6 @@ def make_company(company_name, abbr):
else: else:
company = frappe.get_doc("Company", company_name) company = frappe.get_doc("Company", company_name)
# indempotent
company.create_default_warehouses() company.create_default_warehouses()
if not frappe.db.get_value("Cost Center", {"is_group": 0, "company": company.name}): if not frappe.db.get_value("Cost Center", {"is_group": 0, "company": company.name}):
@ -95,7 +93,7 @@ def set_vat_accounts():
fields=["name"], fields=["name"],
filters = { filters = {
"company": "_Test Company UAE VAT", "company": "_Test Company UAE VAT",
"is_group": 0, "is_group": 0,
"account_type": "Tax" "account_type": "Tax"
} }
) )
@ -103,7 +101,7 @@ def set_vat_accounts():
uae_vat_accounts = [] uae_vat_accounts = []
for account in vat_accounts: for account in vat_accounts:
uae_vat_accounts.append({ uae_vat_accounts.append({
"doctype": "UAE VAT Account", "doctype": "UAE VAT Account",
"account": account.name "account": account.name
}) })
@ -125,7 +123,6 @@ def make_customer():
customer = frappe.get_doc("Customer", "_Test UAE Customer") customer = frappe.get_doc("Customer", "_Test UAE Customer")
def make_supplier(): def make_supplier():
if not frappe.db.exists("Supplier", "_Test UAE Supplier"): if not frappe.db.exists("Supplier", "_Test UAE Supplier"):
frappe.get_doc({ frappe.get_doc({
"supplier_group": "Local", "supplier_group": "Local",
@ -140,15 +137,15 @@ def create_warehouse(warehouse_name, properties=None, company=None):
warehouse_id = erpnext.encode_company_abbr(warehouse_name, company) warehouse_id = erpnext.encode_company_abbr(warehouse_name, company)
if not frappe.db.exists("Warehouse", warehouse_id): if not frappe.db.exists("Warehouse", warehouse_id):
w = frappe.new_doc("Warehouse") warehouse = frappe.new_doc("Warehouse")
w.warehouse_name = warehouse_name warehouse.warehouse_name = warehouse_name
w.parent_warehouse = "All Warehouses - _TCUV" warehouse.parent_warehouse = "All Warehouses - _TCUV"
w.company = company warehouse.company = company
w.account = get_warehouse_account(warehouse_name, company) warehouse.account = get_warehouse_account(warehouse_name, company)
if properties: if properties:
w.update(properties) warehouse.update(properties)
w.save() warehouse.save()
return w.name return warehouse.name
else: else:
return warehouse_id return warehouse_id
@ -166,13 +163,15 @@ def make_item(item_code, properties=None):
if properties: if properties:
item.update(properties) item.update(properties)
item.insert() item.insert()
return item return item
def make_sales_invoices(): def make_sales_invoices():
si = create_sales_invoice(company="_Test Company UAE VAT", def make_sales_invoices_wrapper(emirate, item, tax = True, tourist_tax= False):
si = create_sales_invoice(
company="_Test Company UAE VAT",
customer = '_Test UAE Customer', customer = '_Test UAE Customer',
currency = 'AED', currency = 'AED',
warehouse = 'Finished Goods - _TCUV', warehouse = 'Finished Goods - _TCUV',
@ -180,119 +179,60 @@ def make_sales_invoices():
income_account = 'Sales - _TCUV', income_account = 'Sales - _TCUV',
expense_account = 'Cost of Goods Sold - _TCUV', expense_account = 'Cost of Goods Sold - _TCUV',
cost_center = 'Main - _TCUV', cost_center = 'Main - _TCUV',
sales_taxes_and_charges_template = "UAE VAT 5% - _TCUV", item = item,
item = "_Test UAE VAT Item",
do_not_save=1 do_not_save=1
) )
si.append("taxes", { si.vat_emirate = emirate
"charge_type": "On Net Total", if tax:
"account_head": "VAT 5% - _TCUV", si.append(
"cost_center": "Main - _TCUV", "taxes", {
"description": "VAT 5% @ 5.0", "charge_type": "On Net Total",
"rate": 5.0 "account_head": "VAT 5% - _TCUV",
}) "cost_center": "Main - _TCUV",
si.vat_emirate = 'Dubai' "description": "VAT 5% @ 5.0",
si.submit() "rate": 5.0
}
)
if tourist_tax:
si.tourist_tax_return = 2
si.submit()
si = create_sales_invoice(company="_Test Company UAE VAT", #Define Item Names
customer = '_Test UAE Customer', uae_item = "_Test UAE VAT Item"
currency = 'AED', uae_exempt_item = "_Test UAE VAT Exempt Item"
warehouse = 'Finished Goods - _TCUV', uae_zero_rated_item = "_Test UAE VAT Zero Rated Item"
debit_to = 'Debtors - _TCUV',
income_account = 'Sales - _TCUV',
expense_account = 'Cost of Goods Sold - _TCUV',
cost_center = 'Main - _TCUV',
sales_taxes_and_charges_template = "UAE VAT 5% - _TCUV",
item = "_Test UAE VAT Item",
do_not_save=1
)
si.vat_emirate = 'Sharjah'
si.append("taxes", {
"charge_type": "On Net Total",
"account_head": "VAT 5% - _TCUV",
"cost_center": "Main - _TCUV",
"description": "VAT 5% @ 5.0",
"rate": 5.0
})
si.submit()
si = create_sales_invoice(company="_Test Company UAE VAT", #Sales Invoice with standard rated expense in Dubai
customer = '_Test UAE Customer', make_sales_invoices_wrapper('Dubai', uae_item)
currency = 'AED', #Sales Invoice with standard rated expense in Sharjah
warehouse = 'Finished Goods - _TCUV', make_sales_invoices_wrapper('Sharjah', uae_item)
debit_to = 'Debtors - _TCUV', #Sales Invoice with Tourist Tax Return
income_account = 'Sales - _TCUV', make_sales_invoices_wrapper('Dubai', uae_item, True, True)
expense_account = 'Cost of Goods Sold - _TCUV', #Sales Invoice with Exempt Item
cost_center = 'Main - _TCUV', make_sales_invoices_wrapper('Sharjah', uae_exempt_item, False)
sales_taxes_and_charges_template = "UAE VAT 5% - _TCUV", #Sales Invoice with Zero Rated Item
item = "_Test UAE VAT Item", make_sales_invoices_wrapper('Sharjah', uae_zero_rated_item, False)
do_not_save=1
)
si.tourist_tax_return = 2
si.vat_emirate = 'Dubai'
si.append("taxes", {
"charge_type": "On Net Total",
"account_head": "VAT 5% - _TCUV",
"cost_center": "Main - _TCUV",
"description": "VAT 5% @ 5.0",
"rate": 5.0
})
si.submit()
si = create_sales_invoice(company="_Test Company UAE VAT",
customer = '_Test UAE Customer',
currency = 'AED',
warehouse = 'Finished Goods - _TCUV',
debit_to = 'Debtors - _TCUV',
income_account = 'Sales - _TCUV',
expense_account = 'Cost of Goods Sold - _TCUV',
cost_center = 'Main - _TCUV',
sales_taxes_and_charges_template = "UAE VAT 5% - _TCUV",
item = "_Test UAE VAT Zero Rated Item",
do_not_save=1
)
si.vat_emirate = 'Sharjah'
si.submit()
si = create_sales_invoice(company="_Test Company UAE VAT",
customer = '_Test UAE Customer',
currency = 'AED',
warehouse = 'Finished Goods - _TCUV',
debit_to = 'Debtors - _TCUV',
income_account = 'Sales - _TCUV',
expense_account = 'Cost of Goods Sold - _TCUV',
cost_center = 'Main - _TCUV',
sales_taxes_and_charges_template = "UAE VAT 5% - _TCUV",
item = "_Test UAE VAT Exempt Item",
do_not_save=1
)
si.vat_emirate = 'Sharjah'
si.submit()
def create_purchase_invoices(): def create_purchase_invoices():
pi = make_purchase_invoice( pi = make_purchase_invoice(
company="_Test Company UAE VAT", company="_Test Company UAE VAT",
supplier = '_Test UAE Supplier', supplier = '_Test UAE Supplier',
supplier_warehouse = '_Test UAE VAT Supplier Warehouse - _TCUV', supplier_warehouse = '_Test UAE VAT Supplier Warehouse - _TCUV',
warehouse = '_Test UAE VAT Supplier Warehouse - _TCUV', warehouse = '_Test UAE VAT Supplier Warehouse - _TCUV',
currency = 'AED', currency = 'AED',
cost_center = 'Main - _TCUV', cost_center = 'Main - _TCUV',
expense_account = 'Cost of Goods Sold - _TCUV', expense_account = 'Cost of Goods Sold - _TCUV',
item = "_Test UAE VAT Item", item = "_Test UAE VAT Item",
do_not_save=1, do_not_save=1,
uom = "Nos" uom = "Nos"
) )
pi.append("taxes", { pi.append("taxes", {
"charge_type": "On Net Total", "charge_type": "On Net Total",
"account_head": "VAT 5% - _TCUV", "account_head": "VAT 5% - _TCUV",
"cost_center": "Main - _TCUV", "cost_center": "Main - _TCUV",
"description": "VAT 5% @ 5.0", "description": "VAT 5% @ 5.0",
"rate": 5.0 "rate": 5.0
}) })
pi.recoverable_standard_rated_expenses = 1 pi.recoverable_standard_rated_expenses = 1

View File

@ -54,11 +54,11 @@ def get_chart(emirates, amounts_by_emirate):
labels = [] labels = []
amount = [] amount = []
vat_amount = [] vat_amount = []
for d in emirates: for emirate in emirates:
if d in amounts_by_emirate: if emirate in amounts_by_emirate:
amount.append(amounts_by_emirate[d]["raw_amount"]) amount.append(amounts_by_emirate[emirate]["raw_amount"])
vat_amount.append(amounts_by_emirate[d]["raw_vat_amount"]) vat_amount.append(amounts_by_emirate[emirate]["raw_vat_amount"])
labels.append(d) labels.append(emirate)
datasets = [] datasets = []
datasets.append({'name': _('Amount (AED)'), 'values': amount}) datasets.append({'name': _('Amount (AED)'), 'values': amount})
@ -81,7 +81,8 @@ def append_vat_on_sales(data, filters):
emirates, amounts_by_emirate = standard_rated_expenses_emiratewise(data, filters) emirates, amounts_by_emirate = standard_rated_expenses_emiratewise(data, filters)
append_data(data, '2', _('Tax Refunds provided to Tourists under the Tax Refunds for Tourists Scheme'), append_data(data, '2',
_('Tax Refunds provided to Tourists under the Tax Refunds for Tourists Scheme'),
frappe.format((-1) * get_tourist_tax_return_total(filters), 'Currency'), frappe.format((-1) * get_tourist_tax_return_total(filters), 'Currency'),
frappe.format((-1) * get_tourist_tax_return_tax(filters), 'Currency')) frappe.format((-1) * get_tourist_tax_return_tax(filters), 'Currency'))
@ -104,37 +105,26 @@ def standard_rated_expenses_emiratewise(data, filters):
total_emiratewise = get_total_emiratewise(filters) total_emiratewise = get_total_emiratewise(filters)
emirates = get_emirates() emirates = get_emirates()
amounts_by_emirate = {} amounts_by_emirate = {}
for d in total_emiratewise: for emirate, amount, vat in total_emiratewise:
emirate, amount, vat= d
amounts_by_emirate[emirate] = { amounts_by_emirate[emirate] = {
"legend": emirate, "legend": emirate,
"raw_amount": amount, "raw_amount": amount,
"raw_vat_amount": vat, "raw_vat_amount": vat,
"amount": frappe.format(amount, 'Currency'), "amount": frappe.format(amount, 'Currency'),
"vat_amount": frappe.format(vat, 'Currency'), "vat_amount": frappe.format(vat, 'Currency'),
} }
amounts_by_emirate = append_emiratewise_expenses(data, emirates, amounts_by_emirate) amounts_by_emirate = append_emiratewise_expenses(data, emirates, amounts_by_emirate)
for d, emirate in enumerate(emirates, 97):
if emirate in amounts_by_emirate:
amounts_by_emirate[emirate]["no"] = _('1{0}').format(chr(d))
amounts_by_emirate[emirate]["legend"] = _('Standard rated supplies in {0}').format(emirate)
data.append(amounts_by_emirate[emirate])
else:
append_data(data, _('1{0}').format(chr(d)),
_('Standard rated supplies in {0}').format(emirate),
frappe.format(0, 'Currency'), frappe.format(0, 'Currency'))
return emirates, amounts_by_emirate return emirates, amounts_by_emirate
def append_emiratewise_expenses(data, emirates, amounts_by_emirate): def append_emiratewise_expenses(data, emirates, amounts_by_emirate):
"""Append emiratewise standard rated expenses and vat.""" """Append emiratewise standard rated expenses and vat."""
for d, emirate in enumerate(emirates, 97): for no, emirate in enumerate(emirates, 97):
if emirate in amounts_by_emirate: if emirate in amounts_by_emirate:
amounts_by_emirate[emirate]["no"] = _('1{0}').format(chr(d)) amounts_by_emirate[emirate]["no"] = _('1{0}').format(chr(no))
amounts_by_emirate[emirate]["legend"] = _('Standard rated supplies in {0}').format(emirate) amounts_by_emirate[emirate]["legend"] = _('Standard rated supplies in {0}').format(emirate)
data.append(amounts_by_emirate[emirate]) data.append(amounts_by_emirate[emirate])
else: else:
append_data(data, _('1{0}').format(chr(d)), append_data(data, _('1{0}').format(chr(no)),
_('Standard rated supplies in {0}').format(emirate), _('Standard rated supplies in {0}').format(emirate),
frappe.format(0, 'Currency'), frappe.format(0, 'Currency')) frappe.format(0, 'Currency'), frappe.format(0, 'Currency'))
return amounts_by_emirate return amounts_by_emirate
@ -145,11 +135,9 @@ def append_vat_on_expenses(data, filters):
append_data(data, '9', _('Standard Rated Expenses'), append_data(data, '9', _('Standard Rated Expenses'),
frappe.format(get_standard_rated_expenses_total(filters), 'Currency'), frappe.format(get_standard_rated_expenses_total(filters), 'Currency'),
frappe.format(get_standard_rated_expenses_tax(filters), 'Currency')) frappe.format(get_standard_rated_expenses_tax(filters), 'Currency'))
append_data(data, '10', _('Supplies subject to the reverse charge provision'), append_data(data, '10', _('Supplies subject to the reverse charge provision'),
frappe.format(get_reverse_charge_recoverable_total(filters), 'Currency'), frappe.format(get_reverse_charge_recoverable_total(filters), 'Currency'),
frappe.format(get_reverse_charge_recoverable_tax(filters), 'Currency') frappe.format(get_reverse_charge_recoverable_tax(filters), 'Currency'))
)
def append_data(data, no, legend, amount, vat_amount): def append_data(data, no, legend, amount, vat_amount):
"""Returns data with appended value.""" """Returns data with appended value."""
@ -159,7 +147,7 @@ def get_total_emiratewise(filters):
"""Returns Emiratewise Amount and Taxes.""" """Returns Emiratewise Amount and Taxes."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Sales Invoice', return frappe.db.get_all('Sales Invoice',
filters = query_filters, filters = query_filters,
fields = ['vat_emirate as emirate','sum(total)', 'sum(total_taxes_and_charges)'], fields = ['vat_emirate as emirate','sum(total)', 'sum(total_taxes_and_charges)'],
group_by='vat_emirate', group_by='vat_emirate',
@ -194,26 +182,30 @@ def get_reverse_charge_total(filters):
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['reverse_charge'] = ['=', 'Y'] query_filters['reverse_charge'] = ['=', 'Y']
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Purchase Invoice', try:
filters = query_filters, return frappe.db.get_all('Purchase Invoice',
fields = ['sum(total)'], filters = query_filters,
as_list=True, fields = ['sum(total)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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` p inner join `tabGL Entry` gl `tabPurchase Invoice` p inner join `tabGL Entry` gl
on gl.voucher_no = p.name on
gl.voucher_no = p.name
where where
p.reverse_charge = "Y" p.reverse_charge = "Y"
and p.docstatus = 1 and p.docstatus = 1
and gl.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_reverse_charge_recoverable_total(filters): def get_reverse_charge_recoverable_total(filters):
@ -222,27 +214,33 @@ def get_reverse_charge_recoverable_total(filters):
query_filters['reverse_charge'] = ['=', 'Y'] query_filters['reverse_charge'] = ['=', 'Y']
query_filters['recoverable_reverse_charge'] = ['>', '0'] query_filters['recoverable_reverse_charge'] = ['>', '0']
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Purchase Invoice', try:
filters = query_filters, return frappe.db.get_all('Purchase Invoice',
fields = ['sum(total)'], filters = query_filters,
as_list=True, fields = ['sum(total)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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 * p.recoverable_reverse_charge / 100) from select
`tabPurchase Invoice` p inner join `tabGL Entry` gl sum(debit * p.recoverable_reverse_charge / 100)
on gl.voucher_no = p.name from
`tabPurchase Invoice` p inner join `tabGL Entry` gl
on
gl.voucher_no = p.name
where where
p.reverse_charge = "Y" p.reverse_charge = "Y"
and p.docstatus = 1 and p.docstatus = 1
and p.recoverable_reverse_charge > 0 and p.recoverable_reverse_charge > 0
and gl.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_conditions_join(filters): def get_conditions_join(filters):
@ -251,8 +249,8 @@ def get_conditions_join(filters):
for opts in (("company", " and p.company=%(company)s"), for opts in (("company", " and p.company=%(company)s"),
("from_date", " and p.posting_date>=%(from_date)s"), ("from_date", " and p.posting_date>=%(from_date)s"),
("to_date", " and p.posting_date<=%(to_date)s")): ("to_date", " and p.posting_date<=%(to_date)s")):
if filters.get(opts[0]): if filters.get(opts[0]):
conditions += opts[1] conditions += opts[1]
return conditions return conditions
def get_standard_rated_expenses_total(filters): def get_standard_rated_expenses_total(filters):
@ -260,77 +258,102 @@ def get_standard_rated_expenses_total(filters):
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['recoverable_standard_rated_expenses'] = ['>', 0] query_filters['recoverable_standard_rated_expenses'] = ['>', 0]
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Purchase Invoice', try:
filters = query_filters, return frappe.db.get_all('Purchase Invoice',
fields = ['sum(total)'], filters = query_filters,
as_list=True, fields = ['sum(total)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['recoverable_standard_rated_expenses'] = ['>', 0] query_filters['recoverable_standard_rated_expenses'] = ['>', 0]
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Purchase Invoice', try:
filters = query_filters, return frappe.db.get_all('Purchase Invoice',
fields = ['sum(recoverable_standard_rated_expenses)'], filters = query_filters,
as_list=True, fields = ['sum(recoverable_standard_rated_expenses)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['tourist_tax_return'] = ['>', 0] query_filters['tourist_tax_return'] = ['>', 0]
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Sales Invoice', try:
filters = query_filters, return frappe.db.get_all('Sales Invoice',
fields = ['sum(total)'], filters = query_filters,
as_list=True, fields = ['sum(total)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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."""
query_filters = get_filters(filters) query_filters = get_filters(filters)
query_filters['tourist_tax_return'] = ['>', 0] query_filters['tourist_tax_return'] = ['>', 0]
query_filters['docstatus'] = ['=', 1] query_filters['docstatus'] = ['=', 1]
return frappe.db.get_list('Sales Invoice', try:
filters = query_filters, return frappe.db.get_all('Sales Invoice',
fields = ['sum(tourist_tax_return)'], filters = query_filters,
as_list=True, fields = ['sum(tourist_tax_return)'],
limit = 1 as_list=True,
)[0][0] or 0 limit = 1
)[0][0] or 0
except (IndexError, TypeError):
return 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."""
conditions = get_conditions(filters) conditions = get_conditions(filters)
return frappe.db.sql(""" try:
select sum(i.base_amount) as total from return frappe.db.sql("""
`tabSales Invoice Item` i inner join `tabSales Invoice` s select
on i.parent = s.name sum(i.base_amount) as total
where s.docstatus = 1 and i.is_zero_rated = 1 from
{where_conditions} ; `tabSales Invoice Item` i inner join `tabSales Invoice` s
""".format(where_conditions=conditions), filters)[0][0] or 0 on
i.parent = s.name
where
s.docstatus = 1 and i.is_zero_rated = 1
{where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0
except (IndexError, TypeError):
return 0
def get_exempt_total(filters): def get_exempt_total(filters):
"""Returns the sum of each Sales Invoice Item Amount which is Vat Exempt.""" """Returns the sum of each Sales Invoice Item Amount which is Vat Exempt."""
conditions = get_conditions(filters) conditions = get_conditions(filters)
return frappe.db.sql(""" try:
select sum(i.base_amount) as total from return frappe.db.sql("""
`tabSales Invoice Item` i inner join `tabSales Invoice` s select
on i.parent = s.name sum(i.base_amount) as total
where s.docstatus = 1 and i.is_exempt = 1 from
{where_conditions} ; `tabSales Invoice Item` i inner join `tabSales Invoice` s
""".format(where_conditions=conditions), filters)[0][0] or 0 on
i.parent = s.name
where
s.docstatus = 1 and i.is_exempt = 1
{where_conditions} ;
""".format(where_conditions=conditions), filters)[0][0] or 0
except (IndexError, TypeError):
return 0
def get_conditions(filters): def get_conditions(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."""
conditions = "" conditions = ""
for opts in (("company", " and company=%(company)s"), for opts in (("company", " and company=%(company)s"),
("from_date", " and posting_date>=%(from_date)s"), ("from_date", " and posting_date>=%(from_date)s"),
("to_date", " and posting_date<=%(to_date)s")): ("to_date", " and posting_date<=%(to_date)s")):
if filters.get(opts[0]): if filters.get(opts[0]):
conditions += opts[1] conditions += opts[1]
return conditions return conditions

View File

@ -35,7 +35,12 @@ def get_account_currency(account):
if not account: if not account:
return return
def generator(): def generator():
account_currency, company = frappe.get_cached_value("Account", account, ["account_currency", "company"]) account_currency, company = frappe.get_cached_value(
"Account",
account,
["account_currency",
"company"]
)
if not account_currency: if not account_currency:
account_currency = frappe.get_cached_value('Company', company, "default_currency") account_currency = frappe.get_cached_value('Company', company, "default_currency")
@ -53,8 +58,8 @@ def get_tax_accounts(company):
if not tax_accounts_list and not frappe.flags.in_test: if not tax_accounts_list and not frappe.flags.in_test:
frappe.throw(_('Please set Vat Accounts for Company: "{0}" in UAE VAT Settings').format(company)) frappe.throw(_('Please set Vat Accounts for Company: "{0}" in UAE VAT Settings').format(company))
for d in tax_accounts_list: for tax_account in tax_accounts_list:
for key, name in d.items(): for _, name in tax_account.items():
tax_accounts_dict[name] = name tax_accounts_dict[name] = name
return tax_accounts_dict return tax_accounts_dict
@ -131,8 +136,7 @@ def make_gl_entry(tax, gl_entries, doc, tax_accounts):
if flt(tax.base_tax_amount_after_discount_amount) and tax.account_head in tax_accounts: if flt(tax.base_tax_amount_after_discount_amount) and tax.account_head in tax_accounts:
account_currency = get_account_currency(tax.account_head) account_currency = get_account_currency(tax.account_head)
gl_entries.append(doc.get_gl_dict( gl_entries.append(doc.get_gl_dict({
{
"account": tax.account_head, "account": tax.account_head,
"cost_center": tax.cost_center, "cost_center": tax.cost_center,
"posting_date": doc.posting_date, "posting_date": doc.posting_date,
@ -141,8 +145,8 @@ def make_gl_entry(tax, gl_entries, doc, tax_accounts):
dr_or_cr + "_in_account_currency": tax.base_tax_amount_after_discount_amount \ dr_or_cr + "_in_account_currency": tax.base_tax_amount_after_discount_amount \
if account_currency==doc.company_currency \ if account_currency==doc.company_currency \
else tax.tax_amount_after_discount_amount else tax.tax_amount_after_discount_amount
}, account_currency, item=tax) }, account_currency, item=tax
) ))
return gl_entries return gl_entries
@ -152,4 +156,6 @@ def validate_returns(doc, method):
if country != 'United Arab Emirates': if country != 'United Arab Emirates':
return return
if doc.reverse_charge == 'Y' and flt(doc.recoverable_standard_rated_expenses) != 0: if doc.reverse_charge == 'Y' and flt(doc.recoverable_standard_rated_expenses) != 0:
frappe.throw(_("Recoverable Standard Rated expenses should not be set when Reverse Charge Applicable is Y")) frappe.throw(_(
"Recoverable Standard Rated expenses should not be set when Reverse Charge Applicable is Y"
))