select 
concat(p.code,' - ',p.name)  as Item_style_23_left
,sum(sip.qty) as Qty_sum_2_style_11_right
,round(sum(sip.costPrice)/sum(sip.qty),2) as AVG_Cost_price_style_10_right
,round(sum(sip.unit_price)/sum(sip.qty),2) as AVG_Unit_price_style_10_right
,sum(sip.discount) as Discount_sum_2_style_10_right
,sum(sip.net_total) as Net_total_sum_2_style_11_right
,sum(ifnull(rp.value,0.00)) as Returns_sum_2_style_10_right
,round(sum(sip.net_total - (sip.costPrice*sip.qty) - ifnull(rp.value,0.00)),2) as Profit_sum_2_style_10_right
,round((sum(sip.net_total - (sip.costPrice*sip.qty) - ifnull(rp.value,0.00))/sum(sip.costPrice)*100),2) as `%_sum_2_style_5_right`
from tbl_sale_invoice_products sip
inner join tbl_products p on p.product_id = sip.product_id
inner join tbl_sale_invoice si on si.id = sip.sale_invoice_id
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
left join tbl_return_products rp on rp.product_id = sip.product_id
and rp.invoice_id = sip.sale_invoice_id
where si.created_on between '{from_date}'  and  '{to_date}'
and (bil.branch_id = {branch_id} or {branch_id} = 0)
and (p.category_id = {item_id} or {item_id} = 0)
group by p.product_id
order by p.product_id