You are on page 1of 3

/******************************************************************************

CVS Path : $Source: $


Created : 12 Aug 2017
Created By : Jimi Patel
Last Modified :
Last Modified By :
*******************************************************************************

Table Name : WorkSchedule, WorkScheduleDetails


Purpose : Update record in WorkSchedule and WorkScheduleDetails table.

******************************************************************************/
--[uspUpdWorkScheduleStatus] 5,36,2,8,13
alter procedure [dbo].[uspUpdWorkScheduleStatus]

(
@DailyWorkCompletionID int,
@SOItemID int,
@StatusID int,
@TaskID int,
@ActionedBy int
)
as
begin
begin try
begin transaction updWorkscheduleStatus
begin

-----Start Change WorkSchedule Status


declare @IsComplete int, @IsHitInventory int

select @IsComplete = tsmd.IsCompleted, @IsHitInventory = tsmd.IsHitInventory from


TaskStatusMappingDetails tsmd
inner join TaskStatusMapping tsm on tsmd.TaskStatusMappingID = tsm.ID and
tsm.IsActive=1
where tsmd.IsActive=1 and TaskStatusID = @StatusID and tsm.TaskTypeID = @TaskID

declare @WorkScheduleID int, @EmployeeID int, @AreaCode


varchar(200),@TotScheduleQty decimal(18,2) =0,
@TotCompletionQty decimal(18,2) =0

select @WorkScheduleID = WorkScheduleID, @EmployeeID = EmployeeID,@AreaCode =


AreaID
from DailyWorkCompletion
where ID = @DailyWorkCompletionID

select @TotScheduleQty = isnull(InstallationQty,0) from WorkScheduleDetails where

WorkScheduleID = @WorkScheduleID and AreaID = @AreaCode


and EmployeeID = @EmployeeID and SOItemID = @SOItemID and Isactive = 1

print @TotScheduleQty

if(@IsComplete = 1 and @IsHitInventory = 1 )


begin
select @TotCompletionQty = isnull(SUM(Qty),0) from DailyWorkCompletionDetails dcd
inner join DailyWorkCompletion dc on dcd.DailyWorkCompletionID= dc.ID
--inner join TaskStatusMappingDetails tsmd on tsmd.TaskStatusID = dcd.StatusID and
tsmd.IsCompleted = 1 and tsmd.IsHitInventory = 1
--inner join TaskStatusMapping tsm on tsm.ID = tsmd.TaskStatusMappingID and
tsm.TaskTypeID = dcd.TaskID
where dc.IsActive=1 and dcd.IsActive = 1 and dc.EmployeeID = @EmployeeID
and dc.WorkScheduleID = @WorkScheduleID and dc.AreaID = @AreaCode and dcd.SOItemID
= @SOItemID and dc.ID = @DailyWorkCompletionID
and dcd.StatusID = @StatusID and dcd.TaskID= @TaskID
end
else
begin
set @TotCompletionQty = 0
end

print @TotCompletionQty
print @IsComplete
print @IsHitInventory
if(@IsComplete = 1 and (@TotCompletionQty = @TotScheduleQty) and @IsHitInventory =
1)
begin
Update WorkScheduleDetails
set
StatusID = 1667, --Complete
LastModifiedBy = @ActionedBy,
LastModifiedDate = GETDATE()
where WorkScheduleID = @WorkScheduleID and EmployeeID = @EmployeeID and SOItemID
= @SOItemID
end
else if(@IsComplete = 1 and (@TotCompletionQty != @TotScheduleQty) and
@IsHitInventory = 1)
begin
Update WorkScheduleDetails
set
StatusID = 1666, --Assign
LastModifiedBy = @ActionedBy,
LastModifiedDate = GETDATE()
where WorkScheduleID = @WorkScheduleID and EmployeeID = @EmployeeID and SOItemID
= @SOItemID
end

Begin -- If All WorkScheduleDetails Status Completed then WorkSchedule Status


Complete
declare @Row int =0,@RowCount int

select ROW_NUMBER() over(order by ID) as 'SRNo',ID,StatusID,WorkScheduleID into


tempWorkScheduleDet
from WorkScheduleDetails
where WorkScheduleID = @WorkScheduleID and IsActive = 1

select @RowCount = COUNT(*) from tempWorkScheduleDet

declare @CompleteStatus int =0 ---If All Complete then RowCount =


CompleteStatus
,@Status int

while(@Row<@RowCount)
begin
set @Row = @Row+1;
select @Status = StatusID from tempWorkScheduleDet where WorkScheduleID =
@WorkScheduleID and SRNo = @Row
if(@Status = 1667)--complete
begin
set @CompleteStatus = @CompleteStatus + 1;
end
end

if(@RowCount = @CompleteStatus)
begin
Update WorkSchedule set Status = 1662 ---Complete
where ID = @WorkScheduleID
end
else
begin
Update WorkSchedule set Status = 1661 --InProcess
where ID =@WorkScheduleID
end

drop table tempWorkScheduleDet


End

---End Change Work Schedule Status

end

commit transaction updWorkscheduleStatus


end try
begin catch

declare @ErrorNumber int;


declare @ErrorMessage nvarchar(4000);
declare @ErrorSeverity int;
declare @ErrorDetails varchar(100);

select
@ErrorNumber = ERROR_NUMBER(),
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY()

raiserror
(
@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
1 -- State.
)

rollback transaction updWorkscheduleStatus

end catch

end

You might also like