| | |
| | | WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1 |
| | | ELSE 0 |
| | | END as has_sufficient_stock, |
| | | (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity, |
| | | (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0) - IFNULL(t5.pending_approval_quantity, 0)) as no_quantity, |
| | | IFNULL(t3.shipped_quantity, 0) as shipped_quantity, |
| | | IFNULL(t6.return_quantity, 0) as return_quantity, |
| | | IFNULL(t4.approved_stock_in_num, 0) as approved_stock_in_num, |
| | | IFNULL(t5.pending_approval_quantity, 0) as pending_approval_quantity, |
| | | CASE |
| | | WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) = 0 THEN '待发货' |
| | |
| | | GROUP BY 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 |
| | | FROM shipping_info si |
| | | LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id |
| | | where si.status = '审核通过' OR si.status = '已发货' |
| | | GROUP BY sales_ledger_product_id |
| | | SELECT |
| | | slp.id AS sales_ledger_product_id, |
| | | IFNULL(SUM(so.total_sale_out_num), 0) AS shipped_quantity |
| | | FROM sales_ledger_product slp |
| | | LEFT JOIN ( |
| | | -- 采购台账的「已销售数量」:油品出库不再产 shipping_product_detail, |
| | | -- 改由绑定行的分摊数量反推 —— 出库单的 规格+批次 对上采购入库批次,即为该采购台账卖掉的量 |
| | | SELECT |
| | | pl.id AS purchase_ledger_id, |
| | | sir.product_model_id, |
| | | SUM(b.quantity) AS total_sale_out_num |
| | | FROM stock_out_record_sales_ledger b |
| | | INNER JOIN stock_out_record sor |
| | | ON sor.id = b.stock_out_record_id |
| | | AND sor.approval_status = 1 |
| | | AND (sor.type IS NULL OR sor.type = '') |
| | | INNER JOIN stock_in_record sir |
| | | ON sir.product_model_id = sor.product_model_id |
| | | AND ( |
| | | (sir.batch_no IS NOT NULL AND sir.batch_no = sor.batch_no) |
| | | OR (sir.batch_no IS NULL AND sor.batch_no IS NULL) |
| | | ) |
| | | AND sir.approval_status = 1 |
| | | AND sir.record_type IN ('7', '10') |
| | | LEFT JOIN quality_inspect qi |
| | | ON sir.record_type = '10' |
| | | AND sir.record_id = qi.id |
| | | LEFT JOIN purchase_ledger pl |
| | | ON pl.id = IF(sir.record_type = '7', sir.record_id, qi.purchase_ledger_id) |
| | | WHERE pl.id IS NOT NULL |
| | | GROUP BY pl.id, sir.product_model_id |
| | | ) so ON so.purchase_ledger_id = slp.sales_ledger_id |
| | | AND so.product_model_id = slp.product_model_id |
| | | WHERE slp.type = 2 |
| | | GROUP BY slp.id |
| | | |
| | | UNION ALL |
| | | |
| | | -- 销售台账(type=1): 已发货量直接按到货记录汇总。到货记录本身就是「实际到了多少」的事实, |
| | | -- 不再绕 shipping_info + shipping_product_detail |
| | | SELECT |
| | | sla.sales_ledger_product_id AS sales_ledger_product_id, |
| | | IFNULL(SUM(sla.arrival_quantity), 0) AS shipped_quantity |
| | | FROM sales_ledger_arrival sla |
| | | WHERE sla.sales_ledger_product_id IS NOT NULL |
| | | GROUP BY sla.sales_ledger_product_id |
| | | ) t3 ON t3.sales_ledger_product_id = T1.id |
| | | LEFT JOIN ( |
| | | SELECT rel.sales_ledger_product_id, |
| | |
| | | GROUP BY rel.sales_ledger_product_id |
| | | ) t4 ON t4.sales_ledger_product_id = T1.id |
| | | LEFT JOIN ( |
| | | SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity |
| | | SELECT si.sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity |
| | | FROM shipping_info si |
| | | LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id |
| | | WHERE si.status IN ('待审核', '审核中') |
| | | GROUP BY sales_ledger_product_id |
| | | OR (si.status = '审核通过' AND NOT EXISTS ( |
| | | SELECT 1 FROM stock_out_record sor |
| | | WHERE sor.record_id = si.id |
| | | AND TRIM(sor.record_type) = '13' |
| | | AND sor.approval_status = 1 |
| | | )) |
| | | GROUP BY si.sales_ledger_product_id |
| | | |
| | | UNION ALL |
| | | |
| | | -- 油品出库待审批 / 待确认:还没到货,算「审批中」,与 t3 的到货量互补。 |
| | | -- 只有绑定了卖油台账的出库才占这条分支(代储、客存不产绑定行) |
| | | SELECT b.sales_ledger_product_id AS sales_ledger_product_id, |
| | | IFNULL(SUM(b.quantity), 0) AS pending_approval_quantity |
| | | FROM stock_out_record_sales_ledger b |
| | | INNER JOIN stock_out_record sor |
| | | ON sor.id = b.stock_out_record_id |
| | | AND sor.approval_status IN (0, 3) |
| | | AND (sor.type IS NULL OR sor.type = '') |
| | | WHERE b.sales_ledger_product_id IS NOT NULL |
| | | GROUP BY b.sales_ledger_product_id |
| | | ) t5 ON t5.sales_ledger_product_id = T1.id |
| | | LEFT JOIN ( |
| | | SELECT |
| | | prop.sales_ledger_product_id, |
| | | IFNULL(SUM(prop.return_quantity), 0) AS return_quantity |
| | | FROM purchase_return_order_products prop |
| | | LEFT JOIN purchase_return_orders pro |
| | | ON pro.id = prop.purchase_return_order_id |
| | | GROUP BY prop.sales_ledger_product_id |
| | | |
| | | UNION ALL |
| | | |
| | | SELECT |
| | | si.sales_ledger_product_id, |
| | | IFNULL(SUM(rsp.num), 0) AS return_quantity |
| | | FROM return_sale_product rsp |
| | | INNER JOIN return_management rm ON rm.id = rsp.return_management_id |
| | | INNER JOIN shipping_info si ON si.id = rm.shipping_id |
| | | INNER JOIN stock_in_record sir ON sir.record_id = rsp.id |
| | | AND TRIM(sir.record_type) IN ('14','15') |
| | | AND sir.approval_status = 1 |
| | | WHERE si.sales_ledger_product_id IS NOT NULL |
| | | GROUP BY si.sales_ledger_product_id |
| | | ) t6 ON t6.sales_ledger_product_id = T1.id |
| | | left join product_model pm ON T1.product_model_id = pm.id |
| | | left join product p ON pm.product_id = p.id |
| | | <where> |
| | |
| | | LIMIT 5 |
| | | </select> |
| | | |
| | | <!-- 采购品分布:与「各产品销售金额分析」同口径,直接按采购明细上的品类聚合。 |
| | | 不能再按产品树「原材料」子树过滤 —— 采购台账实际登记的品类(柴油、灭火器)分别挂在 |
| | | 「成品/成品油」「设备设施/公共安全设备」下,按子树过滤会恒为空 --> |
| | | <select id="selectRawMaterialPurchaseAnalysis" resultType="java.util.Map"> |
| | | SELECT |
| | | pr.product_name AS name, |
| | | slp.product_category AS name, |
| | | SUM( slp.tax_inclusive_total_price ) AS value |
| | | FROM |
| | | sales_ledger_product slp |
| | | JOIN product pr ON slp.product_id = pr.id |
| | | WHERE |
| | | slp.type = 2 |
| | | AND pr.parent_id = ( SELECT id FROM product WHERE product_name = '原材料' ) |
| | | AND slp.product_category IS NOT NULL |
| | | AND slp.product_category != '' |
| | | GROUP BY |
| | | pr.id, |
| | | pr.product_name |
| | | slp.product_category |
| | | ORDER BY |
| | | value DESC |
| | | LIMIT 5 |