﻿USE [AIADatabaseV5]
GO
/****** Object:  StoredProcedure [dbo].[EC_UpdateUser]    Script Date: 2/1/2018 12:15:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- ===================================================================
-- Author:		Magic
-- Create date: 27-May-2009
-- Description:	To update record into AIAUser,License and LicenseSubscriptionDetail table
-- ===================================================================

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EC_UpdateUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EC_UpdateUser]
GO

CREATE PROCEDURE [dbo].[EC_UpdateUser]
	-- Add the parameters for the stored procedure here
	@sLoginID varchar(50),
	@sAmountPaid money, 
	@sAmountPending money
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
DECLARE @iLicenseId varchar(20)	
SET @iLicenseId = (SELECT DISTINCT License.Id FROM AIAUser
     INNER JOIN AIAUserToLicenseEdition ON AIAUser.Id = AIAUserToLicenseEdition.UserId
     INNER JOIN LicenseToEdition ON AIAUserToLicenseEdition.LicenseEditionId = LicenseToEdition.Id
     INNER JOIN License ON LicenseToEdition.LicenseId = License.Id
     WHERE AIAUser.LoginId = @sLoginID)

-- Update the LicenseSubscriptionDetail table for the supplied LoginID
UPDATE LicenseSubscriptionDetail SET AmountPaid = @sAmountPaid, AmountPending = @sAmountPending
WHERE LicenseId = @iLicenseId

--Update the License status to Active for the LoginID
UPDATE License SET IsActive = 1 WHERE Id = @iLicenseId

--Update the AIAUser status to Active for the LooginID
UPDATE AIAUser SET IsActive = 1 WHERE LoginId = @sLoginID 
	
END


GO
