Professional Documents
Culture Documents
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
Set Nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET ARITHABORT ON
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
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 @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
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
if @Weekoff_Entry = 'N'
begin
delete from @Emp_Inout where Emp_ID = @emp_Id and For_Date = @Temp_Month_Date
end
end
end
------ %%%%%%%%%
%%%% ---------
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
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)
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
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
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
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
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
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
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)
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
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
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)
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
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
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
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)
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)
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
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
set @Shift_Sec = 0
exec Return_DurInSecond @Shift_D
ur,@Shift_Sec output
set @Fix_OT = 86400 - @Shift_Sec
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 @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
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