update stock_inventory
qualitity = qualitity + #{ew.qualitity},
version = version + 1,
remark = #{ew.remark},
warn_num = #{ew.warnNum},
locked_quantity = locked_quantity + #{ew.lockedQuantity},
update_time = now()
where product_model_id = #{ew.productModelId}
and batch_no is null
and batch_no = #{ew.batchNo}
update stock_inventory
qualitity = qualitity - #{ew.qualitity},
version = version + 1,
remark = #{ew.remark},
update_time = now()
where product_model_id = #{ew.productModelId} and qualitity >= #{ew.qualitity}
and batch_no is null
and batch_no = #{ew.batchNo}
select si.id,
si.qualitity,
COALESCE(si.locked_quantity, 0) as locked_quantity,
si.product_model_id,
si.create_time,
si.update_time,
COALESCE(si.warn_num, 0) as warn_num,
si.version,
(si.qualitity - COALESCE(si.locked_quantity, 0)) as un_locked_quantity,
pm.model,
si.remark,
pm.unit,
p.product_name
from stock_inventory si
left join product_model pm on si.product_model_id = pm.id
left join product p on pm.product_id = p.id
where 1 = 1
and p.product_name like concat('%',#{ew.productName},'%')
exists (
select 1
from stock_in_record sir_po
left join purchase_ledger pl_po_direct
on pl_po_direct.id = sir_po.record_id and sir_po.record_type = '7'
left join quality_inspect qi_po
on qi_po.id = sir_po.record_id and sir_po.record_type = '10'
left join purchase_ledger pl_po_inspect
on pl_po_inspect.id = qi_po.purchase_ledger_id
where sir_po.product_model_id = ${alias}.product_model_id
and (sir_po.batch_no = ${alias}.batch_no
or (sir_po.batch_no is null and ${alias}.batch_no is null))
and (pl_po_direct.purchase_contract_number like concat('%', #{ew.purchaseOrderNo}, '%')
or pl_po_inspect.purchase_contract_number like concat('%', #{ew.purchaseOrderNo}, '%'))
)
WITH RECURSIVE product_tree AS (
SELECT id
FROM product
WHERE id = #{ew.topParentProductId}
UNION ALL
SELECT p.id
FROM product p
INNER JOIN product_tree pt ON p.parent_id = pt.id
)
select
GROUP_CONCAT(DISTINCT batch_no ORDER BY batch_no SEPARATOR ',') as batch_no,
MAX(qualifiedId) as qualifiedId,
MAX(unQualifiedId) as unQualifiedId,
SUM(qualifiedQuantity) as qualifiedQuantity,
SUM(unQualifiedQuantity) as unQualifiedQuantity,
SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
SUM(qualifiedQuantity - qualifiedLockedQuantity - IFNULL(qualifiedPendingOut, 0)) as qualifiedUnLockedQuantity,
SUM(unQualifiedQuantity - unQualifiedLockedQuantity - IFNULL(unQualifiedPendingOut, 0)) as unQualifiedUnLockedQuantity,
SUM(IFNULL(qualifiedPendingOut, 0)) as qualifiedPendingOutQuantity,
SUM(IFNULL(unQualifiedPendingOut, 0)) as unQualifiedPendingOutQuantity,
product_model_id,
MAX(create_time) as create_time,
MAX(update_time) as update_time,
MAX(warn_num) as warn_num,
MAX(version) as version,
model,
MAX(remark) as remark,
unit,
product_name,
product_id,
'combined' as stockType,
(
select sir.oil_product
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as oil_product,
(
select sir.tank_inspection_status
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as tank_inspection_status,
(
select sir.unloading_crane_position
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as unloading_crane_position,
(
select sir.density
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as density,
(
select sir.unload_photos
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as unload_photos,
(
select sir.gross_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as gross_weight,
(
select sir.tare_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as tare_weight,
(
select sir.net_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as net_weight,
(
select sir.loss_quantity
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as loss_quantity,
(
select sir.actual_volume
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as actual_volume,
(
select sir.pound_photos
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
order by sir.id desc
limit 1
) as pound_photos
from (
select
si.batch_no,
si.id as qualifiedId,
null as unQualifiedId,
si.qualitity as qualifiedQuantity,
0 as unQualifiedQuantity,
COALESCE(si.locked_quantity, 0) as locked_quantity,
COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
0 as unQualifiedLockedQuantity,
si.product_model_id,
si.create_time,
si.update_time,
COALESCE(si.warn_num, 0) as warn_num,
si.version,
(si.qualitity - COALESCE(si.locked_quantity, 0)) as un_locked_quantity,
pm.model,
si.remark,
pm.unit,
p.product_name,
p.id as product_id,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = si.product_model_id
and (
(si.batch_no is null and sor.batch_no is null)
or si.batch_no = sor.batch_no
)
and sor.type = '0'
and sor.approval_status = 0
) as qualifiedPendingOut,
0 as unQualifiedPendingOut
from stock_inventory si
left join product_model pm on si.product_model_id = pm.id
left join product p on pm.product_id = p.id
union all
select
su.batch_no,
null as qualifiedId,
su.id as unQualifiedId,
0 as qualifiedQuantity,
su.qualitity as unQualifiedQuantity,
COALESCE(su.locked_quantity, 0) as locked_quantity,
0 as qualifiedLockedQuantity,
COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
su.product_model_id,
su.create_time,
su.update_time,
0 as warn_num,
su.version,
(su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
pm.model,
su.remark,
pm.unit,
p.product_name,
p.id as product_id,
0 as qualifiedPendingOut,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = su.product_model_id
and (
(su.batch_no is null and sor.batch_no is null)
or su.batch_no = sor.batch_no
)
and sor.type = '1'
and sor.approval_status = 0
) as unQualifiedPendingOut
from stock_uninventory su
left join product_model pm on su.product_model_id = pm.id
left join product p on pm.product_id = p.id
) as combined
and combined.product_name in (
select distinct p.product_name
from product p
left join product_model pm on p.id = pm.product_id
where p.product_name like concat('%',#{ew.productName},'%')
or pm.model like concat('%',#{ew.productName},'%')
)
and combined.product_id in (select id from product_tree)
and combined.model like concat('%',#{ew.model},'%')
and combined.batch_no like concat('%',#{ew.batchNo},'%')
and
group by
product_model_id,
model,
unit,
product_name,
product_id
order by create_time desc
WITH RECURSIVE product_tree AS (
SELECT id
FROM product
WHERE id = #{ew.topParentProductId}
UNION ALL
SELECT p.id
FROM product p
INNER JOIN product_tree pt ON p.parent_id = pt.id
)
select
batch_no,
MAX(qualifiedId) as qualifiedId,
MAX(unQualifiedId) as unQualifiedId,
SUM(qualifiedQuantity) as qualifiedQuantity,
SUM(unQualifiedQuantity) as unQualifiedQuantity,
SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
SUM(qualifiedQuantity - qualifiedLockedQuantity - IFNULL(qualifiedPendingOut, 0)) as qualifiedUnLockedQuantity,
SUM(unQualifiedQuantity - unQualifiedLockedQuantity - IFNULL(unQualifiedPendingOut, 0)) as unQualifiedUnLockedQuantity,
SUM(IFNULL(qualifiedPendingOut, 0)) as qualifiedPendingOutQuantity,
SUM(IFNULL(unQualifiedPendingOut, 0)) as unQualifiedPendingOutQuantity,
product_model_id,
MAX(create_time) as create_time,
MAX(update_time) as update_time,
MAX(warn_num) as warn_num,
MAX(version) as version,
model,
MAX(remark) as remark,
unit,
product_name,
product_id,
'combined' as stockType
from (
select
si.batch_no,
si.id as qualifiedId,
null as unQualifiedId,
si.qualitity as qualifiedQuantity,
0 as unQualifiedQuantity,
COALESCE(si.locked_quantity, 0) as locked_quantity,
COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
0 as unQualifiedLockedQuantity,
si.product_model_id,
si.create_time,
si.update_time,
COALESCE(si.warn_num, 0) as warn_num,
si.version,
(si.qualitity - COALESCE(si.locked_quantity, 0)) as un_locked_quantity,
pm.model,
si.remark,
pm.unit,
p.product_name,
p.id as product_id,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = si.product_model_id
and (si.batch_no is null and sor.batch_no is null or si.batch_no = sor.batch_no)
and sor.type = '0'
and sor.approval_status = 0
) as qualifiedPendingOut,
0 as unqualifiedPendingOut
from stock_inventory si
left join product_model pm on si.product_model_id = pm.id
left join product p on pm.product_id = p.id
union all
select
su.batch_no,
null as qualifiedId,
su.id as unQualifiedId,
0 as qualifiedQuantity,
su.qualitity as unQualifiedQuantity,
COALESCE(su.locked_quantity, 0) as locked_quantity,
0 as qualifiedLockedQuantity,
COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
su.product_model_id,
su.create_time,
su.update_time,
0 as warn_num,
su.version,
(su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
pm.model,
su.remark,
pm.unit,
p.product_name,
p.id as product_id,
0 as qualifiedPendingOut,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = su.product_model_id
and (su.batch_no is null and sor.batch_no is null or su.batch_no = sor.batch_no)
and sor.type = '1'
and sor.approval_status = 0
) as unqualifiedPendingOut
from stock_uninventory su
left join product_model pm on su.product_model_id = pm.id
left join product p on pm.product_id = p.id
) as combined
and combined.product_name in (
select distinct p.product_name
from product p
left join product_model pm on p.id = pm.product_id
where p.product_name like concat('%',#{ew.productName},'%') or pm.model like concat('%',#{ew.productName},'%')
)
and combined.product_id in (select id from product_tree)
group by batch_no, product_model_id, model, unit, product_name, product_id
select t.id,
t.inbound_batches,
t.outbound_batches,
t.batch_no,
t.stock_in_num,
t.stock_out_num,
t.record_type,
t.record_id,
t.product_model_id,
t.remark,
t.direction,
t.out_date,
t.create_time,
t.create_user,
t.type,
si.current_stock,
pm.model,
pm.unit,
p.product_name,
su.nick_name as create_by
from (
select sir.id,
sir.inbound_batches,
null as outbound_batches,
sir.batch_no,
sir.stock_in_num,
null as stock_out_num,
sir.record_type,
sir.record_id,
sir.product_model_id,
sir.remark,
'in' as direction,
null as out_date,
sir.create_time,
sir.create_user,
sir.type
from stock_in_record sir
and sir.create_time >= #{ew.reportDate}
and sir.create_time < DATE_ADD(#{ew.reportDate}, INTERVAL 1 DAY)
and sir.create_time >= #{ew.startMonth}
and sir.create_time < DATE_ADD(#{ew.endMonth}, INTERVAL 1 DAY)
and sir.create_time >= #{ew.startDate}
and sir.create_time < DATE_ADD(#{ew.endDate}, INTERVAL 1 DAY)
and (
exists (
select 1 from purchase_ledger pl_po
where pl_po.id = sir.record_id
and sir.record_type = '7'
and pl_po.purchase_contract_number like concat('%', #{ew.purchaseOrderNo}, '%')
)
or exists (
select 1 from quality_inspect qi_po
join purchase_ledger pl_po on pl_po.id = qi_po.purchase_ledger_id
where qi_po.id = sir.record_id
and sir.record_type = '10'
and pl_po.purchase_contract_number like concat('%', #{ew.purchaseOrderNo}, '%')
)
)
union all
select sor.id,
null as inbound_batches,
sor.outbound_batches,
sor.batch_no,
null as stock_in_num,
sor.stock_out_num,
sor.record_type,
sor.record_id,
sor.product_model_id,
sor.remark,
'out' as direction,
sor.out_date,
sor.create_time,
sor.create_user,
sor.type
from stock_out_record sor
and sor.create_time >= #{ew.reportDate}
and sor.create_time < DATE_ADD(#{ew.reportDate}, INTERVAL 1 DAY)
and sor.create_time >= #{ew.startMonth}
and sor.create_time < DATE_ADD(#{ew.endMonth}, INTERVAL 1 DAY)
and sor.create_time >= #{ew.startDate}
and sor.create_time < DATE_ADD(#{ew.endDate}, INTERVAL 1 DAY)
and
) t
left join (
select product_model_id, sum(qualitity) as current_stock
from stock_inventory
group by product_model_id
) si on t.product_model_id = si.product_model_id
left join product_model pm on t.product_model_id = pm.id
left join product p on pm.product_id = p.id
left join sys_user su on t.create_user = su.user_id
order by t.create_time desc, t.id desc
SELECT
combined_data.product_model_id,
pm.model,
pm.unit,
p.product_name,
MAX(current_inventory) as current_stock,
SUM(CASE WHEN record_type = 'in' THEN amount ELSE 0 END) as total_stock_in,
SUM(CASE WHEN record_type = 'out' THEN amount ELSE 0 END) as total_stock_out
FROM (
SELECT
product_model_id,
SUM(qualitity) as current_inventory,
0 as amount,
'' as record_type
FROM stock_inventory
GROUP BY product_model_id
UNION ALL
SELECT
product_model_id,
0 as current_inventory,
SUM(stock_in_num) as amount,
'in' as record_type
FROM stock_in_record
type = 0
and stock_in_record.create_time >= #{ew.startMonth}
and stock_in_record.create_time < DATE_ADD(#{ew.endMonth}, INTERVAL 1 DAY)
and stock_in_record.create_time >= #{ew.startDate}
and stock_in_record.create_time < DATE_ADD(#{ew.endDate}, INTERVAL 1 DAY)
and
GROUP BY product_model_id
UNION ALL
SELECT
product_model_id,
0 as current_inventory,
SUM(stock_out_num) as amount,
'out' as record_type
FROM stock_out_record
type = 0
and stock_out_record.create_time >= #{ew.startMonth}
and stock_out_record.create_time < DATE_ADD(#{ew.endMonth}, INTERVAL 1 DAY)
and stock_out_record.create_time >= #{ew.startDate}
and stock_out_record.create_time < DATE_ADD(#{ew.endDate}, INTERVAL 1 DAY)
and
GROUP BY product_model_id
) combined_data
LEFT JOIN product_model pm ON pm.id = combined_data.product_model_id
LEFT JOIN product p ON p.id = pm.product_id
and p.product_name like concat('%',#{ew.productName},'%')
and pm.model like concat('%',#{ew.model},'%')
GROUP BY
combined_data.product_model_id,
pm.model,
pm.unit,
p.product_name
select ifnull(sum(qualitity), 0)
from stock_inventory
select IFNULL(sum(qualitity), 0)
from stock_inventory
where create_time >= #{now}
and create_time < DATE_ADD(#{now}, INTERVAL 1 DAY)
SELECT SUM(total_count)
FROM (SELECT COUNT(*) as total_count
FROM stock_inventory
WHERE create_time >= #{startDate}
AND create_time <= #{endDate}
UNION ALL
SELECT COUNT(*) as total_count
FROM stock_uninventory
WHERE create_time >= #{startDate}
AND create_time <= #{endDate}) AS combined_counts
SELECT DATE(sir.create_time) AS date,
SUM(sir.stock_in_num) AS count
FROM stock_in_record sir
JOIN product_model pm ON sir.product_model_id = pm.id
JOIN product p ON pm.product_id = p.id
WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId})
AND sir.create_time >= #{startDate}
AND sir.create_time <= #{endDate}
GROUP BY DATE(sir.create_time)
ORDER BY DATE(sir.create_time) ASC
SELECT DATE(sor.create_time) AS date,
SUM(sor.stock_out_num) AS count
FROM stock_out_record sor
JOIN product_model pm ON sor.product_model_id = pm.id
JOIN product p ON pm.product_id = p.id
WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId})
AND sor.create_time >= #{startDate}
AND sor.create_time <= #{endDate}
GROUP BY DATE(sor.create_time)
ORDER BY DATE(sor.create_time) ASC
SELECT IFNULL(SUM(sor.stock_out_num), 0)
FROM stock_out_record sor
WHERE sor.product_model_id = #{productModelId}
AND (sor.batch_no = #{batchNo} OR (#{batchNo} IS NULL AND sor.batch_no IS NULL))
AND sor.type = #{type}
AND sor.approval_status IN (0, 3)
select distinct si.product_model_id,
si.batch_no
from stock_inventory si
where si.product_model_id in
#{productModelId}
and si.batch_no is not null
and si.batch_no != ''
and (si.qualitity - ifnull(si.locked_quantity, 0)) > 0
order by si.product_model_id, si.batch_no
select si.id, si.batch_no, si.locked_quantity, (si.qualitity - IFNULL(sd.qualitity, 0)) as qualitity,
p.product_name, pm.model, pm.unit
from stock_inventory si
left join (
select spd.stock_inventory_id, sum(spd.quantity) as qualitity
from shipping_product_detail spd
where exists (
select 1
from stock_out_record sor
where sor.record_id = spd.shipping_info_id
and sor.record_type = '13'
and sor.type = '0'
and sor.approval_status in (0, 3)
and sor.product_model_id = spd.product_model_id
and (sor.batch_no = spd.batch_no or (sor.batch_no is null and spd.batch_no is null))
)
group by spd.stock_inventory_id
) as sd on sd.stock_inventory_id = si.id
left join product_model pm on si.product_model_id = pm.id
left join product p on pm.product_id = p.id
where si.product_model_id = #{productModelId}
and si.qualitity > IFNULL(sd.qualitity, 0)
select
batch_no,
MAX(qualifiedId) as qualifiedId,
MAX(unQualifiedId) as unQualifiedId,
SUM(qualifiedQuantity) as qualifiedQuantity,
SUM(unQualifiedQuantity) as unQualifiedQuantity,
SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
SUM(IFNULL(qualifiedPendingOut, 0)) as qualifiedPendingOutQuantity,
SUM(IFNULL(unQualifiedPendingOut, 0)) as unQualifiedPendingOutQuantity,
SUM(qualifiedQuantity - qualifiedLockedQuantity - IFNULL(qualifiedPendingOut, 0)) as qualifiedUnLockedQuantity,
SUM(unQualifiedQuantity - unQualifiedLockedQuantity - IFNULL(unQualifiedPendingOut, 0)) as unQualifiedUnLockedQuantity,
product_model_id,
model,
unit,
product_name,
product_id,
MAX(create_time) as create_time,
MAX(update_time) as update_time,
MAX(warn_num) as warn_num,
MAX(version) as version,
MAX(remark) as remark,
'combined' as stockType,
(
select sir.oil_product
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as oil_product,
(
select sir.tank_inspection_status
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as tank_inspection_status,
(
select sir.unloading_crane_position
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as unloading_crane_position,
(
select sir.density
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as density,
(
select sir.unload_photos
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as unload_photos,
(
select sir.gross_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as gross_weight,
(
select sir.tare_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as tare_weight,
(
select sir.net_weight
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as net_weight,
(
select sir.loss_quantity
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as loss_quantity,
(
select sir.actual_volume
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as actual_volume,
(
select sir.pound_photos
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as pound_photos,
(
select sor.out_category
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_category,
(
select sor.out_date
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_date,
(
select sor.out_tonnage
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_tonnage,
(
select sor.customer_name
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_customer_name,
(
select sor.loading_inspection_status
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as loading_inspection_status,
(
select sor.loading_crane_position
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as loading_crane_position,
(
select sor.density
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_density,
(
select sor.driver_name
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_driver_name,
(
select sor.truck_plate_no
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_truck_plate_no,
(
select sor.loading_photos
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as loading_photos,
(
select sor.pound_photos
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_pound_photos,
(
select sor.oil_depot_name
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_oil_depot_name,
(
select sor.tank_no
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_tank_no,
(
select sor.oil_product
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_oil_product,
(
select sor.contact_person
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_contact_person,
(
select sor.contact_phone
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_contact_phone,
(
select sor.gross_weight
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_gross_weight,
(
select sor.tare_weight
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_tare_weight,
(
select sor.net_weight
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_net_weight,
(
select sor.loss_quantity
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_loss_quantity,
(
select sor.actual_volume
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_actual_volume,
(
select sor.driver_phone
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_driver_phone,
(
select sor.escort_name
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_escort_name,
(
select sor.escort_phone
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_escort_phone,
(
select sor.trailer_plate_no
from stock_out_record sor
where sor.product_model_id = combined.product_model_id
order by sor.id desc
limit 1
) as out_trailer_plate_no,
(
select sir.oil_depot_name
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as oil_depot_name,
(
select sir.inbound_category
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as inbound_category,
(
select sir.tank_no
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as tank_no,
(
select sir.inbound_time
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as inbound_time,
(
select sir.original_quantity
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as original_quantity,
(
select sir.customer_name
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as customer_name,
(
select sir.contact_person
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as contact_person,
(
select sir.contact_phone
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as contact_phone,
(
select sir.driver_name
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as driver_name,
(
select sir.driver_phone
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as driver_phone,
(
select sir.escort_name
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as escort_name,
(
select sir.escort_phone
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as escort_phone,
(
select sir.truck_plate_no
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as truck_plate_no,
(
select sir.trailer_plate_no
from stock_in_record sir
where sir.product_model_id = combined.product_model_id
and (sir.batch_no = combined.batch_no or (combined.batch_no is null and sir.batch_no is null))
order by sir.id desc
limit 1
) as trailer_plate_no
from (
select
si.batch_no,
si.id as qualifiedId,
null as unQualifiedId,
si.qualitity as qualifiedQuantity,
0 as unQualifiedQuantity,
COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
0 as unQualifiedLockedQuantity,
si.product_model_id,
pm.model,
pm.unit,
p.product_name,
p.id as product_id,
si.create_time,
si.update_time,
COALESCE(si.warn_num, 0) as warn_num,
si.version,
si.remark,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = si.product_model_id
and (
(si.batch_no is null and sor.batch_no is null)
or si.batch_no = sor.batch_no
)
and sor.type = '0'
and sor.approval_status = 0
) as qualifiedPendingOut,
0 as unQualifiedPendingOut
from stock_inventory si
left join product_model pm on si.product_model_id = pm.id
left join product p on pm.product_id = p.id
union all
select
su.batch_no,
null as qualifiedId,
su.id as unQualifiedId,
0 as qualifiedQuantity,
su.qualitity as unQualifiedQuantity,
0 as qualifiedLockedQuantity,
COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
su.product_model_id,
pm.model,
pm.unit,
p.product_name,
p.id as product_id,
su.create_time,
su.update_time,
0 as warn_num,
su.version,
su.remark,
0 as qualifiedPendingOut,
(
select IFNULL(SUM(sor.stock_out_num), 0)
from stock_out_record sor
where sor.product_model_id = su.product_model_id
and (
(su.batch_no is null and sor.batch_no is null)
or su.batch_no = sor.batch_no
)
and sor.type = '1'
and sor.approval_status = 0
) as unQualifiedPendingOut
from stock_uninventory su
left join product_model pm on su.product_model_id = pm.id
left join product p on pm.product_id = p.id
) as combined
and combined.product_model_id = #{ew.productModelId}
and combined.product_id = #{ew.productId}
group by
batch_no,
product_model_id,
model,
unit,
product_name,
product_id
order by
MAX(update_time) desc