(SELECT count(id) FROM logpm_distribution_bill_lading_scan ldbls WHERE ldbls.stock_list_id = lddd.stock_list_id and ldbls.material_type = 1) AS pick_up_quantity,
CASE
WHEN (lddd.quantity - (SELECT count(id) FROM logpm_distribution_bill_lading_scan ldbls WHERE ldbls.stock_list_id = lddd.stock_list_id and ldbls.material_type = 1)) > 0 THEN 20
WHEN (lddd.quantity - (SELECT count(id) FROM logpm_distribution_bill_lading_scan ldbls WHERE ldbls.stock_list_id = lddd.stock_list_id and ldbls.material_type = 1)) = 0 THEN 10
END AS pickup_status
from logpm_distribution_delivery_details lddd
LEFT JOIN logpm_distribution_stock_list ldsl on lddd.stock_list_id = ldsl.id
LEFT JOIN logpm_distribution_stock lds on lds.bill_lading_id = lddd.bill_lading_id and lddd.stock_list_id = lds.stock_list_id
WHERE
lddd.bill_lading_id = #{id} and lddd.inventory_status in (1,3)
(SELECT GROUP_CONCAT(material_name) FROM logpm_distribution_parcel_list WHERE is_deleted = 0 AND stock_article_id = ldsa.id GROUP BY stock_article_id) AS materialName,
CASE ldsa.is_zero
WHEN 0 THEN
(select COUNT(id) from logpm_distrilbution_bill_package
where stock_article_id = ldsa.id and packet_bar_status in (1,3) and bill_lading_id = #{param.billLadingId} AND is_deleted = 0 )
ELSE
(select sum(quantity) from logpm_distribution_reservation_zero_package
where stock_article_id = ldsa.id and zero_package_status in (1,3) AND type =3 and reservation_id = #{param.billLadingId} AND is_deleted = 0 )
END handQuantity,
(select IFNULL(sum(quantity) ,0) from logpm_distribution_bill_lading_scan
where stock_article_id = ldsa.id AND bill_lading_id = #{param.billLadingId} AND is_deleted = 0 ) pick_up_quantity,
CASE
WHEN (SELECT COUNT(id) FROM logpm_distribution_bill_lading_scan WHERE stock_article_id = ldsa.id AND is_deleted = 0) - (SELECT COUNT(id) FROM logpm_distrilbution_bill_package WHERE stock_article_id = ldsa.id AND packet_bar_status IN (1,3) AND is_deleted = 0) = 0 THEN '20'
WHEN (SELECT COUNT(id) FROM logpm_distribution_bill_lading_scan WHERE stock_article_id = ldsa.id AND is_deleted = 0) - (SELECT COUNT(id) FROM logpm_distrilbution_bill_package WHERE stock_article_id = ldsa.id AND packet_bar_status IN (1,3) AND is_deleted = 0) > 0 THEN '10'
WHEN (SELECT COUNT(id) FROM logpm_distribution_bill_lading_scan WHERE stock_article_id = ldsa.id AND is_deleted = 0) - (SELECT COUNT(id) FROM logpm_distrilbution_bill_package WHERE stock_article_id = ldsa.id AND packet_bar_status IN (1,3) AND is_deleted = 0) < 0 THEN '30'