Exemplo n.º 1
0
        public async Task <ApiResponse> Handle(GetEmployeesAttendanceByDateQuery request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                int month = DateTime.Parse(request.SelectedDate).Month;

                List <EmployeeAttendance> empattendancelist = await _dbContext.EmployeeAttendance.Where(x => x.Date.Date == DateTime.Parse(request.SelectedDate).Date).ToListAsync();  //marked or not

                List <EmployeeDetail> activelist = await _dbContext.EmployeeDetail
                                                   .Include(x => x.EmployeeProfessionalDetail)                  // use to get officeId
                                                   .Include(x => x.EmployeeAttendance)
                                                   .Where(x => x.EmployeeProfessionalDetail.OfficeId == request.OfficeId &&
                                                          x.EmployeeProfessionalDetail.HiredOn.Value.Date <= DateTime.Parse(request.SelectedDate).Date&&
                                                          x.EmployeeProfessionalDetail.EmployeeTypeId == (int)EmployeeTypeStatus.Active &&
                                                          x.IsDeleted == false && x.EmployeeProfessionalDetail.AttendanceGroupId == request.AttendanceGroupId)
                                                   .OrderBy(x => x.EmployeeID)
                                                   .ToListAsync();                                  //EmployeeTypeId is moved to EmployeeProfessionalDetails table

                IList <EmployeeAttendanceModel> empAttModel = new List <EmployeeAttendanceModel>();
                if (request.AttendanceStatus)
                {
                    empAttModel = activelist.SelectMany(x => x.EmployeeAttendance)
                                  .Where(x => x.Date.Date == DateTime.Parse(request.SelectedDate).Date)
                                  .Select(x => new EmployeeAttendanceModel
                    {
                        AttendanceId     = x.AttendanceId,
                        EmployeeId       = x.EmployeeId,
                        Date             = x.Date,
                        InTime           = x.InTime.Value,
                        OutTime          = x.OutTime,
                        AttendanceTypeId = x.AttendanceTypeId,
                        EmployeeName     = x.EmployeeDetails.EmployeeName,
                        EmployeeCode     = x.EmployeeDetails.EmployeeCode,
                        LeaveStatus      = x.AttendanceId == (int)AttendanceType.L ? true : false,
                        OfficeId         = x.EmployeeDetails.EmployeeProfessionalDetail.OfficeId
                    }).ToList();


                    if (empAttModel.Count == 0 || empAttModel == null)
                    {
                        response.data.AttendanceStatus = false;  //attendance already marked
                    }
                    else
                    {
                        response.data.AttendanceStatus = true; //not marked
                    }
                }
                else
                {
                    int count = 0;

                    //Get Default In-time and out-time for an office and for current year and current month
                    PayrollMonthlyHourDetail xPayrollMonthlyHourDetail = _dbContext.PayrollMonthlyHourDetail.FirstOrDefault(x => x.IsDeleted == false && x.OfficeId == request.OfficeId && x.PayrollYear == DateTime.Now.Year &&
                                                                                                                            x.PayrollMonth == month && x.AttendanceGroupId == request.AttendanceGroupId);
                    if (xPayrollMonthlyHourDetail != null)
                    {
                        foreach (var item in activelist)
                        {
                            EmployeeAttendanceModel obj = new EmployeeAttendanceModel();
                            count = empattendancelist.Where(x => x.EmployeeId == item.EmployeeID).ToList().Count();
                            if (count == 0)
                            {
                                obj.EmployeeId       = item.EmployeeID;
                                obj.EmployeeName     = item.EmployeeName;
                                obj.EmployeeCode     = item.EmployeeCode;
                                obj.AttendanceTypeId = (int)AttendanceType.P;
                                obj.Date             = DateTime.UtcNow;
                                obj.InTime           = xPayrollMonthlyHourDetail.InTime;
                                obj.OutTime          = xPayrollMonthlyHourDetail.OutTime;
                                obj.LeaveStatus      = false;
                                obj.OfficeId         = request.OfficeId;
                                empAttModel.Add(obj);
                            }
                        }

                        if (empAttModel.Count == 0 || empAttModel == null)
                        {
                            response.data.AttendanceStatus = false;  //attendance already marked
                        }
                        else
                        {
                            response.data.AttendanceStatus = true; //not marked
                        }
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;

                        response.Message = "Intime and Outtime not set for office";
                    }
                }

                response.data.EmployeeAttendanceList = empAttModel;
                response.StatusCode = response.StatusCode == 0 ? StaticResource.successStatusCode : response.StatusCode;

                //if message is empty then success else there is already some error message
                response.Message = string.IsNullOrEmpty(response.Message) ? "Success" : response.Message;
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = ex.Message;
            }
            return(response);
        }
