Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [meta_vn]
- GO
- /****** Object: StoredProcedure [dbo].[CMRC_sp_ProductAdd2Order] Script Date: 10/10/2015 10:29:18 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER PROCEDURE [dbo].[CMRC_sp_ProductAdd2Order]
- (
- @OrderID INT
- ,@ProductID INT
- ,@Quantity INT
- ,@BranchPriceId INT=NULL
- ,@Attributes VARCHAR(MAX) = NULL
- )
- AS
- IF(@BranchPriceId IS NULL) SET @BranchPriceId=0
- --Nguyen Duc Son sua ngay 6/4/2015 - Them thuoc tinh
- --Nguyen Duc Son sua ngay 4/9/2015 - Them gia nam, bac
- DECLARE @Id INT=NULL
- DECLARE @UnitCost MONEY=0
- --Lấy thống tin bản ghi sản phẩm trong đơn hàng
- SELECT @Id = Order_DetailsID
- FROM (
- SELECT od.Order_DetailsID
- ,dbo.GROUP_CONCAT_D(odap.PAttrID, ',') AS Attributes
- ,obp.ProductBranchPrice as BranchPriceId
- FROM CMRC_OrderDetails od WITH(NOLOCK)
- LEFT JOIN CMRC_OrderDetails_ProductAttributes odap WITH(NOLOCK) ON od.Order_DetailsID = odap.OrderDetailID
- LEFT JOIN CMRC_OrderDetails_BranchPrice obp WITH(NOLOCK) ON od.Order_DetailsID=obp.Order_DetailsID
- WHERE od.OrderID = @OrderID AND ProductID = @ProductID
- GROUP BY od.Order_DetailsID,obp.ProductBranchPrice
- ) at
- WHERE dbo.ArrDiff(at.Attributes, @Attributes) = 0 AND ISNULL(BranchPriceId,0)=ISNULL(@BranchPriceId,0)
- IF (@Id IS NULL OR @Id=0)
- BEGIN
- SET @UnitCost=0
- IF(@BranchPriceId IS NOT NULL AND @BranchPriceId > 0)
- SELECT @UnitCost=Prices FROM CMRC_ProductArea bpr WITH(NOLOCK) WHERE bpr.AutoID=@BranchPriceId AND Active=1
- IF(@UnitCost IS NULL OR @UnitCost=0)
- SELECT @UnitCost = UnitCost
- FROM CMRC_Products WITH(NOLOCK)
- WHERE ProductID = @ProductID
- INSERT INTO CMRC_OrderDetails (
- OrderID
- ,ProductID
- ,Quantity
- ,Discount
- ,price
- ,BackOrders
- )
- VALUES (
- @OrderID
- ,@ProductID
- ,@Quantity
- ,0
- ,@UnitCost
- ,0
- )
- SELECT @Id = SCOPE_IDENTITY()
- INSERT INTO [dbo].[CMRC_OrderDetails_ProductAttributes] (
- [OrderID]
- ,[OrderDetailID]
- ,[PAttrID]
- ,[price]
- ,[prefix]
- )
- SELECT @OrderID
- ,@Id
- ,ap.pattrid
- ,ap.price
- ,ap.prefix
- FROM CMRC_Attributes_Products ap WITH(NOLOCK)
- INNER JOIN dbo.CsvToInt(@Attributes) ids ON ap.pattrid = ids.IntValue
- IF(@BranchPriceId IS NOT NULL AND @BranchPriceId > 0)
- INSERT INTO CMRC_OrderDetails_BranchPrice (
- Order_DetailsID
- ,ProductBranchPrice
- )
- VALUES (
- @Id
- ,@BranchPriceId
- )
- END
- ELSE
- UPDATE CMRC_OrderDetails
- SET Quantity = Quantity + @Quantity
- WHERE Order_DetailsID = @Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement