select 
sr.id as category_id
,concat(sr.code,' - ',sr.name) as category
,concat(b.Code,' - ',b.Name) as Branch_style_28_left
,si.code as Invoice_style_12_left
,date(si.created_on) as Date_style_12_left
,net_total as Net_total_sum_2_style_12_right
,rpt_returns_amount as Returns_sum_2_style_12_right
,rpt_settled_amount as Payments_sum_2_style_12_right
,(net_total-rpt_returns_amount-rpt_settled_amount+rpt_cash_returns) as Balance_sum_2_style_12_right
from tbl_sale_invoice si
inner join tbl_sales_rep sr on sr.id = si.repId
inner join tbl_branch_inventory_locations bil on bil.id = si.location_id
inner join tbl_branch b on b.branch_id = bil.branch_id
where si.status = 4
and si.created_on between '{from_date}'  and  '{to_date}'
and (bil.branch_id = {branch_id} or {branch_id} = 0)
and (si.repId = {rep_id} or {rep_id} = 0)
order by sr.id, b.branch_id