Exemplo n.º 2
0
        public async Task <ApiResponse> Handle(AddPayrollMonthlyHourCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                TimeSpan hours;
                hours         = Convert.ToDateTime(request.OutTime) - Convert.ToDateTime(request.InTime);
                request.Hours = Convert.ToInt32(hours.ToString().Substring(0, 2));

                if (request.SaveForAllOffice)
                {
                    List <PayrollMonthlyHourDetail> payrollMonthlyHourDetailsAdd    = new List <PayrollMonthlyHourDetail>();
                    List <PayrollMonthlyHourDetail> payrollMonthlyHourDetailsUpdate = new List <PayrollMonthlyHourDetail>();

                    List <int> officeIds = _dbContext.OfficeDetail.Where(x => x.IsDeleted == false).Select(x => x.OfficeId).ToList();

                    foreach (int officeId in officeIds)
                    {
                        var payrollinfo = await _dbContext.PayrollMonthlyHourDetail
                                          .FirstOrDefaultAsync(x => x.IsDeleted == false &&
                                                               x.OfficeId == officeId &&
                                                               x.PayrollMonth == request.PayrollMonth &&
                                                               x.PayrollYear == request.PayrollYear &&
                                                               x.AttendanceGroupId == request.AttendanceGroupId);

                        if (payrollinfo == null)
                        {
                            PayrollMonthlyHourDetail obj = new PayrollMonthlyHourDetail();
                            obj.CreatedById       = request.CreatedById;
                            obj.CreatedDate       = request.CreatedDate;
                            obj.Hours             = request.Hours;
                            obj.WorkingTime       = request.WorkingTime; //total working hours
                            obj.InTime            = request.InTime;
                            obj.OutTime           = request.OutTime;
                            obj.PayrollMonth      = request.PayrollMonth;
                            obj.PayrollYear       = request.PayrollYear;
                            obj.IsDeleted         = false;
                            obj.OfficeId          = officeId;
                            obj.AttendanceGroupId = request.AttendanceGroupId;
                            payrollMonthlyHourDetailsAdd.Add(obj);
                        }
                        else
                        {
                            payrollinfo.ModifiedDate      = DateTime.UtcNow;
                            payrollinfo.Hours             = request.Hours;
                            payrollinfo.WorkingTime       = request.WorkingTime;
                            payrollinfo.InTime            = request.InTime;
                            payrollinfo.OutTime           = request.OutTime;
                            payrollinfo.PayrollMonth      = request.PayrollMonth;
                            payrollinfo.PayrollYear       = request.PayrollYear;
                            payrollinfo.OfficeId          = officeId;
                            payrollinfo.AttendanceGroupId = request.AttendanceGroupId;
                            payrollMonthlyHourDetailsUpdate.Add(payrollinfo);
                        }
                    }

                    if (payrollMonthlyHourDetailsAdd.Any())
                    {
                        await _dbContext.AddRangeAsync(payrollMonthlyHourDetailsAdd);

                        await _dbContext.SaveChangesAsync();
                    }

                    if (payrollMonthlyHourDetailsUpdate.Any())
                    {
                        _dbContext.UpdateRange(payrollMonthlyHourDetailsUpdate);
                        await _dbContext.SaveChangesAsync();
                    }
                }
                else
                {
                    var payrollinfo = await _dbContext.PayrollMonthlyHourDetail
                                      .FirstOrDefaultAsync(x => x.IsDeleted == false &&
                                                           x.OfficeId == request.OfficeId &&
                                                           x.PayrollMonth == request.PayrollMonth &&
                                                           x.PayrollYear == request.PayrollYear &&
                                                           x.AttendanceGroupId == request.AttendanceGroupId
                                                           );

                    if (payrollinfo == null)
                    {
                        PayrollMonthlyHourDetail obj = new PayrollMonthlyHourDetail();
                        obj.CreatedById       = request.CreatedById;
                        obj.CreatedDate       = request.CreatedDate;
                        obj.Hours             = request.Hours;
                        obj.WorkingTime       = request.WorkingTime; //total working hours
                        obj.InTime            = request.InTime;
                        obj.OutTime           = request.OutTime;
                        obj.PayrollMonth      = request.PayrollMonth;
                        obj.PayrollYear       = request.PayrollYear;
                        obj.IsDeleted         = false;
                        obj.OfficeId          = request.OfficeId;
                        obj.AttendanceGroupId = request.AttendanceGroupId;
                        await _dbContext.PayrollMonthlyHourDetail.AddAsync(obj);

                        await _dbContext.SaveChangesAsync();
                    }
                    else
                    {
                        response.StatusCode = StaticResource.MandateNameAlreadyExistCode;
                        response.Message    = StaticResource.HoursAlreadySet;
                    }
                }

                response.StatusCode = StaticResource.successStatusCode;
                response.Message    = "Success";
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = ex.Message;
            }
            return(response);
        }
