SP_Cust_PDA_GetItemMasterWh.sql 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- ================================================
  2. -- Template generated FROM Template Explorer using:
  3. -- Create Procedure (New Menu).SQL
  4. --
  5. -- Use the Specify Values for Template Parameters
  6. -- command (Ctrl-Shift-M) to fill IN the parameter
  7. -- values below.
  8. --
  9. -- This block of comments will NOT be included IN
  10. -- the definition of the procedure.
  11. -- ================================================
  12. SET ANSI_NULLS ON
  13. GO
  14. SET QUOTED_IDENTIFIER ON
  15. GO
  16. -- =============================================
  17. -- Author: 曹嘉杰
  18. -- Create date: 2022/10/16
  19. -- Description: 查询料品存储信息
  20. -- =============================================
  21. IF (EXISTS (SELECT * FROM SYS.OBJECTS WHERE NAME = 'SP_Cust_PDA_GetItemMasterWh'))
  22. DROP PROC SP_Cust_PDA_GetItemMasterWh
  23. GO
  24. CREATE PROCEDURE SP_Cust_PDA_GetItemMasterWh
  25. -- Add the parameters for the stored procedure here
  26. @OrgCode NVARCHAR(50),
  27. @WhCode NVARCHAR(50),
  28. @BinCode NVARCHAR(50),
  29. @Filter NVARCHAR(500),
  30. @PageSize INT,
  31. @PageIndex INT
  32. AS
  33. BEGIN
  34. -- SET NOCOUNT ON added to prevent extra result sets FROM
  35. -- interfering with SELECT statements.
  36. SET NOCOUNT ON;
  37. -- Insert statements for procedure here
  38. IF OBJECT_ID('tempdb..#TempTableQoh') IS NOT NULL
  39. BEGIN
  40. DROP TABLE #TempTableQoh
  41. END
  42. DECLARE @ItemCode AS NVARCHAR(255)
  43. IF EXISTS (SELECT 1 FROM BC_BarCode WHERE Code = @Filter AND BarCodeType = 3)
  44. BEGIN
  45. SELECT @ItemCode = Segment1 FROM BC_BarCode WHERE Code = @Filter AND BarCodeType = 3
  46. END
  47. ELSE BEGIN
  48. SET @ItemCode = @Filter
  49. END
  50. select A.[ItemInfo_ItemCode] as [Item_ItemCode],
  51. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,'') as [ItemSeg_Name],
  52. A1.[SPECS] as [ItemID_SPECS],
  53. sum((((A.[StoreQty] - A.[ResvStQty]) - A.[ResvOccupyStQty]) - case when ((((A.[IsProdCancel] = 1) or (A.[MO_EntityID] != 0)) or A.[ProductDate] is not null) or (A.[WP_EntityID] != 0)) then A.[StoreQty] else convert(decimal(24,9),0) end )) as [CanUseQty],
  54. A2.[Round_Precision] as [Round1_Precision],
  55. sum((A.[StoreQty] + A.[ToRetStQty])) as [BalQty],
  56. sum((A.[StoreMainQty] + A.[ToRetStMainQty])) as [BalQty_Main],
  57. sum((((((A.[StoreQty] - A.[ResvStQty]) - A.[ResvOccupyStQty]) - case when ((((A.[IsProdCancel] = 1) or (A.[MO_EntityID] != 0)) or A.[ProductDate] is not null) or (A.[WP_EntityID] != 0)) then A.[StoreQty] else convert(decimal(24,9),0) end ) + A.[SupplyQtySU]) - A.[DemandQtySU])) as [Temp_PAB],
  58. convert(bigint,0) as [Item_ItemID],
  59. convert(bigint,0) as [W_Uom],
  60. convert(bigint,0) as [MainBaseSU_ID]
  61. into #TempTableQoh
  62. from InvTrans_WhQoh as A
  63. left join [CBO_ItemMaster] as A1 on (A.[ItemInfo_ItemID] = A1.[ID])
  64. left join [Base_UOM] as A2 on (A.[StoreUOM] = A2.[ID])
  65. left join [Base_Organization] as A3 on (A.[LogisticOrg] = A3.[ID])
  66. left join [CBO_Wh] as A4 on (A.[Wh] = A4.[ID])
  67. where (((((A1.[Name] is not null and (A1.[Name] != ''))
  68. and (A3.[Code] = @OrgCode))
  69. and (A4.[Code] = @WhCode))
  70. and (A.[ItemInfo_ItemCode] LIKE N'%' + @ItemCode + '%'))
  71. and (A.[BinInfo_Code] LIKE N'%' + @BinCode + '%'))
  72. group by A.[ItemInfo_ItemCode],
  73. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,''),
  74. A1.[SPECS],
  75. A2.[Round_Precision]
  76. HAVING (sum((A.[StoreQty] + A.[ToRetStQty])) > '0')
  77. ORDER BY A.[ItemInfo_ItemCode] ASC,
  78. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,'') ASC,
  79. A1.[SPECS] ASC
  80. OFFSET @PageIndex * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY
  81. IF NOT EXISTS (SELECT 1 FROM #TempTableQoh)
  82. BEGIN
  83. --INSERT INTO #TempTableQoh
  84. select A.[ItemInfo_ItemCode] as [Item_ItemCode],
  85. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,'') as [ItemSeg_Name],
  86. A1.[SPECS] as [ItemID_SPECS],
  87. sum((((A.[StoreQty] - A.[ResvStQty]) - A.[ResvOccupyStQty]) - case when ((((A.[IsProdCancel] = 1) or (A.[MO_EntityID] != 0)) or A.[ProductDate] is not null) or (A.[WP_EntityID] != 0)) then A.[StoreQty] else convert(decimal(24,9),0) end )) as [CanUseQty],
  88. A2.[Round_Precision] as [Round1_Precision],
  89. sum((A.[StoreQty] + A.[ToRetStQty])) as [BalQty],
  90. sum((A.[StoreMainQty] + A.[ToRetStMainQty])) as [BalQty_Main],
  91. sum((((((A.[StoreQty] - A.[ResvStQty]) - A.[ResvOccupyStQty]) - case when ((((A.[IsProdCancel] = 1) or (A.[MO_EntityID] != 0)) or A.[ProductDate] is not null) or (A.[WP_EntityID] != 0)) then A.[StoreQty] else convert(decimal(24,9),0) end ) + A.[SupplyQtySU]) - A.[DemandQtySU])) as [Temp_PAB],
  92. convert(bigint,0) as [Item_ItemID],
  93. convert(bigint,0) as [W_Uom],
  94. convert(bigint,0) as [MainBaseSU_ID]
  95. from InvTrans_WhQoh as A
  96. left join [CBO_ItemMaster] as A1 on (A.[ItemInfo_ItemID] = A1.[ID])
  97. left join [Base_UOM] as A2 on (A.[StoreUOM] = A2.[ID])
  98. left join [Base_Organization] as A3 on (A.[LogisticOrg] = A3.[ID])
  99. left join [CBO_Wh] as A4 on (A.[Wh] = A4.[ID])
  100. where (((((A1.[Name] is not null and (A1.[Name] != ''))
  101. and (A3.[Code] = @OrgCode))
  102. and (A4.[Code] = @WhCode))
  103. and (A.[ItemInfo_ItemName] LIKE N'%' + @ItemCode + '%' OR A1.[Name] LIKE N'%' + @ItemCode + '%'))
  104. and (A.[BinInfo_Code] LIKE N'%' + @BinCode + '%'))
  105. group by A.[ItemInfo_ItemCode],
  106. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,''),
  107. A1.[SPECS],
  108. A2.[Round_Precision]
  109. HAVING (sum((A.[StoreQty] + A.[ToRetStQty])) > '0')
  110. ORDER BY A.[ItemInfo_ItemCode] ASC,
  111. isnull( case when A1.[ItemFormAttribute] in (16, 22) then A.[ItemInfo_ItemName] else A1.[Name] end ,'') ASC,
  112. A1.[SPECS] ASC
  113. OFFSET @PageIndex * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY
  114. PRINT @ItemCode
  115. END
  116. select A.Item_ItemCode,
  117. A.ItemSeg_Name,
  118. A.ItemID_SPECS,
  119. sum(A.CanUseQty) as [CanUseQty],
  120. A.Round1_Precision,
  121. sum(A.BalQty) as [BalQty],
  122. sum(A.BalQty_Main) as [BalQty_Main],
  123. sum(A.Temp_PAB) as [Temp_PAB]
  124. from #TempTableQoh as A
  125. group by A.Item_ItemCode,
  126. A.ItemSeg_Name,
  127. A.ItemID_SPECS,
  128. A.Round1_Precision
  129. having (sum(A.BalQty) > '0')
  130. order by A.Item_ItemCode asc,
  131. A.ItemSeg_Name asc,
  132. A.ItemID_SPECS asc
  133. END
  134. GO
  135. EXEC SP_Cust_PDA_GetItemMasterWh @OrgCode = N'110', @WhCode = N'110301', @BinCode = N'', @Filter = N'沙发', @PageSize = 15, @PageIndex = 1
  136. --EXEC SP_Cust_PDA_GetItemMasterWh @OrgCode = N'110', @WhCode = N'110301', @BinCode = N'5-01-1-1', @Filter = N'', @PageSize = 3, @PageIndex = 2