item_groups=sql("select distinct item_group from `tabSerial No` where territory = '%s' and item_group like '%%%s'"%(cc,filter_values.get('item_group')))
forcolinrange(len(colnames)-1):# this would make all first row blank. just for look
r.append('')
out.append(r)
# Add Totals for each Territory
# -----------------------------
det=sql("select COUNT(CASE WHEN amc_expiry_date > '%s' THEN name ELSE NULL END), COUNT(CASE WHEN amc_expiry_date <= '%s' THEN name ELSE NULL END), COUNT(CASE WHEN warranty_expiry_date > '%s' THEN name ELSE NULL END), COUNT(CASE WHEN warranty_expiry_date <= '%s' THEN name ELSE NULL END) from `tabSerial No` where territory = '%s' and item_group like '%%%s'"%(nowdate,nowdate,nowdate,nowdate,cc,filter_values.get('item_group')))
br_det=sql("select COUNT(CASE WHEN amc_expiry_date > '%s' THEN name ELSE NULL END), COUNT(CASE WHEN amc_expiry_date <= '%s' THEN name ELSE NULL END), COUNT(CASE WHEN warranty_expiry_date > '%s' THEN name ELSE NULL END), COUNT(CASE WHEN warranty_expiry_date <= '%s' THEN name ELSE NULL END) from `tabSerial No` where territory = '%s' and item_group = '%s'"%(nowdate,nowdate,nowdate,nowdate,cc,br[0]))
t_row=[''foriinrange(len(colnames))]
t_row[col_idx['Item Group']]=br[0]
t_row[col_idx['Out of AMC']]=cint(br_det[0][0])
t_row[col_idx['Under AMC']]=cint(br_det[0][1])
t_row[col_idx['Out of Warranty']]=cint(br_det[0][2])