ALTER PROCEDURE [dbo].[spHdTicketChangeHdSection]
	@iHdTicketId int,
	@iHdSectionId int,
	@iPersonId int = NULL
AS
BEGIN
	SET NOCOUNT ON
	PRINT '<<spHdTicketChangeHdSection>>'
	DECLARE
		@sHdTicket nvarchar(255),
		@sHdSection nvarchar(255),
		@iUserPersonId int,
		@sSla nvarchar(255),
		@iSlaId int

	SELECT @sHdTicket = sHdTicketMessageTag + ' ' + ISNULL(sHdTicket, '') FROM tHdTicket WHERE iHdTicketId = @iHdTicketId AND liHdTicketHdSectionId != @iHdSectionId
	SELECT @sHdSection = sHdSection FROM tHdSection WHERE iHdSectionId = @iHdSectionId
	SELECT @iUserPersonId = ISNULL(liHdTicketUserPersonId, (SELECT iPersonId FROM tPerson WHERE bPersonGuest = 1)) FROM tHdTicket WHERE iHdTicketId = @iHdTicketId
	SELECT @iSlaId = liHdTicketSlaId FROM tHdTicket WHERE iHdTicketId = @iHdTicketId
	SELECT @sSla = sSla FROM tSla WHERE iSlaId = @iSlaId
	
	IF NOT EXISTS 
		(
		SELECT * FROM vSlaAccess WHERE liSlaAccessSlaId = @iSlaId AND liSlaAccessHdSectionId = @iHdSectionId AND liSlaAccessPersonId = @iPersonId
		)
		BEGIN
			SELECT @iSlaId = liDefaultSlaSlaId, @sSla = sSla FROM vDefaultSla INNER JOIN tSla ON iSlaId = liDefaultSlaSlaId WHERE liDefaultSlaPersonId = @iUserPersonId AND liDefaultSlaHdSectionId = @iHdSectionId
		END
		
		
	IF
		@sHdTicket IS NULL
		OR @sHdSection IS NULL
		OR @iUserPersonId IS NULL
		OR @iSlaId IS NULL
	BEGIN
		SELECT CAST(0 AS bit) Result
		PRINT '  <<Fail>>'
		RETURN 0
	END
		


	PRINT '  Moving ticket ''' + @sHdTicket + ''' into section ''' + @sHdSection + ''''
	PRINT '  New SLA: ''' + @sSla + ''''

	BEGIN
		DECLARE @QueueOrder int, @EstimatedHours float, @HdSectionId int
		SELECT @QueueOrder = iHdTicketQueueOrder, @EstimatedHours = nHdTicketEstimatedHours, @HdSectionId = liHdTicketHdSectionId FROM tHdTicket WHERE iHdTicketId = @iHdTicketId

		update tHdTicket
		set 
		liHdTicketHdSectionId=@iHdSectionId,
		liHdTicketSolverPersonId=case when hdss.PersonId IS NULL AND hdses.PersonId IS NULL THEN NULL ELSE hdt.liHdTicketSolverPersonId END,
		iHdTicketSolverOrder=case when hdss.PersonId is null AND hdses.PersonId IS NULL then null else hdt.iHdTicketSolverOrder end,
		liHdTicketSlaId = @iSlaId,
		dHdTicketQueueDeadline = null,
		iHdTicketQueueOrder = null
		from tHdTicket as hdt
		left join HdSectionSolver as hdss on hdss.HdSectionId=@iHdSectionId and hdss.PersonId=hdt.liHdTicketSolverPersonId
		LEFT JOIN vHdSectionExceptionalSolver as hdses on hdses.SectionId=@iHdSectionId and hdses.PersonId=hdt.liHdTicketSolverPersonId
		where hdt.iHdTicketId=@iHdTicketId
		and (hdt.liHdTicketHdSectionId is null or hdt.liHdTicketHdSectionId<>@iHdSectionId)

		IF @@rowcount >0 BEGIN
			PRINT '  Reseting estimated queue'
			UPDATE tHdTicket
			SET dHdTicketQueueDeadline = DATEADD ( second, (-@EstimatedHours/nHdSectionQueueHoursPerWeek)*24*7*3600,dHdTicketQueueDeadline)
			FROM tHdTicket  LEFT JOIN tHdSection  ON liHdTicketHdSectionId = iHdSectionId
			WHERE iHdTicketQueueOrder > @QueueOrder
			AND iHdSectionId = @HdSectionId
		END
		ELSE BEGIN
			SELECT CAST(0 AS bit) Result
			PRINT '  <<Fail>>'
			RETURN 0
		END
	END

	PRINT '  Updating history'
	PRINT '    <<Not implemented>>'

	SELECT CAST(1 AS bit) Result
	PRINT '  <<Success>>'
	RETURN 1
END