P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?xml version="1.0" encoding="UTF-8" ?>
<?DOCTYPE sqls SYSTEM "../config/sql.dtd" ?>
 
<sqls>
    <dataSpace name="stock">
        <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 @{guid}, @{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 @{guid}, '', @{getYear(}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="lockStockBook">
            update wm_book_general set batch_mark = '@{batch_mark}'
            where warehouse_id = '@{warehouse_id}' 
              and exists (
                          select 1 from @{detailTableName} document_detail
                          where document_detail.parent_id = '@{document_id}'
                            and document_detail.product_id = wm_book_general.product_id
                )
                and wm_book_general.order_right_code = '@{order_right_code}'
                   and wm_book_general.stock_type_code = '@{stock_type_code}'
        </sql>
        
        <sql name="getStockFlowBasicFields">
        <![CDATA[
               select
                   document_detail.id as doc_detail_id, 
                   @{warehouse_id} as warehouse_id, @{warehouse_code} as warehouse_code, @{warehouse_name} as warehouse_name,
                   @{document_doc_type} as document_doc_type, document.code doc_code, document.doc_date,
                   document_detail.sku_id, document_detail.product_id, document_detail.product_code,
                   document_detail.product_id material_code, document_detail.product_name material_name, 
                   document_detail.batch_no, document_detail.batch_sn, document_detail.valid_from, document_detail.valid_to,
                   @{document_order_right} as document_order_right,
                   @{document_stock_type} as document_stock_type, 
                   @{document_qty} as qty,
                   document.bu_name,md_bu.id bu_id
               from @{detailTableName} document_detail
               left join @{tableName} document on document_detail.parent_id = document.id
               left join md_bu on md_bu.name = document.bu_name
               where  @{filter}
        ]]>
        </sql>
        
        <sql name="getStockFlowBasicFieldsByImplantCancel">
        <![CDATA[
               select
                   document_detail.id as doc_detail_id, 
                   @{warehouse_id} as warehouse_id, @{warehouse_code} as warehouse_code, @{warehouse_name} as warehouse_name,
                   @{document_doc_type} as document_doc_type, document.code doc_code, document.doc_date,
                   document_detail.sku_id, document_detail.product_id, document_detail.product_code, 
                   document_detail.product_id material_code, document_detail.product_name material_name, 
                   document_detail.batch_no, document_detail.batch_sn, document_detail.valid_from, document_detail.valid_to,
                   @{document_order_right} as document_order_right, document.bu_id, document.bu_name, 
                   @{document_stock_type} as document_stock_type,
                   @{document_qty} as qty
               from @{detailTableName} document_detail
               left join @{tableName} document on document_detail.parent_id = document.host_id
               where @{filter} 
        ]]>
        </sql>        
        
        <sql name="getFlowFields">
        <![CDATA[
               select flow.*, null qty
               from @{tableName} flow
               where 1 <> 1 
        ]]>
        </sql>    
        
        <sql name="writeStockFlowBookStock">
        <![CDATA[    
            update wm_book_flow book_flow
            set book_flow.book_detail_id = (
                select id from wm_book_detail book_detail
                where book_detail.warehouse_id = book_flow.warehouse_id
                       and book_detail.sku_id = book_flow.sku_id
                       and book_detail.product_id = book_flow.product_id
                       and @{IfEmpty}(book_detail.batch_no, '--') = @{IfEmpty}(book_flow.batch_no, '--')
                       and @{IfEmpty}(book_detail.batch_sn, '--') = @{IfEmpty}(book_flow.batch_sn, '--')
                       and book_detail.order_right_code = book_flow.order_right_code
                       and book_detail.stock_type_code = book_flow.stock_type_code )
               where book_flow.batch_mark = '@{batch_mark}'
        ]]>
        </sql>
        
        <sql name="writeStockFlowBookProduct">
        <![CDATA[    
            update wm_book_flow book_flow
            set book_flow.product_code = (
                    select product_code from md_prod_sku
                    where book_flow.sku_id = md_prod_sku.id ),
                book_flow.product_name = (
                    select product_name from md_prod_sku
                    where book_flow.sku_id = md_prod_sku.id ),
                book_flow.spec = (
                    select spec from md_prod_sku
                    where book_flow.sku_id = md_prod_sku.id )
               where book_flow.batch_mark = '@{batch_mark}' and exists (
                   select 1 from md_prod_sku
                where book_flow.sku_id = md_prod_sku.id )
        ]]>
        </sql>
        
        <sql name="writeStockFlowBookOtherFields">
        <![CDATA[    
            update wm_book_flow book_flow
            set record_operator =  CASE 
                WHEN book_detail_id is null THEN
                    'insert'
                ELSE
                    'update'
                END,
                book_flow.order_right_name = (
                    select name from wm_order_right
                    where book_flow.order_right_code = wm_order_right.code), 
                book_flow.stock_type_name = (
                    select name from wm_stock_type
                    where book_flow.stock_type_code = wm_stock_type.code) 
               where book_flow.batch_mark = '@{batch_mark}'
        ]]>
        </sql>
        
        <sql name="writeInsertStockDetail">
        <![CDATA[
            insert into wm_book_detail (
                batch_mark, id, bu_id,bu_name,
                org_id, org_code, org_name, account_id,
                warehouse_id, warehouse_code, warehouse_name, 
                sku_id, product_id, product_code, product_name, batch_no, batch_sn, 
                 valid_from, valid_to, order_right_code, stock_type_code,
                qty_total, qty_frozen, qty_available, type_code, type_name, 
                create_time, update_time
            )
            select '@{batch_mark}' as batch_mark, @{guid} as id, bu_id,bu_name,
                '@{org_id}', '@{org_code}', '@{org_name}',account_id,
                warehouse_id, warehouse_code, warehouse_name, 
                book_detail.sku_id, book_detail.product_id, product_code, product_name, batch_no, batch_sn,
                valid_from, valid_to, order_right_code, stock_type_code,
                sum_total, sum_frozen, sum_total - sum_frozen, book_detail.type_code, type_name,
                @{create_time}, @{update_time}
            from (
                select  
                    bu_id, bu_name, account_id,
                    warehouse_id, warehouse_code, warehouse_name, 
                    wm_book_flow.sku_id, product_id, batch_no, batch_sn, valid_from, valid_to,
                    order_right_code, stock_type_code, type_code, type_name, 
                    sum(@{IfEmpty}(qty_add, 0)) - sum(@{IfEmpty}(qty_delete, 0)) as sum_total,
                    sum(@{IfEmpty}(qty_freeze, 0)) - sum(@{IfEmpty}(qty_unfreeze, 0)) as sum_frozen
                from wm_book_flow
                where batch_mark = '@{batch_mark}' and record_operator = 'insert'
                group by bu_id, sku_id, account_id, warehouse_id, warehouse_code, warehouse_name, product_id, batch_no, 
                    valid_from, valid_to, batch_sn, order_right_code, stock_type_code, type_code, type_name
            ) book_detail
            left join md_org_account on md_org_account.id = book_detail.ACCOUNT_ID
            left join MD_PROD_SKU on MD_PROD_SKU.id = book_detail.sku_id
        ]]>
        </sql>
 
        <sql name="getNegativeStockDetail">
            <![CDATA[
                   select *
                   from wm_book_detail 
                   where batch_mark = '@{batch_mark}' 
                     and ((qty_total < 0) or (qty_available < 0) or (qty_total < qty_frozen))
               ]]>
        </sql>    
        
        <sql name="resetStockDetailProduct">
        <![CDATA[
            update wm_book_detail book_detail
            set book_detail.product_code = (
                select product_code from md_prod_sku
                where book_detail.sku_id = md_prod_sku.id ),
                book_detail.product_name = (
                    select product_name from md_prod_sku
                    where book_detail.sku_id = md_prod_sku.id ),
                book_detail.spec = (
                    select spec from md_prod_sku
                    where book_detail.sku_id = md_prod_sku.id ),
                book_detail.unit = (
                    select unit from md_prod_sku
                    where book_detail.sku_id = md_prod_sku.id )
            where book_detail.batch_mark = '@{batch_mark}' and exists (
                    select 1 from md_prod_sku
                    where book_detail.sku_id = md_prod_sku.id )
        ]]>
        </sql>
        
        <sql name="resetStockDetailOtherFields">
        <![CDATA[
            update wm_book_detail book_detail
            set book_detail.qty_available = book_detail.qty_total - book_detail.qty_frozen,
                book_detail.order_right_name = (
                    select name from wm_order_right
                    where book_detail.order_right_code = wm_order_right.code
                ),
                book_detail.stock_type_name = (
                    select name from wm_stock_type
                    where book_detail.stock_type_code = wm_stock_type.code
                )
            where book_detail.batch_mark = '@{batch_mark}'
        ]]>
        </sql>
        
        <sql name="writeUpdateStockDetail">
        <![CDATA[
            update wm_book_detail book_detail
            inner join wm_book_flow on wm_book_flow.book_detail_id = book_detail.id and wm_book_flow.batch_mark = '@{batch_mark}'
            set book_detail.batch_mark = '@{batch_mark}', 
                book_detail.update_time = @{update_time},
                book_detail.qty_total = book_detail.qty_total + (
                    select sum_total_change
                    from (
                        select book_detail_id, (sum(@{IfEmpty}(qty_add, 0)) - sum(@{IfEmpty}(qty_delete, 0))) sum_total_change
                        from wm_book_flow
                        where batch_mark = '@{batch_mark}' and record_operator = 'update'
                        group by book_detail_id
                       ) flow_count
                       where flow_count.book_detail_id = book_detail.id ),
                   book_detail.qty_frozen = book_detail.qty_frozen + (
                    select sum_frozen_change
                    from (
                        select book_detail_id, (sum(@{IfEmpty}(qty_freeze, 0)) - sum(@{IfEmpty}(qty_unfreeze, 0))) as sum_frozen_change
                        from wm_book_flow
                        where batch_mark = '@{batch_mark}' and record_operator = 'update'
                        group by book_detail_id
                       ) flow_count
                   where flow_count.book_detail_id = book_detail.id )
        ]]>
        </sql>    
        
        <sql name="writeDeleteStockDetail">
               delete from wm_book_detail 
               where batch_mark = '@{batch_mark}' and qty_total = 0
        </sql>
    
        <sql name="writeInsertStockGeneral">
        <![CDATA[
            insert into wm_book_general (
                id, 
                org_id, org_code, org_name, 
                warehouse_id, warehouse_code, warehouse_name,
                product_id, order_right_code, stock_type_code,
                qty_total, qty_frozen, qty_available,
                create_time, update_time
            )
            select @{guid}, 
                '@{org_id}', '@{org_code}', '@{org_name}', 
                book_detail.warehouse_id, book_detail.warehouse_code, book_detail.warehouse_name,
                book_detail.product_id, 
                book_detail.order_right_code, book_detail.stock_type_code, 
                book_detail.qty_total, book_detail.qty_frozen,
                book_detail.qty_total - book_detail.qty_frozen as qty_available, 
                @{create_time}, @{update_time}                
            from (
                select org_id, 
                    warehouse_id, warehouse_code, warehouse_name,
                    product_id, order_right_code, stock_type_code, 
                    sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen
                from wm_book_detail
                where batch_mark = '@{batch_mark}'
                group by org_id, warehouse_id, warehouse_code, warehouse_name, product_id, valid_from, valid_to, order_right_code, stock_type_code
            ) book_detail
            left join wm_book_general book_general 
                on book_general.org_id = book_detail.org_id
                and book_general.warehouse_id = book_detail.warehouse_id
                and book_general.product_id = book_detail.product_id
                and book_general.order_right_code = book_detail.order_right_code
                and book_general.stock_type_code = book_general.stock_type_code
            where book_detail.org_id = '@{org_id}' 
                and book_detail.warehouse_id = '@{warehouse_id}'
                and book_general.id is null
        ]]>
        </sql>
        
        <sql name="resetStockGeneralOtherFields">
        <![CDATA[
            update wm_book_general book_general
            left join md_sku on book_general.product_id = md_sku.id
            left join wm_order_right on book_general.order_right_code = wm_order_right.code
            left join wm_stock_type on book_general.stock_type_code = wm_stock_type.code
            set book_general.category_code = md_sku.authorize_category_code,
                book_general.category_name = md_sku.authorize_category_name,
                book_general.product_code = md_sku.code,
                book_general.product_name = md_sku.name,
                book_general.material_code = md_sku.material_code,
                book_general.material_name = md_sku.material_name,
                book_general.order_right_name = wm_order_right.name,
                book_general.stock_type_name = wm_stock_type.name,
                book_general.qty_available = book_general.qty_total - book_general.qty_frozen    
            where book_general.batch_mark = '@{batch_mark}'    
        ]]>
        </sql>
 
        <sql name="writeUpdateStockGeneral">
        <![CDATA[
            update wm_book_general book_general
            set book_general.qty_total = (
                select sum(qty_total) from wm_book_detail book_detail
                where book_general.warehouse_id = book_detail.warehouse_id
                    and book_general.product_id = book_detail.product_id
                    and book_general.order_right_code = book_detail.order_right_code
                    and book_general.stock_type_code = book_general.stock_type_code
                ),
                book_general.qty_frozen = (
                select sum(qty_frozen) from wm_book_detail book_detail
                where book_general.warehouse_id = book_detail.warehouse_id
                    and book_general.product_id = book_detail.product_id
                    and book_general.order_right_code = book_detail.order_right_code
                    and book_general.stock_type_code = book_general.stock_type_code
                ),                
                book_general.batch_mark = '@{batch_mark}',
                book_general.update_time = @{update_time}
            where exists (
                select 1 from wm_book_detail book_detail
                where book_detail.batch_mark = '@{batch_mark}'
                    and book_general.warehouse_id = book_detail.warehouse_id
                    and book_general.product_id = book_detail.product_id
                    and book_general.order_right_code = book_detail.order_right_code
                    and book_general.stock_type_code = book_general.stock_type_code
                )
        ]]>
        </sql>
      
        <sql name="writeDeleteStockGeneral">
               delete from wm_book_general 
               where batch_mark = '@{batch_mark}' and qty_total = 0
        </sql>
       
    </dataSpace>
    
    <dataSpace name="stock-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 @{IfEmpty}(so_implant_detail.batch_no, '--') = @{IfEmpty}(wm_book_detail.batch_no, '--')
                       and @{IfEmpty}(so_implant_detail.batch_sn, '--') = @{IfEmpty}(wm_book_detail.batch_sn, '--')
                   where parent_id = '@{document_id}' 
                     and so_implant_detail.qty > @{IfEmpty}(wm_book_detail.qty_available, 0)    
           ]]>               
        </sql>    
        
        
        
        <sql name="stockOverCheck">
            <![CDATA[
                update wm_book_detail 
                set error_message = '序列号管理的库存数量>1'
                where (batch_sn != '--' and qty_total > 1) ;
            ]]>
        </sql>
        
        <sql name="stockUnderCheck">
            <![CDATA[
                update wm_book_detail 
                set error_message = '库存数量小于0,需检查'
                where qty_total < 0 or qty_available < 0
            ]]>
        </sql>
    </dataSpace>    
    
</sqls>