You are on page 1of 61

USE [ORANGE_TIME_TRACK]

GO
/****** Object: StoredProcedure [dbo].[Rpt_Get_Emp_InOut_Summary] Script Dat
e: 11/16/2010 16:49:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Rpt_Get_Emp_InOut_Summary]


@Grade_Id numeric
,@Month numeric = NULL
,@Month_St_Date datetime
,@Month_En_Date datetime
,@Company_Id numeric
,@Emp_Type_Id numeric
,@Emp_ID numeric
,@Dept_ID numeric = 0
,@Division_ID numeric
,@Category_ID numeric
,@strReport varchar(20) = 'IN-OUT'
,@Weekoff_Entry varchar(1) = 'Y'
AS

Set Nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET ARITHABORT ON

declare @Status as varchar(9)


Declare @For_Date as datetime
Declare @RowID as numeric
Declare @GradeID as numeric

declare @LateMark as varchar(9)


Declare @InTime as smalldatetime
Declare @OutTime as smalldatetime
Declare @PreOutTime as smalldatetime

declare @Is_Join as varchar(1)


Declare @Count as numeric
DECLARE @dblYear as numeric
Declare @numofDay as numeric
declare @varWeekoff_Date as varchar(500)
declare @varHoliday_Date as varchar(500)
set @Count =0
set @RowID =0
set @dblYear = Year(@Month_St_Date)
set @Month = month(@Month_St_Date)
set @numofDay = Datediff(d,@Month_St_Date,@Month_En_Date) + 1
-- for Holiday and Week Off and LEave date
Declare @Total_Holiday_Date as varchar(500)
Declare @Total_LeaveDay_Date as varchar(500)
declare @strOnlyHoliday_date as varchar(500)
set @Total_Holiday_Date = ''
set @Total_LeaveDay_Date = ''
-- for Shift
declare @Shift_St_Time as varchar(10)
declare @Shift_End_Time as varchar(10)
declare @varShift_St_Date as varchar(20)
declare @dtShift_St_Date as datetime
declare @varShift_End_Date as varchar(20)
declare @dtShift_End_Date as datetime
Declare @TempFor_Date as smalldatetime
Declare @dtShift_Actual_St_Time as datetime
Declare @dtShift_Actual_End_Time as datetime
Declare @Late_Comm_Limit as varchar(5)
declare @Late_comm_sec as numeric
Declare @Leave_ID as numeric
Declare @Leave_Name as varchar(10)

declare @Temp_Month_Date as datetime


set @Temp_Month_Date = @Month_St_Date

declare @In_Dur as varchar(10)


declare @In_Out_Flag as varchar(1)
declare @Day_St_Time as datetime
declare @Day_End_Time as datetime
declare @OT_Limit_Sec as numeric
declare @OT_Limit as varchar(10)
declare @Temp_For_Date as varchar(11)
declare @Shift_Sec as numeric
declare @Return_Sec as numeric
declare @Tot_Working_Sec as numeric
declare @Working_Sec as numeric
declare @OT_Sec as numeric
declare @Holiday_Work_Sec as numeric
declare @WeekOff_Work_Sec as numeric
set @Return_Sec = 0
set @Holiday_Work_Sec = 0
set @WeekOff_Work_Sec = 0
declare @In_Date as datetime
declare @Out_Date as datetime
declare @Shift_Dur as varchar(10)
Declare @Temp_Date as datetime
declare @Min_Dur as varchar(10)
declare @Rounding_Shift_Time as numeric
declare @Next_Day_Working_Sec as numeric
Declare @Is_OT as varchar(1)
Declare @Fix_OT as numeric
Declare @Shift_End_DateTime as datetime
Declare @Shift_ST_DateTime as datetime
declare @Last_out_Date as datetime
Declare @Manual_Last_in_Date as datetime
declare @Next_day_Work_Sec as numeric -- previous days working sec
declare @Temp_Working_sec as numeric
declare @varWagesType as varchar(20)
Declare @temp_out_Date as datetime
dECLARE @SHIFT_ID AS NUMERIC
declare @Insert_In_Date as datetime
Declare @Insert_Out_Date as datetime
DECLARE @INSERT_COUNT AS INTEGER
Declare @Late_In as varchar(20)
Declare @Late_Out as varchar(20)
Declare @Early_In as varchar(20)
Declare @Early_Out as varchar(20)
Declare @WORKING_HOURS as varchar(20)
Declare @Late_In_Count as numeric
Declare @Early_out_count as numeric
Declare @Total_less_work_sec as numeric
Declare @Late_In_Sec numeric
Declare @Late_Out_Sec numeric
Declare @Early_In_Sec numeric
Declare @Early_Out_Sec numeric
Declare @Toatl_Working_sec numeric
Declare @Total_work as varchar(20)
Declare @Less_Work as varchar(20)
Declare @More_Work as varchar(20)
Declare @Diff_Sec as numeric
declare @Working_Sec_AfterShift as numeric
Declare @Working_AfterShift_Count as numeric
Declare @Reason as varchar(300)
Declare @Pre_Reason as varchar(300)
Declare @Last_Entry_For_check as datetime
declare @Shift_St_Sec as numeric
declare @Shift_En_sec as numeric
declare @Pre_Inout_Flag as varchar(1)
declare @Pre_In_Date as datetime
Declare @Pre_Shift_St_dateTime as datetime
Declare @Pre_Shift_En_DateTime as datetime
Declare @Break_Duration varchar(10)
Declare @Break_sec numeric
Declare @Break_Deducted char(1)
Declare @More_Work_Sec numeric
declare @Min_Shift_Hour varchar(10)
declare @Min_Shift_Sec numeric
set @Fix_OT = 0
set @Min_Shift_Hour = ''
set @Min_Shift_Sec = 0
set @Reason = ''
set @Pre_Reason = ''
set @Shift_St_Time = ''
set @Shift_End_Time = ''
set @Shift_Dur = ''
set @Late_Comm_Limit = ''
set @Late_comm_sec = 0
set @Leave_Id = 0
set @Leave_Name = ''

select @Late_Comm_Limit = Late_Limit , @OT_Limit = OT_Limit from Gen_Se


tting where Company_ID = @Company_ID
exec Return_DurInSecond @Late_Comm_Limit,@Late_Comm_sec output
exec Return_DurInSecond @OT_Limit,@OT_Limit_Sec output
declare @Emp_Inout table
(
emp_id numeric ,
for_Date datetime,
Dept_id numeric null ,
Grade_id numeric null,
Shift_ID numeric null ,
In_Time datetime null,
Out_Time datetime null,
Duration varchar(20) null,
Duration_sec numeric null,
Late_In varchar(20) null,
Late_Out varchar(20) null,
Early_In varchar(20) null,
Early_Out varchar(20) null,
Leave varchar(5) null,
Shift_Sec numeric null,
Shift_Dur varchar(20) null,
Total_work varchar(20) null,
Less_Work varchar(20) null,
More_Work varchar(20) null,
Reason varchar(200) null,
AB_LEAVE VARCHAR(10) NULL,
Late_In_Sec numeric null,
Late_In_count numeric null,
Early_Out_sec numeric null,
Early_Out_Count numeric null,
Total_Less_work_Sec numeric null,
Shift_St_Datetime datetime null,
Shift_en_Datetime datetime null,
Working_Sec_AfterShift numeric null,
Working_AfterShift_Count numeric null ,
Leave_Reason varchar(250) null,
Inout_Reason varchar(250) null,
More_Work_Sec numeric default 0,
Late_Out_Sec Numeric null,
Early_In_Sec Numeric null,
Toatl_Working_sec Numeric null
)

IF @EMP_ID > 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND EMP_ID = @EMP_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_D
ate <= join_Date and @Month_En_Date >= left_date )
or ( @Month_En_D
ate >= join_Date and @Month_En_Date <= left_date )
or left_date is
null and @Month_En_Date >= Join_Date
))
END
ELSE IF @GRADE_ID = 0 AND @EMP_TYPE_ID = 0 AND @Dept_ID = 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID = 0 AND @EMP_TYPE_ID = 0 AND @Dept_ID > 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPA
NY_ID AND Dept_ID = @Dept_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID = 0 AND @EMP_TYPE_ID > 0 AND @Dept_ID > 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPA
NY_ID AND EMP_TYPE_ID = @EMP_TYPE_ID AND Dept_ID = @Dept_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID > 0 AND @EMP_TYPE_ID > 0 AND @Dept_ID > 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND GRADE_ID = @GRADE_ID AND EMP_TYPE_ID = @EMP_TYPE_ID AND Dept_ID = @Dept
_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID > 0 AND @EMP_TYPE_ID = 0 AND @Dept_ID > 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND GRADE_ID = @GRADE_ID AND Dept_ID = @Dept_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))

END
ELSE IF @GRADE_ID > 0 AND @EMP_TYPE_ID = 0 AND @Dept_ID = 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND GRADE_ID = @GRADE_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID = 0 AND @EMP_TYPE_ID > 0 AND @Dept_ID = 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND EMP_TYPE_ID = @EMP_TYPE_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID > 0 AND @EMP_TYPE_ID > 0 AND @Dept_ID = 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND GRADE_ID = @GRADE_ID AND EMP_TYPE_ID = @EMP_TYPE_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
ELSE IF @GRADE_ID > 0 AND @EMP_TYPE_ID > 0 AND @Dept_ID = 0
BEGIN
DECLARE CUR_EMP CURSOR FOR
SELECT EMP_ID FROM EMP_MASTER WHERE COMPANY_ID = @COMPAN
Y_ID AND GRADE_ID = @GRADE_ID AND EMP_TYPE_ID = @EMP_TYPE_ID
and Division_ID = @Division_ID and Category_ID = @Catego
ry_Id and isnull(Is_Show,'Y') = 'Y'
and Emp_ID in (select emp_Id from
(select emp_id, company_
id, join_Date, isnull(left_Date, @Month_En_Date) as left_Date from left_join_tra
n) qry
where company_id = @comp
any_id and
(( @Month_st_Date >= jo
in_Date and @Month_st_Date <= left_date )
or ( @Month_st_Date <= j
oin_Date and @Month_En_Date >= left_date )
or ( @Month_En_Date >=
join_Date and @Month_En_Date <= left_date )
or left_date is null and
@Month_En_Date >= Join_Date))
END
OPEN CUR_EMP
FETCH NEXT FROM CUR_EMP INTO @EMP_ID
WHILE @@FETCH_STATUS = 0
BEGIN
set @varWeekoff_Date = ''
set @varHoliday_Date = ''
set @Last_out_Date = null
set @out_Date = null
set @in_Date = null
set @Last_Entry_For_check = null
set @Pre_Inout_Flag = ''
set @Pre_Shift_St_DateTime = null
set @Pre_Shift_en_DateTime = null
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0
set @Temp_Month_Date = @month_St_Date
SELECT @Grade_ID = GRADE_ID,@emp_Type_ID = @EMP_
TYPE_ID,@Dept_ID = DEPT_ID
FROM INCREMENT INNER JOIN
(select
MAX(INCREMENT_EFFECTIVE_DATE)AS INCREMENT_EFFECTIVE_DATE , EMP_ID from incremen
t
where Co
mpany_ID = @COMPANY_ID
and Incr
ement_Effective_Date <= @month_en_Date
GROUP BY
EMP_id) QRY ON INCREMENT.INCREMENT_EFFECTIVE_dATE = QRY.INCREMENT_EFFECTIVE_dA
TE
AND INCREMENT.EMP_ID = QRY.EMP_ID
set @insert_In_date = null
set @insert_Out_Date = null
set @Pre_In_Date = null
set @strOnlyHoliday_date = ''
set @Total_Holiday_Date = ''
set @Total_LeaveDay_Date = ''
set @varWeekoff_Date = ''
exec pro_Get_LeaveDate_Date @Company_ID,@Emp_ID,@Month,@dblYear,@Month_
St_Date,@Month_En_Date, @Total_LeaveDay_Date output
exec pro_Get_Holiday_Date_new @Emp_ID,@company_id , @month ,@dblYear, @M
onth_St_Date ,@Month_En_Date ,@Total_Holiday_Date output ,@strOnlyHoliday_date o
utput
exec pro_Get_Weekoff_Days_New @Emp_ID,@Company_Id,@Month , @dblYear,@Mon
th_St_Date ,@Month_En_Date ,@strOnlyHoliday_date,null,null,@varWeekoff_Date outp
ut ,'N'
Declare @Temp_End_Date as datetime
set @Temp_End_Date = Dateadd(d,1,@Month_En_Date)
while @Temp_Month_Date <= @Temp_End_Date
begin
set @working_sec = 0
set @Ot_sec = 0
set @holiday_work_sec = 0
set @Count = 0
set @shift_ID = 0
set @Late_In = ''
set @Late_Out =''
set @Early_In = ''
set @Early_Out = ''
SET @WORKING_HOURS = ''
SET @INSERT_COUNT = 0
set @Late_In_sec = 0
set @Late_Out_Sec = 0
set @Early_In_sec = 0
set @Early_Out_sec = 0
set @Toatl_Working_sec = 0
set @Less_Work = ''
set @More_Work = ''
set @Total_work = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @Break_Duration = ''
set @Break_sec = 0
set @Break_Deducted ='N'
set @More_Work_Sec =0
declare @Break_Dur_Sec_2 as numeric
declare @Break_Duration_2 as varchar(10)
---chirag for 2nd break exec Get_Emp_Curr_Shift @emp_id,
@company_id,@Temp_Month_Date,@Shift_St_Time output,@Shift_End_Time output,@Shift
_Dur output,null,@Break_Duration output,null,@Min_Shift_Hour output,null,null,@s
hift_ID output
exec Get_Emp_Curr_Shift @emp_id,@company_id,@Temp_Month_
Date,@Shift_St_Time output,@Shift_End_Time output,@Shift_Dur output,null,@Break_
Duration output,@Break_Duration_2 output,@Min_Shift_Hour output,null,null,@shift
_ID output

select @Grade_ID = Grade_ID ,@Dept_ID = Dept_ID from inc


rement
where emp_id = @emp_id and company_id = @Company_id
and Increment_Effective_Date = (select max(Increment_Eff
ective_Date) from increment
where emp_id = @emp_id and company_id = @company_id and
Increment_Effective_Date <=
@Temp_Month_Date)

set @Shift_Sec = 0
exec Return_DurInSecond @Shift_Dur,@Shift_Sec output
exec Return_DurInSecond @Shift_St_Time,@Shift_St_Sec out
put
exec Return_DurInSecond @Shift_End_Time,@Shift_En_Sec ou
tput
exec Return_DurInSecond @Break_Duration,@Break_Sec outpu
t
exec Return_DurInSecond @Min_Shift_Hour, @Min_Shift_Sec
output
-----check for 2 nd shift
exec Return_DurInSecond @Break_Duration_2,@Break_Dur_Sec
_2 output --chirag
if not exists(select 1 from shift_master where Deduct_Br
eak_dur=1 and shift_id=@shift_id)
begin
set @Break_Dur_Sec_2=0
end
set @Break_Sec=@Break_Sec+@Break_Dur_Sec_2
-----end check for 2 nd shift
set @Fix_OT = 86400 - @Shift_Sec
--set @Day_St_Time = cast(cast(@Temp_Month_Date as varch
ar(11)) + ' ' + @Shift_St_Time as smalldatetime)
set @Day_St_Time = cast(cast(@Temp_Month_Date as varchar
(11)) + ' ' + '00:00' as smalldatetime)
set @Shift_St_Datetime = cast(cast(@Temp_Month_Date as v
archar(11)) + ' ' + @Shift_St_Time as smalldatetime)

set @Temp_Date = dateadd(d,1,@Temp_Month_Date)


--exec Get_Emp_Curr_Shift @emp_id,@company_id,@Temp_Date
,@Shift_St_Time output,@Shift_End_Time output
--set @Day_End_Time = cast(cast(@Temp_Date as varchar(11
)) + ' ' + @Shift_St_Time as smalldatetime)
set @Day_End_Time = cast(cast(@Temp_Date as varchar(11))
+ ' ' + '00:00' as smalldatetime)
if @Shift_St_Sec > @Shift_En_Sec
set @Shift_End_DateTime = cast(cast(@Temp_Date a
s varchar(11)) + ' ' + @Shift_End_Time as smalldatetime)
else
set @Shift_End_DateTime = cast(cast(@Temp_Month_
Date as varchar(11)) + ' ' + @Shift_End_Time as smalldatetime)

declare CurInout cursor for


select for_date,in_duration,in_out_flag,Reason
from emp_inout_record
where emp_id = @emp_id and company_id = @company
_id
and for_Date >= @Day_St_Time and for_date < @Day
_End_Time
open CurInout
fetch next from CurInout into @for_date,@In_Dur,
@In_Out_Flag ,@Reason
while @@fetch_status = 0
begin
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count =
0
-- Check Priviou
s Entry is In Entry and Current also in Entry
------ %%%%%%%%%
%%%% ---------
if @Pre_Inout_Flag = @In_Ou
t_Flag and @In_out_Flag ='I' and @Pre_In_Date is not null
begin
set @Insert_In_D
ate = @Pre_In_Date
set @Pre_In_Date
= cast(cast(@Pre_In_Date as varchar(11)) as smalldatetime)
set @Insert_Out_
Date = null
if not exists(se
lect Emp_ID from @Emp_inout where Emp_ID = @Emp_ID
and In_T
ime is not null and For_Date = @Pre_In_Date )
begin
if datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime)
if datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime)
end

set @late_In_Sec
= @late_In_Sec * -1
exec Return_Wit
hout_Sec @late_in_sec , @late_in_sec output
exec Return_Wit
hout_Sec @Early_In_Sec , @Early_In_Sec output
if @Late_Comm_se
c >= @late_in_sec set @late_in_sec = 0
if @Late_Comm_se
c >= @Early_In_Sec set @Early_In_Sec = 0
if @late_in_Sec
> 0
set @Lat
e_in_count =1
if @late_in_sec
> 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,'N'
if @Early_In_Sec
> 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output ,'N'
if @Insert_IN_Da
te > @Pre_Shift_En_dateTime
begin
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 1
end

delete from @Emp


_Inout where Emp_ID = @emp_Id and For_Date = @Pre_In_Date
and in_T
ime is null and out_Time is null

INSERT INTO @EMP


_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,Out_Time ,Duratio
n ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id
,@Pre_In_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DATE ,@Insert_Out_DATE
,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Pre_Shift_St_dateTime,@P
re_Shift_En_dateTime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)
SET @Insert_IN_D
ATE = NULL
set @Late_In_Sec
= 0
set @Early_In_Se
c = 0
set @late_in_cou
nt = 0
set @Early_Out_C
ount = 0
set @Total_less_
work_sec = 0
set @late_In = '
'
set @Early_In =
''
set @Pre_Reason
= ''
set @More_Work_S
ec =0
if charindex(cas
t(@Pre_In_Date as varchar(11)),@strOnlyHoliday_Date,0) > 0
begin
if @Weekoff_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Pre_In_Date
end
end
else if charinde
x(cast(@Pre_In_Date as varchar(11)),@varWeekoff_Date,0) > 0
begin

if @Weekoff_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Pre_In_Date
end

end
end
else if @In_Out_Flag = 'O' and @
In_Out_Flag = @Pre_Inout_Flag
begin
set @Insert_Out_
Date = @For_Date
if exists(select
Emp_ID from @Emp_inout where Emp_ID = @Emp_ID
and Out_
Time is not null and For_Date = cast(cast(@Insert_Out_Date as varchar(11)) as sm
alldatetime))
begin
update @Emp_inout
set Late_out =''
, Early_Out = ''
, @Early_Out_Sec = 0
where Emp_ID = @Emp_Id and Out_Time is not null and For_Date = cast(cast(@Ins
ert_Out_Date as varchar(11)) as smalldatetime)
end
if datediff(s,@I
nsert_Out_Date,@Shift_End_Datetime) > 0
set @Ear
ly_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if datediff(s,@I
nsert_Out_Date,@Shift_End_Datetime) < 0
set @Lat
e_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)

set @Late_Out_Se
c = @Late_Out_Sec * -1
exec Return_Wit
hout_Sec @late_out_sec , @late_out_sec output
exec Return_Wit
hout_Sec @Early_Out_Sec ,@Early_Out_Sec output
if @Late_Comm_se
c >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_se
c >= @Early_Out_Sec set @Early_Out_Sec = 0
if @late_out_sec
> 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output ,'N'
if @Early_Out_Se
c > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out output ,'N'
if @Early_Out_Se
c > 0
set @Ear
ly_Out_Count = 1

if @Insert_IN_Da
te > @Shift_End_datetime
begin
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 1
end
delete from @Emp
_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_T
ime is null and out_Time is null

INSERT INTO @EMP


_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,Out_Time ,Duratio
n ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shi
ft_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Les
s_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id
,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DATE ,@Insert_Out_D
ATE ,@working_Hours,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Reason,@late_in_sec,@Early_Out_Sec,@T
otal_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift_End
_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Reason,@More_Work_Sec,@Late_
Out_Sec,@Early_In_Sec,@Toatl_Working_sec)
set @Last_Entry_
For_check = null
SET @Insert_IN_D
ATE = NULL
SET @Insert_Out_
Date = null
set @Late_In_Sec
= 0
set @Early_In_Se
c = 0
set @Early_Out_S
ec = 0
set @Late_Out_Se
c = 0
set @late_In = '
'
set @late_Out =
''
set @Early_In =
''
set @Early_Out =
''
set @WORKING_HOU
RS = ''
set @Total_work
= ''
set @less_work =
''
set @More_work =
''
set @Toatl_Worki
ng_sec = 0
set @Reason = ''
set @late_in_cou
nt = 0
set @Early_Out_C
ount = 0
set @Total_less_
work_sec = 0
set @For_Date =
null
set @More_Work_S
ec=0
if charindex(cas
t(@Temp_Month_Date as varchar(11)),@strOnlyHoliday_Date,0) > 0
begin
if @Weekoff_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
end
end
else if charinde
x(cast(@Temp_Month_Date as varchar(11)),@varWeekoff_Date,0) > 0
begin

if @Weekoff_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
end

end
end
------ %%%%%%%%%
%%%% ---------

set @Pre_Inout_Flag = @In_Out_F


lag
set @Pre_In_Date = @For_Date
set @Pre_Shift_St_dateTime = @Sh
ift_St_Datetime
set @Pre_Shift_En_dateTime =@Shi
ft_End_Datetime
set @Reason = isnull(@Reason,'')
if @Reason <> ''
begin
if @Pre_Reason <
> ''
set @Pre
_Reason = @Pre_Reason + ';' + @Reason
else
set @Pre
_Reason = @Reason
end
if @In_Out_Flag = 'I'
set @In_Date = @for_date
else if @In_Out_Flag = 'O'
set @Out_Date = @For_Dat
e
if @out_Date <= @Last_out_Date
set @out_Date = null
set @Last_Entry_For_check = @Out
_Date
set @Count = @count + 1
if @in_date is null and @out_dat
e is not null
begin
Declare @Check_f
lag as varchar(1)
select @in_date
= max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Date < @Day
_st_time
--and In_out_Fla
g = 'I'
and cast(cast(fo
r_Date as varchar(11))as datetime) <= cast(cast(@Day_St_Time as varchar(11))as d
atetime)
if @In_date is n
ot null
begin
select @Check_flag = isnull(In_Out_flag,'' ) from emp_inout_record where emp_ID
= @Emp_ID and For_Date = @In_date

if @Check_flag = 'O'
set @In_date = null
end
set @Temp_Out_Da
te = @out_Date
if @Temp_Out_Dat
e > @out_Date
set @Las
t_Out_Date = @Temp_Out_Date
else
set @Las
t_Out_Date = @out_Date
set @Next_day_Wo
rk_Sec = datediff(s,@in_date,@out_date)
if @Next_day_Wor
k_Sec > 1197200
begin

set @Next_day_Work_Sec = @Next_day_Work_Sec - (@Fix_OT + @shift_SEc)


set @Working_Sec = @Working_Sec + @Next_day_Work_Sec
set @Temp_For_Date = @in_Date
set @Temp_Working_sec = datediff(s,@in_date,@out_date) - @Next_day_Work_Sec
if @Next_day_Work_Sec > 0
begin
------ %%%%%%%%%%%%% ---------
set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date

if datediff(s,@Insert_In_Date,@Shift_St_Datetime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_In_Date,@Shift_St_Datetime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)

set @late_In_Sec = @late_In_Sec * -1


set @Late_Out_Sec = @Late_Out_Sec * -1
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0
if exists(select EI.Emp_ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte )
begin
select @Toatl_Working_sec = isnull(sum(Duration_Sec) ,0)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte

select @Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)


from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
and EI.In_Time >= @Shift_End_datetime

update @Emp_Inout
set Late_out = ''
, Early_Out = ''
, Total_Work = ''
, less_work = ''
, More_work = ''
, Early_Out_sec = 0
, Total_Less_work_sec = 0
, Early_Out_count = 0
, More_Work_Sec =0
, Toatl_Working_sec = 0
where Emp_ID = @emp_Id and For_Date = @temp_month_Date
set @Early_In_Sec = 0
set @late_In_Sec = 0
end

exec Return_Without_Sec @late_in_sec ,@late_in_sec output


exec Return_Without_Sec @late_out_sec ,@late_out_sec output
exec Return_Without_Sec @Early_In_Sec ,@Early_In_Sec output
exec Return_Without_Sec @Early_Out_Sec ,@Early_Out_Sec output
exec Return_Without_Sec @Working_sec ,@Working_sec output

if @Late_Comm_sec >= @late_in_sec set @late_in_sec = 0


if @Late_Comm_sec >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_sec >= @Early_In_Sec set @Early_In_Sec = 0
if @Late_Comm_sec >= @Early_Out_Sec set @Early_Out_Sec = 0

if @late_in_sec > 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,


'N'
if @late_out_sec > 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output
,'N'

if @Early_In_Sec > 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output


,'N'
if @Early_Out_Sec > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out outp
ut ,'N'
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'

set @Toatl_Working_sec = isnull(@Toatl_Working_sec,0) + @Working_sec

if @Toatl_Working_sec > @Break_Sec


begin
if @Toatl_Working_sec > @Min_Shift_Sec -- Hasmukh 02/04/2009
begin
set @Toatl_Working_sec = @Toatl_Working_sec - @Break_Sec
set @Working_sec = @Working_sec -- @Break_Sec
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'
end
End
if @Toatl_Working_sec > 0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_
work output ,'N'

if @Insert_IN_Date > @Shift_End_datetime


begin
set @Working_Sec_AfterShift = @Working_Sec + @Working_Sec_AfterShift
set @Working_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl_Working_sec - (@Shift_Sec -@Break_Sec)

if @Diff_Sec > 0 and @Diff_Sec >=@OT_Limit_Sec


begin
exec Return_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @More_Work_Sec = @Diff_Sec
end
else if @Diff_Sec < 0 and @Toatl_Working_sec > 0
begin
set @Diff_Sec = @Diff_Sec * -1
set @Total_Less_Work_Sec = @Diff_Sec
exec Return_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end

if @late_in_Sec > 0
set @Late_in_count =1
if @Early_Out_Sec > 0
set @Early_Out_Count = 1

if exists ( select Emp_ID from @Emp_Inout where Emp_ID = @emp_Id and For_Date
= @Temp_Month_Date
and in_Time is null and out_Time is null )
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_Time is null and out_Time is null
end

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,inout_reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DAT
E ,@Insert_Out_DATE ,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift
_End_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)

set @Last_Entry_For_check = null


SET @Insert_IN_DATE = NULL
SET @Insert_Out_Date = null
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec = 0
set @Reason = ''
set @Pre_reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @More_Work_Sec =0

------ %%%%%%%%%%%%% ---------


end
if datediff(d,@in_date,@out_date) > 0
begin
set @Temp_For_Date = dateadd(d,1,@in_Date)
set @Temp_Working_sec = @Next_day_Work_Sec

------ %%%%%%%%%%%%% ---------


set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date

if datediff(s,@Insert_In_Date,@Shift_St_Datetime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_In_Date,@Shift_St_Datetime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
set @late_In_Sec = @late_In_Sec * -1
set @Late_Out_Sec = @Late_Out_Sec * -1

set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0

if exists(select EI.Emp_ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID


and EI.For_DAte = @Temp_For_Date )
begin
select @Toatl_Working_sec = sum(Duration_Sec)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @Temp_For_Date
select @Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
and EI.In_Time >= @Shift_End_datetime

update @Emp_Inout
set Late_out = ''
, Early_Out = ''
, Total_Work = ''
, less_work = ''
, More_work = ''
, Early_Out_sec = 0
, Total_Less_work_sec = 0
, Early_Out_count = 0
, More_Work_Sec =0
, Toatl_Working_sec = 0
where Emp_ID = @emp_Id and For_Date = @Temp_For_Date
set @Early_In_Sec = 0
set @late_In_Sec = 0
end

exec Return_Without_Sec @late_in_sec ,@late_in_sec output


exec Return_Without_Sec @late_out_sec ,@late_out_sec output
exec Return_Without_Sec @Early_In_Sec ,@Early_In_Sec output
exec Return_Without_Sec @Early_Out_Sec ,@Early_Out_Sec output
exec Return_Without_Sec @Working_sec ,@Working_sec output

if @Late_Comm_sec >= @late_in_sec set @late_in_sec = 0


if @Late_Comm_sec >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_sec >= @Early_In_Sec set @Early_In_Sec = 0
if @Late_Comm_sec >= @Early_Out_Sec set @Early_Out_Sec = 0

if @late_in_sec > 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,


'N'
if @late_out_sec > 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output
,'N'

if @Early_In_Sec > 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output


,'N'
if @Early_Out_Sec > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out outp
ut ,'N'
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'

set @Toatl_Working_sec = isnull(@Toatl_Working_sec,0) + @Working_sec

if @Toatl_Working_sec > @Break_Sec


begin
if @Toatl_Working_sec > @Min_Shift_Sec -- Hasmukh 02/04/2009
begin

set @Toatl_Working_sec = @Toatl_Working_sec - @Break_Sec


set @Working_sec = @Working_sec -- @Break_Sec
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'
end
End
if @Toatl_Working_sec > 0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_
work output ,'N'

if @Insert_IN_Date > @Shift_End_datetime


begin
set @Working_Sec_AfterShift = @Working_sec + @Working_Sec_AfterShift
set @Working_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl_Working_sec - (@Shift_Sec -@Break_Sec)

if @Diff_Sec > 0 and @Diff_Sec >=@OT_Limit_Sec


begin
exec Return_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @More_Work_Sec =@Diff_Sec
end
else if @Diff_Sec < 0 and @Toatl_Working_sec > 0
begin
set @Diff_Sec = @Diff_Sec * -1
set @Total_Less_Work_Sec = @Diff_Sec
exec Return_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end

if @late_in_Sec > 0
set @Late_in_count =1
if @Early_Out_Sec > 0
set @Early_Out_Count = 1
if exists ( select Emp_ID from @Emp_Inout where Emp_ID = @emp_Id and For_Date
= @Temp_Month_Date
and in_Time is null and out_Time is null )
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_Time is null and out_Time is null
end

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec )
VALUES (@emp_id ,@Temp_For_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DATE
,@Insert_Out_DATE ,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift
_End_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)

set @Last_Entry_For_check = null


SET @Insert_IN_DATE = NULL
SET @Insert_Out_Date = null
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec = 0
set @Reason = ''
set @Pre_reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @More_Work_Sec =0
------ %%%%%%%%%%%%% ---------

set @Working_Sec = @Working_Sec -@Next_day_Work_Sec


end

set @Next_day_Work_Sec = 0
set @Insert_In_Date = @in_Date
set @Insert_out_Date = @out_Date
set @out_DAte = null
set @in_Date = null
end

if dated
iff(d,@in_date,@out_date) > 0
begin
set @Temp_For_Date = cast(cast(@in_Date as varchar(11) ) as smalldatetime)
set @Temp_Working_sec = @Next_day_Work_Sec
set @Working_sec = @Temp_Working_sec

------ %%%%%%%%%%%%% ---------


set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date
if cast(@Temp_For_Date as varchar(11)) <> cast(@Temp_Month_date as varchar(11))
begin
exec Get_Emp_Curr_Shift @emp_id,@company_id,@Temp_For_Date,@Shift_St_Time output
,@Shift_End_Time output
set @Pre_Shift_St_dateTime = cast(cast(@Temp_For_Date as varchar(11)) + ' ' + @S
hift_St_Time as smalldatetime)
set @Pre_Shift_En_dateTime = cast(cast(@Temp_For_Date as varchar(11)) + ' ' + @S
hift_End_Time as smalldatetime)
end
else
begin
set @Pre_Shift_St_dateTime = @Shift_St_dateTime
set @Pre_Shift_En_dateTime = @Shift_End_dateTime
end

if datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime)
if datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Pre_Shift_St_dateTime)
if datediff(s,@Insert_Out_Date,@Pre_Shift_En_dateTime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Pre_Shift_En_dateTime)
if datediff(s,@Insert_Out_Date,@Pre_Shift_St_dateTime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Pre_Shift_En_dateTime)

set @late_In_Sec = @late_In_Sec * -1


set @Late_Out_Sec = @Late_Out_Sec * -1
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0
if exists(select EI.Emp_ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @Temp_For_Date )
begin
select @Toatl_Working_sec = sum(Duration_Sec)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @Temp_For_Date
select @Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @Temp_For_Date
and EI.In_Time >= @Pre_Shift_En_dateTime

update @Emp_Inout
set Late_out = ''
, Early_Out = ''
, Total_Work = ''
, less_work = ''
, More_work = ''
, Early_Out_sec = 0
, Total_Less_work_sec = 0
, Early_Out_count = 0
, More_Work_Sec=0
, Toatl_Working_sec = 0
where Emp_ID = @emp_Id and For_Date = @Temp_For_Date
set @Early_In_Sec = 0
set @late_In_Sec = 0
end

exec Return_Without_Sec @late_in_sec ,@late_in_sec output


exec Return_Without_Sec @late_out_sec ,@late_out_sec output
exec Return_Without_Sec @Early_In_Sec ,@Early_In_Sec output
exec Return_Without_Sec @Early_Out_Sec ,@Early_Out_Sec output
exec Return_Without_Sec @Working_sec ,@Working_sec output

if @Late_Comm_sec >= @late_in_sec set @late_in_sec = 0


if @Late_Comm_sec >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_sec >= @Early_In_Sec set @Early_In_Sec = 0
if @Late_Comm_sec >= @Early_Out_Sec set @Early_Out_Sec = 0

if @late_in_sec > 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,


'N'
if @late_out_sec > 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output
,'N'

if @Early_In_Sec > 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output


,'N'
if @Early_Out_Sec > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out outp
ut ,'N'
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'

set @Toatl_Working_sec = isnull(@Toatl_Working_sec,0) + @Working_sec

if @Toatl_Working_sec > @Break_Sec


begin
if @Toatl_Working_sec > @Min_Shift_Sec -- Hasmukh 02/04/2009
begin

set @Toatl_Working_sec = @Toatl_Working_sec - @Break_Sec


set @Working_sec = @Working_sec -- @Break_Sec
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'
end
End

if @Toatl_Working_sec > 0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_


work output ,'N'

if @Insert_IN_Date > @Pre_Shift_En_dateTime


begin
set @Working_Sec_AfterShift = @Working_sec + @Working_Sec_AfterShift
set @Working_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl_Working_sec - (@Shift_Sec -@Break_Sec)

if @Diff_Sec > 0 and @Diff_Sec >=@OT_Limit_Sec


begin
exec Return_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @More_Work_Sec =@Diff_Sec
end
else if @Diff_Sec < 0 and @Toatl_Working_sec > 0
begin
set @Diff_Sec = @Diff_Sec * -1
set @Total_Less_Work_Sec = @Diff_Sec
exec Return_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end

if @late_in_Sec > 0
set @Late_in_count =1
if @Early_Out_Sec > 0
set @Early_Out_Count = 1

if exists ( select Emp_ID from @Emp_Inout where Emp_ID = @emp_Id and cast(cas
t(For_Date as varchar(11))as smalldatetime) = cast(cast(@Temp_For_Date as varcha
r(11))as smalldatetime)
and in_Time is null and out_Time is null
)
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_For_Date
and in_Time is null and out_Time is null
end

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id ,@Temp_For_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DATE
,@Insert_Out_DATE ,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift
_End_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)

set @Last_Entry_For_check = null


SET @Insert_IN_DATE = NULL
SET @Insert_Out_Date = null
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec = 0
set @Reason = ''
set @Pre_reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @More_Work_Sec =0
------ %%%%%%%%%%%%% ---------

set @Working_Sec = @Working_Sec - @Next_day_Work_Sec


-- set @Insert_In_Date = @in_Date
-- set @Insert_out_Date = @out_Date
set @In_date = null
set @out_Date = null
if @working_Sec < 0
set @working_Sec = 0
end
if cast(cast(@In_Date as
varchar(11) )as datetime) < cast(cast(@Day_St_Time as varchar(11))as datetime)
and cast(cast(@Out_Date as varchar(11) )as datetime) < cast(cast(@Day_St_Time
as varchar(11))as datetime) and @in_Date is not null and @out_Date is not null
begin
set @out
_DAte = null
set @in_
Date = null
end
end

set @temp_Out_Date = @out_Date

if @in_Date is not null and @out


_Date is not null
begin
set @working_sec
= @working_sec + datediff(s,@in_date,@out_date)
if @Temp_Out_Dat
e > @out_Date
set @Las
t_Out_Date = @Temp_Out_Date
else
set @Las
t_Out_Date = @out_Date
--if datediff(d,
@in_date,@out_date) > 0
begin
set @Temp_For_Date = @in_Date
set @Temp_Working_sec = datediff(s,@in_date,@out_date)

------ %%%%%%%%%%%%% ---------


set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date

if datediff(s,@Insert_In_Date,@Shift_St_Datetime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_In_Date,@Shift_St_Datetime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)

set @late_In_Sec = @late_In_Sec * -1


set @Late_Out_Sec = @Late_Out_Sec * -1
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0
if exists(select EI.Emp_ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte )
begin
select @Toatl_Working_sec = sum(Duration_Sec)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
select @Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
and EI.In_Time >= @Shift_End_datetime

update @Emp_Inout
set Late_out = ''
, Early_Out = ''
, Total_Work = ''
, less_work = ''
, More_work = ''
, Early_Out_sec = 0
, Total_Less_work_sec = 0
, Early_Out_count = 0
, More_Work_Sec = 0
, Toatl_Working_sec = 0
where Emp_ID = @emp_Id and For_Date = @temp_month_Date
set @Early_In_Sec = 0
set @late_In_Sec = 0
end

exec Return_Without_Sec @late_in_sec ,@late_in_sec output


exec Return_Without_Sec @late_out_sec ,@late_out_sec output
exec Return_Without_Sec @Early_In_Sec ,@Early_In_Sec output
exec Return_Without_Sec @Early_Out_Sec ,@Early_Out_Sec output
exec Return_Without_Sec @Working_sec ,@Working_sec output

if @Late_Comm_sec >= @late_in_sec set @late_in_sec = 0


if @Late_Comm_sec >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_sec >= @Early_In_Sec set @Early_In_Sec = 0
if @Late_Comm_sec >= @Early_Out_Sec set @Early_Out_Sec = 0

if @late_in_sec > 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,


'N'
if @late_out_sec > 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output
,'N'

if @Early_In_Sec > 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output


,'N'
if @Early_Out_Sec > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out outp
ut ,'N'
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'

set @Toatl_Working_sec = isnull(@Toatl_Working_sec,0) + @Working_sec

if @Toatl_Working_sec > @Break_Sec


begin
if @Toatl_Working_sec > @Min_Shift_Sec -- Hasmukh 02/04/2009
begin

set @Toatl_Working_sec = @Toatl_Working_sec - @Break_Sec


set @Working_sec = @Working_sec -- @Break_Sec
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'
end
End

if @Toatl_Working_sec > 0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_


work output ,'N'
if @Insert_IN_Date > @Shift_End_datetime
begin
set @Working_Sec_AfterShift = @Working_sec + @Working_Sec_AfterShift
set @Working_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl_Working_sec - (@Shift_Sec -@Break_Sec)

if @Diff_Sec > 0 and @Diff_Sec >=@OT_Limit_Sec


begin
exec Return_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @More_Work_sec =@Diff_Sec
end
else if @Diff_Sec < 0 and @Toatl_Working_sec > 0
begin
set @Diff_Sec = @Diff_Sec * -1
set @Total_Less_Work_Sec = @Diff_Sec
exec Return_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end

if @late_in_Sec > 0
set @Late_in_count =1
if @Early_Out_Sec > 0
set @Early_Out_Count = 1

if exists ( select Emp_ID from @Emp_Inout where Emp_ID = @emp_Id and For_Date
= @Temp_Month_Date
and in_Time is null and out_Time is null )
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_Time is null and out_Time is null
end

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DAT
E ,@Insert_Out_DATE ,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift
_End_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)

set @Last_entry_For_check = null


SET @Insert_IN_DATE = NULL
SET @Insert_Out_Date = null
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec = 0
set @Reason = ''
set @Pre_reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @More_Work_Sec =0
------ %%%%%%%%%%%%% ---------

set @Working_Sec = @Working_Sec - datediff(s,@in_date,@out_date)


if @Break_Deducted ='Y' and @Working_Sec < 0
set @Working_Sec =0
end
-- set @Insert_In_D
ate = @in_Date
-- set @Insert_Out_
Date = @Out_Date
set @in_date = n
ull
set @out_date =
null
end
fetch next from CurInout into @f
or_date,@In_Dur,@In_Out_Flag ,@Reason
end
if @Last_Out_Date is null
begin
if @out_Date is null
begin
select @
Out_Date = max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Da
te < @Day_st_time
and In_o
ut_Flag = 'O' and
cast(cas
t(for_Date as varchar(11))as datetime) <= cast(cast(@Day_St_Time as varchar(11))
as datetime)

if @Out_Date is null and cast(cast(@Temp_Month_Date as varchar(11))as smalldatet


ime) <= cast(cast(@Day_End_time as varchar(11))as smalldatetime)
begin
if exists(select Emp_ID from Emp_inout_Record where Emp_ID = @Emp_ID and For_Dat
e = (
select MIN(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Date
> @Day_End_Time )
and In_Out_Flag ='O' )
begin
select @Out_Date = For_Date from Emp_inout_Record where Emp_ID = @Emp_ID and For
_Date = (
select MIN(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Date
> @Day_End_Time )
and In_Out_Flag ='O'
end
end
select @
in_date = max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Dat
e < @Out_Date
and In_o
ut_Flag = 'I' and
cast(cas
t(for_Date as varchar(11)) as datetime) <= cast(cast(@Day_St_Time as varchar(11)
)as datetime )
end
else if @in_Date is null
select @in_date
= max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Date < @Day
_st_time
and In_out_Flag
= 'I' and
cast(cast(for_Da
te as varchar(11)) as datetime) <= cast(cast(@Day_St_Time as varchar(11))as date
time)

if @Temp_Out_Date > @out


_Date
set @Last_Out_Da
te = @Temp_Out_Date
else
set @Last_Out_Da
te = @out_Date
------ 05:00 In time and
7:00 out Time .....
if @In_Date < @Day_St_ti
me and @Out_Date < @Day_st_Time
begin
set @In_
Date = Null
set @out
_date = null
end
set @Next_day_Work_Sec =
datediff(s,@in_date,@out_date)
if @Next_day_Work_Sec >
1197200
begin
set @Nex
t_day_Work_Sec = @Next_day_Work_Sec - (@Fix_OT + @shift_SEc)
set @Wor
king_Sec = @Working_Sec + @Next_day_Work_Sec
set @Nex
t_day_Work_Sec = 0
set @Ins
ert_In_Date = @in_Date
set @Ins
ert_OUt_Date = @Out_Date
set @out
_DAte = null
set @in_
Date = null
end
if cast(cast(@In_Date as
varchar(11) )as datetime) < cast(cast(@Day_St_Time as varchar(11))as datetime)
and cast(cast(@Out_Date as varchar(11) )as datetime) < cast(cast(@Day_St_Time
as varchar(11))as datetime) and @in_Date is not null and @out_Date is not null
begin
set @out
_DAte = null
set @in_
Date = null
end
end

if not exists(select * from emp_inout_r


ecord
where em
p_id = @emp_id and company_id = @company_id
and for_
Date >= @Day_St_Time and for_date < @Day_End_Time )
begin
if @out_Date is null and
@In_Date is null
begin
select @
in_date = max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and For_Dat
e < @Day_st_time
and In_o
ut_Flag = 'I' and
cast(cas
t(for_Date as varchar(11))as datetime) = cast(cast(@Day_St_Time as varchar(11))a
s datetime)
select @
in_out_Flag = in_out_flag from Emp_inout_record where emp_ID = @Emp_ID and for_D
ate =
(select
max(For_Date) from emp_inout_record where emp_ID = @Emp_ID
and cast
(cast(for_Date as varchar(11))as datetime) = cast(cast(@Day_St_Time as varchar(1
1)) as datetime)
and For_
Date < @Day_st_time )

if @In_d
ate is not null and @in_out_Flag = 'I'
begin
if @Manual_Last_in_Date >= @in_Date
set @in_Date = @Manual_Last_in_Date
end
else if
@in_Date is not null and @in_out_flag = 'O' and @in_Date > @last_out_Date
begin
set @out_Date = @in_Date
select @in_date = max(For_Date) from emp_inout_record where emp_ID = @Emp_ID and
For_Date < @Out_Date
and In_out_Flag = 'I' and
cast(cast(for_Date as varchar(11))as datetime) <= cast(cast(@Day_St_Time as varc
har(11)) as datetime)
if @Temp_Out_Date > @out_Date
set @Last_Out_Date = @Temp_Out_Date
else
set @Last_Out_Date = @out_Date

set @Next_day_Work_Sec = datediff(s,@in_date,@out_date)


if @Next_day_Work_Sec > 1197200
begin
--set @Next_day_Work_Sec = @Next_day_Work_Sec - (54000 + @shift_SEc) 120606
set @Next_day_Work_Sec = @Next_day_Work_Sec - (@Fix_OT + @shift_SEc)
set @Working_Sec = @Working_Sec + @Next_day_Work_Sec
set @Next_day_Work_Sec = 0
set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date
set @out_DAte = null
set @in_Date = null
end
if cast(cast(@In_Date as varchar(11) )as datetime) < cast(cast(@Day_St_Time as
varchar(11))as datetime) and cast(cast(@Out_Date as varchar(11) ) as datetime) <
cast(cast(@Day_St_Time as varchar(11)) as datetime) and @in_Date is not null a
nd @out_Date is not null
begin
set @out_DAte = null
set @in_Date = null
end
end
else
set @in_date = null
end
end
if @out_date is null and @in_date is n
ot null
begin
declare @Check_Next_Entr
y as varchar(1)
select @Check_Next_Entry
= isnull(in_out_Flag ,'')
from Emp_Inout_R
ecord where Emp_ID = @Emp_ID and For_Date in
( select min(For
_Date)
from Emp_Inou
t_Record
where Emp_ID =
@Emp_ID and For_Date >@In_Date )
if @Check_Next_Entry = '
O'
begin
if cast(
@Temp_Month_Date as varchar(11)) = cast(@month_St_Date as varchar(11))
select @Out_Date = max(For_Date) from emp_inout_record where emp_ID = @Emp_ID an
d For_Date < @in_Date
and In_out_Flag = 'O' and
cast(cast(for_Date as varchar(11)) as datetime) = cast(cast(@Day_End_Time as var
char(11))as datetime)
else
select @Out_Date = min(For_Date) from emp_inout_record where emp_ID = @Emp_ID an
d For_Date >= @in_Date
and In_out_Flag = 'O' and
cast(cast(for_Date as varchar(11))as datetime) >= cast(cast(@in_Date as varchar(
11))as datetime)

if @Out
_Date is null
select @Out_Date = min(For_Date) from emp_inout_record where emp_ID = @Emp_ID an
d For_Date >= @in_Date
and In_out_Flag = 'O' and
cast(cast(for_Date as varchar(11))as datetime) > cast(cast(@in_Date as varchar(1
1))as datetime)

set @Temp_Out_DAte =@Out_Date


if cast(
cast(@in_date as varchar(11)) as datetime) = cast(cast(@out_Date as varchar(11))
as datetime) and @out_Date > @Day_End_Time
set @out_Date = null
else if
@in_date > @Day_St_Time and @Out_Date > @Day_End_Time and cast(cast(@In_DAte as
varchar(11))as datetime) > cast(cast(@Day_St_Time as varchar(11))as datetime)
set @out_Date = null
end
end

IF @IN_DATE < @MONTH_ST_DATE


begin
SET @IN_DATE = NULL
end
if @out_date is not null and @in_date i
s not null
begin
set @working_sec = @work
ing_sec + datediff(s,@in_date,@out_date)
if @Temp_Out_Date > @out
_Date
set @Last_Out_Da
te = @Temp_Out_Date
else
set @Last_Out_Da
te = @out_Date

------ %%%%%%%%%%%%% ---------


set @Insert_In_Date = @in_Date
set @Insert_Out_Date = @Out_Date

if datediff(s,@Insert_In_Date,@Shift_St_Datetime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_In_Date,@Shift_St_Datetime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if datediff(s,@Insert_Out_Date,@Shift_End_Datetime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)

set @late_In_Sec = @late_In_Sec * -1


set @Late_Out_Sec = @Late_Out_Sec * -1
set @Working_Sec_AfterShift = 0
set @Working_AfterShift_Count = 0
if exists(select EI.Emp_ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte )
begin
select @Toatl_Working_sec = sum(Duration_Sec)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
select @Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)
from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
and EI.In_Time >= @Shift_End_datetime

update @Emp_Inout
set Late_out = ''
, Early_Out = ''
, Total_Work = ''
, less_work = ''
, More_work = ''
, Early_Out_sec = 0
, Total_Less_work_sec = 0
, Early_Out_count = 0
, More_Work_Sec=0
, Toatl_Working_sec = 0
where Emp_ID = @emp_Id and For_Date = @temp_month_Date
set @Early_In_Sec = 0
set @late_In_Sec = 0
end

exec Return_Without_Sec @late_in_sec ,@late_in_sec output


exec Return_Without_Sec @late_out_sec ,@late_out_sec output
exec Return_Without_Sec @Early_In_Sec ,@Early_In_Sec output
exec Return_Without_Sec @Early_Out_Sec ,@Early_Out_Sec output
exec Return_Without_Sec @Working_sec ,@Working_sec output
if @Late_Comm_sec >= @late_in_sec set @late_in_sec = 0
if @Late_Comm_sec >= @late_out_sec set @late_out_sec = 0
if @Late_Comm_sec >= @Early_In_Sec set @Early_In_Sec = 0
if @Late_Comm_sec >= @Early_Out_Sec set @Early_Out_Sec = 0

if @late_in_sec > 0 exec Return_DurHourMinSec @late_In_Sec ,@late_In output ,


'N'
if @late_out_sec > 0 exec Return_DurHourMinSec @late_Out_Sec ,@late_Out output
,'N'

if @Early_In_Sec > 0 exec Return_DurHourMinSec @Early_In_Sec ,@Early_In output


,'N'
if @Early_Out_Sec > 0 exec Return_DurHourMinSec @Early_Out_Sec ,@Early_Out outp
ut ,'N'
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'

set @Toatl_Working_sec = isnull(@Toatl_Working_sec,0) + @Working_sec

if @Toatl_Working_sec > @Break_Sec


begin
if @Toatl_Working_sec > @Min_Shift_Sec -- Hasmukh 02/04/2009
begin

set @Toatl_Working_sec = @Toatl_Working_sec - @Break_Sec


set @Working_sec = @Working_sec -- @Break_Sec
if @Working_sec > 0 exec Return_DurHourMinSec @Working_Sec ,@WORKING_HOURS outp
ut ,'N'
end
End

if @Toatl_Working_sec > 0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_


work output ,'N'
if @Insert_IN_Date > @Shift_End_datetime
begin
set @Working_Sec_AfterShift = @Working_sec + @Working_Sec_AfterShift
set @Working_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl_Working_sec - (@Shift_Sec -@Break_Sec)

if @Diff_Sec > 0 and @Diff_Sec >=@OT_Limit_Sec


begin
exec Return_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @More_Work_sec =@Diff_Sec
end
else if @Diff_Sec < 0 and @Toatl_Working_sec > 0
begin
set @Diff_Sec = @Diff_Sec * -1
set @Total_Less_Work_Sec = @Diff_Sec
exec Return_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end

if @late_in_Sec > 0
set @Late_in_count =1
if @Early_Out_Sec > 0
set @Early_Out_Count = 1

if exists ( select Emp_ID from @Emp_Inout where Emp_ID = @emp_Id and For_Date
= @Temp_Month_Date
and in_Time is null and out_Time is null )
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_Time is null and out_Time is null
end

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less
_work_sec,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late
_Out_Sec,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DAT
E ,@Insert_Out_DATE ,@working_Hours ,
@working_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Sh
ift_Dur,@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Se
c,@Total_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift
_End_Datetime
,@Working_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@L
ate_Out_Sec,@Early_In_Sec,@Toatl_Working_sec)

set @Last_entry_For_check = null


SET @Insert_IN_DATE = NULL
SET @Insert_Out_Date = null
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec = 0
set @Reason = ''
set @Pre_reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec = 0
set @More_Work_Sec=0
------ %%%%%%%%%%%%% ---------
set @in_date = null
set @out_date = null
end
set @working_Sec = 0
close CurInout
deallocate CurInout

if not exists ( select e.Emp_ID from @EMP_INOUT E where


Emp_ID = @Emp_ID and for_Date = @Temp_Month_Date )
begin
if charindex(cast(@Temp_
Month_Date as varchar(11)),@Total_LeaveDay_Date,0) > 0
begin
select @Leave_ID = Leave_ID, @Reason = Reason from Leave_Approval_Detail where
(@Temp_Month_Date between From_date and To_Date) and
Leave_approval_Id in (select Leave_Approval_Id from leave_Approval
where Company_id = @Company_id and Emp_id = @Emp_ID)

select @Leave_Name = ShortName


from Leave_Master where Leave_Master.Leave_ID = @Leave_ID

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,ab_leave
,Leave_Reason,Late_Out_Sec,Early_In_Sec,Toatl_Working_sec )
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,null ,null,'-'
,
0, '-','-' , '-', '-', '-',0,'-','-','-','-',@Reason,@Leave_Name,@Reason,0,0,0)
end
else if charindex(cast(@
Temp_Month_Date as varchar(11)),@strOnlyHoliday_Date,0) > 0
begin
set @Wee
koff_Entry = @Weekoff_Entry
INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O
ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,ab_leave)
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,null ,null,'-'
,
0, '-','-' , '-', '-', '-',0,'-','-','-','-','','HO')
end
else if charindex(cast(@
Temp_Month_Date as varchar(11)),@varWeekoff_Date,0) > 0
begin
set @Wee
koff_Entry = @Weekoff_Entry

INSERT INTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,O


ut_Time ,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,ab_leave)
VALUES (@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,null ,null,'-'
,
0, '-','-' , '-', '-', '-',0,'-','-','-','-','','W')
end
else
begin
INSERT I
NTO @EMP_INOUT (emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,Out_Time
,Duration ,
Duration_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shif
t_Dur,Total_Work,Less_Work,More_work,Reason,ab_Leave)
VALUES (
@emp_id ,@Temp_Month_Date ,@Dept_id ,@Grade_id ,@Shift_ID,null ,null,'-' ,
0, '-','-' , '-', '-', '-',0,'-','-','-','-','','AB')
end
end
else
begin
if charindex(cast(@Temp_
Month_Date as varchar(11)),@strOnlyHoliday_Date,0) > 0
begin
if @Week
off_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
end
end
else if charindex(cast(@
Temp_Month_Date as varchar(11)),@varWeekoff_Date,0) > 0
begin
if @Week
off_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
End
Else
Begin
Update @EMP_INOUT set ab_leave = 'W' where Emp_ID = @emp_Id and For_Date = @Temp
_Month_Date
end
end
end

set @Temp_Month_Date = dateadd(d,1,@Temp_Month_Date)


end

Declare @Max_date as datetime


Declare @Max_Flag as varchar(1)
Declare @Last_Pre_Max_date_flag as varchar(1)
set @Max_flag = ''
set @Last_Pre_Max_date_flag = ''
select @Max_date = Max(For_Date) from emp_inout_record w
here Emp_ID = @Emp_ID AND cast(cast(FOR_DATE as varchar(11) ) as smalldatetime
) <= cast(cast(@Month_En_Date as varchar(11) ) as smalldatetime )
select @Max_flag = in_out_Flag from emp_inout_record whe
re Emp_ID = @Emp_ID and For_Date = @Max_Date
if @Max_Date is not null
select @Last_Pre_Max_date_flag = isnull(In_Out_
Flag,'') from Emp_Inout_Record where Emp_ID = @Emp_ID
and For_date in ( select Max(For_Date) from emp_
inout_record where Emp_ID = @Emp_ID AND FOR_DATE < @Max_Date )

if isnull(@Last_entry_For_check ,'') = '' and @Max_flag


='O' or ( @Last_Pre_Max_date_flag <> @Max_Flag and @Last_Pre_Max_date_flag <>
'')
begin
if not exists(select Emp_ID from @Emp_In
out where Emp_ID = @Emp_ID
and cast(cast(For_Date a
s varchar(11) ) as smalldatetime) = cast(cast(@Max_Date as varchar(11) ) as smal
ldatetime))
begin
set @Max_date =
null
end
else if isnull(@Last_entry_For_check ,''
) = '' and @In_Out_flag = 'I'
and exists(select Emp_ID
from @Emp_Inout where Emp_ID = @Emp_ID
and cast
(cast(For_Date as varchar(11) ) as smalldatetime) = cast(cast(@Max_Date as varch
ar(11) ) as smalldatetime)
and In_t
ime = @Max_Date
)
begin
set @Max_date =
null
end
else if isnull(@Last_entry_For_check ,''
) = '' and @In_Out_flag = 'O'
begin
set @Max_date = null
end
end
IF @Max_flag = 'I' AND @Max_date is not null
BEGIN
IF exists(select Emp_ID from @Em
p_inout where Emp_ID = @Emp_ID and In_Time = @Max_Date )
begin
set @Max_Date =
null
end
END
ELSE IF @Max_flag = 'O' AND @Max_date is not nul
l
BEGIN
IF exists(select Emp_ID from @Em
p_inout where Emp_ID = @Emp_ID and OUT_Time = @Max_Date )
begin
set @Max_Date =
null
end
END
if @Max_date is not null
begin
set @Temp_Month_Date = cast( cas
t(@Max_date as varchar(11) ) as smalldatetime)
exec Get_Emp_Curr_Shift @emp_id,
@company_id,@Temp_Month_Date,@Shift_St_Time output,@Shift_End_Time output,@Shift
_Dur output,null,null,null,null,null,null,@shift_ID output

set @Shift_Sec = 0
exec Return_DurInSecond @Shift_D
ur,@Shift_Sec output
set @Fix_OT = 86400 - @Shift_Sec

set @Day_St_Time = cast(cast(@Te


mp_Month_Date as varchar(11)) + ' ' + @Shift_St_Time as smalldatetime)
set @Shift_St_Datetime = cast(ca
st(@Temp_Month_Date as varchar(11)) + ' ' + @Shift_St_Time as smalldatetime)

set @Temp_Date = dateadd(d,1,@Te


mp_Month_Date)
exec Get_Emp_Curr_Shift @emp_id,
@company_id,@Temp_Date,@Shift_St_Time output,@Shift_End_Time output
set @Day_End_Time = cast(cast(@T
emp_Date as varchar(11)) + ' ' + @Shift_St_Time as smalldatetime)
set @Shift_End_DateTime = cast(c
ast(@Temp_Month_Date as varchar(11)) + ' ' + @Shift_End_Time as smalldatetime)

if cast(cast(@Max_date as varchar(1
1) ) as smalldatetime) >= cast(cast(@MOnth_St_Date as varchar(11) ) as smalldate
time) and cast(cast(@Max_date as varchar(11) ) as smalldatetime) <= cast(cast(@M
onth_En_DAte as varchar(11) ) as smalldatetime) --and @Max_flag = 'I'
begin
if @Max_Flag = 'I'
begin
set @Ins
ert_IN_DATE = @Max_Date
if dated
iff(s,@Insert_In_Date,@Shift_St_Datetime) > 0
set @Early_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
if dated
iff(s,@Insert_In_Date,@Shift_St_Datetime) < 0
set @late_In_Sec = datediff(s,@Insert_In_Date,@Shift_St_Datetime)
set @Ins
ert_Out_DATE = null
end
else
begin
set @Ins
ert_Out_DATE = @Max_Date
if dated
iff(s,@Insert_Out_Date,@Shift_End_Datetime) > 0
set @Early_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
if dated
iff(s,@Insert_Out_Date,@Shift_End_Datetime) < 0
set @Late_Out_Sec = datediff(s,@Insert_Out_Date,@Shift_End_Datetime)
set @Ins
ert_IN_DATE = null
end

set @late_In_Sec = @lat


e_In_Sec * -1
set @Late_Out_Sec = @La
te_Out_Sec * -1

if exists ( select Emp_I


D from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_Time is null and out_Time is null )
begin
delete f
rom @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
and in_T
ime is null and out_Time is null
end
if exists(select EI.Emp_
ID from @EMP_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte )
begin
select @
Toatl_Working_sec = sum(Duration_Sec)
from @EM
P_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
select @
Working_Sec_AfterShift = isnull(sum(Working_Sec_AfterShift) ,0)
from @EM
P_INOUT as EI where EI.Emp_ID = @Emp_ID
and EI.For_DAte = @temp_Month_DAte
and EI.In_Time >= @Shift_End_datetime
update @
Emp_Inout
set Late
_out = ''
, Earl
y_Out = ''
,
Total_Work = ''
,
less_work = ''
,
More_work = ''
,
Early_Out_sec = 0
,
Total_Less_work_sec = 0
,
Early_Out_count = 0
,
More_Work_Sec =0
,
Toatl_Working_sec = 0
where Em
p_ID = @emp_Id and For_Date = @temp_month_Date
set @Ear
ly_In_Sec = 0
set @lat
e_In_Sec = 0
end
exec Return_Without_Sec
@late_in_sec ,@late_in_sec output
exec Return_Without_Sec
@Early_In_Sec ,@Early_In_Sec output
if @Late_Comm_sec >= @l
ate_in_sec set @late_in_sec = 0
if @Late_Comm_sec >= @E
arly_In_Sec set @Early_In_Sec = 0
if @late_in_sec > 0 ex
ec Return_DurHourMinSec @late_In_Sec ,@late_In output ,'N'
if @Early_In_Sec > 0 ex
ec Return_DurHourMinSec @Early_In_Sec ,@Early_In output ,'N'
if @late_out_sec > 0 ex
ec Return_DurHourMinSec @late_Out_Sec ,@late_Out output ,'N'
if @Toatl_Working_sec >
0 exec Return_DurHourMinSec @Toatl_Working_sec ,@Total_work output ,'N'
if @Insert_IN_Date > @Sh
ift_End_datetime
begin
set @Wor
king_Sec_AfterShift = @Working_sec + @Working_Sec_AfterShift
set @Wor
king_AfterShift_Count = 1
end

set @Diff_Sec = @Toatl


_Working_sec - (@Shift_Sec -@Break_Sec)
if @Diff_Sec > 0 an
d @Diff_Sec >=@OT_Limit_Sec
begin
exec Ret
urn_DurHourMinSec @Diff_Sec , @More_Work output ,'N'
set @Mor
e_Work_Sec =@Diff_Sec
end
else if @Diff_Sec < 0 a
nd @Toatl_Working_sec > 0
begin
set @Dif
f_Sec = @Diff_Sec * -1
set @Tot
al_Less_Work_Sec = @Diff_Sec
exec Ret
urn_DurHourMinSec @Diff_Sec , @less_Work output ,'N'
end
if @late_in_Sec > 0
set @Late_in_cou
nt =1
if @Early_Out_Sec > 0
set @Early_Out_C
ount = 1
INSERT INTO @EMP_INOUT (
emp_id,For_Date , Dept_id ,Grade_id ,Shift_ID,In_Time ,Out_Time ,Duration ,
Duration
_sec , Late_In ,Late_Out , Early_In , Early_Out , Leave ,Shift_Sec,Shift_Dur,To
tal_Work,Less_Work,More_work,Reason,Late_in_sec,Early_Out_sec,Total_Less_work_se
c,Late_in_Count,Early_Out_count,Shift_St_Datetime,Shift_en_Datetime
,Working
_Sec_AfterShift,Working_AfterShift_Count,Inout_Reason,More_Work_Sec,Late_Out_Sec
,Early_In_Sec,Toatl_Working_sec)
VALUES (@emp_id ,@Temp_M
onth_Date ,@Dept_id ,@Grade_id ,@Shift_ID,@Insert_IN_DATE ,@Insert_Out_DATE ,@wo
rking_Hours ,
@working
_sec , @late_In ,@late_Out , @Early_In , @Early_Out , '',@Shift_Sec,@Shift_Dur,
@Total_Work,@Less_Work,@More_work,@Pre_Reason,@late_in_sec,@Early_Out_Sec,@Total
_less_work_sec,@Late_in_Count,@Early_Out_count,@Shift_St_Datetime,@Shift_End_Dat
etime
,@Workin
g_Sec_AfterShift,@Working_AfterShift_Count,@Pre_Reason,@More_Work_Sec,@Late_Out_
Sec,@Early_In_Sec,@Toatl_Working_sec)

SET @Insert_IN_DATE = N
ULL
SET @Insert_Out_Date = n
ull
set @Late_In_Sec = 0
set @Early_In_Sec = 0
set @Early_Out_Sec = 0
set @Late_Out_Sec = 0
set @late_In = ''
set @late_Out = ''
set @Early_In = ''
set @Early_Out = ''
set @WORKING_HOURS = ''
set @Total_work = ''
set @less_work = ''
set @More_work = ''
set @Toatl_Working_sec =
0
set @Pre_Reason = ''
set @late_in_count = 0
set @Early_Out_Count = 0
set @Total_less_work_sec
= 0
set @More_Work_Sec=0
------ %%%%%%%%%%%%% ---
------
end
end

fetch next from cur_Emp into @emp_id


end
close cur_Emp
deallocate cur_Emp

if @strReport = 'IN-OUT'
begin
select E_IO.*,Emp_name,Emp_Code,card_ref_no,emp_master.c
ode,Grade_name,Shift_name,Shift_St_Time,Shift_End_Time,dept_name,
isnull(emp_master.code,'') + cast(emp_ma
ster.emp_code as varchar(50)) + isnull(emp_master.Alpha_Prifix,'') as Emp_Alpha_
Numeric_Code
from @EMP_INOUT as E_IO inner join Emp_master on
Emp_master.emp_ID = E_IO.Emp_Id inner join Shift_master
on
Shift_master.Shift_ID = E_IO.Shift_ID inner join Grade_m
aster on
Grade_Master.grade_id = E_IO.Grade_id left join Departm
ent_master on
Department_master.Dept_id = E_IO.dept_id
where cast(cast(For_Date as varchar(11)) as smalldatetim
e) >= cast(cast(@Month_St_Date as varchar(11)) as smalldatetime)
and cast(cast(For_Date as varchar(11)) as smalldatetime)
<= cast(cast(@Month_En_Date as varchar(11)) as smalldatetime)
-- and ( In_Time is not null or ab_leave is not null )
end
else if @strReport = 'SUMMARY'
begin
--select * from @emp_inout WHERE EMP_ID IN(150,191)
--select * from temp_working_sec where emp_id = 191
--select * from Temp_working_sec WHERE EMP_ID IN(150,191)
--return
select * from
( select E_IO.Emp_ID,Emp_name,Emp_Code,emp_master.code,
Grade_name,Shift_name,dept_name,
sum(Late_in_sec) as Late_in_sec ,sum(Early_Out_s
ec) as Early_Out_sec, sum(Total_Less_Work_sec) as Total_Less_Work_sec,
sum(Late_In_Count) as Late_In_Count,sum(Early_Ou
t_Count) as Early_Out_Count,
sum(Working_sec_afterShift) as Working_sec_after
Shift,sum(Working_afterShift_count) as Working_afterShift_count,
sum(Late_Out_Sec) as Late_Out_Sec,sum(Early_In_S
ec) as Early_In_Sec,
sum(More_Work_Sec) as Total_More_Work_Sec,sum(To
atl_Working_sec) as Toatl_Working_sec,
isnull(emp_master.code,'') + cast(emp_master.emp
_code as varchar(50)) + isnull(emp_master.Alpha_Prifix,'') as Emp_Alpha_Numeric_
Code,
dbo.F_Return_Hours(sum(late_in_sec)) as Late_in_
Hours,
dbo.F_Return_Hours(sum(Early_Out_sec)) as Early_
Out_Hours,
dbo.F_Return_Hours(sum(Total_Less_Work_sec)) as
Total_Less_Work_Hours,
dbo.F_Return_Hours(sum(Working_Sec_AfterShift))
as Working_AfterShift_Hours,
dbo.F_Return_Hours(sum(Late_Out_Sec)) as Late_Ou
t_Hours,
dbo.F_Return_Hours(sum(Early_In_Sec)) as Early_I
n_Hours,
dbo.F_Return_Hours(sum(More_Work_Sec)) as More_W
ork_Hours,
dbo.F_Return_Hours(sum(Toatl_Working_sec)) as To
atl_Working_Hours,
--dbo.F_Return_Hours(sum(twc.ot_SEC)) as Actual_
OT,
--dbo.F_Return_Hours(sum(TWC.Holiday_Work_Sec))
as holiday_Work_Hours,
--dbo.F_Return_Hours(sum(TWC.weekoff_Work_sec))
as weekoff_Work_Hours,
sum(P_day+H_day) as PDays
from @EMP_INOUT as E_IO inner join Emp_master on
Emp_master.emp_ID = E_IO.Emp_Id inner join Shif
t_master on
Shift_master.Shift_ID = E_IO.Shift_ID inner join
Grade_master on
Grade_Master.grade_id = E_IO.Grade_id left OUTE
R join Department_master on
Department_master.Dept_id = E_IO.dept_id left OU
TER join Temp_working_sec TWC on
E_IO.Emp_id =TWC.Emp_id and TWC.For_Date >= E_IO
.For_date
and TWC.For_Date <= E_IO.For_date
where cast(cast(E_IO.For_Date as varchar(11)) as
smalldatetime) >= cast(cast(@Month_St_Date as varchar(11)) as smalldatetime)
and cast(cast(E_IO.For_Date as varchar(11)) as s
malldatetime) <= cast(cast(@Month_En_Date as varchar(11)) as smalldatetime)
group by E_IO.Emp_ID,Emp_name,Emp_Code,emp_maste
r.code,Grade_name,Shift_name,dept_name,emp_master.Alpha_Prifix
)Qry
where Qry.Late_In_Count > 0 or Qry.Early_Out_Count > 0 o
r Total_less_Work_sec > 0 or Total_More_Work_Sec > 0 --or Qry.Working_afterShif
t_count > 0
end
ELSE IF @strReport = 'OFF SHIFT'
begin
select E_IO.Emp_Id,Emp_name,Emp_Code,card_ref_no,emp_mas
ter.code,Grade_name,Shift_name,dept_name,
isnull(emp_master.code,'') + cast(emp_master.emp
_code as varchar(50)) + isnull(emp_master.Alpha_Prifix,'') as Emp_Alpha_Numeric_
Code
from @EMP_INOUT as E_IO inner join Emp_master on
Emp_master.emp_ID = E_IO.Emp_Id inner join Shift_master
on
Shift_master.Shift_ID = E_IO.Shift_ID inner join Grade_m
aster on
Grade_Master.grade_id = E_IO.Grade_id left join Departm
ent_master on
Department_master.Dept_id = E_IO.dept_id
where cast(cast(For_Date as varchar(11)) as smalldatetim
e) >= cast(cast(@Month_St_Date as varchar(11)) as smalldatetime)
and cast(cast(For_Date as varchar(11)) as smalldatetime)
<= cast(cast(@Month_En_Date as varchar(11)) as smalldatetime)
and Working_afterShift_count > 0
end
if @strReport = 'ATTENDANCE SUMMARY'
begin
select E_IO.Emp_ID,For_Date,Emp_name,Emp_Code,emp_master
.code,Grade_name,dept_name,AB_LEAVE,
sum(Late_in_sec) as Late_in_sec ,sum(Early_Out_s
ec) as Early_Out_sec, sum(Total_Less_Work_sec) as Total_Less_Work_sec,
sum(Late_In_Count) as Late_In_Count,sum(Early_Ou
t_Count) as Early_Out_Count,
sum(Working_sec_afterShift) as Working_sec_after
Shift,sum(Working_afterShift_count) as Working_afterShift_count,
sum(Late_Out_Sec) as Late_Out_Sec,sum(Early_In_S
ec) as Early_In_Sec,
sum(More_Work_Sec) as Total_More_Work_Sec,sum(To
atl_Working_sec) as Total_Working_sec,
isnull(emp_master.code,'') + cast(emp_master.emp
_code as varchar(50))+ isnull(emp_master.Alpha_Prifix,'') as Emp_Alpha_Numeric_C
ode,
dbo.F_Return_Hours(sum(late_in_sec)) as Late_in_
Hours,
dbo.F_Return_Hours(sum(Early_Out_sec)) as Early_
Out_Hours,
dbo.F_Return_Hours(sum(Total_Less_Work_sec)) as
Total_Less_Work_Hours,
dbo.F_Return_Hours(sum(Working_Sec_AfterShift))
as Working_AfterShift_Hours,
dbo.F_Return_Hours(sum(Late_Out_Sec)) as Late_Ou
t_Hours,
dbo.F_Return_Hours(sum(Early_In_Sec)) as Early_I
n_Hours,
dbo.F_Return_Hours(sum(More_Work_Sec)) as More_W
ork_Hours,
dbo.F_Return_Hours(sum(Toatl_Working_sec)) as To
tal_Working_Hours
from @EMP_INOUT as E_IO inner join Emp_master on
Emp_master.emp_ID = E_IO.Emp_Id inner join Shif
t_master on
Shift_master.Shift_ID = E_IO.Shift_ID inner join
Grade_master on
Grade_Master.grade_id = E_IO.Grade_id left join
Department_master on
Department_master.Dept_id = E_IO.dept_id
where cast(cast(For_Date as varchar(11)) as smal
ldatetime) >= cast(cast(@Month_St_Date as varchar(11)) as smalldatetime)
and cast(cast(For_Date as varchar(11)) as smalld
atetime) <= cast(cast(@Month_En_Date as varchar(11)) as smalldatetime)
group by E_IO.Emp_ID,Emp_name,Emp_Code,emp_maste
r.code,Grade_name,dept_name,For_Date,AB_LEAVE,emp_master.Alpha_Prifix
order by E_IO.emp_id,for_date
end

RETURN

You might also like