USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_UpdateCustomerBalance]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_UpdateCustomerBalance]
GO
/****** Object:  StoredProcedure [dbo].[proc_UpdateCustomerBalance]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[proc_UpdateCustomerBalance]
(
 @controlNo VARCHAR(20)=NULL,
 @type VARCHAR(20)=NULL
)
AS 
BEGIN
IF(@type='deduct')
BEGIN
	UPDATE cm
	SET cm.availableBalance=ISNULL(cm.availableBalance,0) -rt.cAmt + ISNULL(rt.schemePremium,0)
	FROM dbo.remitTranTemp rt WITH (NOLOCK)
	INNER JOIN dbo.customerMaster cm
	ON rt.createdBy=cm.email
	WHERE rt.controlNo=@controlNo
END
ELSE 
BEGIN
	UPDATE cm
	SET cm.availableBalance=ISNULL(cm.availableBalance,0) + rt.cAmt - ISNULL(rt.schemePremium,0)
	FROM dbo.remitTranTemp rt WITH (NOLOCK)
	INNER JOIN dbo.customerMaster cm
	ON rt.createdBy=cm.email
	WHERE rt.controlNo=@controlNo
	END
END
GO
