liyong
4 天以前 b19637f4579b91085e46712931275f4bacde8a6e
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
170
171
172
173
174
175
176
177
178
179
<?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.sales_contract_no AS customerContractNo,
        sl.customer_name,
        sl.contract_amount AS contract_amount,
        SUM( pl.contract_amount ) AS purchase_amount,
        (
        sl.contract_amount - SUM( pl.contract_amount )) AS balance,
        CONCAT( ROUND( ( sl.contract_amount - SUM( pl.contract_amount )) / sl.contract_amount * 100, 1 ), '%' ) AS balance_ratio
        FROM
        sales_ledger sl
        INNER JOIN purchase_ledger pl ON sl.sales_contract_no = pl.sales_contract_no
        <where>
            <if test="c.customerName != null and c.customerName != ''">
                AND sl.customer_name LIKE CONCAT('%', #{c.customerName}, '%')
            </if>
        </where>
        GROUP BY
        sl.sales_contract_no,
        sl.customer_name,
        sl.contract_amount
        ORDER BY
        sl.sales_contract_no
    </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 (-->
        <!--                       &#45;&#45; 第一个查询:来自 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 = '增专票'-->
        <!--                         AND DATE_FORMAT(il.invoice_date, '%Y-%m') IS NOT NULL  &#45;&#45; 新增:过滤month为NULL的情-->
        <!--                       GROUP BY DATE_FORMAT(il.invoice_date, '%Y-%m')-->
        <!--                   ) a1-->
        <!--                       LEFT JOIN (-->
        <!--                  &#45;&#45; 第二个查询:来自 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 (-->
        <!--                       &#45;&#45; 第二个查询:来自 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 (-->
        <!--                  &#45;&#45; 第一个查询:来自 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 = '增专票'-->
        <!--                    AND DATE_FORMAT(il.invoice_date, '%Y-%m') IS NOT NULL  &#45;&#45; 新增:过滤month为NULL的情-->
        <!--                  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>-->
        <!--            a.month is not null-->
        <!--            <if test="month != null">-->
        <!--                and a.month = #{month}-->
        <!--            </if>-->
        <!--        </where>-->
        select * from (
        SELECT
        month,
        SUM(sales_tax_amount) AS j_tax_amount,
        SUM(purchase_tax_amount) AS x_tax_amount
        FROM (
        SELECT
        DATE_FORMAT(entry_date, '%Y-%m') AS month,
        ROUND(SUM(contract_amount * 0.13), 2) AS sales_tax_amount,
        0 AS purchase_tax_amount
        FROM sales_ledger
        WHERE entry_date IS NOT NULL
        GROUP BY DATE_FORMAT(entry_date, '%Y-%m')
 
        UNION ALL
 
        SELECT
        DATE_FORMAT(entry_date, '%Y-%m') AS month,
        0 AS sales_tax_amount,
        ROUND(SUM(contract_amount * 0.26), 2) AS purchase_tax_amount
        FROM purchase_ledger
        WHERE entry_date IS NOT NULL
        GROUP BY DATE_FORMAT(entry_date, '%Y-%m')
        ) t
        GROUP BY month
        ORDER BY month
        ) a
        <where>
            a.month is not null
            <if test="month != null and month != ''">
                and a.month = #{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>