You are on page 1of 7

ALTER PROCEDURE [dbo].

[SP_PRESENT_OT_CALCULATION_DATA_INSERT_SHIFT_WISE]
@emp_id numeric
,@Company_id numeric
,@For_Date datetime
,@Working_Sec numeric
,@OT_Sec numeric
,@Min_Shift_Sec numeric = 0
,@shift_sec numeric
,@strHolidayDate varchar(500)
,@strWeekoffDate varchar(500)
,@varWagesType varchar(10) ='Daily'
,@Is_OT varchar(1) = 'Y'
,@Is_Break varchar(1) = 'N'
,@Break_Dur_Sec numeric = 0
,@Branch_ID numeric = null
,@Grade_Id numeric = null
,@Emp_Type_ID numeric = null
,@Dept_Id numeric = null
,@Desig_Id numeric = null
,@shift_ID numeric
,@OT_Limit_Sec numeric = 0
,@varHalf_WeekDay_Date varchar(50)
,@Is_Half_day Tinyint
,@Half_Dur_Sec numeric
,@Shift_End_Time varchar(10)
AS

Set Nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET ARITHABORT ON
Declare @Count_Half numeric
declare @Total_Working_Sec numeric
Declare @Min_Working_Sec numeric
Declare @Holiday_Work_Sec numeric
Declare @Weekoff_Work_sec numeric

set @Count_Half = 0

set @Min_Working_Sec = 0 --

declare @P_Day as numeric(18,1)


declare @Half_Day as numeric(18,1)
Declare @Old_working_Sec as numeric
Declare @New_OT_Sec as numeric
declare @Temp_working_sec as numeric
DECLARE @Old_Ot_Sec as numeric
declare @old_Holiday_Work_Sec as numeric
declare @Next_day_Work_Sec as numeric
Declare @Next_Day_Ot_Sec as numeric
Declare @AB_Count as numeric
Declare @AB_For_DATE as datetime
Declare @StrHalfDay as varchar(1) -- for Monthly
Declare @H_Ex_OT as numeric(5,2)
Declare @H_Ex_OT_SEC as numeric
Declare @Fix_hours_sec numeric
Declare @OT_Exception_Time Varchar(10) --For OT calc till
execption time for legal OT

set @H_Ex_OT = 0
set @H_Ex_OT_SEC = 0
set @StrHalfDay = ''
set @AB_Count = 0
set @Old_working_Sec = 0
set @Temp_working_sec = 0
set @Old_Ot_Sec = 0
set @P_Day = 0
set @Half_Day = 0
set @Next_day_Work_Sec = 0
set @Old_Holiday_Work_Sec = 0
set @Fix_hours_sec = 0
set @OT_Exception_Time = '00:00'

select @OT_Exception_Time = OT_Exception_Time from shift_master


where shift_Id = @Shift_Id and Company_Id = @Company_Id

If @OT_Exception_Time = '' or @OT_Exception_Time = '__:__'


set @OT_Exception_Time = '00:00'

select @Old_working_Sec = isnull(Working_Sec,0) , @Old_Ot_Sec =


isnull(Ot_Sec,0) , @Old_Holiday_Work_Sec = isnull(Holiday_work_Sec,0)
from temp_working_sec where emp_id = @emp_id and company_id =
@company_id and for_date = @for_date

set @Working_Sec = @Working_Sec + @Old_working_Sec + @Old_Ot_Sec


+ @OT_SEC + @Old_Holiday_Work_Sec

if @is_Break = 'Y' and @Working_Sec > @Break_Dur_Sec


set @Working_Sec = @Working_Sec - @Break_Dur_Sec

exec Return_Without_Sec @Working_Sec ,@Working_Sec Output


set @Total_Working_Sec = @Working_Sec

set @Holiday_Work_Sec = 0
set @WeekOff_Work_Sec = 0

if charindex(cast(@For_Date as varchar(11)),@strWeekoffDate,0) >


0 and @Is_OT ='Y' and @Total_Working_Sec > 0
begin
set @WeekOff_Work_Sec = @Total_Working_Sec +
@Break_Dur_Sec
set @Total_Working_Sec =0
set @Working_Sec =0
end
else if charindex(cast(@For_Date as
varchar(11)),@strHolidayDate,0) > 0 and @Is_OT ='Y' and
@Total_Working_Sec > 0
begin
set @WeekOff_Work_Sec = 0
set @Holiday_Work_Sec = @Total_Working_Sec +
@Break_Dur_Sec
set @Total_Working_Sec =0
set @Working_Sec =0
end

