| | |
| | | T1.type, |
| | | T1.product_id, |
| | | T1.product_model_id, |
| | | T1.stock_type, |
| | | T1.register, |
| | | T1.register_date, |
| | | T1.approve_status, |
| | |
| | | pm.model as specification_model, |
| | | pm.unit as unit, |
| | | CASE |
| | | WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1 |
| | | WHEN IFNULL( |
| | | CASE |
| | | WHEN T1.stock_type = 'waste' THEN t2.waste_available_quantity |
| | | ELSE t2.qualified_available_quantity |
| | | END, 0 |
| | | ) >= (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) THEN 1 |
| | | ELSE 0 |
| | | END as has_sufficient_stock, |
| | | (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity, |
| | |
| | | FROM |
| | | sales_ledger_product T1 |
| | | LEFT JOIN ( |
| | | SELECT product_model_id, SUM(qualitity) as qualitity, SUM(locked_quantity) as locked_quantity |
| | | FROM stock_inventory |
| | | GROUP BY product_model_id |
| | | SELECT |
| | | t.product_model_id, |
| | | SUM(t.qualified_available_quantity) as qualified_available_quantity, |
| | | SUM(t.waste_available_quantity) as waste_available_quantity |
| | | FROM ( |
| | | SELECT |
| | | si.product_model_id, |
| | | (IFNULL(si.qualitity, 0) - IFNULL(si.locked_quantity, 0) - IFNULL(( |
| | | SELECT SUM(sor.stock_out_num) |
| | | 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 IN (0, 3) |
| | | ), 0)) as qualified_available_quantity, |
| | | 0 as waste_available_quantity |
| | | FROM stock_inventory si |
| | | |
| | | UNION ALL |
| | | |
| | | SELECT |
| | | su.product_model_id, |
| | | 0 as qualified_available_quantity, |
| | | (IFNULL(su.qualitity, 0) - IFNULL(su.locked_quantity, 0) - IFNULL(( |
| | | SELECT SUM(sor.stock_out_num) |
| | | 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 IN (0, 3) |
| | | ), 0)) as waste_available_quantity |
| | | FROM stock_uninventory su |
| | | WHERE COALESCE(su.type, 'unqualified') = 'waste' |
| | | ) t |
| | | GROUP BY t.product_model_id |
| | | ) t2 ON T1.product_model_id = t2.product_model_id |
| | | LEFT JOIN ( |
| | | SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as shipped_quantity |