$sql_w="SELECT TH1.* FROM(SELECT dod.dod_id as id,do.do_id as ids,do.do_inv as no,do.do_cus_code as code,dod.dod_pd_code as p_code,dod.dod_qty as we,do.do_date as dateia ,do.do_status,TH.*
FROM tb_delivery_order_details as dod INNER JOIN tb_delivery_order as do ON dod.do_id=do.do_id
LEFT JOIN (SELECT lod.id as lodid,lopp.lpp_inv_id,lod.pd_id,
CASE WHEN lod.lppd_weight_mom_start <> 0 && lod.lppd_weight_son_start <> 0
THEN lod.lppd_weight_son_start+lod.lppd_weight_mom_start
ELSE
CASE WHEN lod.lppd_weight_mom_start <> 0 && lod.lppd_weight_son_start=0
THEN lod.lppd_weight_mom_start
ELSE lod.lppd_weight_son_start END END as we_st,
CASE WHEN lod.lppd_weight_mom_end <> 0 && lod.lppd_weight_son_end <> 0
THEN lod.lppd_weight_son_end+lod.lppd_weight_mom_end
ELSE
CASE WHEN lod.lppd_weight_mom_end <> 0 && lod.lppd_weight_son_end=0
THEN lod.lppd_weight_mom_end
ELSE lod.lppd_weight_son_end END END as we_end
FROM tb_logistic_plan_detail as lod
INNER JOIN tb_logistic_plan_place as lopp ON lod.lpp_id=lopp.lpp_id
WHERE lod.lpp_id AND (lopp.lpp_date_start BETWEEN '".$sd."' AND '".$ed."') OR (lopp.lpp_date BETWEEN '".$sd."' AND '".$ed."') ORDER BY lod.lpp_id DESC) as TH
ON do.do_id=TH.lpp_inv_id AND dod.dod_pd_code=TH.pd_id
WHERE (do.do_date BETWEEN '".$sd."' AND '".$ed."') GROUP BY dod.dod_id,TH.lodid ORDER BY do.do_inv,do.do_date,dod.dod_id) as TH1 GROUP BY TH1.id";
$Qry_w=mysqli_query($mysqli,$sql_w);
while($rs_w=mysqli_fetch_array($Qry_w)){
$ws[$rs_w['id']][$rs_w['p_code']]=$rs_w['we_st'];
$we[$rs_w['id']][$rs_w['p_code']]=$rs_w['we_end'];}