select    
date(si.created_on) as Invoice_date_style_10_left
,si.code as Invoice_no_style_10_left
,srp.code  as Rep_code_style_12_left
,si.net_total as Invoice_amount_sum_2_style_15_right
,ifnull((
select group_concat(code separator ',')
from tbl_customer_returns
where sale_invoice_id = si.id
group by sale_invoice_id
),'') as Return_no_style_15_left
,   concat('(',  si.rpt_returns_amount ,')') as Return_amounts_sum_2_style_10_right
,(si.net_total+si.rpt_cash_returns) - (si.rpt_settled_amount + si.rpt_returns_amount) as To_pay_value_sum_2_style_15_right
,DATEDIFF(date(now()),date(si.created_on)) as Days_style_6_right
 
,case when (DATEDIFF(date(now()),date(si.created_on)) - 90) > 0 then  (90-DATEDIFF(date(now()),date(si.created_on)) )  else 0 end as Over_due_days_style_6_right

 
, ifnull(si.customer_id ,-1) as category_id
,concat(tbl_customers.code ,' - '  ,tbl_customers.customer_name,'   |  '  ,tbl_customers.address ) as category
from tbl_sale_invoice si
left join tbl_customers on tbl_customers.customer_id = si.customer_id
left join tbl_sales_rep srp on srp.id = si.repId
where (si.customer_id = {customer} or  {customer} =0 )
and date(si.created_on)  between '{date}' and  '{date1}'
and ( (si.net_total+si.rpt_cash_returns) - (si.rpt_settled_amount + si.rpt_returns_amount) ) > 0
and status = '4'
order by ifnull(si.customer_id ,-1)  , DATEDIFF(date(now()),date(si.created_on))  desc