P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?xml version="1.0" encoding="UTF-8" ?>
<?DOCTYPE sqls SYSTEM "../config/sql.dtd" ?>
 
<sqls>
    <dataSpace name="rebate">
    
        <sql name = "refreshStatementAccount">
           <![CDATA[
                insert into so_account (id, year, month, customer_id, customer_code, 
                                        customer_name, amt_order, amt_invoice, amt_receipt, 
                                        amt_statement, account_balance, create_time)
                select UUID_SHORT(), year(CONCAT(doc_date, '-01')) as year, 
                       month(CONCAT(doc_date, '-01')) as month, 
                       so_order.customer_id, so_order.customer_code, so_order.customer_name,
                       ifnull(order_amt_total, 0), ifnull(invoice_amt_total, 0), 
                       ifnull(receipt_amt_total, 0), ifnull(statement_amt_total, 0), 
                       ifnull(receipt_amt_total - statement_amt_total, 0), now()
                from (select DATE_FORMAT(doc_date, '%Y-%m') as doc_date, 
                             customer_id, customer_code, customer_name, ncc_customer_code, 
                             sum(ifnull(amt_total, 0)) as order_amt_total
                      from so_order
                      where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
                        and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
                        and state_code not in ('Input', 'Cancel', 'UnderApproval', 'Rejected')
                        and ifnull(amt_total, '') <> ''
                      group by DATE_FORMAT(doc_date, '%Y-%m'), customer_id, customer_code, customer_name, ncc_customer_code) so_order
                inner join (select customer_id, customer_code, customer_code_ncc, 
                                   customer_name, 
                                   sum(ifnull(amt_total, 0)) as invoice_amt_total
                            from so_invoice
                            where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
                              and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
                              and state_code = 'Open' and is_red = 'F'
                            group by customer_id, customer_code, customer_code_ncc, customer_name) so_invoice on so_order.customer_id = so_invoice.customer_id
                left join (select customer_id, customer_code, customer_code_ncc, 
                                  customer_name, sum(ifnull(amt, 0)) as receipt_amt_total
                           from so_receipt
                           where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
                             and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
                             and state_code = 'Open' and is_red = 'F'
                           group by customer_id, customer_code, customer_code_ncc, customer_name) so_receipt on so_order.customer_id = so_receipt.customer_id
                left join (select customer_id, customer_code, customer_code_ncc, 
                                  customer_name, 
                                  sum(ifnull(amt_total, 0)) as statement_amt_total
                           from so_statement
                           where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
                             and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
                             and state_code = 'Open' and is_red = 'F'
                           group by customer_id, customer_code, customer_code_ncc, customer_name) so_statement on so_order.customer_id = so_statement.customer_id           
           ]]>
        </sql>
        
        <sql name = "addStatementAccount">
           <![CDATA[
                insert into so_account (id, year, month, customer_id, customer_code, 
                                        customer_name, amt_order, amt_invoice, amt_receipt, 
                                        amt_statement, account_balance, create_time)
                select UUID_SHORT(), year(doc_date) as year, month(doc_date) as month, 
                       so_order.customer_id, so_order.customer_code, so_order.customer_name,
                       ifnull(order_amt_total, 0), ifnull(invoice_amt_total, 0), 
                       ifnull(receipt_amt_total, 0), ifnull(statement_amt_total, 0), 
                       ifnull(receipt_amt_total - statement_amt_total, 0), now()
                from (select DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m-%d') as doc_date, 
                             customer_id, customer_code, customer_name, ncc_customer_code, 
                             sum(ifnull(amt_total, 0)) as order_amt_total
                      from so_order
                      where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
                        and state_code not in ('Input', 'Cancel', 'UnderApproval', 'Rejected')
                        and (amt_total is not null or amt_total <> '')
                      group by customer_id, customer_code, customer_name, ncc_customer_code) so_order
                inner join (select customer_id, customer_code, customer_code_ncc, 
                                   customer_name, 
                                   sum(ifnull(amt_total, 0)) as invoice_amt_total
                            from so_invoice
                            where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
                              and state_code = 'Open' and is_red = 'F'
                            group by customer_id, customer_code, customer_code_ncc, customer_name) so_invoice on so_order.customer_id = so_invoice.customer_id
                left join (select customer_id, customer_code, customer_code_ncc, 
                                  customer_name, sum(ifnull(amt, 0)) as receipt_amt_total
                           from so_receipt
                           where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
                             and state_code = 'Open' and is_red = 'F'
                           group by customer_id, customer_code, customer_code_ncc, customer_name) so_receipt on so_order.customer_id = so_receipt.customer_id
                left join (select customer_id, customer_code, customer_code_ncc, 
                                  customer_name, 
                                  sum(ifnull(amt_total, 0)) as statement_amt_total
                           from so_statement
                           where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
                             and state_code = 'Open' and is_red = 'F'
                           group by customer_id, customer_code, customer_code_ncc, customer_name) so_statement on so_order.customer_id = so_statement.customer_id            
           ]]>      
        </sql>
    
        <sql name = "addBookGeneralChannel">
           <![CDATA[
                insert into wm_book_general_channel (
                    id, year, month, org_id, org_code, org_name, 
                    product_id, product_code, product_name, material_code, material_name, spec,
                    qty_total, qty_frozen, qty_available, amt, create_time
                )
                select UUID_SHORT(), @{year}, @{month},
                       org_id as org_id, org_code as org_code, org_name as org_name,
                       product_id, product_code, product_name, material_code, material_name, spec,
                       sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen,
                       sum(qty_available) as qty_available,
                       sum(amt) as amt, now() 
                from wm_book_detail            
                group by org_id, org_code, org_name        
           ]]>
        </sql>
    
        <sql name = "getBookGeneralChannel">
           <![CDATA[
                select *
                from (
                    select *
                    from wm_book_general_channel
                    union all
                    select UUID_SHORT(), '', YEAR(now()) AS year, MONTH (now()) AS month,
                        org_id as org_id, org_code as org_code, org_name as org_name,
                        product_id, product_code, product_name, material_code, material_name, spec,
                        sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen,
                        sum(qty_available) as qty_available, sum(amt) as amt,
                        now()
                    from wm_book_detail 
                    group by org_id, org_code, org_name, 
                          product_id, product_code, product_name, material_code, material_name, spec
                ) wm_book_general_channel
                where @{filter} 
                order by year desc, month desc, org_code
                @{limit}           
           ]]>
        </sql>
    
        <sql name = "getBookFlowState">
           <![CDATA[
                select distinct doc_type as code, doc_type as name 
                from wm_book_flow           
           ]]>
        </sql>
        
        <sql name = "getFreezeWarehouseByDetail">
            <![CDATA[
                 select wm_warehouse.*, wm_warehouse.id as warehouse_id,
                        wm_warehouse.code as warehouse_code,
                        wm_warehouse.name as warehouse_name
                 from wm_warehouse
                 inner join (select * 
                             from wm_book_detail
                             where id = '@{id}') wm_book_detail on wm_warehouse.org_id = wm_book_detail.org_id
                 where wm_warehouse.type_code = 'Freeze'
            ]]>
        </sql>
        
        <sql name="lockRebateBook">
            update @{documentTable} document set update_time = now()
            where document.id = '@{document_id}'
        </sql>
        
        <sql name="getRebateAmtBookFlowBasicFields">
        <![CDATA[
               select 
                   rebate.bu_id,rebate.org_id, md_bu.name bu_name, '@{batch_mark}' batchMark,rebate.id rebate_id, 
                   rebate.code rebate_code, rebate.item_name rebate_name,
                   rebate.year, rebate.season, rebate.doc_date valid_from, rebate.expire_date valid_to,rebate.batch_mark,    
                   rebate.account_id,rebate.account_code,rebate.account_name,
                   @{documentTypeCode} operate_name, document.doc_date, rebate.company_id, md_org.short_name company_name, 
                   document.code doc_code, document_detail.id document_detail_id, @{document_num} as amt                                          
            from @{detailTableName} document_detail
            left join @{tableName} document on @{documentDetailParentId} = document.id
            inner join rebate_amt_detail rebate on rebate.id = @{rebateId} 
               left join md_bu on md_bu.id = rebate.bu_id
               left join md_org on md_org.is_master = 'T' and md_org.id = rebate.company_id 
               where @{filter}
        ]]>
        </sql>
        
        <sql name="getCloseOrderDetailRebateAmtBookFlow">
        <![CDATA[
               select document.bu_id,document.org_id, md_bu.name bu_name, '@{batch_mark}' batch_mark,'@{batch_mark}' rebate_id, document.code rebate_code, 
               concat('订单',document.code,'折扣退回',round(@{document_num}, 0)) rebate_name,
                   Year(CURRENT_DATE()) year, QUARTER(CURRENT_DATE()) season, CURRENT_DATE() doc_date, '@{expireDate}'  valid_to,'@{batch_mark}' batch_mark,    
                   document.customer_id account_id,document.customer_code account_code,document.customer_name account_name,
                   @{documentTypeCode} operate_name, document.doc_date, document.company_id, md_org.short_name company_name, 
                   document.code doc_code, document_detail.id document_detail_id, @{document_num} as amt                                          
            from so_order_detail document_detail
            left join so_order document on document_detail.parent_id = document.id
               left join md_bu on md_bu.id = document.bu_id
               left join md_org on md_org.is_master = 'T' and md_org.id = document.company_id 
               where @{filter}
        ]]>
        </sql>
        
        <sql name="getRebateQtyBookFlowBasicFields">
        <![CDATA[
               select rebate.bu_id,rebate.org_id, md_bu.name bu_name, '@{batch_mark}' batchMark,rebate.id rebate_id, 
                   rebate.code rebate_code, rebate.item_name rebate_name, 
                   rebate.year, rebate.season, rebate.doc_date, rebate.expire_date,
                   rebate.product_id, rebate.product_code, rebate.product_name, 
                   rebate.account_id,rebate.account_code,rebate.account_name,
                   document_detail.sku_id, document_detail.spec,    
                   rebate.emption_product_id, rebate.emption_product_code, rebate.emption_product_name, 
                   rebate.emption_sku_id, rebate.emption_spec,    
                   @{documentTypeCode} operate_name, rebate.company_id, md_org.short_name company_name,
                   document.code doc_code, document_detail.id document_detail_id, @{document_num} as qty                                          
            from @{detailTableName} document_detail
            left join @{tableName} document on @{documentDetailParentId} = document.id
            inner join rebate_qty_detail rebate on rebate.id = @{rebateId} 
               left join md_bu on md_bu.id = rebate.bu_id
               left join md_org on md_org.is_master = 'T' and md_org.id = rebate.company_id 
               where @{filter}
        ]]>
        </sql>
        
        <sql name="getCloseOrderDetailRebateQtyBookFlow">
        <![CDATA[
                   select 
                       document.bu_id,document.org_id, md_bu.name bu_name, '@{batch_mark}' batch_mark,'@{batch_mark}' rebate_id, 
                       document.code rebate_code, concat('订单',document.code,'买赠退回',round(@{document_num}, 0)) rebate_name,
                    Year(CURRENT_DATE()) year, QUARTER(CURRENT_DATE()) season, CURRENT_DATE() doc_date, CURRENT_DATE() valid_from, '@{expireDate}' valid_to,
                       document_detail.product_id, document_detail.product_code, document_detail.product_name, 
                       document.customer_id account_id,document.customer_code account_code,document.customer_name account_name,
                       document_detail.sku_id, document_detail.spec,    
                       null emption_product_id,null emption_product_code,null  emption_product_name, 
                       null emption_sku_id, null emption_spec,    
                       @{documentTypeCode} operate_name, document.company_id, md_org.short_name company_name,
                       document.code doc_code, document_detail.id document_detail_id, @{document_num} as qty                                          
            from so_order_detail document_detail
            left join so_order document on document_detail.parent_id = document.id
               left join md_bu on md_bu.id = document.bu_id
               left join md_org on md_org.is_master = 'T' and md_org.id = document.company_id 
               where @{filter}
        ]]>
        </sql>
            
        <sql name="writeUpdateRebateAmtDetail">
        <![CDATA[
            update rebate_amt_detail book_detail
            inner join (
                select rebate_id, 
                    (sum(IFNULL(num_return, 0)) - sum(IFNULL(num_used, 0))) as num_used_change,
                    (sum(IFNULL(num_freeze, 0)) - sum(IFNULL(num_unfreeze, 0))) as num_frozen_change
                from rebate_amt_flow
                where batch_mark = '@{batch_mark}' and record_operator = 'update'
                group by rebate_id
            ) book_flow 
            on book_flow.rebate_id = book_detail.id
            set book_detail.amt_used = book_detail.amt_used - num_used_change,
                book_detail.amt_freeze = book_detail.amt_freeze + num_frozen_change,                
                book_detail.batch_mark = '@{batch_mark}', 
              book_detail.update_time = @{update_time}
            where exists (
                select 1 from rebate_amt_flow 
                where rebate_amt_flow.rebate_id = book_detail.id
                and rebate_amt_flow.batch_mark = '@{batch_mark}'
            )
        ]]>
        </sql>            
 
        <sql name="writeUpdateRebateQtyDetail">
        <![CDATA[
            update rebate_qty_detail book_detail
            inner join (
                select rebate_id, 
                    (sum(IFNULL(num_return, 0)) - sum(IFNULL(num_used, 0))) as num_used_change,
                    (sum(IFNULL(num_freeze, 0)) - sum(IFNULL(num_unfreeze, 0))) as num_frozen_change
                from rebate_qty_flow
                where batch_mark = '@{batch_mark}' and record_operator = 'update'
                group by rebate_id
            ) book_flow 
            on book_flow.rebate_id = book_detail.id
            set book_detail.qty_used = book_detail.qty_used - num_used_change,
                book_detail.qty_freeze = book_detail.qty_freeze + num_frozen_change,                
                book_detail.batch_mark = '@{batch_mark}', 
              book_detail.update_time = @{update_time}
            where exists (
                select 1 from rebate_qty_flow 
                where rebate_qty_flow.rebate_id = book_detail.id
                and rebate_qty_flow.batch_mark = '@{batch_mark}'
            )
        ]]>
        </sql>            
                
        <sql name="resetRebateAmtDetailOtherFields">
        <![CDATA[
            update rebate_amt_detail book_detail
            set book_detail.amt_net = book_detail.amt_book - book_detail.amt_used,
                book_detail.amt_available = book_detail.amt_book - book_detail.amt_used - book_detail.amt_freeze
            where book_detail.batch_mark = '@{batch_mark}'    
        ]]>
        </sql>
       
       <sql name="resetRebateQtyDetailOtherFields">
        <![CDATA[
            update rebate_qty_detail book_detail
            set book_detail.qty_net = book_detail.qty_book - book_detail.qty_used,
                book_detail.qty_available = book_detail.qty_book - book_detail.qty_used - book_detail.qty_freeze
            where book_detail.batch_mark = '@{batch_mark}'    
        ]]>
        </sql>
 
        <sql name="getNegativeRebateAmtDetail">
            <![CDATA[
                   select count(1)
                   from rebate_amt_detail 
                   where batch_mark = '@{batch_mark}' 
                     and ((amt_available < 0) or (amt_net < 0)) ;
               ]]>
        </sql>    
        
        <sql name="getNegativeRebateQtyDetail">
            <![CDATA[
                   select count(1)
                   from rebate_qty_detail 
                   where batch_mark = '@{batch_mark}' 
                     and ((qty_available < 0) or (qty_net < 0)) ;
               ]]>
        </sql>    
    </dataSpace>
    
    <dataSpace name="rebate-validator">
        <sql name="getNotEnoughImplantDetails">
          <![CDATA[
                   select so_implant_detail.*, wm_book_detail.qty_available 
                from so_implant_detail 
                   left join wm_book_detail 
                       on so_implant_detail.warehouse_id = wm_book_detail.warehouse_id
                       and so_implant_detail.product_id = wm_book_detail.product_id
                       and so_implant_detail.order_right_code = wm_book_detail.order_right_code
                       and so_implant_detail.stock_type_code = wm_book_detail.stock_type_code
                       and ifnull(so_implant_detail.batch_no, '') = ifnull(wm_book_detail.batch_no, '')
                       and ifnull(so_implant_detail.batch_sn, '') = ifnull(wm_book_detail.batch_sn, '')
                   where parent_id = '@{document_id}' 
                     and so_implant_detail.qty > IFNull(wm_book_detail.qty_available, 0)    
           ]]>               
        </sql>    
    </dataSpace>    
    
</sqls>