<?xml version="1.0" encoding="UTF-8" ?>
|
<!DOCTYPE mapper
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<mapper namespace="com.ruoyi.purchase.mapper.PurchaseLedgerMapper">
|
|
<update id="updateContractAmountById">
|
UPDATE purchase_ledger
|
SET contract_amount = #{totalTaxInclusiveAmount}
|
WHERE id = #{id}
|
</update>
|
<select id="selectPurchaseStats" resultType="com.ruoyi.home.dto.IncomeExpenseAnalysisDto">
|
SELECT DATE_FORMAT(entry_date, #{dateFormat}) as dateStr, IFNULL(SUM(contract_amount), 0) as amount
|
FROM purchase_ledger
|
WHERE entry_date BETWEEN #{startDate} AND #{endDate}
|
GROUP BY dateStr
|
</select>
|
|
<select id="selectPurchaseLedgerListPage" resultType="com.ruoyi.purchase.dto.PurchaseLedgerDto">
|
SELECT result.*
|
FROM (
|
SELECT
|
pl.id,
|
pl.purchase_contract_number,
|
pl.sales_contract_no,
|
pl.supplier_id,
|
pl.supplier_name,
|
pl.project_name,
|
pl.contract_amount,
|
pl.entry_date,
|
pl.execution_date,
|
pl.recorder_id,
|
pl.recorder_name,
|
pl.template_name,
|
pl.approve_user_ids,
|
sm.is_white,
|
pl.approval_status,
|
pl.payment_method,
|
pl.remarks,
|
CASE
|
WHEN IFNULL(ls.total_product_count, 0) = 0 THEN '待入库'
|
WHEN IFNULL(ls.full_product_count, 0) >= IFNULL(ls.total_product_count, 0) THEN '完全入库'
|
WHEN IFNULL(ls.approved_product_count, 0) > 0 THEN '入库中'
|
ELSE '待入库'
|
END AS stock_in_status
|
FROM purchase_ledger pl
|
LEFT JOIN supplier_manage sm ON pl.supplier_id = sm.id
|
LEFT JOIN (
|
SELECT
|
product_status.sales_ledger_id,
|
COUNT(1) AS total_product_count,
|
SUM(CASE WHEN product_status.approved_stock_in_num > 0 THEN 1 ELSE 0 END) AS approved_product_count,
|
SUM(CASE WHEN product_status.approved_stock_in_num >= product_status.product_quantity THEN 1 ELSE 0 END) AS full_product_count
|
FROM (
|
SELECT
|
slp.id AS sales_ledger_product_id,
|
slp.sales_ledger_id,
|
IFNULL(slp.quantity, 0) AS product_quantity,
|
IFNULL(approved_qty.approved_stock_in_num, 0) AS approved_stock_in_num
|
FROM sales_ledger_product slp
|
LEFT JOIN (
|
SELECT rel.sales_ledger_product_id,
|
IFNULL(SUM(rel.stock_in_num), 0) AS approved_stock_in_num
|
FROM (
|
SELECT slp.id AS sales_ledger_product_id,
|
sir.stock_in_num
|
FROM stock_in_record sir
|
INNER JOIN sales_ledger_product slp
|
ON slp.type = 2
|
AND TRIM(sir.record_type) = '7'
|
AND sir.record_id = slp.sales_ledger_id
|
AND (
|
(sir.batch_no IS NOT NULL AND sir.batch_no LIKE CONCAT('%-', slp.id))
|
OR (sir.batch_no IS NULL AND sir.product_model_id = slp.product_model_id)
|
)
|
WHERE sir.approval_status = 1
|
|
UNION ALL
|
|
SELECT slp.id AS sales_ledger_product_id,
|
sir.stock_in_num
|
FROM stock_in_record sir
|
INNER JOIN quality_inspect qi
|
ON TRIM(sir.record_type) = '10'
|
AND sir.record_id = qi.id
|
INNER JOIN sales_ledger_product slp
|
ON slp.type = 2
|
AND slp.sales_ledger_id = qi.purchase_ledger_id
|
AND slp.product_model_id = qi.product_model_id
|
WHERE sir.approval_status = 1
|
) rel
|
GROUP BY rel.sales_ledger_product_id
|
) approved_qty ON approved_qty.sales_ledger_product_id = slp.id
|
WHERE slp.type = 2
|
) product_status
|
GROUP BY product_status.sales_ledger_id
|
) ls ON ls.sales_ledger_id = pl.id
|
<where>
|
<if test="c.purchaseContractNumber != null and c.purchaseContractNumber != ''">
|
AND pl.purchase_contract_number LIKE CONCAT('%', #{c.purchaseContractNumber}, '%')
|
</if>
|
<if test="c.approvalStatus != null and c.approvalStatus != ''">
|
AND pl.approval_status = #{c.approvalStatus}
|
</if>
|
<if test="c.supplierName != null and c.supplierName != ''">
|
AND pl.supplier_name LIKE CONCAT('%', #{c.supplierName}, '%')
|
</if>
|
<if test="c.salesContractNo != null and c.salesContractNo != ''">
|
AND pl.sales_contract_no LIKE CONCAT('%', #{c.salesContractNo}, '%')
|
</if>
|
<if test="c.projectName != null and c.projectName != ''">
|
AND pl.project_name LIKE CONCAT('%', #{c.projectName}, '%')
|
</if>
|
<if test="c.entryDateStart != null and c.entryDateStart != ''">
|
AND pl.entry_date >= #{c.entryDateStart}
|
</if>
|
<if test="c.entryDateEnd != null and c.entryDateEnd != ''">
|
AND pl.entry_date <= #{c.entryDateEnd}
|
</if>
|
<if test="c.supplierId != null">
|
AND pl.supplier_id = #{c.supplierId}
|
</if>
|
<if test="c.approvalStatus != null">
|
AND pl.approval_status = #{c.approvalStatus}
|
</if>
|
</where>
|
) result
|
<if test="c.stockInStatus != null and c.stockInStatus != ''">
|
WHERE result.stock_in_status = #{c.stockInStatus}
|
</if>
|
ORDER BY result.entry_date DESC
|
</select>
|
|
<select id="selectTotalPurchaseAmount" resultType="java.math.BigDecimal">
|
SELECT IFNULL(SUM(contract_amount), 0)
|
FROM purchase_ledger
|
<where>
|
<if test="startDate != null and startDate != ''">
|
AND entry_date >= #{startDate}
|
</if>
|
<if test="endDate != null and endDate != ''">
|
AND entry_date <= #{endDate}
|
</if>
|
</where>
|
</select>
|
</mapper>
|