Sunday, 24 November 2019

zkemkeeper

Interop.zkemkeeper.dll regsvr32 error or InitializeComponent
Then you need done the step following:
1. Go to webiste: http://www.zktechnology.com
2. Download ->Software Download->ZK Access for C3 panels
3. Extract files
4. Install
5. Open visual studio -> Solution Project->references-> Add Interop.zkemkeeper.dll
Good luck

Friday, 22 November 2019

HRM

USE [hr]
GO

/****** Object:  Table [dbo].[HRM_DailyAttendance]    Script Date: 22/Nov/2019 05:14:40 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HRM_DailyAttendance](
[AttId] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[EmpId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__EmpId__70D3A237]  DEFAULT ((0)),
[InTime] [datetime] NULL,
[OutTime] [datetime] NULL,
[ReasonForLeave] [varchar](500) NULL CONSTRAINT [df_HRM_DailyAttendance_ReasonForLeave]  DEFAULT (''),
[AttDate] [datetime] NULL,
[ShiftId] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__Shift__71C7C670]  DEFAULT ((0)),
[LeaveId] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__Leave__72BBEAA9]  DEFAULT ((0)),
[CatId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__CatId__73B00EE2]  DEFAULT ((0)),
[DeptId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__DeptI__74A4331B]  DEFAULT ((0)),
[Attendance] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_Daily__Atten__75985754]  DEFAULT ('P'),
[SerialNo] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily__Seria__768C7B8D]  DEFAULT ((0)),
[OfficialOut] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_Daily__Offic__77809FC6]  DEFAULT ('N'),
[Flag] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_DailyA__Flag__7874C3FF]  DEFAULT ('N'),
[HalfDayLeave] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_HalfDayLeave]  DEFAULT ('N'),
[OTRequiredForHoliday] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_OTRequiredForHoliday]  DEFAULT ('N'),
[MisPunch] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_MisPunch]  DEFAULT ('N'),
[Permisable] [numeric](18, 0) NOT NULL DEFAULT ((0)),
[Notes] [varchar](1000) NOT NULL DEFAULT (''),
[LeaveAppId] [numeric](9, 0) NOT NULL CONSTRAINT [df_HRM_DailyAttendance_LeaveAppId]  DEFAULT ((0)),
[ConsiderForSalary] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_ConsiderForSalary]  DEFAULT ('Y'),
[LeaveId2] [numeric](18, 0) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_LeaveId2]  DEFAULT ((0)),
[EntryEmpId] [numeric](18, 0) NOT NULL CONSTRAINT [DF_DailyAtt_EntryEmpId]  DEFAULT ((0)),
[EntryComputer] [varchar](100) NOT NULL CONSTRAINT [DF_DailyAtt_EntryComputer]  DEFAULT (''),
[ImportMachine] [varchar](500) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_ImportMachine]  DEFAULT (''),
[BillNo] [varchar](100) NOT NULL CONSTRAINT [df_Hrm_DailyAttendance_BillNo]  DEFAULT (''),
[CustId] [numeric](18, 0) NOT NULL CONSTRAINT [Df_Hrm_DailyAttendance_CustId]  DEFAULT ((0)),
[LocationId] [numeric](18, 0) NOT NULL CONSTRAINT [df_HRM_DailyAttendance_LocationId]  DEFAULT ((0)),
[othrs] [varchar](9) NULL,
[remarks] [varchar](1000) NULL,
[prodn] [numeric](12, 3) NULL,
[outtime1] [datetime] NULL,
[entrydate] [datetime] NULL,
[ent_down_date] [datetime] NULL,
[otentrydate] [datetime] NULL,
[otentredby] [numeric](10, 0) NULL,
[otaprovedby] [numeric](10, 0) NULL,
[otaproveddate] [datetime] NULL,
[otprocess] [char](1) NULL,
[Latedet] [char](3) NULL,
[LatePunch] [numeric](10, 3) NULL,
[earlypunch] [numeric](10, 3) NULL,
[F_S_half] [varchar](3) NULL,
[forceabsent] [char](1) NULL,
[ShiftStartTime] [datetime] NULL,
[ShiftEndTime] [datetime] NULL,
[LunchStartTime] [datetime] NULL,
[LunchEndTime] [datetime] NULL,
[CalStartTime] [datetime] NULL,
[CalEndTime] [datetime] NULL,
[FHFlag] [char](3) NULL,
[SHFlag] [char](3) NULL,
[FullFlag] [char](3) NULL,
[ChkShiftStartTime] [int] NULL,
[GraceTime] [int] NULL,
[HalfDayOnduty] [char](1) NULL,
[Onduty] [char](1) NULL,
[otentrycomputer] [varchar](40) NULL,
[newsystem] [char](1) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[HRM_DailyAttendance]  WITH CHECK ADD  CONSTRAINT [df_halfdleave] CHECK  (([halfdayleave]='Y' OR [halfdayleave]='N'))
GO

ALTER TABLE [dbo].[HRM_DailyAttendance] CHECK CONSTRAINT [df_halfdleave]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Attendance Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'AttId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Employee Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'EmpId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Reason For Leave' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'ReasonForLeave'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Attendance Date' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'AttDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Shift Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'ShiftId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Leave Type Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'LeaveId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Category Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'CatId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Department Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'DeptId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Attendance Present/Absent' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'Attendance'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SerialNo for multiple regards' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'SerialNo'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Official Out Yes/No' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'OfficialOut'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Flag (Attendance Verification Yes/No)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'Flag'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'OT Required For Holiday (Yes/No)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_DailyAttendance', @level2type=N'COLUMN',@level2name=N'OTRequiredForHoliday'
GO

USE [hr]
GO

/****** Object:  Table [dbo].[HRM_DailyAttendance_Amend]    Script Date: 22/Nov/2019 05:15:12 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HRM_DailyAttendance_Amend](
[AttId] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[EmpId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__EmpId__70D3A237]  DEFAULT ((0)),
[InTime] [datetime] NULL,
[OutTime] [datetime] NULL,
[ReasonForLeave] [varchar](700) NULL CONSTRAINT [df_HRM_DailyAttendance_Amend_ReasonForLeave]  DEFAULT (''),
[AttDate] [datetime] NULL,
[ShiftId] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__Shift__71C7C670]  DEFAULT ((0)),
[LeaveId] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__Leave__72BBEAA9]  DEFAULT ((0)),
[CatId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__CatId__73B00EE2]  DEFAULT ((0)),
[DeptId] [numeric](10, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__DeptI__74A4331B]  DEFAULT ((0)),
[Attendance] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__Atten__75985754]  DEFAULT ('P'),
[SerialNo] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__Seria__768C7B8D]  DEFAULT ((0)),
[OfficialOut] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_Daily_Amend__Offic__77809FC6]  DEFAULT ('N'),
[Flag] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_DailyA_Amend__Flag__7874C3FF]  DEFAULT ('N'),
[HalfDayLeave] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_HalfDayLeave]  DEFAULT ('N'),
[OTRequiredForHoliday] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_OTRequiredForHoliday]  DEFAULT ('N'),
[MisPunch] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_MisPunch]  DEFAULT ('N'),
[Permisable] [numeric](18, 0) NOT NULL DEFAULT ((0)),
[Notes] [varchar](1000) NOT NULL DEFAULT (''),
[LeaveAppId] [numeric](9, 0) NOT NULL CONSTRAINT [df_HRM_DailyAttendance_Amend_LeaveAppId]  DEFAULT ((0)),
[ConsiderForSalary] [varchar](1) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_ConsiderForSalary]  DEFAULT ('Y'),
[LeaveId2] [numeric](18, 0) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_LeaveId2]  DEFAULT ((0)),
[EntryEmpId] [numeric](18, 0) NOT NULL CONSTRAINT [DF_DailyAtt_Amend_EntryEmpId]  DEFAULT ((0)),
[EntryComputer] [varchar](100) NOT NULL CONSTRAINT [DF_DailyAtt_Amend_EntryComputer]  DEFAULT (''),
[ImportMachine] [varchar](500) NOT NULL CONSTRAINT [DF_HRM_DailyAttendance_Amend_ImportMachine]  DEFAULT (''),
[BillNo] [varchar](100) NOT NULL CONSTRAINT [df_Hrm_DailyAttendance_Amend_BillNo]  DEFAULT (''),
[CustId] [numeric](18, 0) NOT NULL CONSTRAINT [Df_Hrm_DailyAttendance_Amend_CustId]  DEFAULT ((0)),
[LocationId] [numeric](18, 0) NOT NULL CONSTRAINT [df_HRM_DailyAttendance_Amend_LocationId]  DEFAULT ((0)),
[othrs] [varchar](9) NULL,
[remarks] [varchar](1000) NULL,
[prodn] [numeric](12, 3) NULL,
[outtime1] [datetime] NULL,
[entrydate] [datetime] NULL,
[ent_down_date] [datetime] NULL,
[otentrydate] [datetime] NULL,
[otentredby] [numeric](10, 0) NULL,
[otaprovedby] [numeric](10, 0) NULL,
[otaproveddate] [datetime] NULL,
[otprocess] [char](1) NULL,
[Latedet] [char](3) NULL,
[LatePunch] [numeric](10, 3) NULL,
[earlypunch] [numeric](10, 3) NULL,
[F_S_half] [varchar](3) NULL,
[forceabsent] [char](1) NULL,
[ShiftStartTime] [datetime] NULL,
[ShiftEndTime] [datetime] NULL,
[LunchStartTime] [datetime] NULL,
[LunchEndTime] [datetime] NULL,
[CalStartTime] [datetime] NULL,
[CalEndTime] [datetime] NULL,
[FHFlag] [char](3) NULL,
[SHFlag] [char](3) NULL,
[FullFlag] [char](3) NULL,
[ChkShiftStartTime] [int] NULL,
[GraceTime] [int] NULL,
[HalfDayOnduty] [char](1) NULL,
[Onduty] [char](1) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
USE [hr]
GO

/****** Object:  Table [dbo].[emp_personal]    Script Date: 22/Nov/2019 05:15:41 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[emp_personal](
[empid] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[cardno] [varchar](15) NOT NULL,
[empcode] [varchar](15) NOT NULL,
[empname] [varchar](50) NOT NULL,
[father_name] [varchar](50) NULL,
[address] [varchar](600) NULL,
[cityid] [numeric](6, 0) NULL,
[pincode] [varchar](7) NULL,
[phone] [varchar](20) NULL,
[email] [varchar](50) NULL,
[blood_group] [varchar](10) NULL,
[d_o_b] [datetime] NULL,
[d_o_j] [datetime] NULL,
[gender] [varchar](20) NULL,
[m_status] [int] NULL,
[e_cont_name] [varchar](50) NULL,
[e_cont_phone] [varchar](20) NULL,
[e_cont_address] [varchar](600) NULL,
[photo_path] [varchar](90) NULL,
[photo] [varbinary](max) NULL,
[stateid] [numeric](6, 0) NULL,
[loginid] [numeric](6, 0) NULL,
[entrydate] [datetime] NULL,
[entrycomputer] [varchar](30) NULL,
[leave_approval] [char](1) NULL,
[locationid] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
SET ANSI_PADDING OFF
ALTER TABLE [dbo].[emp_personal] ADD [resign] [char](1) NULL DEFAULT ('N')
ALTER TABLE [dbo].[emp_personal] ADD [resingdate] [datetime] NULL
ALTER TABLE [dbo].[emp_personal] ADD [report_to] [numeric](10, 0) NULL DEFAULT ((0))
SET ANSI_PADDING ON
ALTER TABLE [dbo].[emp_personal] ADD [esiapplicable] [char](1) NULL DEFAULT ('N')
ALTER TABLE [dbo].[emp_personal] ADD [contractorid] [numeric](10, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [loginname] [varchar](40) NULL
ALTER TABLE [dbo].[emp_personal] ADD [password] [varchar](50) NULL
ALTER TABLE [dbo].[emp_personal] ADD [resign_type] [varchar](20) NULL
ALTER TABLE [dbo].[emp_personal] ADD [idproof] [varbinary](max) NULL
ALTER TABLE [dbo].[emp_personal] ADD [addrproof] [varbinary](max) NULL
ALTER TABLE [dbo].[emp_personal] ADD [ageproof] [varbinary](max) NULL
ALTER TABLE [dbo].[emp_personal] ADD [approvaldept] [varchar](30) NULL
ALTER TABLE [dbo].[emp_personal] ADD [accid] [numeric](12, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [adv_approval] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [MulticompEmp] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [cod_approval] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [peraddress] [varchar](500) NULL
ALTER TABLE [dbo].[emp_personal] ADD [expdtreleave] [datetime] NULL
ALTER TABLE [dbo].[emp_personal] ADD [actreldate] [datetime] NULL
ALTER TABLE [dbo].[emp_personal] ADD [ManuvalPunch] [char](1) NULL DEFAULT ('N')
ALTER TABLE [dbo].[emp_personal] ADD [d_o_b_actual] [datetime] NULL
ALTER TABLE [dbo].[emp_personal] ADD [ot_app] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [esiapp] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [pfapp] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [qualification] [varchar](100) NULL
ALTER TABLE [dbo].[emp_personal] ADD [preexp] [numeric](12, 4) NULL
ALTER TABLE [dbo].[emp_personal] ADD [appraiser] [numeric](12, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [rev_officer] [numeric](12, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [promo_year] [numeric](5, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [w_off] [varchar](20) NULL
ALTER TABLE [dbo].[emp_personal] ADD [mop_coordinator] [numeric](12, 0) NULL
ALTER TABLE [dbo].[emp_personal] ADD [mop_coord] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [power_user] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [webuser] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [l_break_date] [datetime] NULL
ALTER TABLE [dbo].[emp_personal] ADD [mop_req] [char](1) NULL
ALTER TABLE [dbo].[emp_personal] ADD [resign_enteredby] [numeric](12, 0) NULL
 CONSTRAINT [PK_emp_personal] PRIMARY KEY CLUSTERED
(
[cardno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [hr]
GO

/****** Object:  Table [dbo].[emp_employementdet]    Script Date: 22/Nov/2019 05:15:56 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[emp_employementdet](
[empid] [numeric](10, 0) NOT NULL,
[pfno] [varchar](30) NULL,
[esino] [varchar](30) NULL,
[bank_accno] [varchar](40) NULL,
[bank_name] [varchar](80) NULL,
[panno] [varchar](20) NULL,
[aadharno] [varchar](20) NULL,
[voter_id] [varchar](20) NULL,
[drive_licence] [varchar](20) NULL,
[licence_validity] [datetime] NULL,
[loginname] [varchar](30) NULL,
[login_password] [varchar](30) NULL,
[deptid] [numeric](7, 0) NULL,
[catid] [numeric](7, 0) NULL,
[shiftrotationpattern] [varchar](20) NULL,
[entrydate] [datetime] NULL,
[complementno] [varchar](30) NULL,
[uanno] [varchar](40) NULL,
[deptchangeid] [numeric](12, 0) NULL,
[deptchangeon] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[empid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[scalemaster]    Script Date: 22/Nov/2019 05:16:24 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[scalemaster](
[id] [numeric](6, 0) IDENTITY(1,1) NOT NULL,
[empid] [numeric](6, 0) NOT NULL,
[deptid] [numeric](6, 0) NOT NULL,
[catid] [numeric](6, 0) NOT NULL,
[desigid] [numeric](6, 0) NOT NULL,
[adid] [numeric](6, 0) NOT NULL,
[amount] [numeric](12, 2) NOT NULL,
[inc_date] [datetime] NOT NULL,
[employer_amt] [numeric](12, 2) NULL DEFAULT ((0)),
[entrydate] [datetime] NULL,
[entrycomputer] [varchar](40) NULL,
[loginid] [numeric](10, 0) NULL,
[active_scale] [char](1) NULL,
[rval] [int] NULL,
 CONSTRAINT [PK_scale] PRIMARY KEY CLUSTERED
(
[empid] ASC,
[inc_date] ASC,
[adid] ASC,
[catid] ASC,
[amount] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[scalemaster]  WITH CHECK ADD FOREIGN KEY([catid])
REFERENCES [dbo].[hrm_category] ([catid])
GO

ALTER TABLE [dbo].[scalemaster]  WITH CHECK ADD FOREIGN KEY([deptid])
REFERENCES [dbo].[department] ([deptid])
GO

ALTER TABLE [dbo].[scalemaster]  WITH CHECK ADD FOREIGN KEY([adid])
REFERENCES [dbo].[hrm_allowancededuction] ([adid])
GO


USE [hr]
GO

/****** Object:  Table [dbo].[Designation]    Script Date: 22/Nov/2019 05:16:44 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Designation](
[desigid] [numeric](6, 0) IDENTITY(1,1) NOT NULL,
[designation] [varchar](40) NOT NULL,
[catid] [numeric](6, 0) NOT NULL,
[entryempid] [numeric](6, 0) NULL,
[entrycomputer] [varchar](30) NULL,
[entrydatetime] [datetime] NULL,
[ltaapplicable] [char](1) NULL,
[latepunchallow] [char](1) NULL DEFAULT ('N'),
[DesigGroup] [numeric](1, 0) NULL DEFAULT ((1)),
[Desig_grp] [int] NULL,
[slab_desig] [int] NULL,
[staff_worker] [char](1) NULL,
[edu_allow] [numeric](12, 2) NULL,
[sodexo] [numeric](12, 0) NULL,
[desig_slno] [int] NULL,
[Grade] [int] NULL,
PRIMARY KEY CLUSTERED
(
[desigid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Designation]  WITH CHECK ADD FOREIGN KEY([catid])
REFERENCES [dbo].[hrm_category] ([catid])
GO

ALTER TABLE [dbo].[Designation]  WITH CHECK ADD CHECK  (([designation]<>''))
GO

USE [hr]
GO

/****** Object:  Table [dbo].[Hrm_ShiftRotation]    Script Date: 22/Nov/2019 05:17:10 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Hrm_ShiftRotation](
[Id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[EmpId] [numeric](18, 0) NOT NULL,
[ShiftId] [numeric](18, 0) NOT NULL,
[ChangeDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[ShiftRotationPeriod] [numeric](18, 0) NOT NULL,
[CatId] [numeric](18, 0) NOT NULL,
[DeptId] [numeric](18, 0) NOT NULL,
[StartDate] [datetime] NULL,
[loginid] [numeric](12, 0) NULL,
[EntryComputer] [varchar](30) NULL,
[entrydate] [datetime] NULL,
[chflag] [char](1) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[HRM_Shift]    Script Date: 22/Nov/2019 05:17:43 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HRM_Shift](
[ShiftId] [int] IDENTITY(1,1) NOT NULL,
[Shift] [varchar](50) NULL,
[Description] [varchar](50) NULL,
[Stime] [datetime] NULL,
[ETime] [datetime] NULL,
[LunchStart] [datetime] NULL,
[LunchEnd] [datetime] NULL,
[NextDay] [varchar](1) NOT NULL CONSTRAINT [DF__HRM_Shift__NextD__3E131840]  DEFAULT ('N'),
[NightShift] [varchar](10) NOT NULL CONSTRAINT [DF_hrm_HRM_shift]  DEFAULT ('N'),
[EntryEmpId] [numeric](18, 0) NOT NULL CONSTRAINT [DF_Shift_EntryEmpId]  DEFAULT ((0)),
[EntryComputer] [varchar](100) NOT NULL CONSTRAINT [DF_Shift_EntryComputer]  DEFAULT (''),
[active] [varchar](2) NULL,
[locationid] [int] NULL,
[entrydate] [datetime] NULL,
[companyid] [numeric](2, 0) NULL,
[previousday] [char](1) NULL,
[shiftname] [varchar](20) NULL,
 CONSTRAINT [PK_HRM_Shift_ShiftID] PRIMARY KEY CLUSTERED
(
[ShiftId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[hrm_leavemaster]    Script Date: 22/Nov/2019 05:18:27 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[hrm_leavemaster](
[id] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[appno] [varchar](30) NOT NULL,
[appdate] [datetime] NULL,
[empid] [numeric](10, 0) NOT NULL,
[approvalby] [numeric](10, 0) NOT NULL,
[elbal] [numeric](10, 2) NULL,
[leave_from] [datetime] NULL,
[leave_to] [datetime] NULL,
[leavedays] [numeric](12, 2) NULL,
[reason] [varchar](500) NULL,
[entrydatetime] [datetime] NULL,
[logid] [numeric](10, 0) NULL,
[entrycomputer] [varchar](30) NULL,
[approveddate] [datetime] NULL,
[leavetype] [numeric](10, 0) NULL,
[adv_amount] [numeric](12, 2) NULL,
[returnon] [datetime] NULL,
[approved] [char](1) NULL,
[adv_approved_amt] [numeric](12, 2) NULL,
[adv_approved_on] [datetime] NULL,
[adv_approvedby] [numeric](10, 0) NULL,
[adv_approvalby] [numeric](10, 0) NULL,
[adv_amtpaid] [numeric](12, 2) NULL,
PRIMARY KEY CLUSTERED
(
[appno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[hrm_leavedet]    Script Date: 22/Nov/2019 05:18:47 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[hrm_leavedet](
[id] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[tranid] [numeric](10, 0) NOT NULL,
[appno] [varchar](30) NULL,
[leavedate] [datetime] NULL,
[halfday] [char](2) NULL,
[approved] [char](1) NULL,
[approveddate] [datetime] NULL,
[approval_computer] [varchar](50) NULL,
[approval_login] [numeric](12, 0) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[hrm_leavedet]  WITH CHECK ADD  CONSTRAINT [CK__hrm_leave__halfd__6AEFE058] CHECK  (([halfday]='FH' OR [halfday]='SH' OR [halfday]='F'))
GO

ALTER TABLE [dbo].[hrm_leavedet] CHECK CONSTRAINT [CK__hrm_leave__halfd__6AEFE058]
GO

USE [hr]
GO

/****** Object:  Table [dbo].[HRM_LeaveType]    Script Date: 22/Nov/2019 05:19:04 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HRM_LeaveType](
[LeaveId] [numeric](18, 0) IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[LeaveType] [varchar](50) NOT NULL CONSTRAINT [DF_HRM_LeaveType_LeaveType]  DEFAULT (' '),
[Description] [varchar](50) NOT NULL CONSTRAINT [DF_HRM_LeaveType_Description]  DEFAULT (' '),
[Paid] [varchar](50) NOT NULL CONSTRAINT [DF__HRM_LeaveT__Paid__7A5D0C71]  DEFAULT ('Y'),
[LeaveDays] [numeric](18, 0) NOT NULL CONSTRAINT [DF__HRM_Leave__Leave__7C4554E3]  DEFAULT ((0)),
[SlNo] [numeric](9, 0) NOT NULL CONSTRAINT [DF_HRM_LeaveType_SlNo]  DEFAULT ((0)),
[ExcessLeaveAllowed] [varchar](3) NOT NULL DEFAULT ('N'),
[MaxLimitToCarryforward] [numeric](5, 2) NOT NULL CONSTRAINT [Df_HRM_LeaveTypes_MaxLimitToCarryforward]  DEFAULT ((0)),
[MaxLimitReq] [char](1) NOT NULL CONSTRAINT [Df_HRM_LeaveTypes_MaxLimitReq]  DEFAULT ('N'),
[EntryEmpId] [numeric](18, 0) NOT NULL CONSTRAINT [DF_LeaveType_EntryEmpId]  DEFAULT ((0)),
[EntryComputer] [varchar](100) NOT NULL CONSTRAINT [DF_LeaveType_EntryComputer]  DEFAULT (''),
[LeaveEncashment] [varchar](1) NOT NULL CONSTRAINT [df_HRM_LeaveType_LeaveEncashment]  DEFAULT ('N'),
[entrydate] [datetime] NULL,
[disppayroll] [char](1) NULL,
[staff_worker] [char](1) NULL,
 CONSTRAINT [pk_hrm_leavetype_LeaveId] PRIMARY KEY CLUSTERED
(
[LeaveId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Leave Type Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_LeaveType', @level2type=N'COLUMN',@level2name=N'LeaveId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Leave Type' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_LeaveType', @level2type=N'COLUMN',@level2name=N'LeaveType'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Paid Leave (Yes/No)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_LeaveType', @level2type=N'COLUMN',@level2name=N'Paid'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'No of Leave days ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HRM_LeaveType', @level2type=N'COLUMN',@level2name=N'LeaveDays'
GO


USE [hr]
GO

/****** Object:  Table [dbo].[hrm_weekoff]    Script Date: 22/Nov/2019 05:19:30 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[hrm_weekoff](
[id] [numeric](12, 0) IDENTITY(1,1) NOT NULL,
[empid] [numeric](10, 0) NULL,
[catid] [numeric](10, 0) NULL,
[week_off] [varchar](15) NULL,
[eff_from] [datetime] NULL,
[eff_to] [datetime] NULL,
[entrydate] [datetime] NULL,
[entrycomputer] [varchar](40) NULL,
[loginid] [numeric](12, 0) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[hrm_weeklyoff]    Script Date: 22/Nov/2019 05:19:42 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[hrm_weeklyoff](
[LDate] [datetime] NULL,
[LDesc] [varchar](50) NOT NULL,
[Year] [numeric](18, 0) NOT NULL,
[Day] [varchar](10) NOT NULL,
[DateChange] [datetime] NULL,
[EntryEmpId] [numeric](18, 0) NOT NULL,
[EntryComputer] [varchar](100) NOT NULL,
[ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[LocationId] [numeric](18, 0) NOT NULL
) ON [PRIMARY]
SET ANSI_PADDING OFF
ALTER TABLE [dbo].[hrm_weeklyoff] ADD [FinancialYear] [varchar](25) NOT NULL
ALTER TABLE [dbo].[hrm_weeklyoff] ADD [leaveid] [numeric](10, 0) NULL
ALTER TABLE [dbo].[hrm_weeklyoff] ADD [empid] [numeric](10, 0) NULL
ALTER TABLE [dbo].[hrm_weeklyoff] ADD [catid] [numeric](10, 0) NULL

GO

SET ANSI_PADDING OFF
GO

USE [hr]
GO

/****** Object:  Table [dbo].[Leave]    Script Date: 22/Nov/2019 05:20:09 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Leave](
[LDate] [datetime] NULL,
[LDesc] [varchar](50) NOT NULL CONSTRAINT [DF_Leave_LDesc]  DEFAULT (' '),
[Year] [numeric](18, 0) NOT NULL CONSTRAINT [DF__Leave__Year__5FFE1BF0]  DEFAULT ((0)),
[Day] [varchar](10) NOT NULL CONSTRAINT [DF_Leave_Day]  DEFAULT (' '),
[DateChange] [datetime] NULL,
[EntryEmpId] [numeric](18, 0) NOT NULL CONSTRAINT [DF_Leave_EntryEmpId]  DEFAULT ((0)),
[EntryComputer] [varchar](100) NOT NULL CONSTRAINT [DF_Leave_EntryComputer]  DEFAULT (''),
[ID] [numeric](18, 0) IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[LocationId] [numeric](18, 0) NOT NULL CONSTRAINT [df_Leave_LocationId]  DEFAULT ('1')
) ON [PRIMARY]
SET ANSI_PADDING OFF
ALTER TABLE [dbo].[Leave] ADD [FinancialYear] [varchar](25) NOT NULL CONSTRAINT [df_Leave_FinancialYear]  DEFAULT ('')
ALTER TABLE [dbo].[Leave] ADD [leaveid] [numeric](10, 0) NULL
 CONSTRAINT [pk_leave_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [sandfitsetime]
GO

/****** Object:  Table [dbo].[DeviceLogs]    Script Date: 22/Nov/2019 05:21:15 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[DeviceLogs](
[DeviceLogId] [int] IDENTITY(1,1) NOT NULL,
[DownloadDate] [datetime] NULL,
[DeviceId] [int] NOT NULL,
[UserId] [nvarchar](50) NOT NULL,
[LogDate] [datetime] NOT NULL,
[Direction] [nvarchar](255) NULL,
[AttDirection] [nvarchar](255) NULL,
[C1] [nvarchar](255) NULL,
[C2] [nvarchar](255) NULL,
[C3] [nvarchar](255) NULL,
[C4] [nvarchar](255) NULL,
[C5] [nvarchar](255) NULL,
[C6] [nvarchar](255) NULL,
[C7] [nvarchar](255) NULL,
[WorkCode] [nvarchar](255) NULL,
[ChkData] [char](1) NULL,
[ChkData1] [char](1) NULL,
 CONSTRAINT [PK_DeviceLogs] PRIMARY KEY CLUSTERED
(
[DeviceLogId] ASC,
[UserId] ASC,
[LogDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [sandfitsetime]
GO

/****** Object:  Table [dbo].[Devices]    Script Date: 22/Nov/2019 05:21:31 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Devices](
[DeviceId] [int] IDENTITY(1,1) NOT NULL,
[DeviceFName] [nvarchar](255) NOT NULL,
[DeviceSName] [nvarchar](255) NOT NULL,
[DeviceDirection] [nvarchar](255) NULL,
[SerialNumber] [nvarchar](255) NULL,
[ConnectionType] [nvarchar](255) NULL,
[IpAddress] [nvarchar](255) NULL,
[BaudRate] [nvarchar](255) NULL,
[CommKey] [nvarchar](255) NOT NULL CONSTRAINT [DF_Devices_CommKey]  DEFAULT (0),
[ComPort] [nvarchar](255) NULL,
[LastLogDownloadDate] [datetime] NULL CONSTRAINT [DF_Devices_LastLogDownloadDate]  DEFAULT ('1900-01-01 00:00'),
[C1] [nvarchar](255) NULL,
[C2] [nvarchar](255) NULL,
[C3] [nvarchar](255) NULL,
[C4] [nvarchar](255) NULL,
[C5] [nvarchar](255) NULL,
[C6] [nvarchar](255) NULL,
[C7] [nvarchar](255) NULL,
[TransactionStamp] [nvarchar](50) NULL DEFAULT ('0'),
[LastPing] [datetime] NULL,
[DeviceType] [nvarchar](255) NULL,
[OpStamp] [nvarchar](255) NULL,
[DownLoadType] [int] NULL,
[Timezone] [nvarchar](50) NULL,
[DeviceLocation] [nvarchar](50) NULL,
[TimeOut] [nvarchar](50) NULL,
 CONSTRAINT [PK_Devices] PRIMARY KEY CLUSTERED
(
[DeviceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


USE [sandfitsetime]
GO

/****** Object:  Table [dbo].[DevicesStatus]    Script Date: 22/Nov/2019 05:23:13 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[DevicesStatus](
[DeviceStatusId] [int] IDENTITY(1,1) NOT NULL,
[DeviceId] [int] NULL,
[DeviceStatus] [nvarchar](255) NULL,
[LastDeviceStatusOn] [datetime] NULL,
 CONSTRAINT [DevicesStatus_PK] PRIMARY KEY CLUSTERED
(
[DeviceStatusId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

USE [production]
GO

/****** Object:  Table [dbo].[HR_Controls]    Script Date: 22/Nov/2019 05:29:23 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HR_Controls](
[TranMasId] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[Locationid] [int] NULL,
[Yrmon] [int] NULL,
[CategoryId] [numeric](10, 0) NULL,
[WeeklyOff] [varchar](50) NULL,
[GraceTimeMin] [numeric](10, 0) NULL,
[GraceTimeCount] [numeric](10, 0) NULL,
[OTMaxHrs] [numeric](10, 0) NULL,
[OTMinHrs] [numeric](10, 0) NULL,
[RegPermissionMaxMinutes] [numeric](10, 0) NULL,
[RegPermissionMinMinutes] [numeric](10, 0) NULL,
[SplPermissionMaxMinutes] [numeric](10, 0) NULL,
[SplPermissionCount] [numeric](10, 0) NULL,
[ChkShiftStTime] [numeric](10, 0) NULL,
[EffFrom] [datetime] NULL,
[Active] [char](1) NULL,
[CreatedBy] [varchar](50) NULL,
[CreatedDate] [datetime] NULL,
[CreatedSystem] [varchar](100) NULL,
[DesigGroup] [numeric](1, 0) NULL,
[LatePermissionCount] [int] NULL,
[EarlyOutPunch] [numeric](10, 0) NULL DEFAULT ((0)),
[RegPermissionTotMinutes] [numeric](10, 0) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [production]
GO

/****** Object:  Table [dbo].[HR_ShiftChangeRequest]    Script Date: 22/Nov/2019 05:32:29 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HR_ShiftChangeRequest](
[TranId] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[LocationId] [int] NULL,
[EntryDate] [date] NULL,
[DeptId] [int] NULL,
[CatId] [int] NULL,
[EmpId] [int] NULL,
[ShiftId] [int] NULL,
[ShiftStartDate] [date] NULL,
[ShiftEndDate] [date] NULL,
[CreatedBy] [varchar](200) NULL,
[CreatedDate] [datetime] NULL,
[CreatedSystem] [varchar](200) NULL,
[Status] [char](1) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [production]
GO

/****** Object:  Table [dbo].[Hr_TwelveHrShiftCtrlHrs]    Script Date: 22/Nov/2019 05:32:52 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Hr_TwelveHrShiftCtrlHrs](
[TranId] [int] IDENTITY(1,1) NOT NULL,
[LocationId] [int] NULL,
[ShiftEndTimeMaxMinutes] [int] NULL
) ON [PRIMARY]

GO


USE [production]
GO

/****** Object:  Table [dbo].[HR_Permission]    Script Date: 22/Nov/2019 05:33:18 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HR_Permission](
[TranId] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[LocationId] [int] NULL,
[Yrmon] [int] NULL,
[EntryDate] [date] NULL,
[EmpId] [numeric](10, 0) NULL,
[PermissionHrsMin] [numeric](10, 0) NULL,
[FromTime] [datetime] NULL,
[ToTime] [datetime] NULL,
[Reason] [varchar](250) NULL,
[Approved] [char](1) NULL,
[CreatedBy] [varchar](100) NULL,
[CreatedDate] [datetime] NULL,
[CreatedSystem] [varchar](100) NULL,
[ApprovedBy] [varchar](100) NULL,
[ApprovedDate] [datetime] NULL,
[ApprovedSystem] [varchar](100) NULL,
[PermissionType] [char](2) NULL,
[PermissionLevel] [char](2) NULL,
[Download] [char](1) NULL DEFAULT ('N'),
[UnApprovalRemarks] [varchar](150) NULL,
 CONSTRAINT [UC_HR_Permission] UNIQUE NONCLUSTERED
(
[EmpId] ASC,
[FromTime] ASC,
[Approved] ASC,
[PermissionType] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Friday, 15 November 2019

Device log insert

--Exec sp_hrm_Devicelog_Insert

CREATE Procedure sp_hrm_Devicelog_Insert

As
Begin
--Declare @AttDate Date = '2017-12-30'
Drop table tempLog
--Drop table tempLog1
Drop table tempLog2

Declare @Str As Varchar(100)
Set @Str =  ('Select * into tempLog From '+ 'App91.dbo.DeviceLogs_'+ CONVERT(VARCHAR(10), Month(GETDATE() ))+'_'+CONVERT(VARCHAR(10), YEAR(GETDATE() ))  + ' order by logdate')
Exec (@Str)

--Set @Str =  ('Select * into tempLog1 From '+ 'App91.dbo.DeviceLogs_'+ CONVERT(VARCHAR(10), Month(DATEADD(DD,1,GETDATE()) ))+'_'+CONVERT(VARCHAR(10), YEAR(DATEADD(DD,1, GETDATE() )))  + ' order by logdate')
--Exec (@Str)

Set @Str =  ('Select * into tempLog2 From '+ 'App91.dbo.DeviceLogs_'+ CONVERT(VARCHAR(10), Month(DATEADD(DD,-1,GETDATE()) ))+'_'+CONVERT(VARCHAR(10), YEAR(DATEADD(DD,-1, GETDATE() )))  + ' order by logdate')
Exec (@Str)

insert into sandfitsetime.dbo.DeviceLogs ( DownloadDate,DeviceId,UserId,LogDate,C1 )

select distinct M.* from (
select GETDATE() DownloadDate,deviceid DeviceId,userid UserId,logdate Logdate,c1 c1 from tempLog a with(Nolock)
where LogDate Not In (select LogDate from sandfitsetime.dbo.DeviceLogs with(Nolock)  where UserId = a.UserId)
--Union All
--select GETDATE(),deviceid,userid,logdate,c1 from tempLog1 a
--where LogDate Not In (select LogDate from sandfitsetime.dbo.DeviceLogs where UserId = a.UserId)
Union All
select GETDATE(),deviceid,userid,logdate,c1 from tempLog2 a with(Nolock)
where LogDate Not In (select LogDate from sandfitsetime.dbo.DeviceLogs with(Nolock) where UserId = a.UserId)
  ) M
 order by m.LogDate
End





Daily Attendance Absents


Daily attendance Individual




--Exec Sp_HRMDailyAttendance_UI_Pending 1,'2019-10-26',824
CREATE PROCEDURE Sp_HRMDailyAttendance_UI_Pending
 @LocationId  numeric(2),   
 @AttDate Date,
 @EmpId Numeric(10,0)
    AS     
   
    BEGIN     
SET NOCOUNT ON;   
DECLARE     
 @CardNo Numeric(10,0),   
 @ShiftDate Date,   
 @ShiftStartTime DateTime,   
 @ShiftEndTime DateTime,   
 @ActualStartTime DateTime,   
 @MaxEmpId Numeric(10,0),   
 @MaxCardNo Numeric(10,0),   
 @MaxShiftStartTime DateTime,   
 @NextDay Char(1),   
 @PreviousDay Char(1),
 @FHFlag Varchar(20),
 @SHFlag Varchar(20),
 @FullFlag Varchar(20),
 @FHFlagOD Varchar(20),
 @SHFlagOD Varchar(20),
 @FullFlagOD Varchar(20),
 @Reason Varchar(800),
 @LeaveId Int,
 @ODAndPermission Varchar(20)

insert into hr.dbo.HRM_DailyAttendance_Amend select EmpId,InTime,OutTime,substring(ReasonForLeave,1,100),AttDate,ShiftId,LeaveId,CatId,
DeptId,Attendance,SerialNo,OfficialOut,Flag,HalfDayLeave,OTRequiredForHoliday,MisPunch,Permisable,Notes,LeaveAppId,
ConsiderForSalary,LeaveId2,EntryEmpId,EntryComputer,ImportMachine,BillNo,CustId,LocationId,othrs,remarks,prodn,outtime1,
entrydate,ent_down_date,otentrydate,otentredby,otaprovedby,otaproveddate,otprocess,Latedet,LatePunch,earlypunch,F_S_half,
forceabsent,ShiftStartTime,ShiftEndTime,LunchStartTime,LunchEndTime,CalStartTime,CalEndTime,FHFlag,SHFlag,FullFlag,ChkShiftStartTime,GraceTime,
HalfDayOnduty,Onduty
from hr.dbo.HRM_DailyAttendance where AttDate = @AttDate and empid = @EmpId

Delete hr.dbo.HRM_DailyAttendance where AttDate = @AttDate and empid = @EmpId

 Declare  @Count As Integer
    Select @Count = COUNT(*) From hr.dbo.HRM_DailyAttendance where AttDate = @AttDate and LocationId = @LocationId and empid = @empid;
       
    If @Count > 0
    begin
    RAISERROR('This date Already Downloaded. Please Check...', 16, 10) with nowait;
    RETURN;
    End 
    Else
   
Declare cur_HrmDailyAttendanceTest Cursor for   
   
select Distinct b.EmpId,b.CardNo,d.NextDay,Isnull(d.previousday,'N')previousday   
from  hr.dbo.emp_personal b with (nolock)
Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
where b.locationid = @LocationId and b.resign = 'N'
and @AttDate between c.StartDate and c.EndDate 
and b.empid = @EmpId
order by cardno 
   
Open cur_HrmDailyAttendanceTest;
   
fetch next from cur_HrmDailyAttendanceTest into @EmpId, @CardNo,@NextDay,@PreviousDay;   
   
While @@FETCH_STATUS = 0   
Begin   
   
BEGIN TRANSACTION INS;   
   
If @NextDay = 'N' and @PreviousDay = 'N'   
   
 Insert into hr.dbo.HRM_DailyAttendance (EmpId,InTime,AttDate,Shiftid,CatId,DeptId, 
 EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date,ShiftStartTime,ShiftEndTime,
 LunchStartTime,LunchEndTime,ChkShiftStartTime,GraceTime)   
 select Top 1 b.EmpId,LogDate as ActualStartTime, convert(varchar(10),LogDate,20) ShiftDate, 
 d.ShiftId,Isnull(ss.CatId,ed.catid)Catid,ISNULL(ss.deptid,ed.deptid)DeptId, 
 10,'Server','Y',@LocationId,GETDATE(), 
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ShiftStartTime,   
 case d.NextDay when 'Y' then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8)     
 else   
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8) end as shiftetime,   
 case when d.NextDay = 'Y' and CAST(d.LunchStart as time) < CAST('08:00' as time)  then 
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchStart,8)   
 Else   
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.LunchStart,8) End as LunchStartTime,   
 case when d.NextDay = 'Y' and CAST(d.LunchEnd as time) < CAST('08:00' as time)  then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchEnd,8)   
 Else   
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.LunchEnd,8) End As LunchEndTime,
 ISNULL(ChkShiftStTime,30),
 Isnull(hc.GraceTimeMin,5) 
 from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and Convert(date,LogDate) = @AttDate 
 and LogDate between   
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.Stime,8) )   
 and   
 convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.etime,8)   
 and UserId = @CardNo   
 order by LogDate Asc; 
   
 Else if @NextDay = 'N' and @PreviousDay = 'Y'   
 Insert into hr.dbo.HRM_DailyAttendance (EmpId,InTime,AttDate,Shiftid,CatId,DeptId, 
 EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date,ShiftStartTime,ShiftEndTime,
 LunchStartTime,LunchEndTime,ChkShiftStartTime,GraceTime)   
 select Top 1 b.EmpId, LogDate as ActualStartTime,convert(varchar(10),@AttDate,20) ShiftDate,
 d.ShiftId,Isnull(ss.CatId,ed.catid)Catid,ISNULL(ss.deptid,ed.deptid)DeptId,
 10,'Server','Y',@LocationId,GETDATE(),   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.Stime,8) ShiftStartTime,   
 case d.NextDay when 'Y' then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8)     
 else   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8) end as shiftetime,   
  case when d.NextDay = 'Y' and CAST(d.LunchStart as time) < CAST('08:00' as time)  then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchStart,8)   
 Else   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchStart,8) End as LunchStartTime,   
 case when d.NextDay = 'Y' and CAST(d.LunchEnd as time) < CAST('08:00' as time)  then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchEnd,8)   
 Else   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchEnd,8) End As LunchEndTime,
 ISNULL(ChkShiftStTime,30),
 Isnull(hc.GraceTimeMin,5)   
 from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno   and b.locationid = @LocationId
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 where @AttDate between c.StartDate and c.EndDate     
 and LogDate between   
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End,20)+' '+convert(varchar(8),d.Stime,8) )   
 and   
 convert(varchar(10),case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End,20)+' '+convert(varchar(8),d.etime,8)   
 and UserId = @CardNo   
 order by LogDate Asc;   
 Else   
if @NextDay = 'Y'   
 Insert into hr.dbo.HRM_DailyAttendance (EmpId,InTime,AttDate,Shiftid,CatId,DeptId, 
 EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date,ShiftStartTime,ShiftEndTime,
 LunchStartTime,LunchEndTime,ChkShiftStartTime,GraceTime)
 select Top 1 b.EmpId, LogDate as ActualStartTime,convert(varchar(10),LogDate,20) ShiftDate,d.ShiftId,
 Isnull(ss.CatId,ed.catid)Catid,ISNULL(ss.deptid,ed.deptid)DeptId,10,'Server','Y',@LocationId,GETDATE(),
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ShiftStartTime,   
 case d.NextDay when 'Y' then convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8)     
 else convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8) end as shiftetime,   
 case when d.NextDay = 'Y' and CAST(d.LunchStart as time) < CAST('08:00' as time)  then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchStart,8)   
 Else   
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.LunchStart,8) End as LunchStartTime,   
 case when d.NextDay = 'Y' and CAST(d.LunchEnd as time) < CAST('08:00' as time)  then   
 convert(varchar(10),dateadd(d,1,@AttDate),20)+' '+convert(varchar(8),d.LunchEnd,8)   
 Else   
 convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.LunchEnd,8) End As LunchEndTime,
 ISNULL(ChkShiftStTime,30),
 Isnull(hc.GraceTimeMin,5)     
 from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and Convert(date,LogDate) = @AttDate 
 and LogDate between   
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.Stime,8) )   
 and   
 convert(varchar(10),dateadd(d,1,LogDate),20)+' '+convert(varchar(8),d.etime,8)   
 and UserId = @CardNo   
 order by LogDate Asc;   
   
COMMIT TRANSACTION INS;   
   
BEGIN TRANSACTION UPD;   
   
Declare @ActualEndTime DateTime   
   
If @NextDay = 'N' and @PreviousDay = 'N'   

Select Top 1 @ActualEndTime = LogDate From
 (
select * from (
 select Top 1  LogDate from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno and b.locationid = @LocationId
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and UserId = @CardNo   
 and LogDate
  between
  DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ) 
  and
  case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
  DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  Else
  DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  End
  And (Logdate >= convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
 order by LogDate )T
 Union All
 select Top 1  LogDate from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno and b.locationid = @LocationId
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and UserId = @CardNo   
 and LogDate
  between
  DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ) 
  and
  --DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
  DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  Else
  DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  End
  And (Logdate <= convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
  order by LogDate Desc)M

 
If @NextDay = 'N' and @PreviousDay = 'Y'   

Select Top 1 @ActualEndTime = LogDate  From
 (
select * from (
 select  Top 1 LogDate   
 from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno  and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where @AttDate between c.StartDate and c.EndDate and   
 convert(date,LogDate) = (case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End)   
 and LogDate between   
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.Stime,8) )   
 and
 case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
 DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.etime,8))   
 Else
 DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.etime,8))   
 End
 And (Logdate >= convert(varchar(10),(case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End) ,20)+' '+convert(varchar(8),d.etime,8))
 and UserId = @CardNo   
 order by LogDate )T
 Union All
 select  Top 1 LogDate   
 from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno  and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where @AttDate between c.StartDate and c.EndDate and   
 convert(date,LogDate) = (case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End)   
 and LogDate between   
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.Stime,8) )   
 and   
 case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
 DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.etime,8))   
 Else
 DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),LogDate,20)+' '+convert(varchar(8),d.etime,8))   
 End
 And (Logdate <= convert(varchar(10),(case When d.previousday = 'Y' Then DateAdd(dd,1,@AttDate) Else @AttDate End) ,20)+' '+convert(varchar(8),d.etime,8))
 and UserId = @CardNo   
 order by LogDate Desc)M

   
Else If @NextDay = 'Y' and @PreviousDay = 'N' 

Select Top 1 @ActualEndTime = LogDate From
 (
select * from (
   select Top 1  LogDate from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno  and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and Convert(date,LogDate) between @AttDate and  DATEADD(dd,1,@AttDate) 
 and UserId = @CardNo   
 and LogDate
 between
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ) 
 and
 case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
 DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),dateadd(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
 Else
 DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),dateadd(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
 End
 --And (Logdate >= convert(varchar(10),DATEADD(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
 And (Logdate >= convert(varchar(10),DATEADD(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
order by LogDate )T
Union All
select Top 1  LogDate from sandfitsetime.dbo.DeviceLogs a with (nolock)   
 Inner join hr.dbo.emp_personal b with (nolock) on a.userId = b.cardno  and b.locationid = @LocationId 
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 Left join hr.dbo.Designation hh with (nolock) on  hh.desigid = ss.desigid
 Left Join hr.dbo.Hrm_ShiftRotation c with (nolock) on c.EmpId = b.empid   
 Left Join hr.dbo.HRM_Shift d with (nolock) on d.ShiftId = c.ShiftId   
 Left Join production.dbo.HR_Controls hc with (nolock) on hc.DesigGroup = hh.DesigGroup and ss.catid = hc.CategoryId and b.locationid = @LocationId and hc.active = 'Y'   
 Inner Join Hr_TwelveHrShiftCtrlHrs TH on th.LocationId = b.locationid
 where   
 Convert(date,LogDate) between c.StartDate and c.EndDate   
 and Convert(date,LogDate) between @AttDate and  DATEADD(dd,1,@AttDate) 
 and UserId = @CardNo   
 and LogDate
 between
 DateAdd (MINUTE,-ISNULL(ChkShiftStTime,30),convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.Stime,8) ) 
 and
 case when DATEDIFF(MI,d.Stime,d.ETime)/60 < 10 Then
  DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),dateadd(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
  Else
  DATEADD(MINUTE,th.ShiftEndTimeMaxMinutes,convert(varchar(10),dateadd(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
  End
 --DATEADD(MINUTE,DATEDIFF(MINUTE,d.Stime,d.ETime)+30,convert(varchar(10),@AttDate,20)+' '+convert(varchar(8),d.etime,8))
 And (Logdate <= convert(varchar(10),DATEADD(dd,1,@AttDate),20)+' '+convert(varchar(8),d.etime,8))
 order by LogDate Desc)M
 order by LogDate desc
     
Update hr.dbo.HRM_DailyAttendance set outtime = @ActualEndTime  where EmpId = @EmpId and AttDate = @AttDate and locationid = @LocationId ;   
 
COMMIT TRANSACTION UPD;   
   
BEGIN TRANSACTION UPDAtt;   
 
Update hr.dbo.HRM_DailyAttendance set Attendance =   
 (   
Select   
Case when     
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between DATEADD(n,-ISNULL(hc.ChkShiftStTime,30),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8)) and   
DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.ShiftStartTime)   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)     
Then 'P'   
When   
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between DATEADD(n,-ISNULL(hc.ChkShiftStTime,30),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8)) and   
DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.ShiftStartTime)   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) < DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.LunchStartTime)     
Then 'P'   
When   
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) > DATEADD(n,+Isnull(hc.GraceTimeMin,5),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8))   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.LunchEndTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)     
Then 'P'   
When   
(DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between ShiftStartTime and DATEADD(n,+Isnull(hc.GraceTimeMin,5),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8))   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)     
)   
Then 'P'   
Else 'A'   
End as Attendance   
from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId  and Active = 'Y'
where a.EmpId = @EmpId and AttDate = @AttDate and a.locationid = @LocationId   
)   
where EmpId = @EmpId and AttDate = @AttDate  and locationid = @LocationId 

COMMIT TRANSACTION UPDAtt;   
 
BEGIN TRANSACTION UPDAttFlag;   

Update hr.dbo.HRM_DailyAttendance set latedet =   
 (   
Select   
Case when     
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between DATEADD(n,-ISNULL(hc.ChkShiftStTime,30),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8)) and   
--DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.ShiftStartTime)
a.ShiftStartTime
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)   
Then 'P'   
When   
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between DATEADD(n,-ISNULL(hc.ChkShiftStTime,30),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8)) and   
DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.ShiftStartTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) < DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.LunchStartTime)   
Then 'FH'   
When   
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) > DATEADD(n,+Isnull(hc.GraceTimeMin,5),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8))   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.LunchEndTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)   
Then 'SH'   
When   
 (DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between ShiftStartTime and DATEADD(n,+Isnull(hc.GraceTimeMin,5),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8))   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime) 
)   
Then 'LP'   
Else 'A'   
End as AttFlag   
from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'
where a.empid = @EmpId and AttDate = @AttDate and a.locationid = @LocationId   
 )   
where EmpId = @EmpId and AttDate = @AttDate  and locationid = @LocationId 

COMMIT TRANSACTION UPDAttFlag;   

--General Permission 
BEGIN TRANSACTION UPDGeneralPermission;   

Update hr.dbo.HRM_DailyAttendance set Attendance = b.Attendance,Latedet = b.AttFlag
from hr.dbo.HRM_DailyAttendance mm   
Inner join   
 (select a.empid,
case when c.PermissionLevel = 'SS' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0)  >= ShiftEndTime   
then 'P'   
When c.PermissionLevel = 'SE' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) between DATEADD(mi,-PermissionHrsMin,a.ShiftEndTime) and ShiftEndTime       
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= ShiftStartTime  + Isnull(hc.GraceTimeMin,5) 
Then 'P'
Else a.Attendance   

End as Attendance,

case when c.PermissionLevel = 'SS' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= ShiftEndTime   
then 'GP'   
When c.PermissionLevel = 'SE' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) between DATEADD(mi,-PermissionHrsMin,a.ShiftEndTime) and ShiftEndTime     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= ShiftStartTime + Isnull(hc.GraceTimeMin,5) 
Then 'GP' 
Else a.Latedet 

End as AttFlag,FromTime,ToTime   
from hr.dbo.HRM_DailyAttendance a with (nolock)   
Inner join HR_Permission c with (nolock) on a.EmpId = c.EmpId and c.EntryDate = @AttDate
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'
and c.Locationid = @LocationId   
where a.EmpId = @EmpId and AttDate = @AttDate and a.LocationId = @LocationId and c.Approved = 'Y' and c.PermissionType = 'G') b on mm.empid = b.empid
where mm.EmpId = @EmpId and AttDate = @AttDate and  locationid = @LocationId 

Update HR_Permission set Download = 'Y' where EmpId = (select EmpId from hr.dbo.emp_personal where cardno = @CardNo ) and EntryDate = @AttDate
and Approved = 'Y'

COMMIT TRANSACTION UPDGeneralPermission;   
 
--Special Permission 
BEGIN TRANSACTION UPDSpecialPermission; 

Update hr.dbo.HRM_DailyAttendance set Attendance = b.Attendance,Latedet = b.AttFlag
from hr.dbo.HRM_DailyAttendance mm   
Inner join   
 (select a.empid,
case when c.PermissionLevel = 'SS' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= ShiftEndTime   
then 'P'   
When c.PermissionLevel = 'SE' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) between DATEADD(mi,-PermissionHrsMin,a.ShiftEndTime) and ShiftEndTime       
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= ShiftStartTime   
Then 'P' 
Else a.Attendance 

End as Attendance,
case when c.PermissionLevel = 'SS' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= ShiftEndTime   
then 'SP'   
When c.PermissionLevel = 'SE' and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) between DATEADD(mi,-PermissionHrsMin,a.ShiftEndTime) and ShiftEndTime     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= ShiftStartTime   
Then 'SP'   
Else a.Latedet 
 
End as AttFlag,FromTime,ToTime   
from hr.dbo.HRM_DailyAttendance a with (nolock)   
Inner join HR_Permission c with (nolock) on a.EmpId = c.EmpId and c.EntryDate = @AttDate
and c.Locationid = @LocationId   
where a.EmpId = @EmpId and AttDate = @AttDate and a.LocationId = @LocationId and c.Approved = 'Y' and c.PermissionType = 'S') b on mm.empid = b.empid
where mm.EmpId = @EmpId and AttDate = @AttDate and  locationid = @LocationId

Update HR_Permission set Download = 'Y' where EmpId = (select EmpId from hr.dbo.emp_personal where cardno = @CardNo ) and EntryDate = @AttDate
and Approved = 'Y'

COMMIT TRANSACTION UPDSpecialPermission;   

--Late Permission 
BEGIN TRANSACTION UPDLatePermission; 

Update hr.dbo.HRM_DailyAttendance set Attendance = b.Attendance,Latedet = b.AttFlag
from hr.dbo.HRM_DailyAttendance mm   
Inner join   
 (select a.empid,
case when DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0)  between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0)  >= ShiftEndTime       
then 'P'   
Else a.Attendance 
End as Attendance,
case when DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0)  between  ShiftStartTime and  DATEADD(mi,PermissionHrsMin,a.ShiftStartTime)     
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0)  >= ShiftEndTime       
then 'LTP'   
Else a.Latedet 
End as AttFlag,FromTime,ToTime   
from hr.dbo.HRM_DailyAttendance a with (nolock)   
Inner join HR_Permission c with (nolock) on a.EmpId = c.EmpId and c.entryDate = @AttDate
and c.Locationid = @LocationId 
where a.EmpId = @EmpId and AttDate = @AttDate and a.LocationId = @LocationId and c.Approved = 'Y' and c.PermissionType = 'L' ) b on mm.empid = b.empid
where mm.EmpId = @EmpId and AttDate = @AttDate and  locationid = @LocationId   

Update HR_Permission set Download = 'Y' where EmpId = (select EmpId from hr.dbo.emp_personal where cardno = @CardNo ) and EntryDate = @AttDate
and Approved = 'Y'

COMMIT TRANSACTION UPDLatePermission; 

BEGIN TRANSACTION UPDMissPunch; 

Update hr.dbo.HRM_DailyAttendance set
InTime = Case when InTime between DATEADD(minute,-Isnull(ChkShiftStartTime,30),shiftstarttime) and LunchStartTime then InTime else null end,
outtime = Case when outtime between DATEADD(MINUTE,1,LunchStartTime) and ShiftEndTime then outtime else null end,
Latedet = 'MP'
where AttDate = @AttDate  and locationid = @LocationId  and InTime = outtime and EmpId = @EmpId

COMMIT TRANSACTION UPDMissPunch;
 
BEGIN TRANSACTION UPDHalfMisPunch; 

update hr.dbo.HRM_DailyAttendance
set HalfDayLeave =  Isnull((case when Latedet = 'FH' Then  'Y' when Latedet = 'SH' Then 'Y' Else 'N' end),'N') ,
MisPunch = (case when Latedet = 'MP' Then  'Y' else 'N' end)
where EmpId = @EmpId and AttDate = @AttDate  and locationid = @LocationId   

COMMIT TRANSACTION UPDHalfMisPunch;   

BEGIN TRANSACTION CalTimeUPD; 

Update hr.dbo.HRM_DailyAttendance set CalStartTime =   
 (   
Select Case when Attendance = 'P' and HalfDayLeave = 'N' Then   
ShiftStartTime   
When Attendance = 'P' and HalfDayLeave = 'Y' and Latedet = 'FH' Then   
ShiftStartTime   
when Attendance = 'P' and HalfDayLeave = 'Y' and Latedet =  'SH' Then   
LunchEndTime   
when Attendance = 'P' and Latedet = 'LP' Then   
ShiftStartTime   
End as CalStartTime   
from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'   
where a.EmpId = @EmpId and AttDate = @AttDate and a.locationid = @LocationId   
), CalEndTime =   
(   
Select Case when Attendance = 'P'and HalfDayLeave = 'N' Then   
ShiftEndTime   
When Attendance = 'P' and HalfDayLeave = 'Y' and Latedet = 'FH' Then
LunchStartTime   
when Attendance = 'P' and HalfDayLeave = 'Y' and Latedet =  'SH' Then   
ShiftEndTime   
when Attendance = 'P' and Latedet = 'LP' Then   
ShiftEndTime   
End as CalEndTime   
from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'   
where a.empid = @EmpId and AttDate = @AttDate and a.locationid = @LocationId   

where EmpId = @EmpId and AttDate = @AttDate  and locationid = @LocationId   

COMMIT TRANSACTION CalTimeUPD;   

--BEGIN TRANSACTION TotMinUPD;   
   
--Update HR_DailyAttendance set TotalMin =   
-- (   
--Select DATEDIFF(MINUTE,CalStartTime,CalEndTime)
--from HR_DailyAttendance a with (nolock)
--Inner Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
--Inner join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
--Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = 1 and Active = 'Y'   
--where CardNo = @CardNo and ShiftDate = @AttDate  and a.locationid = 1 
--) 
--where CardNo = @CardNo and ShiftDate = @AttDate and locationid = 1
   
--COMMIT TRANSACTION TotMinUPD; 

--BEGIN TRANSACTION ExtraMinUPD;   
   
--Update HR_DailyAttendance set ExtraMin =   
-- (   
--Select DATEDIFF(MINUTE,CalEndTime,ActualEndTime)
--from HR_DailyAttendance a with (nolock)
--Inner Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
--Inner join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
--Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y' 
--where CardNo = @CardNo and ShiftDate = @AttDate and a.locationid = @LocationId   
--) 
--where CardNo = @CardNo and ShiftDate = @AttDate  and locationid = @LocationId   
   
--COMMIT TRANSACTION ExtraMinUPD; 

BEGIN TRANSACTION LateMinUPD;   

Update hr.dbo.HRM_DailyAttendance set LatePunch =   
 (   
Select Case when CalStartTime < InTime and ShiftStartTime = CalStartTime then DATEDIFF(MINUTE,CalStartTime,InTime)
end
from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'
where a.empid = @EmpId and AttDate = @AttDate and a.locationid = @LocationId   

where EmpId = @EmpId and AttDate = @AttDate and locationid = @LocationId   

COMMIT TRANSACTION LateMinUPD; 

BEGIN TRANSACTION LateDetA;

 Update hr.dbo.hrm_dailyattendance set Attendance = Latedet where Attendance = 'P' and Latedet = 'A' and LeaveId = 0 and
 EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate

COMMIT TRANSACTION LateDetA; 


BEGIN TRANSACTION WorkerLateAtt;
Declare @LateCount Integer,
@GraceTimeCount Integer,
@LateDt varchar(10)

select @LateDt = Latedet  from hr.dbo.hrm_dailyattendance where EmpId = @EmpId and AttDate = @AttDate and Latedet = 'LP'

select @LateCount = COUNT(*),@GraceTimeCount = GraceTimeCount from hr.dbo.hrm_dailyattendance a
Left join HR_Controls b on a.CatId = b.CategoryId and active = 'Y'
where a.AttDate between (select StartDate from YrMonDesc where Status = 'A' ) and
(select EndDate from YrMonDesc where Status = 'A') and EmpId = @EmpId and Latedet = 'LP'
and a.CatId not in (5,6,7,15,16,17,27,31)
group by GraceTimeCount

If @LateCount > @GraceTimeCount and @LateDt = 'LP'

Begin

    Update hr.dbo.hrm_dailyattendance set HalfDayLeave = 'Y',Latedet = 'SH' where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate

End

COMMIT TRANSACTION WorkerLateAtt;

--Download Flag
BEGIN TRANSACTION DownLoadFlag;   
Declare @AStTime DateTime,
 @AEndTime DateTime

select @AStTime = InTime,@AEndTime = outtime from hr.dbo.HRM_DailyAttendance where EmpId = @EmpId and AttDate = @AttDate and locationid = @LocationId   

Update sandfitsetime.dbo.DeviceLogs set ChkData = 'Y' where LogDate = @AStTime and UserId = @CardNo

Update sandfitsetime.dbo.DeviceLogs set ChkData = 'Y' where LogDate = @AEndTime and UserId = @CardNo

Update sandfitsetime.dbo.DeviceLogs set ChkData1 = 'Y' where LogDate between DATEADD(SECOND,1,@AStTime) and DATEADD(SECOND,-1,@AEndTime) and UserId = @CardNo

COMMIT TRANSACTION DownLoadFlag; 
   
Fetch next from cur_HrmDailyAttendanceTest into @EmpId, @CardNo,@NextDay,@PreviousDay;   
   
End   
 
Close cur_HrmDailyAttendanceTest
Deallocate   cur_HrmDailyAttendanceTest

SET NOCOUNT OFF;     

--ElOdComLeave
BEGIN TRANSACTION ElOdComLeave; 


Insert Into hr.dbo.HRM_DailyAttendance
(EmpId,ReasonForLeave,AttDate,Leaveid,CatId,DeptId,Attendance,Latedet, 
EntryEmpId,EntryComputer,ImportMachine,LocationId,entrydate)
select  a.empid,a.reason,@AttDate,a.leavetype,isnull(ss.catid,e.catid),isnull(ss.deptid,e.deptid),
Case when c.LeaveType = 'absent' Then 'A'
Else 'A' End as LeaveTypeAttendance,
Case when c.LeaveType = 'absent' Then 'L'
Else c.LeaveType End as LeaveType,
10,'Server','N',@LocationId,GETDATE()
from hr.dbo.hrm_leavemaster a
Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno
Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
Inner Join hr.dbo.emp_personal d on a.empid = d.empid
Inner join hr.dbo.emp_employementdet e on d.empid = e.empid
Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
where
leavedate = @AttDate and b.approved = 'Y'
and d.locationid = @LocationId
and a.leavetype <> 3 and a.leavetype <> 6
and d.empid not in (select EmpId from hr.dbo.HRM_DailyAttendance where AttDate = @AttDate and locationid = @LocationId)
and d.empid = @EmpId

COMMIT TRANSACTION ElOdComLeave; 

--Absenties
BEGIN TRANSACTION Absenties;

--Declare @Leave Integer = 0,
--@Desc Varchar(100)
----select  COUNT(*), LDesc from hr.dbo.leave where LDate = '2017-03-05' and locationid = 1 group by ldesc
--select  @Leave = COUNT(*),@Desc = LDesc from hr.dbo.leave where LDate = @AttDate and locationid = @LocationId
--group by LDesc

--If @Leave = 0
--Begin

Insert Into hr.dbo.HRM_DailyAttendance
(EmpId,AttDate,LeaveId,CatId,DeptId,Attendance,Latedet, 
EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date)
select a.empid,@AttDate,0,isnull(ss.catid,b.catid),isnull(ss.deptid,b.deptid),
Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
10,'Server','N',@LocationId,GETDATE()
from hr.dbo.emp_personal  a
Inner join hr.dbo.emp_employementdet b on a.empid = b.empid and catid not in (21,22)
Inner join hr.dbo.department  c on c.deptid = b.deptid
Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
where a.locationid = @LocationId and resign <> 'Y' and b.catid = 27
and a.empid not in (select empid from hr.dbo.HRM_DailyAttendance where locationid = @LocationId and AttDate = @AttDate and empid = @empid )
and a.empid  = @empid
--and a.empid not in (select a.empid from hr.dbo.emp_personal a
--     Inner join hr.dbo.hrm_weekoff wh on a.empid = wh.empid
--     and @Attdate between wh.eff_from and wh.eff_to
--     and week_off = DATENAME(dw,@Attdate)
--       )
Insert Into hr.dbo.HRM_DailyAttendance
(EmpId,AttDate,LeaveId,CatId,DeptId,Attendance,Latedet, 
EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date)
select a.empid,@AttDate,6,isnull(ss.catid,b.catid),isnull(ss.deptid,b.deptid),
Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
10,'Server','N',@LocationId,GETDATE()
from hr.dbo.emp_personal  a
Inner join hr.dbo.emp_employementdet b on a.empid = b.empid and catid not in (21,22)
Inner join hr.dbo.department  c on c.deptid = b.deptid
Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
where a.locationid = @LocationId and resign <> 'Y' and b.catid <> 27
and a.empid not in (select empid from hr.dbo.HRM_DailyAttendance where locationid = @LocationId and AttDate = @AttDate and empid = @empid )
and a.empid = @empid

--End

--Weekly holiday-->Weekoff employee absent
--Desabled on 050517 instructed by Mr.sureshbabu
--If @Leave > 0 and @Desc = 'Weekly Holiday'
--Begin

--Insert Into hr.dbo.HRM_DailyAttendance
-- (EmpId,AttDate,LeaveId,CatId,DeptId,Attendance,Latedet, 
--EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date)
--select t.empid,@AttDate,6,isnull(ss.catid,b.catid),isnull(ss.deptid,b.deptid),
--Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
--Case when Isnull(ss.catid,b.catid) = 27 Then 'P' Else 'A' End ,
--10,'Server','N',@LocationId,GETDATE()
--from hr.dbo.emp_Personal a
--Inner Join hr.dbo.emp_employementdet b on a.empid = b.empid
--Inner Join  (select a.empid from hr.dbo.emp_personal a
--Inner join hr.dbo.hrm_weekoff wh on a.empid = wh.empid
--and @Attdate between wh.eff_from and wh.eff_to) t on a.empid = t.empid
--Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
--and a.locationid = @LocationId and resign <> 'Y'
-- where a.empid not in (select empid from hr.dbo.HRM_DailyAttendance where locationid = @LocationId and AttDate = @AttDate)
-- and a.empid = @EmpId

--End

COMMIT TRANSACTION Absenties;

--SundayAbsenties
BEGIN TRANSACTION SundayAbsenties;

Declare @SundayLeave Integer = 0
--select  COUNT(*), LDesc from hr.dbo.leave where LDate = '2017-03-05' and locationid = 1 group by ldesc
select  @SundayLeave = COUNT(*) from hr.dbo.leave where LDate = @AttDate and locationid = @LocationId and LDesc = 'Weekly Holiday'
group by LDesc

If @SundayLeave > 0
Begin
--Declare @Day varchar(20)  =  (select  DATENAME(dw,convert(date,(@AttDate))))
--Declare @WeekDay varchar(20)  = ''
--select @WeekDay = week_off from hr.dbo.hrm_weekoff where empid = @EmpId and @AttDate between eff_from and eff_to

--If  @WeekDay = ''
--Begin
Delete hr.dbo.hrm_dailyattendance where  EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate and Attendance = 'A'  and CatId in (5,6,7,15,16,17,40) and EmpId not in (select empid from hr.dbo.hrm_weekoff where  @AttDate between eff_from and eff_to) and ISNULL(Onduty,'N') = 'N'
--End

End


COMMIT TRANSACTION SundayAbsenties;


--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--NFHAbsenties
BEGIN TRANSACTION NFHAbsenties;

Declare @NFHLeave Integer = 0
--select  COUNT(*), LDesc from hr.dbo.leave where LDate = '2017-03-05' and locationid = 1 group by ldesc
select  @NFHLeave = COUNT(*) from hr.dbo.leave where LDate = @AttDate and locationid = @LocationId and LDesc <> 'Weekly Holiday'
group by LDesc

If @NFHLeave >  0
Begin
--Declare @Day varchar(20)  =  (select  DATENAME(dw,convert(date,(@AttDate))))
--Declare @WeekDay varchar(20)  = ''
--select @WeekDay = week_off from hr.dbo.hrm_weekoff where empid = @EmpId and @AttDate between eff_from and eff_to

--If  @WeekDay = ''
--Begin
--and EmpId not in (select empid from hr.dbo.hrm_weekoff where  @AttDate between eff_from and eff_to)

Delete hr.dbo.hrm_dailyattendance where  EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate and Attendance = 'A'  and CatId in (5,6,7,15,16,17,40) and ISNULL(Onduty,'N') = 'N'
--End

End

COMMIT TRANSACTION NFHAbsenties;
--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

--***************************************

--WeekOffAbsenties
BEGIN TRANSACTION WeekOffAbsenties;

Declare @WeekOffLeave Integer = 0

select  @WeekOffLeave = COUNT(*) from hr.dbo.hrm_weeklyoff where LDate = @AttDate and empid = @EmpId and locationid = @LocationId

If  @WeekOffLeave > 0
Begin
Print @WeekOffLeave
Delete hr.dbo.hrm_dailyattendance where  EmpId = @EmpId and EmpId in (select EmpId from hr.dbo.hrm_weeklyoff where LDate = @AttDate and locationid = @LocationId) and LocationId = @LocationId and AttDate = @AttDate and Attendance = 'A'  and CatId in (5,6,7,15,16,17,40) and ISNULL(Onduty,'N') = 'N'
End
COMMIT TRANSACTION WeekOffAbsenties;

--***************************************
 
--ElComLeave Update   
Declare cur_ElOdComLeave Cursor for   
   
select  d.empid,d.cardno,b.leavedate
from hr.dbo.hrm_leavemaster a
Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno
Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
Inner Join hr.dbo.emp_personal d on a.empid = d.empid
Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
where leavedate = @AttDate  and b.approved = 'Y' and a.leavetype <> 3 and a.empid = @EmpId
   
Open cur_ElOdComLeave;   
   
fetch next from cur_ElOdComLeave into @EmpId,@CardNo,@attDate;   
   
While @@FETCH_STATUS = 0   
Begin   

   BEGIN TRANSACTION ElOdComLeave;
       
 select @FHFlag = Isnull(Case when b.halfday = 'FH' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,Null) ,
     @SHFlag = IsNull(Case when b.halfday = 'SH' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,Null) ,
     @FullFlag = IsNull(Case when b.halfday = 'F' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,Null) ,
     @Reason = substring(reason,1,100) ,@LeaveId = a.leavetype
 from hr.dbo.hrm_leavemaster a
 Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno
 Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
 Inner Join hr.dbo.emp_personal d on a.empid = d.empid
 Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
 where d.cardno = @CardNo and leavedate = @AttDate and a.leavetype <> 3 and a.leavetype <> 6 and a.empid = @EmpId And b.approved = 'Y'

 Update hr.dbo.HRM_DailyAttendance  set FHFlag = @FHFlag,SHFlag = @SHFlag ,FullFlag = @FullFlag,
 ReasonForLeave = @Reason,LeaveId = @LeaveId
 where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate

 Print @FHFlag
 print @SHFlag
 print @FullFlag
 print @Reason
 print @LeaveId
 print @EmpId
 print @LocationId
 print @AttDate

 If @SHFlag <> 'L'
 Begin

 --Update hr.dbo.hrm_dailyattendance set
 --HalfDayLeave = IsNull(
 --(select case when
 --InTime between  DATEADD(minute,-Isnull(chkshiftStartTime,30),intime)
 --and DATEADD(minute,+Isnull(GraceTime,5),intime) then 'Y' else Isnull(HalfDayLeave,'N') end
 --from hr.dbo.HRM_DailyAttendance
 --where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 --and Attendance = 'P' and  Isnull(SHFlag,'L') <> 'L'
 --and InTime between  DATEADD(minute,-Isnull(chkshiftStartTime,30),intime)
 --and DATEADD(minute,+Isnull(GraceTime,5),intime)
 --and outtime >= LunchStartTime),'N'),Latedet = @SHFlag
 --where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 --and InTime Is Not Null and outTime Is Not Null

 Update hr.dbo.hrm_dailyattendance set HalfDayLeave = 'Y' where EmpId = @EmpId and AttDate = @AttDate


 End
 
 If @FHFlag <> 'L'
 Begin

 --update hr.dbo.HRM_DailyAttendance set HalfDayLeave =
 --Isnull((select  case when
 --InTime between  DATEADD(minute,-Isnull(chkshiftStartTime,30),intime)
 --and DATEADD(minute,+Isnull(GraceTime,5),intime) then 'Y' else Isnull(HalfDayLeave,'N') end
 --from hr.dbo.HRM_DailyAttendance
 --where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 --and Attendance = 'P' and  Isnull(FHFlag,'L') <> 'L'
 --and InTime between  ShiftStartTime
 --and DATEADD(minute,+Isnull(GraceTime,5),LunchEndTime)
 --and outtime >= ShiftEndTime),'N'),Latedet = @FHFlag
 --where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 --and InTime Is Not Null and outTime Is Not Null
 Update hr.dbo.hrm_dailyattendance set HalfDayLeave = 'Y' where EmpId = @EmpId and AttDate = @AttDate

 End

 If @FullFlag is not null
 Begin
  update hr.dbo.HRM_DailyAttendance set Attendance = 'A',MisPunch = 'N',HalfDayLeave = 'N',Latedet = @FullFlag
  where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 End

 --Update hr.dbo.hrm_dailyattendance set Attendance = 'A'  where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 --and InTime Is Null and outTime Is Null
     
COMMIT TRANSACTION ElOdComLeave; 
 
Fetch next from cur_ElOdComLeave into @empid,@CardNo,@attDate;   
   
End   
   
Close cur_ElOdComLeave;   
   
Deallocate cur_ElOdComLeave; 

--Od Update
Declare cur_OD Cursor for   
   
select  d.empid,d.cardno,b.leavedate
from hr.dbo.hrm_leavemaster a
Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno
Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
Inner Join hr.dbo.emp_personal d on a.empid = d.empid
Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id in (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
where leavedate = @AttDate  and b.approved = 'Y' and a.leavetype = 3 and a.empid = @EmpId and d.locationid  = @LocationId
   
Open cur_OD;   
   
fetch next from cur_OD into @EmpId,@CardNo,@attDate;   
   
While @@FETCH_STATUS = 0   
Begin   

   BEGIN TRANSACTION OD;
             
 select @FHFlagOD = Isnull(Case when b.halfday = 'FH' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,hd.FHflag) ,
        @Reason = substring(a.reason,1,100) ,@LeaveId = a.leavetype,
@ODAndPermission = Case when hd.OutTime between hp.fromtime and hp.ToTime Then  'P'Else hd.Attendance end
 from hr.dbo.hrm_leavemaster a
 Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno and b.approved = 'Y'
 Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
 Inner Join hr.dbo.emp_personal d on a.empid = d.empid
 Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id in (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
 Left Join hr.dbo.hrm_dailyattendance hd on hd.EmpId = d.empid and hd.AttDate = @AttDate
 Left Join HR_Permission hp on hd.EmpId = hp.EmpId and hp.EntryDate = hd.AttDate and hp.approved = 'Y'
 where d.cardno = @CardNo and leavedate = @AttDate  and a.empid = @EmpId and halfday = 'FH' and d.LocationId = @LocationId
 
 select @SHFlagOD = IsNull(Case when b.halfday = 'SH' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,hd.SHFlag) ,
     @Reason = substring(a.reason,1,100) ,@LeaveId = a.leavetype,
@ODAndPermission = Case when hd.InTime between hp.fromtime and hp.ToTime Then  'P' Else hd.Attendance end
 from hr.dbo.hrm_leavemaster a
 Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno and b.approved = 'Y'
 Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
 Inner Join hr.dbo.emp_personal d on a.empid = d.empid
 Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id in (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
 Left Join hr.dbo.hrm_dailyattendance hd on hd.EmpId = d.empid and hd.AttDate = @AttDate
 Left Join HR_Permission hp on hd.EmpId = hp.EmpId and hp.EntryDate = hd.AttDate and hp.approved = 'Y'
 where d.cardno = @CardNo and leavedate = @AttDate   and a.empid = @EmpId and HalfDay = 'SH' and d.LocationId = @LocationId
 
 select
     @FullFlagOD = IsNull(Case when b.halfday = 'F' Then (Case when c.LeaveType = 'absent' Then 'L' Else c.LeaveType End) End,hd.FullFlag) ,
     @Reason = substring(reason,1,100) ,@LeaveId = a.leavetype,@ODAndPermission = 'P'
 from hr.dbo.hrm_leavemaster a
 Inner join hr.dbo.hrm_leavedet b on a.appno = b.appno and b.approved = 'Y'
 Inner join hr.dbo.HRM_LeaveType c on a.leavetype = c.leaveid
 Inner Join hr.dbo.emp_personal d on a.empid = d.empid
 Left Join hr.dbo.scalemaster ss with (nolock) on a.empid = ss.EmpId and ss.id in (select MAX(id) from  hr.dbo.scalemaster where empid =a.empid )
 Left Join hr.dbo.hrm_dailyattendance hd on hd.EmpId = d.empid and hd.AttDate = @AttDate
 where d.cardno = @CardNo and leavedate = @AttDate   and a.empid = @EmpId and halfday = 'F' and d.LocationId = @LocationId

 
 Declare @Check1 Integer = 0

 select @Check1 = COUNT(*) from hr.dbo.hrm_dailyattendance where EmpId = @EmpId and AttDate = @AttDate
 
 If @Check1 >0
 Begin
 Print @ODAndPermission
 Print 'ODAndPer'
 print @ODAndPermission

If @ODAndPermission = 'P'
Begin
Update hr.dbo.HRM_DailyAttendance  set FHFlag = @FHFlagOD,SHFlag = @SHFlagOD ,FullFlag = @FullFlagOD,
Onduty = 'Y',Attendance = 'P'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

If @ODAndPermission <> 'P'
Begin
Update hr.dbo.HRM_DailyAttendance  set FHFlag = @FHFlagOD,SHFlag = @SHFlagOD ,FullFlag = @FullFlagOD,
Onduty = 'Y'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

 Print @FHFlag
 print @SHFlag
 print @FullFlag
 print @Reason
 print @LeaveId
 print @EmpId
 Print @CardNo
 print @LocationId
 print @AttDate
 
 End



 If @Check1 = 0
 Begin

 Insert into hr.dbo.hrm_dailyattendance (EmpId,AttDate,CatId,DeptId,Attendance,EntryEmpId,EntryComputer,ImportMachine,LocationId,ent_down_date,Latedet,SHFlag,FHFlag,FullFlag,Onduty)
 select @EmpId,@AttDate,Isnull(ss.CatId,ed.catid)Catid,ISNULL(ss.deptid,ed.deptid)DeptId,'A', 10,'Server','Y',@LocationId,GETDATE(),'A',@SHFlagOD,@FHFlagOD,@FullFlagOD,'Y'
 from hr.dbo.emp_personal b
 Inner join hr.dbo.emp_employementdet ed with (nolock) on b.empid = ed.empid   
 Left Join hr.dbo.scalemaster ss with (nolock) on b.empid = ss.EmpId and ss.id = (select MAX(id) from  hr.dbo.scalemaster where empid =b.empid )
 where b.empid = @EmpId and LocationId = @LocationId
 End

 Print @SHFlagOD
 print 'SH'
 Print @FHFlagOD
 Print 'FH'
 Print @FullFlagOD
 Print 'FLL'
 Print @ODAndPermission
 
 If @SHFlagOD = 'OD'
 Begin

If @ODAndPermission = 'P'
Begin
Update hr.dbo.hrm_dailyattendance set Onduty = 'Y', HalfDayOnduty = 'Y' , Latedet = 'OD',Attendance = 'P'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

If @ODAndPermission <> 'P'
Begin
Update hr.dbo.hrm_dailyattendance set Onduty = 'Y', HalfDayOnduty = 'Y' , Latedet = 'OD'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

 End
 
 If @FHFlagOD = 'OD'
 Begin
If @ODAndPermission = 'P'

Begin
Update hr.dbo.hrm_dailyattendance set Onduty = 'Y', HalfDayOnduty = 'Y',Latedet = 'OD',Attendance = 'P'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

If @ODAndPermission <> 'P'
Begin
Update hr.dbo.hrm_dailyattendance set Onduty = 'Y', HalfDayOnduty = 'Y',Latedet = 'OD'
where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
End

 End
 
 If @FullFlagOD = 'OD'
 Begin
 Update hr.dbo.hrm_dailyattendance set HalfDayLeave = 'N', Onduty = 'Y',HalfDayOnduty = 'N',Latedet = 'OD',MisPunch = 'N',Attendance = 'A'
 where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 End
 
 If @SHFlagOD = 'OD' and @FHFlagOD = 'OD'
 Begin
 Update hr.dbo.hrm_dailyattendance set HalfDayLeave = 'N', Onduty = 'Y', HalfDayOnduty = 'N',Latedet = 'OD',MisPunch = 'N'
 where EmpId = @EmpId and LocationId = @LocationId and AttDate = @AttDate
 End

Update hr.dbo.hrm_dailyattendance set HalfDayLeave = (
select distinct ISNULL( Case when
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) between DATEADD(n,-ISNULL(hc.ChkShiftStTime,30),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8)) and   
DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.ShiftStartTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) < DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.LunchStartTime)
and halfday = 'SH' then  'N'
When
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) > DATEADD(n,+Isnull(hc.GraceTimeMin,5),convert(varchar(10),a.InTime,20)+' '+convert(varchar(8),a.ShiftStartTime,8))   
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.InTime), 0) <= DATEADD(MINUTE,Isnull(hc.GraceTimeMin,5),a.LunchEndTime) 
and DATEADD(MINUTE, DATEDIFF(MINUTE, 0, a.OutTime), 0) >= DATEADD(n,-ISNULL(hc.EarlyOutPunch,0),a.ShiftEndTime)   
and halfday = 'FH'
Then 'N'
Else HalfDayLeave end,HalfDayLeave) from hr.dbo.HRM_DailyAttendance a with (nolock)
Left Join hr.dbo.scalemaster b with (nolock) on b.empid = a.EmpId and id in (select MAX(id) from  hr.dbo.scalemaster where empid =a.EmpId )
Left join hr.dbo.Designation d with (nolock) on  d.desigid = b.desigid
Left Join HR_Controls hc with (nolock) on   hc.DesigGroup = d.DesigGroup and hc.CategoryId = a.CatId and hc.Locationid = @LocationId and Active = 'Y'
Inner join hr.dbo.hrm_leavemaster lm on a.EmpId = lm.empid
Inner join hr.dbo.hrm_leavedet Ld on ld.appno = lm.appno and a.AttDate = ld.leavedate and ld.approved = 'Y'
where a.EmpId = @EmpId and AttDate = @AttDate and ld.approved = 'Y' and a.Onduty = 'Y' and lm.leavetype = 3 )
where EmpId = @EmpId and AttDate = @AttDate and Onduty = 'Y'

   
COMMIT TRANSACTION OD; 

Fetch next from cur_OD into @empid,@CardNo,@attDate;   

End   
   
Close cur_OD;   
   
Deallocate cur_OD; 
End