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
"""select distinct account_head
from `tabPurchase Tax Detail` from `tabPurchase Tax Detail`
where parenttype = 'Payable Voucher' where parenttype = 'Payable Voucher'
and add_deduct_tax = 'Add' and add_deduct_tax = 'Add'
and category != 'For Valuation' and category in ('For Total', 'For Both')
order by account_head asc""" and docstatus=1
order by account_head asc""")]
tax_acc.append('Total Tax') tax_acc.append('Total Tax')
tax_acc.append('GrandTotal') tax_acc.append('GrandTotal')

View File

@ -1,8 +1,16 @@
# 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')
@ -26,18 +34,52 @@ cl = cl[:-2]
# add the values # add the values
for r in res: for r in res:
#Get amounts for income account
income_acc_list = sql("""select income_account, sum(amount)
from `tabRV Detail`
where parent = %s
and parenttype='Receivable Voucher'
group by income_account""", (r[col_idx['ID']],))
#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 net_total = 0
for i in income_acc: 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 = 0
val = flt(val and val[0][0] or 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 net_total += val
r.append(val) r.append(val)
r.append(net_total) 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 total_tax = 0
for c in cl: for c in cl:
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)) val = 0
val = flt(val and val[0][0] or 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 total_tax += val
r.append(val) r.append(val)
r.append(total_tax) r.append(total_tax)