Exemplo n.º 3
0
        public async Task <ApiResponse> Handle(EditEmployeeAttendanceByDateCommand model, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                TimeSpan?totalworkhour;
                TimeSpan?totalovertime;
                int?     overtime = 0, workingHours = 0;
                DateTime OfficeInTime  = new DateTime();
                DateTime OfficeOutTime = new DateTime();

                var existrecord = await _dbContext.EmployeeAttendance.FirstOrDefaultAsync(x => x.AttendanceId == model.AttendanceId);

                if (existrecord != null)
                {
                    PayrollMonthlyHourDetail payrollMonthlyHourDetail = await _dbContext.PayrollMonthlyHourDetail.FirstOrDefaultAsync(x => x.OfficeId == model.OfficeId && x.PayrollYear == model.Date.Year && x.PayrollMonth == model.Date.Month);

                    int officeDailyHour   = payrollMonthlyHourDetail.Hours.Value;
                    int officeMonthlyHour = payrollMonthlyHourDetail.WorkingTime.Value;

                    OfficeInTime  = new DateTime(model.InTime.Value.Year, model.InTime.Value.Month, model.InTime.Value.Day, payrollMonthlyHourDetail.InTime.Value.Hour, payrollMonthlyHourDetail.InTime.Value.Minute, payrollMonthlyHourDetail.InTime.Value.Second);
                    OfficeOutTime = new DateTime(model.OutTime.Value.Year, model.OutTime.Value.Month, model.OutTime.Value.Day, payrollMonthlyHourDetail.OutTime.Value.Hour, payrollMonthlyHourDetail.OutTime.Value.Minute, payrollMonthlyHourDetail.OutTime.Value.Second);

                    if (model.InTime < OfficeInTime)
                    {
                        totalovertime = OfficeInTime - model.InTime;
                        overtime     += totalovertime.Value.Hours;
                        if (model.OutTime <= OfficeOutTime)
                        {
                            totalworkhour = model.OutTime - OfficeInTime;
                            workingHours += totalworkhour.Value.Hours;
                        }
                        if (model.OutTime > OfficeOutTime)
                        {
                            totalovertime = model.OutTime - OfficeOutTime;
                            overtime     += totalovertime.Value.Hours;
                            totalworkhour = OfficeOutTime - OfficeInTime;
                            workingHours += totalworkhour.Value.Hours;
                        }
                    }

                    if (model.InTime >= OfficeInTime && model.InTime < OfficeOutTime)
                    {
                        if (model.OutTime <= OfficeOutTime)
                        {
                            totalworkhour = model.OutTime - model.InTime;
                            workingHours += totalworkhour.Value.Hours;
                        }
                        if (model.OutTime > OfficeOutTime)
                        {
                            totalovertime = model.OutTime - OfficeOutTime;
                            overtime     += totalovertime.Value.Hours;
                            totalworkhour = OfficeOutTime - model.InTime;
                            workingHours += totalworkhour.Value.Hours;
                        }
                    }

                    if (model.InTime >= OfficeOutTime)
                    {
                        workingHours  = 0;
                        totalovertime = model.OutTime - model.InTime;
                        overtime     += totalovertime.Value.Hours;
                    }

                    if (model.OutTime <= OfficeInTime)
                    {
                        workingHours  = 0;
                        totalovertime = model.OutTime - model.InTime;
                        overtime     += totalovertime.Value.Hours;
                    }


                    totalworkhour = model.OutTime - model.InTime;
                    if (totalworkhour.ToString() == "00:00:00" || existrecord.AttendanceTypeId == (int)AttendanceType.A)
                    {
                        existrecord.AttendanceTypeId = (int)AttendanceType.A;
                        existrecord.InTime           = model.Date;
                        existrecord.OutTime          = model.Date;
                        totalworkhour = model.Date.Date - model.Date.Date;
                    }
                    else
                    {
                        existrecord.TotalWorkTime  = workingHours.ToString();
                        existrecord.HoverTimeHours = overtime;
                    }


                    existrecord.InTime           = model.InTime;
                    existrecord.OutTime          = model.OutTime;
                    existrecord.AttendanceTypeId = model.AttendanceTypeId;
                    existrecord.ModifiedById     = model.ModifiedById;
                    existrecord.ModifiedDate     = model.ModifiedDate;
                    existrecord.IsDeleted        = model.IsDeleted;

                    _dbContext.EmployeeAttendance.Update(existrecord);
                    await _dbContext.SaveChangesAsync();

                    response.StatusCode = StaticResource.successStatusCode;
                    response.Message    = "Success";
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = ex.Message;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(AddAttendanceDetailCommand request, CancellationToken cancellationToken)
        {
            DateTime    OfficeInTime  = new DateTime();
            DateTime    OfficeOutTime = new DateTime();
            ApiResponse response      = new ApiResponse();

            try
            {
                if (!request.EmployeeAttendance.Any() && request.EmployeeAttendance == null)
                {
                    throw new Exception(StaticResource.NoAttendanceToAdd);
                }

                var existrecord = await _dbContext.EmployeeAttendance.Where(x => x.Date.Date == request.EmployeeAttendance[0].Date.Date).ToListAsync();

                //gets the total working hours in a day for an office
                PayrollMonthlyHourDetail payrollMonthlyHourDetail = await _dbContext.PayrollMonthlyHourDetail
                                                                    .FirstOrDefaultAsync(x => x.OfficeId == request.EmployeeAttendance.First().OfficeId &&
                                                                                         x.PayrollYear == request.EmployeeAttendance.First().Date.Year &&
                                                                                         x.PayrollMonth == request.EmployeeAttendance.First().Date.Month &&
                                                                                         x.AttendanceGroupId == request.EmployeeAttendance.First().AttendanceGroupId);

                int?officeDailyHour   = payrollMonthlyHourDetail.Hours;
                int?officeMonthlyHour = payrollMonthlyHourDetail.WorkingTime;


                if (officeDailyHour != null && officeMonthlyHour != null)
                {
                    var financiallist = await _dbContext.FinancialYearDetail.FirstOrDefaultAsync(x => x.IsDefault == true);

                    foreach (var list in request.EmployeeAttendance)
                    {
                        TimeSpan?totalworkhour;
                        TimeSpan?totalovertime;
                        int?     overtime = 0, workingHours = 0, workingMinutes = 0, overtimeMinutes = 0;

                        var isemprecord = existrecord.Where(x => x.EmployeeId == list.EmployeeId && x.Date.Date == list.Date.Date).ToList();
                        totalworkhour = list.OutTime - list.InTime;

                        if (isemprecord.Count == 0)
                        {
                            if (totalworkhour.ToString() == "00:00:00" || list.AttendanceTypeId == (int)AttendanceType.A)
                            {
                                list.AttendanceTypeId = 2;
                                list.InTime           = list.Date;
                                list.OutTime          = list.Date;
                                totalworkhour         = list.Date.Date - list.Date.Date;
                            }

                            OfficeInTime  = new DateTime(list.InTime.Value.Year, list.InTime.Value.Month, list.InTime.Value.Day, payrollMonthlyHourDetail.InTime.Value.Hour, payrollMonthlyHourDetail.InTime.Value.Minute, payrollMonthlyHourDetail.InTime.Value.Second);
                            OfficeOutTime = new DateTime(list.OutTime.Value.Year, list.OutTime.Value.Month, list.OutTime.Value.Day, payrollMonthlyHourDetail.OutTime.Value.Hour, payrollMonthlyHourDetail.OutTime.Value.Minute, payrollMonthlyHourDetail.OutTime.Value.Second);

                            if (list.InTime < OfficeInTime)
                            {
                                totalovertime    = OfficeInTime - list.InTime;
                                overtime        += totalovertime.Value.Hours;
                                overtimeMinutes += totalovertime.Value.Minutes;
                                if (list.OutTime <= OfficeOutTime)
                                {
                                    totalworkhour   = list.OutTime - OfficeInTime;
                                    workingHours   += totalworkhour.Value.Hours;
                                    workingMinutes += totalworkhour.Value.Minutes;
                                }
                                if (list.OutTime > OfficeOutTime)
                                {
                                    totalovertime    = list.OutTime - OfficeOutTime;
                                    overtime        += totalovertime.Value.Hours;
                                    overtimeMinutes += totalovertime.Value.Minutes;
                                    totalworkhour    = OfficeOutTime - OfficeInTime;
                                    workingHours    += totalworkhour.Value.Hours;
                                    workingMinutes  += totalworkhour.Value.Minutes;
                                }

                                list.TotalWorkTime   = workingHours.ToString();
                                list.WorkTimeMinutes = workingMinutes;
                                list.HoverTimeHours  = overtime;
                                list.OvertimeMinutes = overtimeMinutes;
                            }

                            else if (list.InTime >= OfficeInTime)
                            {
                                if (list.OutTime <= OfficeOutTime)
                                {
                                    totalworkhour   = list.OutTime - list.InTime;
                                    workingHours   += totalworkhour.Value.Hours;
                                    workingMinutes += totalworkhour.Value.Minutes;
                                }
                                if (list.OutTime > OfficeOutTime)
                                {
                                    totalovertime    = list.OutTime - OfficeOutTime;
                                    overtime        += totalovertime.Value.Hours;
                                    overtimeMinutes += totalovertime.Value.Minutes;
                                    totalworkhour    = OfficeOutTime - list.InTime;
                                    workingHours    += totalworkhour.Value.Hours;
                                    workingMinutes  += totalworkhour.Value.Minutes;
                                }

                                list.TotalWorkTime   = workingHours.ToString();
                                list.WorkTimeMinutes = workingMinutes;
                                list.HoverTimeHours  = overtime;
                                list.OvertimeMinutes = overtimeMinutes;
                            }
                            else
                            {
                                list.TotalWorkTime  = workingHours.ToString();
                                list.HoverTimeHours = overtime;
                            }

                            list.FinancialYearId = financiallist.FinancialYearId;
                            list.CreatedById     = list.CreatedById;
                            list.CreatedDate     = DateTime.UtcNow;
                            list.IsDeleted       = false;
                            EmployeeAttendance obj = _mapper.Map <EmployeeAttendance>(list);
                            await _dbContext.EmployeeAttendance.AddAsync(obj);

                            await _dbContext.SaveChangesAsync();
                        }

                        EmployeeMonthlyAttendance xEmployeeMonthlyAttendanceRecord = await _dbContext.EmployeeMonthlyAttendance.FirstOrDefaultAsync(x => x.EmployeeId == list.EmployeeId && x.Month == list.Date.Month && x.Year == list.Date.Year && x.IsDeleted == false);

                        EmployeeMonthlyAttendance xEmployeeMonthlyAttendance = new EmployeeMonthlyAttendance();

                        //If Employee record is present in monthly attendance table then
                        if (xEmployeeMonthlyAttendanceRecord != null)
                        {
                            //if Employee is absent without any leave
                            if (totalworkhour.ToString() == "00:00:00" || list.AttendanceTypeId == (int)AttendanceType.A)
                            {
                                xEmployeeMonthlyAttendance.AbsentHours  = xEmployeeMonthlyAttendance.AbsentHours == null ? 0 : xEmployeeMonthlyAttendance.AbsentHours;
                                xEmployeeMonthlyAttendance.AbsentHours += officeDailyHour;
                            }

                            //update total attendance hours
                            if ((workingHours != 0 || workingMinutes != 0) && overtime == 0)
                            {
                                xEmployeeMonthlyAttendanceRecord.AttendanceHours   += totalworkhour.Value.Hours;
                                xEmployeeMonthlyAttendanceRecord.AttendanceMinutes += workingMinutes.Value;
                                xEmployeeMonthlyAttendanceRecord.OverTimeMinutes   += overtimeMinutes.Value;
                            }
                            //update total attendance hours and also add overtime hours
                            else if ((workingHours != 0 || workingMinutes != 0) && overtime != 0)
                            {
                                xEmployeeMonthlyAttendanceRecord.AttendanceHours   += totalworkhour.Value.Hours;
                                xEmployeeMonthlyAttendanceRecord.OvertimeHours      = xEmployeeMonthlyAttendanceRecord.OvertimeHours ?? 0;
                                xEmployeeMonthlyAttendanceRecord.OvertimeHours     += overtime;
                                xEmployeeMonthlyAttendanceRecord.AttendanceMinutes += workingMinutes.Value;
                                xEmployeeMonthlyAttendanceRecord.OverTimeMinutes   += overtimeMinutes.Value;
                            }

                            //updating employee monthly attendance record
                            _dbContext.EmployeeMonthlyAttendance.Update(xEmployeeMonthlyAttendanceRecord);
                            await _dbContext.SaveChangesAsync();
                        }
                        else// if employee monthly attendance record does not exists then add a record
                        {
                            // int monthDays = GetMonthDays(modellist.FirstOrDefault().Date.Month, modellist.FirstOrDefault().Date.Year);

                            int monthDays = DateTime.DaysInMonth(list.Date.Year, list.Date.Month);

                            //if employee is absent without any leave
                            if (totalworkhour.ToString() == "00:00:00" || list.AttendanceTypeId == (int)AttendanceType.A)
                            {
                                xEmployeeMonthlyAttendance.IsDeleted       = false;
                                xEmployeeMonthlyAttendance.EmployeeId      = list.EmployeeId;
                                xEmployeeMonthlyAttendance.Month           = list.Date.Month;
                                xEmployeeMonthlyAttendance.Year            = list.Date.Year;
                                xEmployeeMonthlyAttendance.AttendanceHours = 0;
                                xEmployeeMonthlyAttendance.OvertimeHours   = 0;
                                xEmployeeMonthlyAttendance.AbsentHours     = officeDailyHour;
                                xEmployeeMonthlyAttendance.OfficeId        = list.OfficeId;
                                xEmployeeMonthlyAttendance.TotalDuration   = officeMonthlyHour;
                            }
                            else
                            {
                                xEmployeeMonthlyAttendance.IsDeleted         = false;
                                xEmployeeMonthlyAttendance.EmployeeId        = list.EmployeeId;
                                xEmployeeMonthlyAttendance.Month             = list.Date.Month;
                                xEmployeeMonthlyAttendance.Year              = list.Date.Year;
                                xEmployeeMonthlyAttendance.AttendanceHours   = workingHours;
                                xEmployeeMonthlyAttendance.OvertimeHours     = overtime;
                                xEmployeeMonthlyAttendance.OfficeId          = list.OfficeId;
                                xEmployeeMonthlyAttendance.TotalDuration     = officeMonthlyHour;
                                xEmployeeMonthlyAttendance.AttendanceMinutes = list.WorkTimeMinutes.Value;
                                xEmployeeMonthlyAttendance.OverTimeMinutes   = list.OvertimeMinutes.Value;
                            }

                            Advances xAdvances = await _dbContext.Advances.Where(x => x.IsDeleted == false && x.IsApproved == true &&
                                                                                 x.EmployeeId == list.EmployeeId && x.OfficeId == list.OfficeId && x.IsDeducted == false &&
                                                                                 x.AdvanceDate <= DateTime.Now).FirstOrDefaultAsync();

                            if (xAdvances != null)
                            {
                                xEmployeeMonthlyAttendance.AdvanceId         = xAdvances.AdvancesId;
                                xEmployeeMonthlyAttendance.IsAdvanceApproved = xAdvances.IsApproved;
                                xEmployeeMonthlyAttendance.AdvanceAmount     = xAdvances.AdvanceAmount - xAdvances.RecoveredAmount;
                            }

                            await _dbContext.EmployeeMonthlyAttendance.AddAsync(xEmployeeMonthlyAttendance);

                            await _dbContext.SaveChangesAsync();
                        }
                    }

                    //If Employee is not present then check the leave table and update leave record accordingly
                    List <EmployeeApplyLeave> xEmployeeLeaveDetailList = await _dbContext.EmployeeApplyLeave
                                                                         .Include(x => x.EmployeeDetails.EmployeeProfessionalDetail)
                                                                         .Where(x => x.FromDate.Date >= request.EmployeeAttendance.First().Date.Date&&
                                                                                x.ToDate.Date <= request.EmployeeAttendance.First().Date.Date&&
                                                                                x.EmployeeDetails.EmployeeProfessionalDetail.OfficeId == request.EmployeeAttendance.First().OfficeId &&
                                                                                x.IsDeleted == false)
                                                                         .ToListAsync();

                    //If leave Exists then check the status of leave if approved then add the working hours of the day in attendance hours
                    if (xEmployeeLeaveDetailList.Count != 0)
                    {
                        int monthDays = DateTime.DaysInMonth(request.EmployeeAttendance.First().Date.Year, request.EmployeeAttendance.First().Date.Month);

                        foreach (EmployeeApplyLeave xEmployeeApplyLeave in xEmployeeLeaveDetailList)
                        {
                            EmployeeMonthlyAttendance xEmployeeMonthlyAttendanceRecord = await _dbContext.EmployeeMonthlyAttendance.FirstOrDefaultAsync(x => x.EmployeeId == xEmployeeApplyLeave.EmployeeId && x.Month == request.EmployeeAttendance.First().Date.Date.Month&& x.Year == request.EmployeeAttendance.First().Date.Date.Year&& x.IsDeleted == false);

                            if (xEmployeeMonthlyAttendanceRecord == null)
                            {
                                EmployeeMonthlyAttendance xEmployeeMonthlyAttendance = new EmployeeMonthlyAttendance();

                                if (xEmployeeApplyLeave.ApplyLeaveStatusId == 1)
                                {
                                    //remove hardcoded attendance hours once all office hours are available in master table
                                    xEmployeeMonthlyAttendance.IsDeleted       = false;
                                    xEmployeeMonthlyAttendance.OfficeId        = request.EmployeeAttendance.First().OfficeId;
                                    xEmployeeMonthlyAttendance.EmployeeId      = xEmployeeApplyLeave.EmployeeId;
                                    xEmployeeMonthlyAttendance.Month           = request.EmployeeAttendance.First().Date.Month;
                                    xEmployeeMonthlyAttendance.Year            = request.EmployeeAttendance.First().Date.Year;
                                    xEmployeeMonthlyAttendance.AttendanceHours = xEmployeeMonthlyAttendance.AttendanceHours != null ? xEmployeeMonthlyAttendance.AttendanceHours.Value : 0;
                                    //xEmployeeMonthlyAttendance.AttendanceHours += officeDailyHour;
                                    xEmployeeMonthlyAttendance.LeaveHours   += xEmployeeMonthlyAttendance.LeaveHours != null ? xEmployeeMonthlyAttendance.LeaveHours : 0;
                                    xEmployeeMonthlyAttendance.LeaveHours   += officeDailyHour;
                                    xEmployeeMonthlyAttendance.TotalDuration = officeMonthlyHour;

                                    Advances xAdvances = await _dbContext.Advances.Where(x => x.IsDeleted == false && x.IsApproved == true && x.IsDeducted == false &&
                                                                                         x.EmployeeId == xEmployeeApplyLeave.EmployeeId && x.OfficeId == request.EmployeeAttendance.First().OfficeId &&
                                                                                         x.AdvanceDate <= DateTime.Now).FirstOrDefaultAsync();

                                    if (xAdvances != null)
                                    {
                                        xEmployeeMonthlyAttendance.AdvanceId         = xAdvances.AdvancesId;
                                        xEmployeeMonthlyAttendance.IsAdvanceApproved = xAdvances.IsApproved;
                                        xEmployeeMonthlyAttendance.AdvanceAmount     = xAdvances.AdvanceAmount - xAdvances.RecoveredAmount;
                                    }

                                    await _dbContext.EmployeeMonthlyAttendance.AddAsync(xEmployeeMonthlyAttendance);

                                    await _dbContext.SaveChangesAsync();
                                }
                                else
                                {
                                    xEmployeeMonthlyAttendance.IsDeleted     = false;
                                    xEmployeeMonthlyAttendance.OfficeId      = request.EmployeeAttendance.First().OfficeId;
                                    xEmployeeMonthlyAttendance.EmployeeId    = xEmployeeApplyLeave.EmployeeId;
                                    xEmployeeMonthlyAttendance.Month         = request.EmployeeAttendance.First().Date.Month;
                                    xEmployeeMonthlyAttendance.Year          = request.EmployeeAttendance.First().Date.Year;
                                    xEmployeeMonthlyAttendance.AbsentHours   = xEmployeeMonthlyAttendance.AbsentHours == null ? 0 : xEmployeeMonthlyAttendance.AbsentHours;
                                    xEmployeeMonthlyAttendance.AbsentHours  += officeDailyHour;
                                    xEmployeeMonthlyAttendance.TotalDuration = officeMonthlyHour;

                                    Advances xAdvances = await _dbContext.Advances.Where(x => x.IsDeleted == false && x.IsApproved == true && x.IsDeducted == false &&
                                                                                         x.EmployeeId == xEmployeeApplyLeave.EmployeeId && x.OfficeId == request.EmployeeAttendance.First().OfficeId &&
                                                                                         x.AdvanceDate < DateTime.Now).FirstOrDefaultAsync();

                                    if (xAdvances != null)
                                    {
                                        xEmployeeMonthlyAttendance.AdvanceId         = xAdvances.AdvancesId;
                                        xEmployeeMonthlyAttendance.AdvanceAmount     = xAdvances.AdvanceAmount - xAdvances.RecoveredAmount;
                                        xEmployeeMonthlyAttendance.IsAdvanceApproved = xAdvances.IsApproved;
                                    }

                                    await _dbContext.EmployeeMonthlyAttendance.AddAsync(xEmployeeMonthlyAttendance);

                                    await _dbContext.SaveChangesAsync();
                                }
                            }
                            else//if Employee Monthly Attendance Record is present then add leave hours
                            {
                                if (xEmployeeApplyLeave.ApplyLeaveStatusId == (int)ApplyLeaveStatus.Approve)
                                {
                                    xEmployeeMonthlyAttendanceRecord.LeaveHours = officeDailyHour;
                                    _dbContext.EmployeeMonthlyAttendance.Update(xEmployeeMonthlyAttendanceRecord);
                                    await _dbContext.SaveChangesAsync();
                                }
                            }
                        }
                    }

                    response.StatusCode = StaticResource.successStatusCode;
                    response.Message    = "Success";
                }
                else
                {
                    response.StatusCode = StaticResource.failStatusCode;
                    response.Message    = "Office Hours Not Set";
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = ex.Message;
            }
            return(response);
        }