/*
select top 1 @P_Day = Calculate_Days
,@Min_Shift_Sec= Minimum_Hour *3600 from Shift_MasteR_Detail
Where @Total_Working_Sec
>=( From_hour * 3600) and @Total_Working_Sec <= ( To_Hour * 3600 )
and shift_ID = @shift_ID

if exists(select OT_Applicable from


Shift_Master_Detail where @Total_Working_Sec >=( From_hour * 3600) and
@Total_Working_Sec <= ( To_Hour * 3600 )
and shift_ID =
@shift_ID and Fix_OT_hours >0 ) and @Is_OT ='Y'
begin

select top 1 @OT_Sec =


Fix_OT_Hours * 3600 from Shift_MasteR_Detail
Where
@Total_Working_Sec >=( From_hour * 3600) and @Total_Working_Sec <=
( To_Hour * 3600 )
and shift_ID =
@shift_ID and Fix_OT_hours >0
end
else if exists(select OT_Applicable
from Shift_Master_Detail where @Total_Working_Sec >=( From_hour * 3600)
and @Total_Working_Sec <= ( To_Hour * 3600 )
and shift_ID =
@shift_ID and Fix_OT_hours = 0 and Minimum_Hour >0 ) and @Is_OT
='Y'
begin

select top 1 @OT_Sec =


@Total_Working_Sec - Minimum_Hour * 3600
from
Shift_MasteR_Detail
Where
@Total_Working_Sec >=( From_hour * 3600) and @Total_Working_Sec <=
( To_Hour * 3600 )
and shift_ID =
@shift_ID and Fix_OT_hours =0

if @OT_Sec < 0
set @Working_Sec
= @Working_Sec + ( @OT_Sec * -1)

end
else if exists(select OT_Applicable
from Shift_Master_Detail where @Total_Working_Sec >=( From_hour * 3600)
and @Total_Working_Sec <= ( To_Hour * 3600 )

and shift_ID = @shift_ID and Fix_OT_hours = 0 and Minimum_Hour


=0 ) and @Is_OT ='Y'
begin

if @is_Break = 'Y' and @Working_Sec > @Break_Dur_Sec

begin

select top 1 @OT_Sec = @Total_Working_Sec -


( @shift_sec - @Break_Dur_Sec )

,@working_sec = @working_sec -
( @Total_Working_Sec - @shift_sec + @Break_Dur_Sec )

from Shift_MasteR_Detail

Where @Total_Working_Sec >=( From_hour * 3600) and


@Total_Working_Sec <= ( To_Hour * 3600 )

and shift_ID = @shift_ID and Fix_OT_hours =0 and


@Total_Working_Sec > @shift_sec

end

else

begin

select top 1 @OT_Sec = @Total_Working_Sec -


@shift_sec

,@working_sec = @working_sec -
( @Total_Working_Sec - @shift_sec )

from Shift_MasteR_Detail

Where @Total_Working_Sec >=( From_hour * 3600) and


@Total_Working_Sec <= ( To_Hour * 3600 )

and shift_ID = @shift_ID and Fix_OT_hours =0 and


@Total_Working_Sec > @shift_sec

end
end
*/
--New for OT calc after shift end time Hasmukh 140610
--select @for_date
select top 1 @P_Day =
Calculate_Days ,@Min_Shift_Sec= Minimum_Hour *3600 from
Shift_MasteR_Detail
Where
@Total_Working_Sec >=( From_hour * 3600) and @Total_Working_Sec <=
( To_Hour * 3600 )
and
shift_ID = @shift_ID

if exists(select OT_Applicable from


Shift_Master_Detail where @Total_Working_Sec >=( From_hour * 3600) and
@Total_Working_Sec <= ( To_Hour * 3600 )

and shift_ID = @shift_ID and Fix_OT_hours = 0 and Minimum_Hour


=0 ) and @Is_OT ='Y'
begin

If
@OT_Exception_Time > @Shift_End_Time
Begin

select top 1 @OT_Sec = Case When CONVERT(CHAR(8),For_Date,8) >=


@OT_Exception_Time then

Datediff(s,@Shift_End_Time,@OT_Exception_Time)

else

Datediff(s,@Shift_End_Time,CONVERT(CHAR(8),For_Date,8))

end from emp_inout_record

where emp_ID = @Emp_id and In_out_Flag = 'O' and


cast(cast(For_date as varchar(11)) + ' ' + '00:00:00' as
smalldatetime) = @For_Date

