<?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.InvoicePurchaseMapper">
|
|
|
<select id="selectPurchaseReport" resultType="com.ruoyi.purchase.dto.InvoicePurchaseReportDto">
|
select sl.customer_contract_no,
|
sl.customer_name,
|
sl.project_name,
|
sl.contract_amount,
|
pl.contract_amount as purchase_amount,
|
sum(slp.tax_exclusive_total_price) as sale_tax_exclusive_total_price,
|
sum(pr.tax_exclusive_total_price) as tax_exclusive_total_price,
|
(sl.contract_amount-pl.contract_amount) as balance,
|
CONCAT(FORMAT((sl.contract_amount - pl.contract_amount) / sl.contract_amount * 100, 2), '%') AS balance_ratio,
|
sl.contract_amount-sum(slp.tax_exclusive_total_price)-(pl.contract_amount-sum(pr.tax_exclusive_total_price)) as balance_amount
|
from purchase_ledger pl
|
left join sales_ledger sl on pl.sales_ledger_id = sl.id
|
left join product_record pr on pr.purchase_ledger_id = pl.id
|
left join sales_ledger_product slp on slp.sales_ledger_id = sl.id
|
|
group by sl.customer_contract_no, sl.customer_name, sl.project_name, sl.contract_amount, pl.contract_amount
|
having sl.customer_contract_no is not null
|
<where>
|
<if test="c.customerName != null and c.customerName != ''">
|
and sl.customer_name like concat('%',#{c.customerName},'%')
|
</if>
|
</where>
|
</select>
|
<select id="listVat" resultType="com.ruoyi.purchase.dto.VatDto">
|
select *
|
from (SELECT
|
COALESCE(a1.month, a2.month) AS month,
|
IFNULL(a1.tax_amount, 0) AS j_tax_amount,
|
IFNULL(a2.x_tax_amount, 0) AS x_tax_amount
|
FROM (
|
-- 第一个查询:来自 invoice_ledger 的税额
|
SELECT
|
DATE_FORMAT(il.invoice_date, '%Y-%m') AS month,
|
ROUND(SUM(pr.invoice_amount - pr.invoice_amount / (1 + pr.tax_rate / 100)), 2) AS tax_amount
|
FROM invoice_ledger il
|
LEFT JOIN invoice_registration_product pr ON pr.id = il.invoice_registration_product_id
|
WHERE il.invoice_no IS NOT NULL
|
AND invoice_type = '增专票'
|
GROUP BY DATE_FORMAT(il.invoice_date, '%Y-%m')
|
) a1
|
LEFT JOIN (
|
-- 第二个查询:来自 ticket_registration 的税额
|
SELECT
|
DATE_FORMAT(a.issue_date, '%Y-%m') AS month,
|
SUM(a.invoice_amount) AS x_tax_amount
|
FROM (
|
SELECT DISTINCT pr.id,
|
tr.issue_date,
|
ROUND(pr.tickets_amount / (1 + pr.tax_rate / 100), 2) AS un_tickets_price,
|
ROUND(pr.tickets_amount - pr.tickets_amount / (1 + pr.tax_rate / 100), 2) AS invoice_amount
|
FROM product_record pr
|
LEFT JOIN purchase_ledger pl ON pl.id = pr.purchase_ledger_id
|
LEFT JOIN sales_ledger sl ON sl.id = pl.sales_ledger_id
|
LEFT JOIN ticket_registration tr ON tr.purchase_ledger_id = pl.id
|
LEFT JOIN product_model pm ON pm.id = pr.product_model_id
|
WHERE type = 2
|
AND tr.invoice_number IS NOT NULL
|
) a
|
GROUP BY DATE_FORMAT(a.issue_date, '%Y-%m')
|
) a2 ON a1.month = a2.month
|
|
UNION ALL
|
|
SELECT
|
COALESCE(a1.month, a2.month) AS month,
|
IFNULL(a1.tax_amount, 0) AS tax_amount,
|
IFNULL(a2.x_tax_amount, 0) AS x_tax_amount
|
FROM (
|
-- 第二个查询:来自 ticket_registration 的税额(反过来补全没有匹配到的)
|
SELECT
|
DATE_FORMAT(a.issue_date, '%Y-%m') AS month,
|
SUM(a.invoice_amount) AS x_tax_amount
|
FROM (
|
SELECT DISTINCT pr.id,
|
tr.issue_date,
|
ROUND(pr.tickets_amount / (1 + pr.tax_rate / 100), 2) AS un_tickets_price,
|
ROUND(pr.tickets_amount - pr.tickets_amount / (1 + pr.tax_rate / 100), 2) AS invoice_amount
|
FROM product_record pr
|
LEFT JOIN purchase_ledger pl ON pl.id = pr.purchase_ledger_id
|
LEFT JOIN sales_ledger sl ON sl.id = pl.sales_ledger_id
|
LEFT JOIN ticket_registration tr ON tr.purchase_ledger_id = pl.id
|
LEFT JOIN product_model pm ON pm.id = pr.product_model_id
|
WHERE type = 2
|
AND tr.invoice_number IS NOT NULL
|
) a
|
GROUP BY DATE_FORMAT(a.issue_date, '%Y-%m')
|
) a2
|
LEFT JOIN (
|
-- 第一个查询:来自 invoice_ledger 的税额
|
SELECT
|
DATE_FORMAT(il.invoice_date, '%Y-%m') AS month,
|
ROUND(SUM(pr.invoice_amount - pr.invoice_amount / (1 + pr.tax_rate / 100)), 2) AS tax_amount
|
FROM invoice_ledger il
|
LEFT JOIN invoice_registration_product pr ON pr.id = il.invoice_registration_product_id
|
WHERE il.invoice_no IS NOT NULL
|
AND invoice_type = '增专票'
|
GROUP BY DATE_FORMAT(il.invoice_date, '%Y-%m')
|
) a1 ON a1.month = a2.month
|
WHERE a1.month IS NULL
|
ORDER BY month
|
)as a
|
<where>
|
<if test="month != null">
|
and a.month = date_format(#{month})
|
</if>
|
</where>
|
|
</select>
|
<select id="listVat1" resultType="com.ruoyi.purchase.dto.VatDto">
|
# SELECT
|
# DATE_FORMAT(il.invoice_date, '%Y-%m') AS month,
|
# ROUND(SUM(pr.invoice_amount - pr.invoice_amount / (1 + pr.tax_rate / 100)), 2) AS tax_amount
|
# FROM invoice_ledger il
|
# LEFT JOIN invoice_registration_product pr ON pr.id = il.invoice_registration_product_id
|
# WHERE il.invoice_no IS NOT NULL
|
# AND invoice_type = '增专票'
|
# GROUP BY DATE_FORMAT(il.invoice_date, '%Y-%m')
|
# ORDER BY month;
|
# select DATE_FORMAT(a.issue_date, '%Y-%m'), sum(a.invoice_amount) as xTaxAmount
|
# from (SELECT distinct pr.id,
|
# tr.issue_date,
|
# ROUND(pr.tickets_amount / (1 + pr.tax_rate / 100), 2) AS un_tickets_price,
|
# ROUND(pr.tickets_amount - pr.tickets_amount / (1 + pr.tax_rate / 100),
|
# 2) AS invoice_amount
|
# FROM product_record pr
|
# LEFT JOIN purchase_ledger pl ON pl.id = pr.purchase_ledger_id
|
# LEFT JOIN sales_ledger sl ON sl.id = pl.sales_ledger_id
|
# LEFT JOIN ticket_registration tr ON tr.purchase_ledger_id = pl.id
|
# LEFT JOIN product_model pm ON pm.id = pr.product_model_id
|
# WHERE type = 2
|
# and tr.invoice_number is not null) a
|
# GROUP BY DATE_FORMAT(a.issue_date, '%Y-%m')
|
</select>
|
|
</mapper>
|