USE [FastMoneyPro_Remit] GO /****** Object: StoredProcedure [dbo].[PROC_PointMasterDetails] Script Date: 7/4/2019 11:35:48 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --SELECT * FROM dbo.PointSetup --SELECT * FROM PointHistory --SELECT TOP 10 createdBy,pCountry,pAgent, * FROM dbo.remitTran(NOLOCK) ORDER BY id desc --EXEC PROC_PointMasterDetails @flag='send',@user='mahmet@gmail.com',@tranId='100485514' --EXEC PROC_PointMasterDetails @flag='cancel',@user='mahmet@gmail.com',@tranId='100485514' --SELECT bonusPoint FROM dbo.customerMaster WHERE customerId=85149 CREATE OR ALTER PROC [dbo].[PROC_PointMasterDetails] ( @flag VARCHAR(100) ,@customerId VARCHAR(100) = NULL ,@tranId BIGINT = NULL ,@holdtranId BIGINT = NULL ) AS SET XACT_ABORT ON SET NOCOUNT ON BEGIN TRY DECLARE @condition1 VARCHAR(200)=NULL ,@condition2 INT=NULL ,@typeOneValue MONEY=NULL ,@typeTwoValue MONEY=NULL ,@result MONEY=NULL ,@email VARCHAR(200)=NULL ,@availablePoint MONEY=NULL ,@schemeStartDate DATETIME=NULL ,@schemeEndDate DATETIME=NULL ,@ReferalCode VARCHAR(30) =NULL ,@ReferalCustomerId BIGINT ,@ReferalValue INT ,@ReferalAvailablePoint INT -- IF CUSTOMER IS REFERAL AND FIRST TRANSACTION GIVE 5000 POINT EACH /*point type 1 : 송금횟수로 포인트 누적(txncount saving) 2 : 송금액으로 포인트 누적(amount saving ) 9 : 포인트 사용 (point use) 11: 송금횟수로 포인트 누적 취소(Cancel of txncount saving) 21: 송금액으로 포인트 누적 취소(Cancel of txnAmount saving) 91: 포인트 사용 취소(Cancel of point use) */ SELECT TOP 1 @email=email, @availablePoint=ISNULL(bonusPoint,0), @ReferalCode=referelCode FROM dbo.customerMaster(NOLOCK) WHERE customerId=@customerId IF NOT EXISTS(SELECT 'X' FROM customerMaster (NOLOCK) WHERE customerId = @customerId AND ISNULL(isActive,'N')='Y') BEGIN EXEC proc_errorHandler '1', 'Invalid Customer.', @customerId RETURN END IF NOT EXISTS(SELECT 'x' FROM PointSetup (NOLOCK) WHERE GETDATE() BETWEEN schemeStartDate AND schemeEndDate) BEGIN EXEC proc_errorHandler '1', 'Invalid Point.', @customerId RETURN END SELECT @schemeStartDate=schemeStartDate, @schemeEndDate=schemeEndDate FROM PointSetup (NOLOCK) WHERE conditionType=1 SELECT @condition1 = COUNT(1) FROM dbo.remitTran(NOLOCK) WHERE createdBy=@email AND approvedDate BETWEEN @schemeStartDate AND @schemeEndDate SELECT @condition2 = cAmt FROM dbo.remitTran(NOLOCK) WHERE holdTranId = @holdtranId AND createdBy=@email SELECT @typeOneValue = ISNULL(value,0) FROM PointSetup (NOLOCK) WHERE conditionType=1 AND isActive = 1 AND isDeleted = 0 AND (CONVERT(INT, @condition1) % CONVERT(INT,condition1))= 0 SELECT @typeTwoValue = ISNULL(value,0) FROM PointSetup (NOLOCK) WHERE @condition2 BETWEEN condition1 AND condition2 AND isActive = 1 AND isDeleted = 0 AND conditionType = '2' SET @result = @typeOneValue + @typeTwoValue IF @flag='send' BEGIN IF EXISTS (SELECT 'A' FROM PointHistory(NOLOCK) WHERE customerId=@customerId AND tranId = @holdtranId and pointType in('1','2') ) BEGIN EXEC proc_errorHandler '1', 'Already Exist History.', @customerId RETURN END ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- IF CUSTOMER IS REFERAL AND FIRST TRANSACTION GIVE 5000 POINT EACH ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- IF @condition1 = 1 BEGIN SELECT @ReferalCustomerId = customerId, @ReferalAvailablePoint = bonusPoint FROM dbo.customerMaster(NOLOCK) WHERE membershipId = @ReferalCode AND approvedDate IS NOT NULL IF @ReferalCustomerId IS NOT NULL BEGIN SELECT @ReferalValue = 5000 UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint,0) + ISNULL(@ReferalValue,0) WHERE customerId = @ReferalCustomerId SET @ReferalAvailablePoint = ISNULL(@ReferalAvailablePoint,0) + @ReferalValue INSERT INTO dbo.PointHistory (customerId, availablePoint, tranId, pointType, point,createdBy, createdDate) SELECT @ReferalCustomerId,@ReferalAvailablePoint,@holdtranId, 1,@ReferalValue,@email,GETDATE() END END IF @result <> 0 BEGIN UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint,0) + ISNULL(@result,0) WHERE customerId = @customerId IF @typeOneValue <> 0 BEGIN SET @availablePoint = ISNULL(@availablePoint,0) + ISNULL(@typeOneValue,0) INSERT INTO dbo.PointHistory (customerId, availablePoint, tranId, pointType, point,createdBy, createdDate) SELECT @customerId, @availablePoint,@holdtranId, 1,@typeOneValue,@email,GETDATE() END IF @typeTwoValue <> 0 BEGIN SET @availablePoint = ISNULL(@availablePoint,0) + ISNULL(@typeTwoValue,0) INSERT INTO dbo.PointHistory (customerId, availablePoint, tranId, pointType, point,createdBy, createdDate) SELECT @customerId, @availablePoint,@holdtranId, 2,@typeTwoValue,@email,GETDATE() END EXEC proc_errorHandler '0', 'Success.', @customerId RETURN END EXEC proc_errorHandler '0', 'No Reward.', @customerId RETURN END IF @flag='cancel' BEGIN IF EXISTS (SELECT 'A' FROM PointHistory(NOLOCK) WHERE customerId=@customerId AND tranId = @holdtranId and pointType in('11','21')) BEGIN EXEC proc_errorHandler '1', 'Already Exist History.', @customerId RETURN END IF @result <>0 BEGIN UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint,0) + ISNULL(@result,0) WHERE customerId = @customerId IF @typeOneValue <> 0 BEGIN SET @availablePoint = ISNULL(@availablePoint,0) - ISNULL(@typeOneValue,0) INSERT INTO dbo.PointHistory (customerId, availablePoint, tranId, pointType, point,createdBy, createdDate) SELECT @customerId, @availablePoint,@holdtranId, '11',@typeOneValue,@email,GETDATE() END IF @typeTwoValue <> 0 BEGIN SET @availablePoint = ISNULL(@availablePoint,0) - ISNULL(@typeTwoValue,0) INSERT INTO dbo.PointHistory (customerId, availablePoint, tranId, pointType, point,createdBy, createdDate) SELECT @customerId, @availablePoint,@holdtranId, '21',@typeTwoValue,@email,GETDATE() END EXEC proc_errorHandler '0', 'Success.', @customerId RETURN END EXEC proc_errorHandler '0', 'No Reward.', @customerId RETURN END IF @flag='expectedreward' BEGIN SELECT @result AS expectedReward EXEC proc_errorHandler '0', 'Success.', @customerId RETURN END END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK TRAN PRINT ERROR_MESSAGE() END CATCH