and CONVERT(CHAR(8),For_Date,8) >= @Shift_End_Time


End
Else
Begin

select top 1 @OT_Sec =


Datediff(s,@Shift_End_Time,CONVERT(CHAR(8),For_Date,8)) from
emp_inout_record

where emp_ID = @Emp_id and In_out_Flag = 'O' and


cast(cast(For_date as varchar(11)) + ' ' + '00:00:00' as
smalldatetime) = @For_Date

and CONVERT(CHAR(8),For_Date,8) >= @Shift_End_Time

End
end
if charindex(cast(@For_Date as
varchar(11)),@varHalf_WeekDay_Date,0) > 0 and @Is_Half_day = 1 And
@Is_OT ='Y' and @working_sec > 0
begin
If @working_sec > @Half_Dur_Sec

Begin
set @working_sec =
@Half_Dur_Sec
End
else
Begin
set @working_sec =
@Working_Sec + @Break_Dur_Sec
End
If @Total_Working_Sec > @Half_Dur_Sec

set @OT_Sec = (@Total_Working_Sec +


@Break_Dur_Sec) - @Half_Dur_Sec
end

if (@OT_Sec < 0 and (@OT_Sec * -1 < @OT_Limit_Sec )) or


(@OT_Sec > 0 and @OT_Sec < @OT_Limit_Sec)
begin
set @OT_Sec =0
end

-- if @is_Break = 'Y' and @Working_Sec


> @Break_Dur_Sec and @Working_Sec >= @shift_sec
-- set @Working_Sec =
@Working_Sec - @Break_Dur_Sec
-- else if @is_Break = 'Y' and
@Working_Sec =0 and @Holiday_Work_Sec > @Break_Dur_Sec and @Working_Sec
>= @shift_sec
-- set @Holiday_Work_Sec =
@Holiday_Work_Sec - @Break_Dur_Sec

set @ot_sec
=dbo.Pro_Rounding_Sec_OT(@ot_sec)
set @Holiday_Work_Sec =
dbo.Pro_Rounding_Sec_OT(@Holiday_Work_Sec)
set @Weekoff_Work_sec =
dbo.Pro_Rounding_Sec_OT(@Weekoff_Work_sec)
declare @temp_date datetime

select @temp_date=
Isnull(max(for_date),'') from emp_inout_record where emp_id = @emp_id
and company_id = @company_id and in_out_flag='I'and for_date<@for_date
if @temp_date<> ''
begin
if not exists(select Emp_ID from temp_working_sec where
emp_id = @emp_id and company_id = @company_id and for_date = @temp_date)
begin

insert into
temp_working_sec(emp_id,company_id,for_date,working_sec,ot_sec,Holiday_
Work_Sec,min_shift_sec,H_day,P_day,AB_Count,H_Ex_OT_Sec,shift_sec,Total
_Working_Sec
,Branch_ID,Grade_Id,Emp_type_Id,Dept_Id,Desig_Id,Weekoff_Work_sec,Shift
_ID,Weekday_OT_After_Sunset)

values(@emp_id,@company_id,@temp_date,@working_sec,@ot_sec,@Holiday_Wor
k_Sec,@min_shift_sec,@Half_Day,@P_Day,@Ab_Count,@H_Ex_OT_Sec,@Shift_Sec
,@Total_Working_Sec
,@Branch_ID,@Grade_Id,@Emp_type_Id,@Dept_Id,@Desig_Id,@Weekoff_Work_sec
,@shift_ID,0)
end
else
begin
UPDATE Temp_Working_Sec
SET Working_Sec = @working_sec, OT_Sec =
@ot_sec,
Holiday_Work_Sec = @Holiday_Work_Sec,
Min_Shift_Sec = @min_shift_sec, H_Day = @Half_Day, P_Day = @P_Day,
AB_Count = @Ab_Count,
H_Ex_OT_SEC = @H_Ex_OT_Sec, SHIFT_SEC
= @Shift_Sec, Total_Working_Sec = @Total_Working_Sec, Branch_ID =
@Branch_ID,
Grade_ID = @Grade_Id,
Emp_Type_ID = @Emp_type_Id, Dept_ID = @Dept_Id, Desig_Id = @Desig_Id,
WeekOff_Work_Sec = @Weekoff_Work_sec,
Shift_ID =
@shift_ID,Weekday_OT_After_Sunset = 0
where emp_id = @emp_id and company_id =
@company_id and for_date = @temp_date

end
end
set @temp_date=''

RETURN