Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. USE [meta_vn]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[CMRC_sp_ProductAdd2Order] Script Date: 10/10/2015 10:29:18 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER PROCEDURE [dbo].[CMRC_sp_ProductAdd2Order]
  10. (
  11. @OrderID INT
  12. ,@ProductID INT
  13. ,@Quantity INT
  14. ,@BranchPriceId INT=NULL
  15. ,@Attributes VARCHAR(MAX) = NULL
  16. )
  17. AS
  18. IF(@BranchPriceId IS NULL) SET @BranchPriceId=0
  19.  
  20. --Nguyen Duc Son sua ngay 6/4/2015 - Them thuoc tinh
  21. --Nguyen Duc Son sua ngay 4/9/2015 - Them gia nam, bac
  22.  
  23. DECLARE @Id INT=NULL
  24. DECLARE @UnitCost MONEY=0
  25.  
  26. --Lấy thống tin bản ghi sản phẩm trong đơn hàng
  27. SELECT @Id = Order_DetailsID
  28. FROM (
  29. SELECT od.Order_DetailsID
  30. ,dbo.GROUP_CONCAT_D(odap.PAttrID, ',') AS Attributes
  31. ,obp.ProductBranchPrice as BranchPriceId
  32. FROM CMRC_OrderDetails od WITH(NOLOCK)
  33. LEFT JOIN CMRC_OrderDetails_ProductAttributes odap WITH(NOLOCK) ON od.Order_DetailsID = odap.OrderDetailID
  34. LEFT JOIN CMRC_OrderDetails_BranchPrice obp WITH(NOLOCK) ON od.Order_DetailsID=obp.Order_DetailsID
  35. WHERE od.OrderID = @OrderID AND ProductID = @ProductID
  36. GROUP BY od.Order_DetailsID,obp.ProductBranchPrice
  37. ) at
  38. WHERE dbo.ArrDiff(at.Attributes, @Attributes) = 0 AND ISNULL(BranchPriceId,0)=ISNULL(@BranchPriceId,0)
  39.  
  40. IF (@Id IS NULL OR @Id=0)
  41. BEGIN
  42.  
  43. SET @UnitCost=0
  44. IF(@BranchPriceId IS NOT NULL AND @BranchPriceId > 0)
  45. SELECT @UnitCost=Prices FROM CMRC_ProductArea bpr WITH(NOLOCK) WHERE bpr.AutoID=@BranchPriceId AND Active=1
  46.  
  47. IF(@UnitCost IS NULL OR @UnitCost=0)
  48. SELECT @UnitCost = UnitCost
  49. FROM CMRC_Products WITH(NOLOCK)
  50. WHERE ProductID = @ProductID
  51.  
  52. INSERT INTO CMRC_OrderDetails (
  53. OrderID
  54. ,ProductID
  55. ,Quantity
  56. ,Discount
  57. ,price
  58. ,BackOrders
  59. )
  60. VALUES (
  61. @OrderID
  62. ,@ProductID
  63. ,@Quantity
  64. ,0
  65. ,@UnitCost
  66. ,0
  67. )
  68.  
  69. SELECT @Id = SCOPE_IDENTITY()
  70.  
  71. INSERT INTO [dbo].[CMRC_OrderDetails_ProductAttributes] (
  72. [OrderID]
  73. ,[OrderDetailID]
  74. ,[PAttrID]
  75. ,[price]
  76. ,[prefix]
  77. )
  78. SELECT @OrderID
  79. ,@Id
  80. ,ap.pattrid
  81. ,ap.price
  82. ,ap.prefix
  83. FROM CMRC_Attributes_Products ap WITH(NOLOCK)
  84. INNER JOIN dbo.CsvToInt(@Attributes) ids ON ap.pattrid = ids.IntValue
  85.  
  86.  
  87. IF(@BranchPriceId IS NOT NULL AND @BranchPriceId > 0)
  88. INSERT INTO CMRC_OrderDetails_BranchPrice (
  89. Order_DetailsID
  90. ,ProductBranchPrice
  91. )
  92. VALUES (
  93. @Id
  94. ,@BranchPriceId
  95. )
  96. END
  97. ELSE
  98. UPDATE CMRC_OrderDetails
  99. SET Quantity = Quantity + @Quantity
  100. WHERE Order_DetailsID = @Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement