2026-08-07 67a4e02e5a927681028c7c99c216b13e3efa8a21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?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.AccountStatementMapper">
 
    <select id="listPageAccountStatement" resultType="com.ruoyi.account.bean.vo.StatementAccountVo">
    SELECT lj.*,
        <choose>
        <when test="req.accountType == 1">
            c.customer_name as customerName
        </when>
        <when test="req.accountType == 2">
            s.supplier_name as customerName
        </when>
        <otherwise>
            '' as customerName
        </otherwise>
    </choose>
    FROM account_statement AS lj
            <!-- 动态 LEFT JOIN -->
            <choose>
        <when test="req.accountType == 1">
            LEFT JOIN customer AS c
                ON lj.customer_id = c.id
        </when>
        <when test="req.accountType == 2">
            LEFT JOIN supplier_manage AS s
                ON lj.customer_id = s.id
        </when>
    </choose>
    WHERE 1=1
            <if test="req.accountType != null">
                 AND lj.account_type = #{req.accountType}
            </if>
            <if test="req.customerId != null">
                 AND lj.customer_id = #{req.customerId}
            </if>
            <if test="req.startDate != null and req.endDate != null">
                  AND DATE_FORMAT(CONCAT(lj.statement_month, '-01'), '%Y-%m-%d')
                    BETWEEN #{req.startDate} AND #{req.endDate}
            </if>
    ORDER BY lj.statement_month DESC
    </select>
    <select id="selectVatDtoPage" resultType="com.ruoyi.purchase.dto.VatDto">
        SELECT * FROM (
            SELECT
                asi.id,
                asi.invoice_number AS invoiceNumber,
                asi.issue_date AS issueDate,
                'sales' AS type,
                asi.invoice_type AS invoiceType,
                asi.tax_price AS taxAmount,
                asi.tax_inclusive_price AS totalAmount,
                c.customer_name AS customerName,
                NULL AS supplierName,
                DATE_FORMAT(asi.issue_date, '%Y-%m') AS month
            FROM account_sales_invoice asi
            LEFT JOIN customer c ON c.id = asi.customer_id
            WHERE (asi.status IS NULL OR asi.status = 0)
            UNION ALL
            SELECT
                api.id,
                api.invoice_number AS invoiceNumber,
                api.issue_date AS issueDate,
                'purchase' AS type,
                api.invoice_type AS invoiceType,
                api.tax_price AS taxAmount,
                api.tax_inclusive_price AS totalAmount,
                NULL AS customerName,
                s.supplier_name AS supplierName,
                DATE_FORMAT(api.issue_date, '%Y-%m') AS month
            FROM account_purchase_invoice api
            LEFT JOIN supplier_manage s ON s.id = api.supplier_id
            WHERE (api.status IS NULL OR api.status = 0)
        ) AS invoice_detail
        <where>
            <if test="month != null and month != ''">
                AND month = #{month}
            </if>
            <if test="type != null and type != ''">
                AND type = #{type}
            </if>
        </where>
        ORDER BY issueDate DESC, type
    </select>
 
    <select id="selectVatSummary" resultType="com.ruoyi.purchase.dto.VatSummaryDto">
        SELECT
            month,
            SUM(jTaxAmount) AS jTaxAmount,
            SUM(xTaxAmount) AS xTaxAmount,
            SUM(jTaxAmount) - SUM(xTaxAmount) AS taxAmount
        FROM (
            SELECT
                DATE_FORMAT(issue_date, '%Y-%m') AS month,
                SUM(tax_price) AS jTaxAmount,
                0 AS xTaxAmount
            FROM account_sales_invoice
            WHERE (status IS NULL OR status = 0)
            <if test="year != null and year != ''">
                AND YEAR(issue_date) = #{year}
            </if>
            GROUP BY DATE_FORMAT(issue_date, '%Y-%m')
            UNION ALL
            SELECT
                DATE_FORMAT(issue_date, '%Y-%m') AS month,
                0 AS jTaxAmount,
                SUM(tax_price) AS xTaxAmount
            FROM account_purchase_invoice
            WHERE (status IS NULL OR status = 0)
            <if test="year != null and year != ''">
                AND YEAR(issue_date) = #{year}
            </if>
            GROUP BY DATE_FORMAT(issue_date, '%Y-%m')
        ) t
        GROUP BY month
        ORDER BY month
    </select>
 
    <select id="listVatDetail" resultType="com.ruoyi.purchase.dto.VatDto">
        SELECT *
        FROM (
            -- 进项税明细(采购订单)
            SELECT
                pl.purchase_contract_number AS invoiceNo,
                pl.sales_contract_no AS salesContractNo,
                pl.supplier_name AS supplierName,
                NULL AS customerName,
                '进项' AS orderType,
                pl.entry_date AS invoiceDate,
                slp.tax_rate AS taxRate,
                ROUND(SUM(slp.tax_inclusive_total_price - slp.tax_exclusive_total_price), 2) AS jTaxAmount,
                0 AS xTaxAmount,
                ROUND(SUM(slp.tax_inclusive_total_price - slp.tax_exclusive_total_price), 2) AS taxAmount
            FROM sales_ledger_product slp
            LEFT JOIN purchase_ledger pl ON pl.id = slp.sales_ledger_id
            WHERE slp.type = 2
              AND slp.tax_rate IS NOT NULL
              AND slp.tax_rate > 0
            GROUP BY pl.id
 
            UNION ALL
 
            -- 销项税明细(销售订单)
            SELECT
                sl.sales_contract_no AS invoiceNo,
                sl.sales_contract_no AS salesContractNo,
                NULL AS supplierName,
                sl.customer_name AS customerName,
                '销项' AS orderType,
                sl.entry_date AS invoiceDate,
                slp.tax_rate AS taxRate,
                0 AS jTaxAmount,
                ROUND(SUM(slp.tax_inclusive_total_price - slp.tax_exclusive_total_price), 2) AS xTaxAmount,
                ROUND(SUM(slp.tax_inclusive_total_price - slp.tax_exclusive_total_price), 2) AS taxAmount
            FROM sales_ledger_product slp
            LEFT JOIN sales_ledger sl ON sl.id = slp.sales_ledger_id
            WHERE slp.type = 1
              AND slp.tax_rate IS NOT NULL
              AND slp.tax_rate > 0
            GROUP BY sl.id
        ) a
        <where>
            <if test="month != null and month != ''">
                AND DATE_FORMAT(a.invoiceDate, '%Y-%m') = #{month}
            </if>
        </where>
        ORDER BY a.invoiceDate DESC
    </select>
</mapper>