query+='SUM(CASE WHEN t2.idx BETWEEN '+str(id1)+' AND '+str(id2)+' THEN t2.percentage_allocation ELSE NULL END)'
id1+=6
id2+=6
ifi<1:
query+=','
elifperiod=='Quarterly':
id2=3
foriinrange(4):
query+='SUM(CASE WHEN t2.idx BETWEEN '+str(id1)+' AND '+str(id2)+' THEN t2.percentage_allocation ELSE NULL END)'
id1+=3
id2+=3
ifi<3:
query+=','
elifperiod=='Monthly':
foriinrange(12):
query+='SUM(CASE WHEN t2.idx ='+str(id1)+' THEN t2.percentage_allocation ELSE NULL END)'
id1+=1
ifi<11:
query+=','
# msgprint(query)
# Main Query
dist=sql("select %s from `tabBudget Distribution` t1, `tabBudget Distribution Detail` t2 where t1.name = '%s' and t2.parent = t1.name and t1.fiscal_year = '%s'"%(query,dist_id,fiscal_year))
dist=distanddist[0]or0
# msgprint(dist)
bug=[]
foriindist:
i=iandfloat(i)or0
bug.append(i)
# msgprint(bug)
returnbug
#============ function for appending target amt and actual amt in a proper order =======================
defappending_func(ran,tl,lst,actual,flt):
c=2
foriinrange(ran):
#==== for each itemgroup their actual amt is appended/inserted between target amt
iftl==0:
lst.insert(c,actualandflt(actual[0][i])or0)
#======== here actual amt is appended/inserted b/w target amt for a particular territory/sales person/sales partner only if target is not zero
#============ report display function =====================
forrinres:
query=''
grp=[]
list_range,count,ap,tot_target,tl=0,0,0,0,0
#============= ANNUAL REPORT ===================
ifperiod=='Annual':
tar_det=sql("select item_group, target_amount, distribution_id from `tabTarget Detail` where parent = %s and parenttype = %s and fiscal_year = %s",(r[col_idx['ID']],based_on,fiscal_year))
# msgprint(tar_det)
#================ Target based on individual item group ==============
ifgroup_by=='Item Group':
foriintar_det:
ifi[0]!='':
grp_target=iandflt(i[1])or0
igrp=[i[0],grp_target]
grp.append(igrp)
# msgprint(grp)
list_range+=1
count=3
#============== Total target(will be displayed only if target is specified by the user) ============
else:
foriintar_det:
# ======= here target is considered and not sum of target of item groups
ifi[0]=='':
tot_target=tar_detandflt(i[1])or0
# msgprint(tot_target)
#================== Actual Amount =============================================
actual=sql("select sum(t2.amount) from `tab%s` t1, `tab%s Detail` t2, `tabItem` t3 where t2.parent = t1.name and t1.%s = '%s' and t3.name = t2.item_code and t3.item_group = '%s' and t1.docstatus = 1 and t1.docstatus != 2 and %s"%(under,under_detail,based,r[col_idx['ID']],item_group,condition))
msgprint(actual)
actual=actualandflt(actual[0][0])or0
i.append(actual)
else:
actual=sql("select sum(net_total) from `tab%s` where %s = '%s' and docstatus = 1 and %s"%(under,based,r[col_idx['ID']],condition))
actual=actualandflt(actual[0][0])or0
elifbased_on=='Sales Person':
ifgroup_by=='Item Group':
foriingrp:
item_group=i[0]
actual=sql("select sum(t2.amount) from `tab%s` t1, `tab%s Detail` t2, `tabSales Team` t3, `tabItem` t4 where t2.parent = t1.name and t3.parent = t1.name and t3.%s = '%s' and t4.name = t2.item_code and t4.item_group = '%s' and t1.docstatus != 2 and t1.docstatus = 1 and %s"%(under,under_detail,based,r[col_idx['ID']],item_group,condition))
actual=actualandflt(actual[0][0])or0
# msgprint(actual)
i.append(actual)
else:
actual=sql("select sum(t1.net_total) from `tab%s` t1, `tabSales Team` t2 where t2.%s = '%s' and t2.parenttype='%s' and t1.docstatus != 2 and t2.parent = t1.name and %s"%(under,based,r[col_idx['ID']],under,condition))
tar_det=sql("select item_group, target_amount, distribution_id from `tabTarget Detail` where parent = %s and parenttype = %s and fiscal_year = %s",(r[col_idx['ID']],based_on,fiscal_year))
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabItem` t3 where t2.parent = t1.name and t1.%s = '%s' and t3.name = t2.item_code and t3.item_group = '%s' and t1.docstatus = 1 and t1.docstatus != 2 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
# msgprint(actual)
i=appending_func(2,tl,i,actual,flt)
else:
actual=sql("select %s from `tab%s` where %s = '%s' and docstatus = 1 and %s"%(query,under,based,r[col_idx['ID']],condition))
# msgprint(actual)
elifbased_on=='Sales Person':
ifgroup_by=='Item Group':
foriingrp:
item_group=i[0]
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabSales Team` t3, `tabItem` t4 where t2.parent = t1.name and t3.parent = t1.name and t3.%s = '%s' and t4.name = t2.item_code and t4.item_group = '%s' and t1.docstatus != 2 and t1.docstatus = 1 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
# msgprint(actual)
i=appending_func(2,tl,i,actual,flt)
else:
actual=sql("select %s from `tab%s` t1, `tabSales Team` t2 where t2.%s = '%s' and t2.parenttype='%s' and t1.docstatus != 2 and t2.parent = t1.name and %s"%(query,under,based,r[col_idx['ID']],under,condition))
tar_det=sql("select item_group, target_amount, distribution_id from `tabTarget Detail` where parent = %s and parenttype = %s and fiscal_year = %s",(r[col_idx['ID']],based_on,fiscal_year))
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabItem` t3 where t2.parent = t1.name and t1.%s = '%s' and t3.name = t2.item_code and t3.item_group = '%s' and t1.docstatus = 1 and t1.docstatus != 2 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
# msgprint(actual)
#================common function
i=appending_func(4,tl,i,actual,flt)
else:
actual=sql("select %s from `tab%s` where %s = '%s' and docstatus = 1 and %s"%(query,under,based,r[col_idx['ID']],condition))
# msgprint(actual)
elifbased_on=='Sales Person':
ifgroup_by=='Item Group':
foriingrp:
item_group=i[0]
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabSales Team` t3, `tabItem` t4 where t2.parent = t1.name and t3.parent = t1.name and t3.%s = '%s' and t4.name = t2.item_code and t4.item_group = '%s' and t1.docstatus != 2 and t1.docstatus = 1 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
# msgprint(actual)
i=appending_func(4,tl,i,actual,flt)
else:
actual=sql("select %s from `tab%s` t1, `tabSales Team` t2 where t2.%s = '%s' and t2.parenttype='%s' and t1.docstatus != 2 and t2.parent = t1.name and %s"%(query,under,based,r[col_idx['ID']],under,condition))
tar_det=sql("select item_group, target_amount, distribution_id from `tabTarget Detail` where parent = %s and parenttype = %s and fiscal_year = %s",(r[col_idx['ID']],based_on,fiscal_year))
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabItem` t3 where t2.parent = t1.name and t1.%s = '%s' and t3.name = t2.item_code and t3.item_group = '%s' and t1.docstatus = 1 and t1.docstatus != 2 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
actual=sql("select %s from `tab%s` where %s = '%s' and docstatus = 1 and %s"%(query,under,based,r[col_idx['ID']],condition))
# msgprint(actual)
elifbased_on=='Sales Person':
ifgroup_by=='Item Group':
foriingrp:
item_group=i[0]
actual=sql("select %s from `tab%s` t1, `tab%s Detail` t2, `tabSales Team` t3, `tabItem` t4 where t2.parent = t1.name and t3.parent = t1.name and t3.%s = '%s' and t4.name = t2.item_code and t4.item_group = '%s' and t1.docstatus != 2 and t1.docstatus = 1 and %s"%(query,under,under_detail,based,r[col_idx['ID']],item_group,condition))
# msgprint(actual)
i=appending_func(12,tl,i,actual,flt)
else:
actual=sql("select %s from `tab%s` t1, `tabSales Team` t2 where t2.%s = '%s' and t2.parenttype='%s' and t1.docstatus != 2 and t2.parent = t1.name and %s"%(query,under,based,r[col_idx['ID']],under,condition))
# msgprint(actual)
iftl==1:
r=appending_func(12,tl,r,actual,flt)
# msgprint(r)
#-------------DISPLAY OF TARGET vs ACTUAL ON BASIS OF TOTAL TARGET / ITEM GROUP
ifgroup_by=='Item Group':
forcolinrange(len(colnames)-1):# this would make all first row blank. just for look