Project

General

Profile

Inventory Patching.sql

Muhammad Arif, 06/06/2024 10:31 AM

 
1
-- Check Inventory Tables
2
Select * from warehouse_inventory_header where org_id ='KYMMSENG' order by txn_date desc
3
Select * from warehouse_inventory_detail where warehouse_inventory_hdr_id = 'KSWHGS0001-SUNTORY-20240606'
4
Select * from mst_warehouse_item wi where wi.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'
5

    
6
-- Patch inventory -- (Note: warehouse_inventory_detail is stock UOM)
7
        --Update warehouse_inventory_detail set
8
        --opening_gs = x.closing_gs,
9
        --reserved_gs = x.reserved_gs,
10
        --closing_gs = x.closing_gs,
11
        --opening_bs = x.closing_bs,
12
        --closing_bs = x.closing_bs,
13
        --updated_by = 'System', updated_date = getdate()
14
        --from (
15
        --Select * from warehouse_inventory_detail where warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240605'
16
        --) x
17
        --where warehouse_inventory_detail.warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240606' and warehouse_inventory_detail.item_id = x.item_id and x.uom_id = warehouse_inventory_detail.uom_id
18

    
19
-- Patch closing stock of yesterday's inventory --
20
        --Update warehouse_inventory_detail set closing_gs = y.closing_gs, closing_bs = y.closing_bs from (
21
        --Select dd.warehouse_inventory_dtl_id, dd.item_id, dd.warehouse_inventory_hdr_id,  x.closing_gs, x.closing_bs from warehouse_inventory_detail dd 
22
        --inner join (
23
        --Select warehouse_inventory_dtl_id, warehouse_inventory_hdr_id, item_id, uom_id, opening_gs, stock_in_gs,
24
        --stock_out_gs, reserved_gs, (opening_gs + stock_in_gs - stock_out_gs) [closing_gs] , opening_bs, stock_in_bs, stock_out_bs, (opening_bs + stock_in_bs - stock_out_bs) [closing_bs]
25
        --from warehouse_inventory_detail where warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240605' 
26

    
27
        --) x on x.warehouse_inventory_dtl_id = dd.warehouse_inventory_dtl_id 
28
        --where dd.closing_gs <> x.closing_gs or dd.closing_bs <> x.closing_bs
29
        --) y where y.warehouse_inventory_dtl_id = warehouse_inventory_detail.warehouse_inventory_dtl_id and y.warehouse_inventory_hdr_id = warehouse_inventory_detail.warehouse_inventory_hdr_id and y.item_id = warehouse_inventory_detail.item_id and warehouse_inventory_detail.warehouse_inventory_hdr_id = 'KSWHGS0001-SUNTORY-20240605' 
30

    
31

    
32
-- Insert into warehouse_inventory_detail where not exists --
33
        --insert into warehouse_inventory_detail (warehouse_inventory_dtl_id, warehouse_inventory_hdr_id, item_id, uom_id, opening_gs, stock_in_gs, stock_out_gs, reserved_gs, closing_gs, opening_bs, stock_in_bs, stock_out_bs, closing_bs, status, created_date, created_by, updated_date ,updated_by, tenant_id, org_id)
34
        --Select newid(), 'KSWHGS0001-SUNTORY-20240606', item_id, uom_id, closing_gs, 0, 0, reserved_gs, closing_gs, closing_bs, 0, 0, closing_bs, status, GEtDAte(), 'System', GetDate() ,'System', tenant_id, org_id
35
        --from warehouse_inventory_detail dd where warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240605' 
36
        --and not exists (Select 1 from warehouse_inventory_detail d where d.warehouse_inventory_hdr_id = 'KSWHGS0001-SUNTORY-20240606' and d.item_id = dd.item_id)
37

    
38

    
39
        -- Check mst_warehouse_item --
40
        Select * from mst_warehouse_item wi
41
        inner join warehouse_inventory_detail d on d.item_id = wi.item_id
42
        where wi.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'  and d.warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240606'
43

    
44
-- Check duplication in mst_warehouse_item
45
        Select * from mst_warehouse_item w where exists (
46
        Select 
47
        item_id from mst_warehouse_item wi where wi.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'  
48
        and w.item_id = wi.item_id
49
        group by item_id having count(1) > 1
50
        )
51
        and w.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'  
52
        order by item_id
53

    
54
-- Delete Duplication in mst_Warehouse_item
55
        --delete from mst_warehouse_item where tenant_id = 'KYMMSENG' and warehouse_item_id in (
56
        Select warehouse_item_id from mst_warehouse_item w where exists (
57
        Select 
58
        item_id from mst_warehouse_item wi where wi.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'  
59
        and w.item_id = wi.item_id
60
        group by item_id having count(1) > 1
61
        )
62
        and w.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3' and tenant_id ='KYMMSENG'
63
        --)
64

    
65
-- Update quantity in mst_warehouse_item (quantity in lowest uom ) --
66
        --Update mst_warehouse_item set
67
        --quantity_available = x.quantity
68
        --from (
69
        --Select wi.warehouse_item_id, wi.item_id, quantity_available, wi.warehouse_id, d.closing_gs, p.cf_oth_uom, d.closing_gs * p.cf_oth_uom [quantity] from mst_warehouse_item wi
70
        --inner join warehouse_inventory_detail d on d.item_id = wi.item_id
71
        --inner join product_item p on p.item_id = d.item_id 
72
        --where wi.warehouse_id ='0086d5d6-b9f1-44ba-a35c-1051b438ebf3'  and d.warehouse_inventory_hdr_id ='KSWHGS0001-SUNTORY-20240606'
73
        --and Cast(d.closing_gs * p.cf_oth_uom as decimal(18,2)) <> wi.quantity_available
74

    
75
        --) x
76
        --where mst_warehouse_item.item_id = x.item_id and mst_warehouse_item.warehouse_item_id = x.warehouse_item_id