USE [LegitimateNational]
GO
/****** Object:  StoredProcedure [App].[Pr_ApplicationLicenceRouteDetailUpdate]    Script Date: 2025/11/03 10:50:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [App].[Pr_ApplicationLicenceRouteDetailUpdate]
	@ApplicationLicenceID BIGINT,
	@ApplicationLicenceRouteDetailID BIGINT,
	@RouteOrigin VARCHAR(MAX),
	@RouteDestination VARCHAR(MAX),
	@RouteDescription VARCHAR(MAX),	
	@UserID BIGINT
AS
BEGIN	
	SET NOCOUNT ON;

	/********************************************************************************************
	Get user details and perform security checks
	*********************************************************************************************/	
	DECLARE @EntityIDToCheck INT = 1022 /*ApplicationLicences*/
	DECLARE @ItemIDToCheck BIGINT = @ApplicationLicenceID
	DECLARE @Action VARCHAR(255) = 'Update'
	DECLARE @OfficeLocationID BIGINT, @DivisionTypeID SMALLINT, @ProvinceID SMALLINT, @RegionID SMALLINT, @HasAccess BIT
	
	EXEC App.Pr_GetUserLocationAndSecurity @UserID, @EntityIDToCheck, @ItemIDToCheck, @Action, @OfficeLocationID OUTPUT, @DivisionTypeID OUTPUT, @ProvinceID OUTPUT, @RegionID OUTPUT, @HasAccess OUTPUT

	IF @HasAccess = 0
	BEGIN
		RAISERROR('User location and security error', 16, 1)
		RETURN
	END	
	
	/********************************************************************************************
	Validate 
	*********************************************************************************************/	
	--todo make sure values can be changed now
	--todo audit change

	/********************************************************************************************
	Update application licence record
	*********************************************************************************************/	
	UPDATE App.ApplicationLicenceRouteDetails
	SET
		RouteOrigin = @RouteOrigin,
		RouteDestination = @RouteDestination,
		RouteDescription = @RouteDescription
	WHERE ApplicationLicenceID = @ApplicationLicenceID 
	AND ApplicationLicenceRouteDetailID =  @ApplicationLicenceRouteDetailID

		
END
