Changed query to make it more efficient

This commit is contained in:
Anand Doshi 2011-06-27 16:30:29 +05:30
parent c0d6502c9f
commit f295f212b1
2 changed files with 82 additions and 41 deletions

View File

@ -14,14 +14,13 @@ for i in expense_acc:
coloptions.append('') coloptions.append('')
# Add tax head columns # Add tax head columns
tax_acc = [c[0] for c in sql("select distinct account_head from `tabPurchase Tax Detail` where parenttype='Payable Voucher' and category in ('For Total', 'For Both') and add_deduct_tax = 'Add' and docstatus=1 order by idx asc")] tax_acc = [c[0] for c in sql("""select distinct account_head
from `tabPurchase Tax Detail`
"""select distinct account_head where parenttype = 'Payable Voucher'
from `tabPurchase Tax Detail` and add_deduct_tax = 'Add'
where parenttype = 'Payable Voucher' and category in ('For Total', 'For Both')
and add_deduct_tax = 'Add' and docstatus=1
and category != 'For Valuation' order by account_head asc""")]
order by account_head asc"""
tax_acc.append('Total Tax') tax_acc.append('Total Tax')
tax_acc.append('GrandTotal') tax_acc.append('GrandTotal')
@ -69,11 +68,11 @@ for r in res:
#Get tax for account heads #Get tax for account heads
acc_head_tax = sql("""select account_head, tax_amount acc_head_tax = sql("""select account_head, tax_amount
from `tabPurchase Tax Detail` from `tabPurchase Tax Detail`
where parent = '%s' where parent = '%s'
and parenttype = 'Payable Voucher' and parenttype = 'Payable Voucher'
and add_deduct_tax = 'Add' and add_deduct_tax = 'Add'
and category in ('For Total', 'For Both')""" %(r[col_idx['ID']],)) and category in ('For Total', 'For Both')""" %(r[col_idx['ID']],))
#Convert the result to dictionary for easy retrieval #Convert the result to dictionary for easy retrieval
acc_head_tax_dict = {} acc_head_tax_dict = {}

View File

@ -1,24 +1,32 @@
# add additional columns # add additional columns
cl = [c[0] for c in sql("select distinct account_head from `tabRV Tax Detail` where parenttype='Receivable Voucher' and docstatus=1 order by idx asc")] cl = [c[0] for c in sql("""select distinct account_head
from `tabRV Tax Detail`
where parenttype='Receivable Voucher'
and docstatus=1
order by account_head asc""")]
income_acc = [c[0] for c in sql("select distinct income_account from `tabRV Detail` where parenttype='Receivable Voucher' and docstatus=1 order by idx asc")] income_acc = [c[0] for c in sql("""select distinct income_account
from `tabRV Detail`
where parenttype='Receivable Voucher'
and docstatus=1
order by income_account asc""")]
income_acc.append('Net Total') income_acc.append('Net Total')
for i in income_acc: for i in income_acc:
colnames.append(i) colnames.append(i)
coltypes.append('Currency') coltypes.append('Currency')
colwidths.append('100px') colwidths.append('100px')
coloptions.append('') coloptions.append('')
cl.append('Total Tax') cl.append('Total Tax')
cl.append('Grand Total') cl.append('Grand Total')
for c in cl: for c in cl:
colnames.append(c) colnames.append(c)
coltypes.append('Currency') coltypes.append('Currency')
colwidths.append('100px') colwidths.append('100px')
coloptions.append('') coloptions.append('')
income_acc = income_acc[:-1] income_acc = income_acc[:-1]
cl = cl[:-2] cl = cl[:-2]
@ -26,19 +34,53 @@ cl = cl[:-2]
# add the values # add the values
for r in res: for r in res:
net_total = 0
for i in income_acc:
val = sql("select sum(amount) from `tabRV Detail` where parent = %s and parenttype='Receivable Voucher' and income_account = %s", (r[col_idx['ID']], i))
val = flt(val and val[0][0] or 0)
net_total += val
r.append(val)
r.append(net_total)
total_tax = 0 #Get amounts for income account
for c in cl: income_acc_list = sql("""select income_account, sum(amount)
val = sql("select tax_amount from `tabRV Tax Detail` where parent = %s and parenttype='Receivable Voucher' and account_head = %s", (r[col_idx['ID']], c)) from `tabRV Detail`
val = flt(val and val[0][0] or 0) where parent = %s
total_tax += val and parenttype='Receivable Voucher'
r.append(val) group by income_account""", (r[col_idx['ID']],))
r.append(total_tax)
r.append(net_total+total_tax) #convert the result to dictionary for easy retrieval
income_acc_dict = {}
for ia in income_acc_list:
income_acc_dict[ia[0]] = ia[1]
income_acc_keys = income_acc_dict.keys()
net_total = 0
for i in income_acc:
val = 0
#check if income account exists in dict
if i in income_acc_keys:
val = income_acc_dict[i]
val = flt(val and val or 0)
net_total += val
r.append(val)
r.append(net_total)
#Get tax for account heads
acc_head_tax = sql("""select account_head, tax_amount
from `tabRV Tax Detail`
where parent = '%s'
and parenttype = 'Receivable Voucher'""" %(r[col_idx['ID']],))
#Convert the result to dictionary for easy retrieval
acc_head_tax_dict = {}
for a in acc_head_tax:
acc_head_tax_dict[a[0]] = a[1]
acc_head_keys = acc_head_tax_dict.keys()
total_tax = 0
for c in cl:
val = 0
#check if account head exists in dict
if c in acc_head_keys:
val = acc_head_tax_dict[c]
val = flt(val and val or 0)
total_tax += val
r.append(val)
r.append(total_tax)
r.append(net_total+total_tax)