IF(a.warehousing_outbound_type = 1,c.code,m.code) as SAL_ORDER,
IF(a.warehousing_outbound_type = 1,d.code,q.code) as CUS_ID,
IF(a.warehousing_outbound_type = 1,d.name,q.name) as CUS_NAME,
i.cargo_name AS STORE_ID,
a.warehousing_time as OUT_DTM,
a.remark as REMARKS,
j.CODE AS MAT_ID,
j.NAME AS MAT_NAME,
aa.spec AS SPECIFICATION,
aa.outbound_unit AS UNIT,
aa.outbound_number AS OUT_QUANTITY,
'2' as OUT_FLAG,
s.contract_no as contract_no,
g.f_real_name as CREATE_EMP_ID,
a.f_creator_time as CREATE_DTM,
k.f_real_name as MODIFY_EMP_ID,
a.f_last_modify_time as MODIFY_DTM,
'0' as CANCLE_YN,
'1' as OUT_TYPE,
'N' as READ_FLAG,
null as READ_TIME,
'' as ERR_MESSAGE,
'N' as ESB_STATUS,
null as ESB_DTM,
'' as ESB_MESSAGE,
IF(a.warehousing_outbound_type = 1,LPAD(n.ord_ln,3,0),LPAD(o.ord_ln,3,0)) as SAL_LINE,
aa.batch_number as BATCH_ID,
concat(ab.code,'-',LPAD(ab.ord_ln,3,0)) as PUR_ORDER
FROM
jg_warehousing_outbound a
RIGHT JOIN jg_warehousing_outbound_product aa ON a.id = aa.warehousing_outbound_id AND aa.f_delete_mark is null
-- LEFT JOIN jg_warehousing_storage_product ab ON aa.batch_number = ab.batch_number AND ab.f_delete_mark is null
-- LEFT JOIN jg_warehousing_storage ac ON ab.warehousing_storage_id = ac.id AND ac.f_delete_mark is null
LEFT JOIN jg_business_batch ab ON aa.batch_number = ab.batch_number AND ab.f_delete_mark is null
LEFT JOIN jg_warehousing_notification b ON a.warehousing_id = b.id AND b.f_delete_mark IS NULL
LEFT JOIN jg_business_order c ON b.business_id = c.id AND c.f_delete_mark IS NULL
LEFT JOIN jg_subject_basic d ON c.first_subject_basic_id = d.id AND d.f_delete_mark IS NULL
LEFT JOIN base_organize e ON a.company_id = e.f_id AND e.f_delete_mark IS NULL
LEFT JOIN base_organize f ON a.department_id = f.f_id AND f.f_delete_mark IS NULL
LEFT JOIN base_user g ON a.f_creator_user_id = g.f_id AND g.f_delete_mark IS NULL
LEFT JOIN jg_contract h ON c.contract_id = h.id AND h.f_delete_mark IS NULL
LEFT JOIN jg_business_cargo i ON aa.outbound_area_id = i.id AND i.f_delete_mark IS NULL
LEFT JOIN jg_product j ON aa.product_id = j.id AND j.f_delete_mark is null
LEFT JOIN base_user k ON a.f_last_modify_user_id = k.f_id AND k.f_delete_mark IS NULL
LEFT JOIN jg_delivery_order l on a.warehousing_id = l.id and l.f_delete_mark is null
LEFT JOIN jg_business_order m on l.business_id = m.id and m.f_delete_mark is null
LEFT JOIN jg_business_order_product_relational n on c.id = n.business_order_id and n.f_delete_mark is null and aa.product_id = n.product_id and aa.outbound_unit = n.sales_main_unit_id and aa.ord_ln = n.ord_ln
LEFT JOIN jg_business_order_product_relational o on m.id = o.business_order_id and o.f_delete_mark is null and aa.product_id = o.product_id and aa.outbound_unit = o.sales_main_unit_id and aa.ord_ln = o.ord_ln
LEFT JOIN jg_contract p ON m.contract_id = p.id AND p.f_delete_mark IS NULL
LEFT JOIN jg_subject_basic q ON m.first_subject_basic_id = q.id AND q.f_delete_mark IS NULL
LEFT JOIN jg_business_order r ON a.business_order_no = r.code AND r.f_delete_mark is null
LEFT JOIN jg_contract s ON r.contract_id = s.id AND s.f_delete_mark is null