Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 13.02 KB | None | 0 0
  1. USE [ILS]
  2. GO
  3. /****** Object:  UserDefinedFunction [dbo].[KC_CHECK_APPLY]    Script Date: 3/16/2017 5:34:06 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.            
  9. -- =============================================
  10. -- Author:  <vbelko>
  11. -- Create date: <01/03/17>
  12. -- Description: <INVENTORY_ADJUSTMENT.Процедура запускаемая при нажатии "ОК" для выполнения корректировки.
  13. -- Валидация LPN на доступное количество и что текущий INTERNAL_LOC_INV до сих пор соответствует переданному списку.
  14. -- EXEC KC_CHECK_APPLY N'S',@list,1
  15. --declare @list dbo.InvMgmtListGo
  16. -- insert into @list (IntLocNum, Location, LPN,
  17.  
  18. -- allocated_qty, on_hand_qty, in_transit_qty
  19. -- ,suspense_qty , inventory_sts , PLANT
  20. -- ) values  ( 965,N'AA-09-34-7',N'AD_13_3_02','7','230','0','0','N','1000'  )
  21.  
  22.  
  23. --select * from dbo.KC_CHECK_APPLY(N'S', @list, 1)
  24. -- =============================================
  25. ALTER function [dbo].[KC_CHECK_APPLY] (
  26.    @TransType nvarchar(25)
  27.   ,@list InvMgmtListGo READONLY
  28.   ,@Num int
  29. )
  30. --таблица возвращаемая после всех обработок    
  31. returns @t table (
  32.      row_num int
  33.     ,IntLocNum int
  34.     ,LPN nvarchar(50)
  35.     ,Location nvarchar(25)
  36.     ,error_msg nvarchar(25)
  37. )
  38. AS BEGIN
  39. -- объявляем таблицу для записи ошибок
  40.  
  41. declare @error_table table (
  42.      row_num int
  43.     ,error_msg nvarchar(32)
  44. )
  45.  --объявляем таблицу в которую заносятся данные из @list
  46.  
  47.                
  48. declare @__t table (
  49.     row_num int, IntLocNum int, LPN nvarchar(50), Location nvarchar(25)
  50.     ,allocated_qty numeric (19,5), on_hand_qty numeric (19,5), in_transit_qty numeric (19,5)
  51.  ,suspense_qty numeric (19,5) ,inventory_sts nvarchar (25), PLANT nvarchar (25)
  52. )
  53. ;with
  54.      finish_table as (
  55.         select
  56.              ROW_NUMBER () over (order by IntLocNum) row_num   
  57.             ,IntLocNum, Location, LPN
  58.             ,allocated_qty, on_hand_qty, in_transit_qty
  59.             ,suspense_qty, inventory_sts, PLANT
  60.         from @list
  61.     )
  62.  
  63. insert into @__t (row_num, IntLocNum, LPN, Location,allocated_qty, on_hand_qty, in_transit_qty
  64.             ,suspense_qty, inventory_sts, PLANT)
  65. select row_num, IntLocNum, LPN, Location, allocated_qty, on_hand_qty, in_transit_qty
  66.             , suspense_qty, inventory_sts, PLANT
  67. from finish_table
  68.  
  69.  
  70. --create error message table
  71. declare
  72.      @max int = (select max(row_num) from @__t)
  73.     ,@i int = 1
  74.     ,@msg nvarchar(25);
  75. --заполнение таблицы с ошибками @error_table
  76. while @i !> @max
  77. begin
  78.  
  79.     if exists (
  80.         select l.LPN
  81.         from @__t l
  82.              join LOCATION_INVENTORY li
  83.                 on l.IntLocNum = li.INTERNAL_LOCATION_INV
  84.                 and li.on_hand_qty = l.on_hand_qty
  85.                 and li.allocated_qty = l.allocated_qty
  86.                 and li.in_transit_qty = l.in_transit_qty
  87.                 and li.SUSPENSE_QTY = l.suspense_qty
  88.                 and li.LOCATION = l.Location
  89.                 and li.LOGISTICS_UNIT = l.LPN
  90.             inner join KC_LPN kc
  91.                 on li.LOGISTICS_UNIT = kc.LPN
  92.                 and kc.ERP_PLANT = l.PLANT
  93.         where l.row_num = @i
  94.     )
  95.     begin        
  96.         if (@TransType = N'S' or @TransType = 'W') and exists (
  97.             select top 1 1
  98.             from @__t l
  99.                 inner join location_inventory li on li.internal_location_inv = l.IntLocNum
  100.                  join location loc on loc.LOCATION = li.LOCATION and li.LOCATION =  l.Location
  101.                 -- left join WORK_INSTRUCTION wi on li.LOGISTICS_UNIT = wi.LOGISTICS_UNIT
  102.  
  103.             where
  104.                 l.row_num = @i
  105.                 --and li.on_hand_qty > 0
  106.                 --and li.allocated_qty = 0
  107.                 --and li.in_transit_qty = 0
  108.                 --and loc.LOCATION_CLASS = 'Inventory'
  109.                 --and loc.ACTIVE = 'Y'
  110.                 --and loc.LOCATION_STS != 'Frozen'
  111.                 --and wi.CONDITION in (N'Open', N'In Process')
  112.                 --and (wi.LOGISTICS_UNIT is not null
  113.                 --and wi.LOGISTICS_UNIT = l.LPN)
  114.            
  115.         )
  116.         begin
  117.             set @msg = null
  118.             insert into @error_table(row_num, error_msg) values (@i, @msg)
  119.             set @i += 1          
  120.         end
  121.        
  122.         else if (@TransType = N'S' or @TransType = 'W') and exists (
  123.             select top 1 1
  124.             from @__t l
  125.                  left join location_inventory li on l.IntLocNum=li.internal_location_inv
  126.                  join location loc on loc.LOCATION = li.LOCATION
  127.             where
  128.                 l.row_num = @i
  129.                 and li.allocated_qty > 0
  130.                 and loc.LOCATION_CLASS='Inventory'
  131.                 and loc.ACTIVE = 'Y'
  132.                 and loc.LOCATION_STS != 'Frozen'
  133.                 and li.LOCATION =  l.Location
  134.     )
  135.     begin
  136.     set @msg = N'ERR01'
  137.     insert into @error_table(row_num, error_msg) values (@i, @msg)
  138.     set @i += 1
  139.    
  140.     end
  141.  
  142. else
  143.     if (@TransType = N'S' or @TransType = 'W') and exists (
  144.         select top 1 1
  145.         from @__t l                          
  146.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  147.              join location loc on loc.LOCATION = li.LOCATION
  148.         where
  149.             l.row_num = @i
  150.             and li.SUSPENSE_QTY > 0
  151.             and loc.LOCATION_CLASS ='Inventory'
  152.             and loc.ACTIVE = 'Y'
  153.             and loc.LOCATION_STS != 'Frozen'
  154.             and li.LOCATION =  l.Location  
  155.     )
  156.             begin
  157.             set @msg = 'ERR02'
  158.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  159.             set @i += 1
  160.            
  161.             end
  162. else
  163.     if (@TransType = N'S' or @TransType = 'W') and exists (
  164.         select top 1 1
  165.         from @__t l                          
  166.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  167.              join location loc on loc.LOCATION = li.LOCATION
  168.         --where
  169.         --  l.row_num = @i
  170.         --  and li.ON_HAND_QTY = 0
  171.         --  and loc.LOCATION_CLASS = 'Inventory'
  172.         --  and loc.ACTIVE= 'Y'
  173.         --  and loc.LOCATION_STS != 'Frozen'
  174.         --  and li.LOCATION =  l.Location  
  175.     )
  176.             begin
  177.             set @msg = 'ERR03'
  178.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  179.             set @i += 1
  180.            
  181.             end
  182.  
  183. else
  184.     if (@TransType = N'S' or @TransType = 'W') and exists (
  185.         select top 1 1
  186.         from @__t l                          
  187.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  188.              join location loc on loc.LOCATION = li.LOCATION
  189.         --where
  190.         --  l.row_num = @i
  191.         --  and li.ON_HAND_QTY = 0
  192.         --  and li.ALLOCATED_QTY = 0      
  193.         --  and li.IN_TRANSIT_QTY = 0
  194.         --  and li.SUSPENSE_QTY = 0
  195.         --  and loc.LOCATION_CLASS='Inventory'
  196.         --  and loc.ACTIVE='Y'
  197.         --  and loc.LOCATION_STS != 'Frozen'
  198.         --  and li.LOCATION =  l.Location      
  199.     )
  200.         begin
  201.             set @msg = 'ERR04'
  202.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  203.             set @i += 1
  204.            
  205.         end
  206.  
  207. if @TransType = 'T' and exists (
  208.         select top 1 1
  209.         from @__t l
  210.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  211.              join location loc on loc.LOCATION = li.LOCATION
  212.              --left join WORK_INSTRUCTION wi on li.LOGISTICS_UNIT = wi.LOGISTICS_UNIT
  213.  
  214.         where
  215.             l.row_num = @i
  216.             and li.on_hand_qty > 0
  217.             and li.allocated_qty = 0
  218.             and li.in_transit_qty = 0
  219.             and loc.LOCATION_CLASS <> 'Receiving Dock'
  220.             --and loc.LOCATION_CLASS <> 'Shipping Dock'
  221.             and loc.LOCATION_CLASS <> 'P&D'
  222.             and loc.ACTIVE = 'Y'
  223.             and loc.LOCATION_STS <> 'Frozen'
  224.             and li.LOCATION =  l.Location
  225.             --and wi.CONDITION in (N'Open', N'In Process')
  226.             --and (wi.LOGISTICS_UNIT is not null
  227.             --and wi.LOGISTICS_UNIT = l.LPN)
  228.             --and loc.LOCATION=l.Location
  229.         )
  230.         begin
  231.         set @msg = null
  232.         insert into @error_table(row_num, error_msg) values (@i, @msg)
  233.         set @i += 1
  234.        
  235.         end
  236.  
  237. else    if @TransType = N'T' and exists (
  238.         select top 1 1
  239.         from @__t l
  240.              left join location_inventory li on l.IntLocNum=li.internal_location_inv
  241.              join location loc on loc.LOCATION = li.LOCATION
  242.         --where
  243.         --  l.row_num = @i
  244.         --  and li.allocated_qty > 0
  245.         --  and loc.LOCATION_CLASS <> 'Receiving Dock'
  246.         --  --and loc.LOCATION_CLASS <> 'Shipping Dock'
  247.         --  and loc.LOCATION_CLASS <> 'P&D'
  248.         --  and loc.ACTIVE = 'Y'
  249.         --  and loc.LOCATION_STS <> 'Frozen'
  250.         --  and li.LOCATION =  l.Location
  251.     )
  252.     begin
  253.     set @msg = N'ERR01'
  254.     insert into @error_table(row_num, error_msg) values (@i, @msg)
  255.     set @i += 1
  256.    
  257.     end
  258.  
  259. else
  260.     if @TransType = 'T' and exists (
  261.         select top 1 1
  262.         from @__t l                          
  263.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  264.              join location loc on loc.LOCATION = li.LOCATION
  265.         --where
  266.         --  l.row_num = @i
  267.         --  and li.SUSPENSE_QTY > 0
  268.         --  and loc.LOCATION_CLASS <> 'Receiving Dock'
  269.         --  --and loc.LOCATION_CLASS <> 'Shipping Dock'
  270.         --  and loc.LOCATION_CLASS <> 'P&D'
  271.         --  and loc.ACTIVE = 'Y'
  272.         --  and loc.LOCATION_STS <> 'Frozen'
  273.         --  and li.LOCATION =  l.Location
  274.                
  275.     )
  276.             begin
  277.             set @msg = 'ERR02'
  278.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  279.             set @i += 1
  280.  
  281.             end
  282. else
  283.     if @TransType = 'T' and exists (
  284.         select top 1 1
  285.         from @__t l                          
  286.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  287.              join location loc on loc.LOCATION = li.LOCATION
  288.         --where
  289.         --  l.row_num = @i
  290.         --  and li.ON_HAND_QTY = 0
  291.         --  and loc.LOCATION_CLASS <> 'Receiving Dock'
  292.         --  --and loc.LOCATION_CLASS <> 'Shipping Dock'
  293.         --  and loc.LOCATION_CLASS <> 'P&D'
  294.         --  and loc.ACTIVE = 'Y'
  295.         --  and loc.LOCATION_STS <> 'Frozen'
  296.         --  and li.LOCATION =  l.Location  
  297.     )
  298.             begin
  299.             set @msg = 'ERR03'
  300.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  301.             set @i += 1
  302.        
  303.             end
  304.  
  305. else
  306.     if @TransType = 'T' and exists (
  307.         select top 1 1
  308.         from @__t l                          
  309.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  310.              join location loc on loc.LOCATION = li.LOCATION
  311.         --where
  312.         --  l.row_num = @i
  313.         --  and li.ON_HAND_QTY = 0
  314.         --  and li.ALLOCATED_QTY = 0      
  315.         --  and li.IN_TRANSIT_QTY = 0
  316.         --  and li.SUSPENSE_QTY = 0
  317.         --  and loc.LOCATION_CLASS <> 'Receiving Dock'
  318.         --  --and loc.LOCATION_CLASS <> 'Shipping Dock'
  319.         --  and loc.LOCATION_CLASS <> 'P&D'
  320.         --  and loc.ACTIVE = 'Y'
  321.         --  and loc.LOCATION_STS <> 'Frozen'
  322.         --  and li.LOCATION =  l.Location  
  323.     )
  324.         begin
  325.             set @msg = 'ERR04'
  326.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  327.             set @i += 1
  328.        
  329.         end
  330.  
  331. if @TransType = N'Q' and exists (
  332.         select top 1 1
  333.         from @__t l
  334.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  335.              join location loc on loc.LOCATION = li.LOCATION
  336.              left join WORK_INSTRUCTION wi on li.LOGISTICS_UNIT = wi.LOGISTICS_UNIT
  337.  
  338.         --where
  339.         --  l.row_num = @i
  340.         --  and li.on_hand_qty > 0
  341.         --  and li.allocated_qty = 0
  342.         --  and li.in_transit_qty = 0
  343.         --  and loc.LOCATION_CLASS = 'Inventory'
  344.         --  and loc.LOCATION = l.Location
  345.            
  346.         )
  347.         begin
  348.         set @msg = null
  349.         insert into @error_table(row_num, error_msg) values (@i, @msg)
  350.         set @i += 1
  351.         end
  352. else   
  353.     if @TransType = N'Q' and exists (
  354.         select top 1 1
  355.         from @__t l
  356.              left join location_inventory li on l.IntLocNum=li.internal_location_inv
  357.              join location loc on loc.LOCATION = li.LOCATION
  358.         --where
  359.         --  l.row_num = @i
  360.         --  and li.allocated_qty > 0
  361.         --  and loc.LOCATION_CLASS = 'Inventory'
  362.         --  and loc.LOCATION = l.Location
  363.     )
  364.     begin
  365.     set @msg = N'ERR01'
  366.     insert into @error_table(row_num, error_msg) values (@i, @msg)
  367.     set @i += 1
  368.    
  369.     end
  370.  
  371. else
  372.     if @TransType = 'Q' and exists (
  373.         select top 1 1
  374.         from @__t l                          
  375.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  376.              join location loc on loc.LOCATION = li.LOCATION
  377.         --where
  378.         --  l.row_num = @i
  379.         --  and li.SUSPENSE_QTY > 0
  380.         --  and li.LOCATION =  l.Location
  381.         --  and loc.LOCATION_CLASS = 'Inventory'
  382.         --  and loc.LOCATION = l.Location  
  383.     )
  384.             begin
  385.             set @msg = 'ERR02'
  386.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  387.             set @i += 1
  388.             end
  389. else
  390.     if @TransType = 'Q' and exists (
  391.         select top 1 1
  392.         from @__t l                          
  393.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  394.              join location loc on loc.LOCATION = li.LOCATION
  395.         where
  396.             l.row_num = @i
  397.             and li.ON_HAND_QTY = 0
  398.             and loc.LOCATION_CLASS = 'Inventory'
  399.             and loc.LOCATION = l.Location  
  400.     )
  401.             begin
  402.             set @msg = 'ERR03'
  403.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  404.             set @i += 1
  405.             end
  406.  
  407. else
  408.     if @TransType = 'Q' and exists (
  409.         select top 1 1
  410.         from @__t l                          
  411.              left join location_inventory li on l.IntLocNum = li.internal_location_inv
  412.              join location loc on loc.LOCATION = li.LOCATION
  413.         --where
  414.         --  l.row_num = @i
  415.         --  and li.ON_HAND_QTY = 0
  416.         --  and li.ALLOCATED_QTY = 0      
  417.         --  and li.IN_TRANSIT_QTY = 0
  418.         --  and li.SUSPENSE_QTY = 0    
  419.     )
  420.         begin
  421.             set @msg = 'ERR04'
  422.             insert into @error_table(row_num, error_msg)  values (@i, @msg)
  423.             set @i += 1
  424.         end
  425.  
  426.  
  427. else
  428. set @msg = null
  429. -- вставляем обработанные строки и возникшие для них ошибки
  430. insert into @t (
  431.      row_num
  432.     ,IntLocNum
  433.     ,LPN
  434.     ,Location
  435.     ,error_msg
  436. )
  437.  
  438. select t.row_num, t.IntLocNum, t.LPN, t.Location, e.error_msg
  439. from @__t t join (select row_num, error_msg from @error_table)e on e.row_num = t.row_num
  440.  
  441.  
  442. end
  443. -- else
  444. --set @msg = 'ERR06'
  445. -- insert into @t (
  446. --   row_num
  447. --  ,IntLocNum
  448. --  ,LPN
  449. --  ,Location
  450. --  ,error_msg
  451. --)
  452.  
  453. --select t.row_num, t.IntLocNum, t.LPN, t.Location, e.error_msg
  454. --from @__t t join (select row_num, error_msg from @error_table)e on e.row_num = t.row_num
  455.  
  456. end
  457.  
  458.    
  459.  
  460. return
  461. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement