create procedure [dbo].[UpdateTasks]
@inToDoIds IntList readonly,
@inChangedBySysuserID int,
@inNewDueDate datetime,
@inNewSysuserID int,
@inNewNote varchar(max)
as
set nocount on
declare @theTaskID int
declare @theDependencyDateTasks table
(
DependencyDateTaskID int identity( 1, 1 ),
TaskID int,
ToDoID int,
OrderId int,
ProfileID int,
SecModuleID int,
SecModuleDesc varchar(30),
Identifier varchar(64)
)
declare @theTasksThatDependOnTasksWeUpdatedPreviously table
(
ID int identity( 1, 1 ),
TaskId int,
OtherToDoID int,
NewTaskId int,
OrderId int,
ProfileID int,
SecModuleID int,
SecModuleDesc varchar(30),
Identifier varchar(64)
)
declare @theInnerCounter int
declare @theInnerRecordID int
declare @theInnerRecordCount int
declare @theCopyTaskCount int
declare @theUpdatedTaskId int
declare @theUpdatedToDoId int
declare @theUpdatedToDoOrderId int
declare @theUpdatedTaskProfileID int
declare @theUpdatedTaskSecModuleID int
declare @theUpdatedTaskSecModuleDesc varchar(30)
declare @theUpdatedTaskIdentifier varchar(64)
declare @theTempTableID int
declare @theIsFreeTask smallint
declare @theNewTaskID int
declare @theFreedTaskID int
declare @theTempToDoID int
declare @theTempOtherTaskID int
declare @theTempToDoOrderId int
declare @theTempTaskProfileID int
declare @theTempTaskSecModuleID int
declare @theTempTaskSecModuleDesc varchar(30)
declare @theTempTaskIdentifier varchar(64)
declare @theCompletionOfOtherTasksDependencyDateId int
set @theCompletionOfOtherTasksDependencyDateId = ( select DependencyDateID from DependencyDate where [Description] = 'Completion of Other Task' )
declare @theToDoCounter int = 1
declare @theToDoRecordCount int
declare @theCurrentToDoID int
declare @theToDoIDs table
(
ID int identity( 1, 1 ),
ToDoID int
)
insert into @theToDoIDs( ToDoID )
select ToDoID = ToDoParam.Item
from @inToDoIds as ToDoParam
set @theToDoRecordCount = @@rowcount
while( @theToDoCounter <= @theToDoRecordCount )
begin
select
@theCurrentToDoID = ToDoID
from @theToDoIDs
where ID = @theToDoCounter
if( @inNewDueDate is not null or @inNewSysuserID is not null )
begin
update ToDo set
OriginalDueDate =
case
when @inNewDueDate is null then ToDo.OriginalDueDate
when isnull( ToDo.OriginalDueDate, 0 ) != 0 then ToDo.OriginalDueDate
when isnull( Tasks.DependencyDateID, 0 ) = 0 then Tasks.FreeTaskDate
else ToDo.DueDate
end,
DueDate = isnull( @inNewDueDate, ToDo.DueDate ),
DependencyDateID =
case
when @inNewDueDate is null then ToDo.DependencyDateID
when ToDo.DependencyDateID = @theCompletionOfOtherTasksDependencyDateId then ToDo.DependencyDateID
else null
end,
CoordinatorTypeFID =
case
when @inNewSysuserID is null then ToDo.CoordinatorTypeFID
else null
end
from ToDo
inner join Tasks on ToDo.TaskID = Tasks.TaskID
where ToDo.ToDoID = @theCurrentToDoID
end
update Tasks set
DependencyDateID =
case
when Tasks.DependencyDateID = @theCompletionOfOtherTasksDependencyDateId then Tasks.DependencyDateID
when @inNewDueDate is null then Tasks.DependencyDateID
else null
end,
FreeTaskDate = isnull( @inNewDueDate, FreeTaskDate )
from Tasks
inner join ToDo on Tasks.TaskID = ToDo.TaskID
where ToDo.ToDoID = @theCurrentToDoID and
Tasks.FreeTask = 1
insert into @theDependencyDateTasks
(
TaskID,
ToDoID,
OrderId,
ProfileID,
SecModuleID,
SecModuleDesc,
Identifier
)
select
Tasks.TaskID,
ToDo.ToDoID,
ToDo.OrderID,
ToDo.ProfileID,
SecModules.ModulePriKey,
SecModules.[Description],
ToDo.Identifier
from Tasks
inner join ToDo on Tasks.TaskID = ToDo.TaskID
inner join Profiles on ToDo.ProfileID = Profiles.ProfileID
inner join SecModules on Profiles.SecModulesFID = SecModules.ModulePriKey
where ToDo.ToDoID = @theCurrentToDoID and
Tasks.FreeTask = 0
set @theInnerRecordCount = @@rowcount
set @theInnerCounter = 1
while( @theInnerCounter <= @theInnerRecordCount )
begin
select top 1
@theUpdatedTaskId = TaskID,
@theUpdatedToDoId = ToDoID,
@theUpdatedToDoOrderId = OrderId,
@theInnerRecordID = DependencyDateTaskID,
@theUpdatedTaskProfileID = ProfileID,
@theUpdatedTaskSecModuleID = SecModuleID,
@theUpdatedTaskSecModuleDesc = SecModuleDesc,
@theUpdatedTaskIdentifier = Identifier
from @theDependencyDateTasks
order by DependencyDateTaskID
insert into Tasks
(
ProfileID,
TaskDescription,
CoordinatorTypeID,
DependencyDateID,
DependencyTaskFID,
DueDays,
DueType,
IsActive,
FreeTask,
FreeTaskDate,
SysUserFID
)
select
ProfileID = Tasks.ProfileID,
TaskDescription = Tasks.TaskDescription,
CoordinatorTypeID = case
when @inNewSysuserID is null then Tasks.CoordinatorTypeID
else null
end,
DependencyDateID = case
when Tasks.DependencyDateID = @theCompletionOfOtherTasksDependencyDateId then Tasks.DependencyDateID
when @inNewDueDate is null then Tasks.DependencyDateID
else null
end,
DependencyTaskFID = case
when Tasks.DependencyDateID = @theCompletionOfOtherTasksDependencyDateId then Tasks.DependencyTaskFID
when @inNewDueDate is null then Tasks.DependencyTaskFID
else null
end,
DueDays = Tasks.DueDays,
DueType = Tasks.DueType,
IsActive = 1,
FreeTask = 1,
FreeTaskDate = isnull( @inNewDueDate, Tasks.FreeTaskDate ),
SysUserFID = Tasks.SysUserFID
from Tasks
where Tasks.TaskID = @theUpdatedTaskId
set @theCopyTaskCount = @@rowcount
if( @theCopyTaskCount = 1 )
begin
set @theTaskID = scope_identity()
update ToDo set
TaskID = @theTaskID
from ToDo
where ToDo.ToDoID = @theUpdatedToDoId
insert into @theTasksThatDependOnTasksWeUpdatedPreviously
(
TaskId,
OtherToDoID,
NewTaskId,
OrderId,
ProfileID,
SecModuleID,
SecModuleDesc,
Identifier
)
select
TaskId = Tasks.TaskID,
OtherToDoID = ToDo.ToDoID,
NewTaskId = @theTaskID,
OrderId = @theUpdatedToDoOrderId,
ProfileID = @theUpdatedTaskProfileID,
SecModuleID = @theUpdatedTaskSecModuleID,
SecModuleDesc = @theUpdatedTaskSecModuleDesc,
Identifier = @theUpdatedTaskIdentifier
from Tasks
inner join ToDo on
ToDo.TaskID = Tasks.TaskID and
ToDo.OrderID = @theUpdatedToDoOrderId
where ToDo.DependencyTaskFID = @theUpdatedTaskId and
ToDo.ProfileID = @theUpdatedTaskProfileID and
ToDo.Identifier = @theUpdatedTaskIdentifier
end
delete from @theDependencyDateTasks
where DependencyDateTaskID = @theInnerRecordID
set @theInnerCounter = @theInnerCounter + 1
end
while( exists( select top 1 1 from @theTasksThatDependOnTasksWeUpdatedPreviously ) )
begin
select top 1
@theTempTableID = TempTasks.ID,
@theUpdatedTaskId = TempTasks.TaskId,
@theUpdatedToDoId = TempTasks.OtherToDoID,
@theNewTaskID = TempTasks.NewTaskId,
@theTempToDoOrderId = TempTasks.OrderId,
@theTempOtherTaskID = Tasks.TaskID,
@theIsFreeTask = Tasks.FreeTask,
@theTempToDoID = ToDo.ToDoID,
@theTempTaskProfileID = TempTasks.ProfileID,
@theTempTaskSecModuleID = TempTasks.SecModuleID,
@theTempTaskSecModuleDesc = TempTasks.SecModuleDesc,
@theTempTaskIdentifier = TempTasks.Identifier
from @theTasksThatDependOnTasksWeUpdatedPreviously as TempTasks
left outer join Tasks on Tasks.TaskID = TempTasks.TaskID
left outer join ToDo on ToDo.ToDoID = TempTasks.OtherToDoID
order by TempTasks.ID
if( @theTempOtherTaskID is not null and @theTempToDoID is not null)
begin
if( @theIsFreeTask = 1 )
begin
update Tasks set
DependencyTaskFID = @theNewTaskID
from Tasks
where Tasks.TaskID = @theUpdatedTaskId
end
else
begin
insert into Tasks
(
ProfileID,
TaskDescription,
CoordinatorTypeID,
DependencyDateID,
DueDays,
DueType,
IsActive,
FreeTask,
FreeTaskDate,
SysUserFID,
DependencyTaskFID
)
select
Tasks.ProfileID,
Tasks.TaskDescription,
ToDo.CoordinatorTypeFID,
ToDo.DependencyDateID,
ToDo.DueDays,
ToDo.DueType,
1,
1,
Tasks.FreeTaskDate,
Tasks.SysUserFID,
@theNewTaskID
from Tasks
inner join ToDo on
ToDo.TaskID = Tasks.TaskID and
ToDo.OrderID = @theTempToDoOrderId
where ToDo.ToDoID = @theUpdatedToDoId and
Tasks.TaskID = @theUpdatedTaskId
set @theCopyTaskCount = @@rowcount
if( @theCopyTaskCount = 1 )
begin
set @theFreedTaskID = scope_identity()
update ToDo set
TaskID = @theFreedTaskID,
DependencyTaskFID = @theNewTaskID
from ToDo
where ToDo.ToDoID = @theUpdatedToDoId
insert into @theTasksThatDependOnTasksWeUpdatedPreviously
(
TaskId,
OtherToDoID,
NewTaskId,
OrderId,
ProfileID,
SecModuleID,
SecModuleDesc,
Identifier
)
select
TaskId = Tasks.TaskID,
OtherToDoID = ToDo.ToDoID,
NewTaskId = @theFreedTaskID,
OrderId = @theTempToDoOrderId,
ProfileID = @theTempTaskProfileID,
SecModuleID = @theTempTaskSecModuleID,
SecModuleDesc = @theTempTaskSecModuleDesc,
Identifier = @theTempTaskIdentifier
from Tasks
inner join ToDo on
ToDo.TaskID = Tasks.TaskID and
ToDo.OrderID = @theTempToDoOrderId
where ToDo.DependencyTaskFID = @theUpdatedTaskId and
ToDo.ProfileID = @theTempTaskProfileID and
ToDo.Identifier = @theTempTaskIdentifier
end
end
end
delete from @theTasksThatDependOnTasksWeUpdatedPreviously
where ID = @theTempTableID
end
set @theToDoCounter = @theToDoCounter + 1
end
update ToDo set
TaskChangedOnDate = getdate(),
TaskChangedBySysuserFID = @inChangedBySysuserID,
OpenUserFlag =
case
when @inNewSysuserID is null then OpenUserFlag
else 1
end,
UserID = isnull( @inNewSysuserID, UserID ),
TaskUserFID = isnull( @inNewSysuserID, TaskUserFID )
from ToDo
inner join @theToDoIDs as theToDoIDs on theToDoIDs.ToDoID = ToDo.ToDoID
if( @inNewNote is not null )
begin
update ToDoNote set
Notes = @inNewNote,
ChangedBySysuserFID = @inChangedBySysuserID,
ChangedDateTime = getdate()
from @theToDoIDs as theToDoIDs
inner join ToDoNote on ToDoNote.ToDoFID = theToDoIDs.ToDoID
insert into ToDoNote
(
ToDoFID,
ChangedBySysuserFID,
ChangedDateTime,
Notes
)
select
ToDo.ToDoID,
@inChangedBySysuserID,
getdate(),
@inNewNote
from @theToDoIDs as theToDoIDs
inner join ToDo on ToDo.ToDoID = theToDoIDs.ToDoID
left outer join ToDoNote as ExistingNotes on ExistingNotes.ToDoFID = theToDoIDs.ToDoID
where
ExistingNotes.ToDoNoteID is null
end
GO
GRANT EXECUTE ON [dbo].[UpdateTasks] TO [MssExec]
GO