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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?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.basic.mapper.CustomerMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ruoyi.basic.pojo.Customer">
        <id column="id" property="id" />
    </resultMap>
    <select id="listPage" resultType="com.ruoyi.basic.vo.CustomerVo">
        select
        c.*,
        u.nick_name usage_user_name,
        (
        select group_concat(u2.user_name separator ', ')
        from customer_user cu
        left join sys_user u2 on cu.user_id = u2.user_id
        where cu.customer_id = c.id
        and cu.user_id != c.usage_user
        ) as together_user_names,
        (
        select group_concat(cu.user_id separator ',')
        from customer_user cu
        where cu.customer_id = c.id
        and cu.user_id != c.usage_user
        ) as user_ids_str
        from customer c
        left join sys_user u on c.usage_user = u.user_id
        <where>
            <if test="c.customerName != null and c.customerName != ''">
                and customer_name like concat('%', #{c.customerName}, '%')
            </if>
            <if test="c.customerType != null and c.customerType != ''">
                and customer_type = #{c.customerType}
            </if>
            <!-- 公海查询:type = 1(公海客户)-->
            <if test="c.type != null and c.type == 1">
                and type = #{c.type}
            </if>
            <!-- 私海查询:type = 0(私海客户)或者 type = 1(公海客户)且已被分配,并且是自己领用、自己创建或者共享给自己的客户 -->
            <if test="c.type != null and c.type == 0">
                and (
                    (type = #{c.type} or (type = 1 and is_assigned = 1))
                    and (
                        c.usage_user = #{loginUserId}
                        or c.create_user = #{loginUserId}
                        or exists (
                            select 1 from customer_user cu
                            where cu.customer_id = c.id
                            and cu.user_id = #{loginUserId}
                        )
                    )
                )
            </if>
        </where>
        order by c.id desc
    </select>
 
    <select id="list" resultType="com.ruoyi.basic.vo.CustomerVo">
        select
        c.*,
        u.user_name usage_user_name,
        (
        select group_concat(u2.user_name separator ', ')
        from customer_user cu
        left join sys_user u2 on cu.user_id = u2.user_id
        where cu.customer_id = c.id
        and cu.user_id != c.usage_user
        ) as together_user_names,
        (
        select group_concat(cu.user_id separator ',')
        from customer_user cu
        where cu.customer_id = c.id
        and cu.user_id != c.usage_user
        ) as user_ids_str
        from customer c
        left join sys_user u on c.usage_user = u.user_id
        <where>
            <if test="c.ids != null and c.ids.length > 0">
                and c.id in
                <foreach collection="c.ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="c.customerName != null and c.customerName != ''">
                and customer_name like concat('%', #{c.customerName}, '%')
            </if>
            <if test="c.customerType != null and c.customerType != ''">
                and customer_type = #{c.customerType}
            </if>
            <!-- 公海查询:type = 1(公海客户)-->
            <if test="c.type != null and c.type == 1">
                and type = #{c.type}
            </if>
            <!-- 私海查询:type = 0(私海客户)或者 type = 1(公海客户)且已被分配,并且是自己领用、自己创建或者共享给自己的客户 -->
            <if test="c.type != null and c.type == 0">
                and (
                    (type = #{c.type} or (type = 1 and is_assigned = 1))
                    and (
                        c.usage_user = #{loginUserId}
                        or c.create_user = #{loginUserId}
                        or exists (
                            select 1 from customer_user cu
                            where cu.customer_id = c.id
                            and cu.user_id = #{loginUserId}
                        )
                    )
                )
            </if>
        </where>
        order by c.id desc
    </select>
    <!--
      客户往来(回款台账主表):
        应收金额 = 各销售台账的应收金额之和,
        单条台账应收金额 = 该台账已出库金额 − 已退货款 − 已回款金额(不小于 0)。
      即「回款金额」是累计回款,「应收金额」是还没收回来的部分。
      注意 出库金额 = sum(出库数量 * 6位小数的含税单价),会带有「分」以下的零头,
           而回款是按分录入的整数金额,两者相减会出现 0.0045、0.004 这类无意义的尾差,
           因此单条台账的应收金额按分计算,分以下的尾差不计入。
      注意 必须先按台账算好应收、再按客户汇总(与 customewTransactionsDetails 口径一致)。
           若先按客户汇总再取整,各家台账的尾差会累加,出现「主表 0.01、明细全是 0」的对不上问题。
      注意 ★ 出库金额按「销售明细的含税总额」折算,不能直接用「出库数量 × 含税单价」:
              出库金额 = Σ ROUND( 该明细含税总额 × LEAST(1, 该明细已出库量 / 该明细销售数量), 2 )
            原因:销售数量可能带 3 位小数(如 47.328),而出库数量按手册 §5.6 只保留 2 位(47.33)。
            直接相乘得 47.33 × 172 = 8140.76,而开票金额是 47.328 × 172 = 8140.42,
            凭空多出 0.34 的应收 —— 实测黑龙江北盛壶关分公司 13370.02 已全额回款,
            页面上却仍显示欠 0.34。
            · LEAST(1, ...) 用来封顶:出库量 ≥ 销售数量时最多算整条明细的开票金额,超发不增加应收;
              出库量 < 销售数量(部分发货)时按比例计。
            · 每条明细单独 ROUND 到分,避免 0.095 这类分以下零头累积成应收。
      注意 出库金额按台账 (sales_ledger.id) 聚合,同一发货单多批号出库时不会重复计数。
    -->
    <select id="customewTransactions" resultType="com.ruoyi.sales.vo.CustomerTransactionsVo">
        select T1.customer_id,
               c.customer_name,
               T1.contractAmounts,
               IFNULL(T2.receiptPaymentAmount, 0) AS receiptPaymentAmount,
               IFNULL(T5.receiptableAmount, 0) AS receiptableAmount
        from (select customer_id, sum(contract_amount) as contractAmounts from sales_ledger group by customer_id) T1
        left join (select customer_id, sum(collection_amount) as receiptPaymentAmount from account_sales_collection group by customer_id) T2 on T1.customer_id = T2.customer_id
        left join (
            select
                sl.customer_id,
                sum(GREATEST(
                    TRUNCATE(IFNULL(T3.outboundAmount, 0) - IFNULL(T4.returnAmount, 0)
                                 - IFNULL(T6.receiptPaymentAmount, 0), 2),
                    0)) AS receiptableAmount
            from sales_ledger sl
            left join (
                select
                    sl.id,
                    sum(t.collection_amount) as receiptPaymentAmount
                from (
                    select
                        ascc.collection_amount,
                        s.sales_ledger_id
                    from account_sales_collection ascc
                    left join stock_out_record sor on FIND_IN_SET(sor.id, ascc.stock_out_record_ids) > 0
                    left join shipping_info s on sor.record_id = s.id
                    where sor.record_type='13'
                      and sor.approval_status=1
                    group by ascc.id, s.sales_ledger_id
                ) t
                left join sales_ledger sl on t.sales_ledger_id = sl.id
                group by sl.id
            ) T6 on T6.id = sl.id
            left join (
                SELECT
                    sl.id,
                    sum(ROUND(slp.tax_inclusive_total_price
                              * LEAST(1, ship.out_qty / NULLIF(slp.quantity, 0)), 2)) as outboundAmount
                FROM (
                    SELECT sor.record_id AS shipping_id, SUM(sor.stock_out_num) AS out_qty
                    FROM stock_out_record sor
                    WHERE sor.record_type='13' AND sor.approval_status=1
                    GROUP BY sor.record_id
                ) ship
                JOIN shipping_info s ON s.id = ship.shipping_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
                WHERE slp.type = 1
                group by sl.id
            ) T3 on T3.id = sl.id
            left join (
                select
                    sl.id,
                    sum(rm.refund_amount) as returnAmount
                from return_management rm
                left join shipping_info si on rm.shipping_id = si.id
                left join sales_ledger sl on si.sales_ledger_id = sl.id
                where rm.status=1
                group by sl.id
            ) T4 on T4.id = sl.id
            group by sl.customer_id
        ) T5 on T5.customer_id = T1.customer_id
        left join customer c on T1.customer_id = c.id
        <where>
            <if test="customerName!=null and customerName!=''">
                AND c.customer_name LIKE CONCAT('%', #{customerName}, '%')
            </if>
        </where>
        order by T1.customer_id desc
    </select>
    <!--
      客户往来明细(按销售台账):应收金额 = 该台账已出库 − 已退货 − 已回款。
      单条回款只统计一次:同一回款可能同时命中该台账的多条出库记录,
      先用 GROUP BY ascc.id, sl.id 去重,再按台账求和。
      金额同样按分(2 位小数)计算,分以下的尾差不计入。
    -->
    <select id="customewTransactionsDetails"
            resultType="com.ruoyi.sales.vo.CustomerTransactionsDetailsVo">
        select sl.id salesLedgerId,
               sl.sales_contract_no,
               sl.execution_date,
               sl.contract_amount,
               IFNULL(T1.receiptPaymentAmount, 0) AS receiptPaymentAmount,
               GREATEST(
                   TRUNCATE(IFNULL(T2.outboundAmount, 0) - IFNULL(T3.returnAmount, 0)
                                - IFNULL(T1.receiptPaymentAmount, 0), 2),
                   0) AS receiptableAmount
        from sales_ledger sl
        left join (
            select
                sl.id,
                sum(t.collection_amount) as receiptPaymentAmount
            from (
                select
                    ascc.collection_amount,
                    s.sales_ledger_id
                from account_sales_collection ascc
                left join stock_out_record sor on FIND_IN_SET(sor.id, ascc.stock_out_record_ids) > 0
                left join shipping_info s on sor.record_id = s.id
                where sor.record_type='13'
                  and sor.approval_status=1
                group by ascc.id, s.sales_ledger_id
            ) t
            left join sales_ledger sl on t.sales_ledger_id = sl.id
            group by sl.id
        )T1 on T1.id = sl.id
        left join (
            SELECT
                sl.id,
                sum(ROUND(slp.tax_inclusive_total_price
                          * LEAST(1, ship.out_qty / NULLIF(slp.quantity, 0)), 2)) as outboundAmount
            FROM (
                SELECT sor.record_id AS shipping_id, SUM(sor.stock_out_num) AS out_qty
                FROM stock_out_record sor
                WHERE sor.record_type='13' AND sor.approval_status=1
                GROUP BY sor.record_id
            ) ship
            JOIN shipping_info s ON s.id = ship.shipping_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
            WHERE slp.type = 1
            group by  sl.id
        )T2 on T2.id = sl.id
        left join (
            select sl.id,
                   sum(rm.refund_amount) as returnAmount
            from return_management rm
                     left join shipping_info si on rm.shipping_id = si.id
                     left join sales_ledger sl on si.sales_ledger_id = sl.id
            where rm.status=1
            group by sl.id
        )T3 on T3.id = sl.id
        where sl.customer_id = #{customerId}
        order by sl.id desc
    </select>
</mapper>