<?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.account.mapper.sales.AccountInvoiceApplicationMapper">
|
|
<select id="listPageAccountInvoiceApplication"
|
resultType="com.ruoyi.account.bean.vo.sales.AccountInvoiceApplicationVo">
|
select * from (select aia.*,
|
c.customer_name,
|
GROUP_CONCAT(sour.outbound_batches SEPARATOR ',') AS outboundBatches
|
from account_invoice_application aia
|
left join customer c on aia.customer_id = c.id
|
<!-- 手工发货单出库按 stock_out_record_ids 找,油品出库按绑定行 stock_out_binding_ids 反查所在的出库单 -->
|
left join stock_out_record sour on FIND_IN_SET(sour.id, aia.stock_out_record_ids) > 0
|
OR EXISTS (select 1 from stock_out_record_sales_ledger b
|
where b.stock_out_record_id = sour.id
|
and FIND_IN_SET(b.id, aia.stock_out_binding_ids) > 0)
|
GROUP BY aia.id)A
|
<where>
|
<if test="req.customerId != null">
|
AND A.customer_id = #{req.customerId}
|
</if>
|
<if test="req.invoiceApplicationNo != null and req.invoiceApplicationNo != ''">
|
AND A.invoice_application_no LIKE CONCAT('%',#{req.invoiceApplicationNo},'%')
|
</if>
|
<if test="req.status != null">
|
AND A.status = #{req.status}
|
</if>
|
<if test="req.startDate != null and req.endDate != null">
|
AND A.apply_date BETWEEN #{req.startDate} AND #{req.endDate}
|
</if>
|
</where>
|
order by A.id desc
|
</select>
|
<!-- 可开票的销售出库候选。
|
① record_type='13' 的手工发货单出库 → b.id 为 NULL,用 stock_out_record_ids 判重;
|
② 油品出库(type 为空)按「出库单×客户」拆到绑定行 → 用 stock_out_binding_ids 判重。
|
两个 id 空间都是小整数,混在一列里会撞号,所以必须按 b.id 是否为空分开比。 -->
|
<select id="getOutboundBatchesByCustomer"
|
resultType="com.ruoyi.account.bean.vo.sales.SalesOutboundVo">
|
SELECT
|
sor.id,
|
sor.outbound_batches,
|
IFNULL(b.customer_name, sl.customer_name) AS customerName,
|
sor.create_time as shippingDate,
|
IFNULL(pb.product_name, p.product_name) AS productName,
|
IFNULL(pmb.model, pm.model) as specification_model,
|
IFNULL(slpb.tax_rate, slp.tax_rate) as tax_rate,
|
CASE
|
WHEN b.id IS NOT NULL THEN b.quantity * slpb.tax_inclusive_unit_price
|
ELSE sor.stock_out_num * slp.tax_inclusive_unit_price
|
END as outboundAmount,
|
s.shipping_no,
|
IFNULL(slb.sales_contract_no, sl.sales_contract_no) as sales_contract_no,
|
b.id AS bindingId,
|
IFNULL(b.customer_id, sl.customer_id) AS customer_id
|
FROM stock_out_record sor
|
LEFT JOIN stock_out_record_sales_ledger b ON b.stock_out_record_id = sor.id
|
left join shipping_info s on sor.record_id = s.id
|
LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
|
LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id and slp.type = 1
|
LEFT JOIN sales_ledger slb ON slb.id = b.sales_ledger_id
|
LEFT JOIN sales_ledger_product slpb ON slpb.id = b.sales_ledger_product_id and slpb.type = 1
|
left join product_model pm on slp.product_model_id = pm.id
|
left join product p on pm.product_id = p.id
|
left join product_model pmb on sor.product_model_id = pmb.id
|
left join product pb on pmb.product_id = pb.id
|
WHERE sor.approval_status=1
|
AND (
|
sor.record_type='13'
|
OR (b.id IS NOT NULL AND (sor.type IS NULL OR sor.type = ''))
|
)
|
AND IFNULL(b.customer_id, sl.customer_id) = #{customerId}
|
AND NOT EXISTS (
|
SELECT 1
|
FROM account_invoice_application a
|
WHERE a.status != 2
|
AND (
|
(b.id IS NULL AND FIND_IN_SET(sor.id, a.stock_out_record_ids) > 0)
|
OR (b.id IS NOT NULL AND FIND_IN_SET(b.id, a.stock_out_binding_ids) > 0)
|
)
|
)
|
order by sor.id DESC, IFNULL(b.id, 0)
|
</select>
|
<select id="existsByStockOutRecordId" resultType="java.lang.Boolean">
|
SELECT COUNT(*) > 0
|
FROM account_invoice_application
|
WHERE status != 2
|
AND (
|
<foreach collection="stockOutRecordIds" item="id" separator=" OR ">
|
FIND_IN_SET(#{id}, stock_out_record_ids) > 0
|
</foreach>
|
)
|
</select>
|
<!-- 油品出库的绑定行与出库单是两个独立 id 空间,判重要分开查 -->
|
<select id="existsByStockOutBindingId" resultType="java.lang.Boolean">
|
SELECT COUNT(*) > 0
|
FROM account_invoice_application
|
WHERE status != 2
|
AND (
|
<foreach collection="stockOutBindingIds" item="id" separator=" OR ">
|
FIND_IN_SET(#{id}, stock_out_binding_ids) > 0
|
</foreach>
|
)
|
</select>
|
<select id="selectAccountInvoiceApplicationList"
|
resultType="com.ruoyi.account.bean.vo.sales.AccountInvoiceApplicationVo">
|
select
|
*
|
from (
|
select aia.*,
|
asi.id AS sales_invoice_id,
|
asi.status AS sales_invoice_status,
|
c.customer_name,
|
GROUP_CONCAT(sour.outbound_batches SEPARATOR ',') AS outboundBatches
|
from account_invoice_application aia
|
left join customer c on aia.customer_id = c.id
|
left join stock_out_record sour on FIND_IN_SET(sour.id, aia.stock_out_record_ids) > 0
|
OR EXISTS (select 1 from stock_out_record_sales_ledger b
|
where b.stock_out_record_id = sour.id
|
and FIND_IN_SET(b.id, aia.stock_out_binding_ids) > 0)
|
left join account_sales_invoice asi on aia.id = asi.account_invoice_application_id
|
GROUP BY aia.id
|
)A
|
WHERE A.customer_id = #{customerId}
|
AND (COALESCE(A.sales_invoice_id,'') = '' OR A.sales_invoice_status = 1)
|
AND A.status = 1
|
order by A.id desc
|
</select>
|
|
</mapper>
|