/// <summary> /// Tính toán thời gian làm việc thực tế của nhân viên. /// </summary> /// <param name="profile"></param> /// <param name="listWorkDay"></param> /// <param name="listAllShiftItem"></param> /// <param name="listRosterByProfile"></param> /// <param name="listPregnancy"></param> /// <param name="gradeByProfile"></param> /// <returns></returns> public static List <WorkDay> ComputeWorkDays(Hre_Profile profile, List <Att_Workday> listInOut, List <Cat_ShiftItem> listAllShiftItem, List <Att_Roster> listRosterByProfile, List <Att_Pregnancy> listPregnancy, Cat_GradeAttendance gradeCfgByProfile, string userLogin) { List <WorkDay> result = new List <WorkDay>(); if (profile != null && listInOut != null && listInOut.Count() > 0) { listInOut = listInOut.OrderBy(inout => inout.WorkDate).ThenBy(inout => inout.InTime1).ToList(); List <DateTime> listWorkDate = listInOut.Select(d => d.WorkDate.Date).Distinct().OrderBy(d => d).ToList(); if (listWorkDate != null && listWorkDate.Count() > 0) { Dictionary <DateTime, Cat_OrgStructure> listOrgStructure = Att_AttendanceLib.GetDailyLines(profile, listRosterByProfile, listWorkDate.FirstOrDefault(), listWorkDate.LastOrDefault()); foreach (DateTime workDate in listWorkDate) { Cat_OrgStructure orgLine = null; if (listOrgStructure.ContainsKey(workDate)) { //luôn luôn tồn tại 1 line orgLine = listOrgStructure[workDate]; } List <Att_Workday> listInOutByWorkDate = listInOut.Where(io => io.WorkDate.Date == workDate).ToList(); if (listInOutByWorkDate != null && listInOutByWorkDate.Count() > 0) { Att_Pregnancy pregnancyByWorkDay = listPregnancy.Where(d => d.DateStart != null && d.DateEnd != null && d.DateStart.Value <= workDate && d.DateEnd >= workDate).FirstOrDefault(); List <IGrouping <Cat_Shift, Att_Workday> > listInOutGroup = listInOutByWorkDate.Where(d => d.Cat_Shift != null).GroupBy(d => d.Cat_Shift).ToList(); foreach (IGrouping <Cat_Shift, Att_Workday> inOutGroup in listInOutGroup) { List <Cat_ShiftItem> listShiftItem = listAllShiftItem.Where(it => it.ShiftID == inOutGroup.Key.ID).ToList(); WorkDay workDayItem = CreateWorkDay(profile, inOutGroup.ToList(), orgLine, inOutGroup.Key, listShiftItem, pregnancyByWorkDay, gradeCfgByProfile, userLogin); if (workDayItem != null) { result.Add(workDayItem); } } } } } } return(result); }
public static void SendMailForgetTAMScanLog() { using (var context = new VnrHrmDataContext()) { BaseService _base = new BaseService(); string body2; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); try { string _typeTemplate = EnumDropDown.EmailType.E_SENDMAILFORGETTAMSCANLOG.ToString(); var template = unitOfWork.CreateQueryable <Sys_TemplateSendMail>(s => s.Type == _typeTemplate).FirstOrDefault(); if (template == null) { return; } if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday) { return; } //Common.HostPath = System.Configuration.ConfigurationSettings.AppSettings["URLHost"].ToString(); var date1 = DateTime.Now; if (date1.Day > 26) { date1 = date1.AddMonths(1); } DateTime dateStart = new DateTime(date1.Year, date1.Month, 26); dateStart = dateStart.AddMonths(-1); DateTime dateEnd = new DateTime(date1.Year, date1.Month, 25); if (dateEnd >= DateTime.Now.Date) { dateEnd = DateTime.Now.Date.AddDays(-1); } dateEnd = dateEnd.AddDays(1).AddMilliseconds(-1); var dayOffs = unitOfWork.CreateQueryable <Cat_DayOff>(s => dateStart <= s.DateOff && s.DateOff <= dateEnd).Select(s => s.DateOff).ToList(); var inOuts = unitOfWork.CreateQueryable <Att_Workday>(s => s.ShiftID != null && dateStart <= s.WorkDate && s.WorkDate <= dateEnd) .Select(s => new { s.InTime1, s.ProfileID, s.ShiftID, s.OutTime1, s.WorkDate }).OrderByDescending(s => s.InTime1).ToList(); string status = ProfileStatusSyn.E_HIRE.ToString(); var profiles = unitOfWork.CreateQueryable <Hre_Profile>(s => s.Email != null && s.StatusSyn == status && (s.DateQuit == null || s.DateQuit > DateTime.Now)).ToList(); string key = AppConfig.E_SERVER_MAIL.ToString(); //Sys_AppConfig AppConfigServerSendMail = unitOfWork.CreateQueryable<Sys_AppConfig>(s => s.Info == key).FirstOrDefault(); //string host = AppConfigServerSendMail.Value1; //string emailFrom = AppConfigServerSendMail.Value2; //string pass = AppConfigServerSendMail.Value3; //bool isSSL = false; //string subject = System.Configuration.ConfigurationManager.AppSettings["SendMailInOut"]; //string emailTitle = System.Configuration.ConfigurationManager.AppSettings["SendMailInOut"]; //if (AppConfigServerSendMail.Value6 != null) // bool.TryParse(AppConfigServerSendMail.Value6, out isSSL); //int port = AppConfigServerSendMail.Value30.AsInt(); var shifts = unitOfWork.CreateQueryable <Cat_Shift>().ToList(); string approve = RosterStatus.E_APPROVED.ToString(); var rosters = unitOfWork.CreateQueryable <Att_Roster>(s => s.MonShiftID != null && s.DateStart <= dateEnd && dateStart <= s.DateEnd && s.Status == approve).ToList <Att_Roster>(); List <Hre_Profile> profileHasIns = profiles; List <Att_Roster> lstRosterTypeGroup = new List <Att_Roster>(); List <Att_RosterGroup> lstRosterGroup = new List <Att_RosterGroup>(); var listHoliday = unitOfWork.CreateQueryable <Cat_DayOff>(s => dateStart <= s.DateOff && s.DateOff <= dateEnd).ToList <Cat_DayOff>(); string E_FIRST = PregnancyLeaveEarlyType.E_FIRST.ToString(); string E_LAST = PregnancyLeaveEarlyType.E_LAST.ToString(); var profileIDEarlys = unitOfWork.CreateQueryable <Att_Pregnancy>(s => (s.TypePregnancyEarly == E_FIRST || s.TypePregnancyEarly == E_LAST) && s.DateStart <= dateEnd && dateStart <= s.DateEnd).Select(s => s.ProfileID).ToList(); var approveKey = LeaveDayStatus.E_APPROVED.ToString(); var E_FULLSHIFT = LeaveDayDurationType.E_FULLSHIFT.ToString(); var leavdays = unitOfWork.CreateQueryable <Att_LeaveDay>(s => s.DateStart <= dateEnd && dateStart <= s.DateEnd && s.Status == approveKey) .Select(s => new { s.ProfileID, s.DateStart, s.DateEnd, s.DurationType }).ToList(); var leavdaysFullShifts = leavdays.Where(s => s.DurationType == E_FULLSHIFT).ToList(); //string contenFile = File.ReadAllText(UIController.GetPath("~/TemplateTemp/InOutTemplate.html")); string codeEmp = "85S"; if (codeEmp == null) { profiles = profiles.Where(s => s.CodeEmp == codeEmp).ToList(); } var profileIDs = profileHasIns.Select(s => s.ID).ToList(); var grades = unitOfWork.CreateQueryable <Att_Grade>(s => s.MonthStart <= dateEnd).OrderByDescending(d => d.MonthStart) .Select(d => new { d.ProfileID, d.MonthStart, d.GradeAttendanceID }).ToList(); List <Guid> gardeIDs = grades.Select(d => d.GradeAttendanceID.Value).ToList(); List <Cat_GradeAttendance> garCfgs = unitOfWork.CreateQueryable <Cat_GradeAttendance>(s => s.Code != "GENERAL DIRECTOR" && s.Code != "DIRECTOR").ToList(); garCfgs = garCfgs.Where(s => gardeIDs.Contains(s.ID)).ToList(); var workHistorys = unitOfWork.CreateQueryable <Hre_WorkHistory>(d => d.OrganizationStructureID != null && profileIDs.Contains(d.ProfileID)).ToList <Hre_WorkHistory>(); foreach (var profile in profileHasIns) { string body = string.Empty; var isSend = false; int index = 1; var profileLeavesFullShifts = leavdaysFullShifts.Where(s => s.ProfileID == profile.ID).ToList(); List <DateTime> dateLeaveFullShits = new List <DateTime>(); foreach (var leavesFullShift in profileLeavesFullShifts) { for (DateTime date = leavesFullShift.DateStart; date <= leavesFullShift.DateEnd; date = date.AddDays(1)) { dateLeaveFullShits.Add(date); } } for (DateTime date = dateStart; date <= dateEnd; date = date.AddDays(1)) { if (date < profile.DateHire) { continue; } if (date.DayOfWeek == DayOfWeek.Sunday || dayOffs.Contains(date.Date) || dateLeaveFullShits.Contains(date.Date)) { continue; } Att_AttendanceServices.GetRosterGroup(profileIDs, date, date, out lstRosterTypeGroup, out lstRosterGroup); bool isSendIn = true; bool isSendOut = true; var inout = inOuts.FirstOrDefault(s => s.ProfileID == profile.ID && s.WorkDate == date); var listRosterByProfile = rosters.Where(d => d.ProfileID == profile.ID).ToList(); var listWorkHistoryByProfile = workHistorys.Where(d => d.ProfileID == profile.ID).ToList(); var grade = grades.Where(d => d.ProfileID == profile.ID).OrderByDescending(d => d.MonthStart).FirstOrDefault(); Cat_GradeAttendance gradeCfg = garCfgs.FirstOrDefault(d => grade != null && d.ID == grade.GradeAttendanceID); if (gradeCfg != null && gradeCfg.AttendanceMethod != AttendanceMethod.E_FULL.ToString()) { var listRosterEntity = listRosterByProfile.Select(d => new Att_RosterEntity { ID = d.ID, ProfileID = d.ProfileID, RosterGroupName = d.RosterGroupName, Type = d.Type, Status = d.Status, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, MonShift2ID = d.MonShiftID, TueShift2ID = d.TueShift2ID, WedShift2ID = d.WedShift2ID, ThuShift2ID = d.ThuShift2ID, FriShift2ID = d.FriShift2ID, SatShift2ID = d.SatShift2ID, SunShift2ID = d.SunShift2ID }).ToList(); var listRosterGroupEntity = lstRosterGroup.Select(d => new Att_RosterGroupEntity { ID = d.ID, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, RosterGroupName = d.RosterGroupName }).ToList(); Dictionary <DateTime, Cat_Shift> listDailyShifts = Att_AttendanceLib.GetDailyShifts(profile != null ? profile.ID : Guid.Empty, date, date, listRosterEntity, listRosterGroupEntity, shifts); bool isWorkDate = Att_WorkDayHelper.IsWorkDay(date, gradeCfg, listDailyShifts, listHoliday); if (isWorkDate) { var catshift = shifts.FirstOrDefault(s => inout != null && s.ID == inout.ShiftID); if (catshift != null && inout != null) { double minInLate = 0; double minOutEarly = 0; var leaveDayProfiles = leavdays.Where(s => s.ProfileID == profile.ID).ToList(); var leaveDay = leaveDayProfiles.FirstOrDefault(s => s.DateStart.Date <= date && date <= s.DateEnd.Date); var dateBreakOut = new DateTime(date.Year, date.Month, date.Day, catshift.udCoBreakOut.Hour, catshift.udCoBreakOut.Minute, 0); var dateBreakIn = new DateTime(date.Year, date.Month, date.Day, catshift.udCoBreakIn.Hour, catshift.udCoBreakIn.Minute, 1); if (inout.InTime1 != null) //kiem tra vao tre { var dateInShift = new DateTime(date.Year, date.Month, date.Day, catshift.udCoIn.Hour, catshift.udCoIn.Minute, 0); // gio vao ca var dateInReal = new DateTime(date.Year, date.Month, date.Day, inout.InTime1.Value.Hour, inout.InTime1.Value.Minute, 0); // gio vao thuc te minInLate = dateInReal.Subtract(dateInShift).TotalMinutes; if (minInLate >= 30) // neu di tre >30 phut { if (leaveDay != null && leaveDay.DateStart != null) // neu dang ky nghi { if (leaveDay.DateStart <= dateInReal && dateInReal <= leaveDay.DateEnd) // neu gio bat dau nghi { dateInShift = leaveDay.DateEnd; } else if (dateInReal > leaveDay.DateStart && dateInReal > leaveDay.DateEnd)// neu gio vao > gio dang ky nghi { dateInShift = leaveDay.DateEnd; } if (dateBreakIn <= dateInReal)// neu gio ra trong vung nghi giua ca { dateInShift = dateBreakOut; } minInLate = dateInReal.Subtract(dateInShift).TotalMinutes; } } } if (inout.OutTime1 != null) //kiem tra ve som { var dateOutShift = new DateTime(date.Year, date.Month, date.Day, catshift.udCoOut.Hour, catshift.udCoOut.Minute, 0); // gio ra ca var dateOutReal = new DateTime(date.Year, date.Month, date.Day, inout.OutTime1.Value.Hour, inout.OutTime1.Value.Minute, 0); // gio vao thuc te minOutEarly = dateOutShift.Subtract(dateOutReal).TotalMinutes; if (minOutEarly >= 30) // neu ve som > 30 phut { if (dateBreakIn <= dateOutReal && dateOutReal <= dateBreakOut) // neu gio ra trong vung nghi giua ca { dateOutReal = dateBreakOut; } if (leaveDay != null && leaveDay.DateStart != null) // neu dang ky nghi { if (leaveDay.DateStart <= dateOutReal && dateOutReal <= leaveDay.DateEnd) // neu gio bat dau nghi { dateOutShift = leaveDay.DateStart; } else if (dateOutReal < leaveDay.DateStart && dateOutReal < leaveDay.DateEnd)// neu gio vao > gio dang ky nghi { dateOutShift = leaveDay.DateStart; } minOutEarly = dateOutShift.Subtract(dateOutReal).TotalMinutes; } } } string minstr = string.Empty; if (minInLate < 30 && inout.InTime1 != null) { isSendIn = false; } if (minOutEarly < 30 && inout.OutTime1 != null) { isSendOut = false; } if (inout.OutTime1 == null || inout.InTime1 == null) { isSendOut = true; } if (isSendIn || isSendOut) { isSend = true; } if (inout.InTime1 != null) { if (profileIDEarlys.Contains(profile.ID)) { if (minInLate < 90) { isSendIn = false; } else { minInLate -= 60; } } if (minInLate <= 0) { minInLate = 0; } if (minOutEarly <= 0) { minOutEarly = 0; } double sum = 0; if (minInLate >= 30) { sum += minInLate; } if (minOutEarly >= 30) { sum += minOutEarly; } minstr = sum.ToString(); } if (isSendOut || isSendIn) { if (body == string.Empty) { body = "<tr><td style=\"text-align: center\" class=\"dvtCellInfo\">"; } body += "<tr>"; body += "<td>"; body += index; body += "</td>"; body += "<td style=\"text-align: left\" class=\"dvtCellInfo\">"; body += profile.ProfileName; body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += profile.CodeEmp; body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += date.ToShortDateString(); body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += (inout.InTime1 != null ? inout.InTime1.Value.ToString("HH:mm") : string.Empty); body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += (inout.OutTime1 != null ? inout.OutTime1.Value.ToString("HH:mm") : string.Empty); body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += minstr; body += "</td>"; index++; } } else { isSend = true; body += "<tr>"; body += "<td>"; body += index; body += "</td>"; body += "<td style=\"text-align: left\" class=\"dvtCellInfo\">"; body += profile.ProfileName; body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += profile.CodeEmp; body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += date.ToShortDateString(); body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += "</td>"; body += "<td style=\"text-align: center\" class=\"dvtCellInfo\">"; body += "</td>"; index++; } body += "</tr>"; } } } string emailTo = profile.Email; if (isSend && !String.IsNullOrEmpty(emailTo)) { try { string[] strsParaKey = new[] { "[ProfileName]" , "[Table]" , "[Date]" }; string[] strsParaValues = new[] { profile.ProfileName, body, "" }; string body1 = ReplaceContentFile(template.Content, strsParaKey, strsParaValues); body2 = body1; _base.SendMail(template.Subject, emailTo, body1, null); //var body1 = att_OvertimeDAO.ReplaceContentFileContent(contenFile, strsParaKey, strsParaValues); //string result = Common.SendMail(host, port, subject, emailTitle, body1, emailFrom, emailTo, true, isSSL, pass, true); //if (!result.Contains("OK")) // Response.Write("<br/>" + "Error send mail: " + emailTo + " " + result); } catch { } //Response.Write("<br/>Sended: " + emailTo + " " + DateTime.Now.ToLongTimeString()); } } } catch (Exception ex) { } } }
private List <Att_AnnualDetail> AnalyzeAnnualSickPerProfile(Hre_ProfileEntity Profile, List <Cat_GradeAttendance> lstGradeCfg, List <Att_Grade> lstGrade, DateTime BeginYear, DateTime EndYear, List <Att_LeaveDay> lstLeaveSick, List <Att_AnnualDetail> lstAnnualDetailInDB, Att_AnnualLeave AnnualLeave, List <Att_Roster> lstRosterInYear, List <Att_RosterGroup> lstRosterGroup, List <Hre_WorkHistory> lstWorkHistory, List <Cat_DayOff> lstDayOff, bool IsFrom1To31, List <Cat_Shift> shifts) { List <Att_AnnualDetail> lstResult = new List <Att_AnnualDetail>(); double leaveBeginYearToMonth = 0; double leaveBeginYearToMonth_Init = 0; string E_ROSTERGROUP = RosterType.E_ROSTERGROUP.ToString(); List <Att_Roster> lstRoster_byProfile = lstRosterInYear.Where(m => m.ProfileID == Profile.ID && m.Type != E_ROSTERGROUP).ToList(); List <Att_Roster> lstRosterTypeGroup = lstRosterInYear.Where(m => m.ProfileID == Profile.ID && m.Type == E_ROSTERGROUP).ToList(); for (DateTime Month = BeginYear; Month <= EndYear; Month = Month.AddMonths(1)) { var gradeByProfileByTime = lstGrade.Where(m => m.ProfileID == Profile.ID && m.MonthStart <= Month).OrderByDescending(m => m.MonthStart).FirstOrDefault(); if (gradeByProfileByTime == null) { continue; } var GradeCfg = lstGradeCfg.Where(m => m.ID == gradeByProfileByTime.GradeAttendanceID).FirstOrDefault(); if (GradeCfg == null) { continue; } DateTime BeginMonth = Month; DateTime EndMonth = BeginMonth.AddMonths(1).AddMinutes(-1); if (!IsFrom1To31) { Att_AttendanceServices.GetRangeMaxMinGrade(new List <Cat_GradeAttendance>() { GradeCfg }, Month, out BeginMonth, out EndMonth); } double Availale = AnnualLeave == null ? 0 : AnnualLeave.InitSickValue; double LeaveInMonth = 0; List <Att_LeaveDay> lstSickInMonth = lstLeaveSick.Where(m => m.ProfileID == Profile.ID && m.DateStart <= EndMonth && m.DateEnd >= BeginMonth).ToList(); var listRosterEntity = lstRoster_byProfile.Select(d => new Att_RosterEntity { ID = d.ID, ProfileID = d.ProfileID, RosterGroupName = d.RosterGroupName, Type = d.Type, Status = d.Status, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, MonShift2ID = d.MonShiftID, TueShift2ID = d.TueShift2ID, WedShift2ID = d.WedShift2ID, ThuShift2ID = d.ThuShift2ID, FriShift2ID = d.FriShift2ID, SatShift2ID = d.SatShift2ID, SunShift2ID = d.SunShift2ID }).ToList(); var listRosterGroupEntity = lstRosterGroup.Select(d => new Att_RosterGroupEntity { ID = d.ID, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, RosterGroupName = d.RosterGroupName }).ToList(); foreach (var item in lstSickInMonth) { if (item.DateStart >= BeginMonth && item.DateEnd <= EndMonth) { //Chi cần lấy TotalDuration hoặc lấy giờ rồi lấy ca if (item.DurationType == LeaveDayDurationType.E_FULLSHIFT.ToString()) { LeaveInMonth += item.TotalDuration ?? 0; } else { //Lấy ra ca làm việc DateTime DateBeginLeave = item.DateStart.Date; DateTime DateEndLeave = item.DateEnd.Date; Dictionary <DateTime, Cat_Shift> dicShift = Att_AttendanceLib.GetDailyShifts(Profile != null ? Profile.ID : Guid.Empty, DateBeginLeave, DateEndLeave, listRosterEntity, listRosterGroupEntity, shifts); if (dicShift != null && dicShift.ContainsKey(DateBeginLeave)) { Cat_Shift shift = dicShift[DateBeginLeave]; if (shift != null) { double HourWorkday = 8; if (shift.WorkHours != null && shift.WorkHours != 0) { HourWorkday = shift.WorkHours ?? 8; } LeaveInMonth += item.Duration / HourWorkday; } } } } else { DateTime DateBegin = BeginMonth > item.DateStart ? BeginMonth : item.DateStart; DateTime DateEnd = EndMonth < item.DateEnd ? BeginMonth : item.DateEnd; Dictionary <DateTime, Cat_Shift> dicShift = Att_AttendanceLib.GetDailyShifts(Profile != null ? Profile.ID : Guid.Empty, DateBegin, DateEnd, listRosterEntity, listRosterGroupEntity, shifts); for (DateTime dateCheck = DateBegin; dateCheck <= DateEnd; dateCheck = dateCheck.AddDays(1)) { if (Att_WorkDayHelper.IsWorkDay(dateCheck, GradeCfg, dicShift, lstDayOff) && !lstDayOff.Any(m => m.DateOff == dateCheck)) { LeaveInMonth++; } } } } Att_AnnualDetail AnnualDetail = lstAnnualDetailInDB.Where(m => m.ProfileID == Profile.ID && m.MonthYear == Month).FirstOrDefault(); if (AnnualDetail == null) { AnnualDetail = new Att_AnnualDetail(); } if (Profile.DateQuit != null) { DateTime MonthQuit = Profile.DateQuit.Value.Day >= 15 ? Profile.DateQuit.Value.AddMonths(1) : Profile.DateQuit.Value; MonthQuit = new DateTime(MonthQuit.Year, MonthQuit.Month, 1); if (Month >= MonthQuit) { AnnualDetail.IsDelete = true; } } AnnualDetail.Available = Availale; AnnualDetail.InitAvailable = AnnualLeave == null ? 0 : AnnualLeave.InitSickValue; AnnualDetail.ProfileID = Profile.ID; AnnualDetail.Year = BeginYear.Year; AnnualDetail.MonthYear = Month; AnnualDetail.MonthBeginInYear = MonthStartAnl; //todo: Phần tử này để trong cấu hình chung if (AnnualLeave != null && AnnualLeave.MonthResetAnlOfBeforeYear != null) { AnnualDetail.MonthResetInitAvailable = AnnualLeave.MonthResetAnlOfBeforeYear.Value;//todo: MonthReset Này để trong chế độ lương } else { AnnualDetail.MonthResetInitAvailable = 12;//todo: MonthReset Này để trong chế độ lương } AnnualDetail.MonthStartProfile = AnnualLeave == null ? 1 : AnnualLeave.MonthStart; if (AnnualDetail.MonthResetInitAvailable != null && AnnualDetail.MonthResetInitAvailable != 12 && AnnualDetail.Year != null) { DateTime MonthReset = new DateTime(AnnualDetail.Year.Value, AnnualDetail.MonthResetInitAvailable.Value, 1); AnnualDetail.IsHaveResetInitAvailable = true; if (Month <= MonthReset) //Trong những tháng có { double delta = leaveBeginYearToMonth_Init + LeaveInMonth - AnnualDetail.InitAvailable.Value; if (delta > 0) { AnnualDetail.TotalLeaveBefFromInitAvailable = leaveBeginYearToMonth_Init; AnnualDetail.LeaveInMonthFromInitAvailable = LeaveInMonth - delta; AnnualDetail.LeaveInMonth = delta; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; leaveBeginYearToMonth += delta; leaveBeginYearToMonth_Init += LeaveInMonth - delta; } else { AnnualDetail.TotalLeaveBefFromInitAvailable = leaveBeginYearToMonth_Init; AnnualDetail.LeaveInMonthFromInitAvailable = LeaveInMonth; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = 0; leaveBeginYearToMonth_Init += LeaveInMonth; } } else //sau những tháng kho có { AnnualDetail.InitAvailable = 0; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = LeaveInMonth; leaveBeginYearToMonth += LeaveInMonth; } } else //Bình thường { AnnualDetail.IsHaveResetInitAvailable = false; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = LeaveInMonth; leaveBeginYearToMonth += LeaveInMonth; } double remain = (AnnualDetail.Available ?? 0) + (AnnualDetail.InitAvailable ?? 0) - (AnnualDetail.TotalLeaveBefFromInitAvailable ?? 0) - (AnnualDetail.LeaveInMonthFromInitAvailable ?? 0) - (AnnualDetail.TotalLeaveBef ?? 0) - (AnnualDetail.LeaveInMonth ?? 0); AnnualDetail.Remain = remain; AnnualDetail.Type = AnnualLeaveDetailType.E_SICK_LEAVE.ToString(); lstResult.Add(AnnualDetail); } return(lstResult); }
/// <summary> /// Hàm tính toán Phép năm dành cho từng nhân viên /// </summary> /// <param name="Profile">Nhân Viên</param> /// <param name="lstGradeCfg">Ds Chế Độ lương</param> /// <param name="lstGrade">Ds Grade của Nhân viên</param> /// <param name="BeginYear">Ngày bắt đầu của năm</param> /// <param name="EndYear">Ngày Kết Thúc Của Năm</param> /// <param name="lstLeaveAnl">Ds nghỉ phép năm</param> /// <param name="lstAnnualDetailInDB">Ds AnnualDetail trong DB</param> /// <param name="AnnualLeave">AnnualLeave</param> /// <param name="lstRosterInYear">Ds Roster</param> /// <param name="lstRosterGroup">Ds RosterGroup</param> /// <param name="lstWorkHistory">Ds WorkingHistory</param> /// <param name="lstDayOff">Ds Ngày Nghỉ Lễ </param> /// <returns></returns> private List <Att_AnnualDetail> AnalyzeAnnualDetailPerProfile(Hre_ProfileMultiField Profile, List <Cat_GradeAttendance> lstGradeCfg, List <Att_Grade> lstGrade, DateTime BeginYear, DateTime EndYear, List <Att_LeaveDayInfo> lstLeaveAnl, List <Att_AnnualDetail> lstAnnualDetailInDB, Att_AnnualLeave AnnualLeave, List <Att_RosterInfo> lstRosterInYearIn, List <Att_RosterGroup> lstRosterGroup, List <Cat_DayOff> lstDayOff, bool IsFrom1To31, Cat_Position Position, List <Sys_AllSetting> lstAllSetting, List <Hre_HDTJob> lstHDTJob, out List <Att_AnnualDetail> AnalyzeAnnualDetailPerProfile_Update, string userLogin) { AnalyzeAnnualDetailPerProfile_Update = new List <Att_AnnualDetail>(); using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoAtt_Roster = new CustomBaseRepository <Att_Roster>(unitOfWork); var repoCat_Shift = new Cat_ShiftRepository(unitOfWork); var shifts = repoCat_Shift.FindBy(s => s.IsDelete == null).ToList(); var lstRosterID = lstRosterInYearIn.Select(s => s.ID).ToList(); var lstRosterInYear = repoAtt_Roster.FindBy(s => lstRosterID.Contains(s.ID)).ToList(); List <Att_AnnualDetail> lstResult = new List <Att_AnnualDetail>(); double leaveBeginYearToMonth = 0; double leaveBeginYearToMonth_Init = 0; string E_ROSTERGROUP = RosterType.E_ROSTERGROUP.ToString(); List <Att_Roster> lstRoster_byProfile = lstRosterInYear.Where(m => m.ProfileID == Profile.ID && m.Type != E_ROSTERGROUP).ToList(); List <Att_Roster> lstRosterTypeGroup = lstRosterInYear.Where(m => m.ProfileID == Profile.ID && m.Type == E_ROSTERGROUP).ToList(); for (DateTime Month = BeginYear; Month <= EndYear; Month = Month.AddMonths(1)) { var gradeByProfileByTime = lstGrade.Where(m => m.ProfileID == Profile.ID && m.MonthStart <= Month).OrderByDescending(m => m.MonthStart).FirstOrDefault(); if (gradeByProfileByTime == null) { continue; } var GradeCfg = lstGradeCfg.Where(m => m.ID == gradeByProfileByTime.GradeAttendanceID).FirstOrDefault(); if (GradeCfg == null) { continue; } DateTime BeginMonth = Month; DateTime EndMonth = BeginMonth.AddMonths(1).AddMinutes(-1); if (!IsFrom1To31) { Att_AttendanceServices.GetRangeMaxMinGrade(new List <Cat_GradeAttendance>() { GradeCfg }, Month, out BeginMonth, out EndMonth); } int AnnualLeaveMonthStart = AnnualLeave != null ? AnnualLeave.MonthStart : 1; double AnnualLeaveInitAnlValue = AnnualLeave != null ? AnnualLeave.InitAnlValue : 0; double Availale = Att_AttendanceLib.GetAnnualLeaveReceive(BeginYear.Year, Month, GradeCfg, Profile.DateHire, Profile.DateEndProbation, Profile.DateQuit, AnnualLeaveMonthStart, AnnualLeaveInitAnlValue, Position, Profile, lstLeaveAnl, lstAllSetting, lstHDTJob, lstDayOff.Select(m => m.DateOff).ToList(), lstLeaveAnl, userLogin); double LeaveInMonth = 0; List <Att_LeaveDayInfo> lstAnlInMonth = lstLeaveAnl.Where(m => m.ProfileID == Profile.ID && m.DateStart <= EndMonth && m.DateEnd >= BeginMonth).ToList(); foreach (var item in lstAnlInMonth) { var listRosterEntity = lstRoster_byProfile.Select(d => new Att_RosterEntity { ID = d.ID, ProfileID = d.ProfileID, RosterGroupName = d.RosterGroupName, Type = d.Type, Status = d.Status, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, MonShift2ID = d.MonShiftID, TueShift2ID = d.TueShift2ID, WedShift2ID = d.WedShift2ID, ThuShift2ID = d.ThuShift2ID, FriShift2ID = d.FriShift2ID, SatShift2ID = d.SatShift2ID, SunShift2ID = d.SunShift2ID }).ToList(); var listRosterGroupEntity = lstRosterGroup.Select(d => new Att_RosterGroupEntity { ID = d.ID, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, RosterGroupName = d.RosterGroupName }).ToList(); if (item.DateStart >= BeginMonth && item.DateEnd <= EndMonth) { //Chi cần lấy TotalDuration hoặc lấy giờ rồi lấy ca if (item.DurationType == LeaveDayDurationType.E_FULLSHIFT.ToString()) { LeaveInMonth += item.LeaveDays ?? 0; } else { //Lấy ra ca làm việc DateTime DateBeginLeave = item.DateStart.Date; DateTime DateEndLeave = item.DateEnd.Date; Dictionary <DateTime, Cat_Shift> dicShift = Att_AttendanceLib.GetDailyShifts(Profile != null ? Profile.ID : Guid.Empty, DateBeginLeave, DateEndLeave, listRosterEntity, listRosterGroupEntity, shifts); if (dicShift != null && dicShift.ContainsKey(DateBeginLeave)) { Cat_Shift shift = dicShift[DateBeginLeave]; if (shift != null) { double HourWorkday = shift.udStandardWorkHours > 0 ? shift.udStandardWorkHours : 8.0; LeaveInMonth += item.LeaveHours.Value / HourWorkday; } } } } else { DateTime DateBegin = BeginMonth > item.DateStart ? BeginMonth : item.DateStart; DateTime DateEnd = EndMonth < item.DateEnd ? BeginMonth : item.DateEnd; Dictionary <DateTime, Cat_Shift> dicShift = Att_AttendanceLib.GetDailyShifts(Profile != null ? Profile.ID : Guid.Empty, DateBegin, DateEnd, listRosterEntity, listRosterGroupEntity, shifts); for (DateTime dateCheck = DateBegin; dateCheck <= DateEnd; dateCheck = dateCheck.AddDays(1)) { if (Att_WorkDayHelper.IsWorkDay(dateCheck, GradeCfg, dicShift, lstDayOff) && !lstDayOff.Any(m => m.DateOff == dateCheck)) { LeaveInMonth++; } } } } Att_AnnualDetail AnnualDetail = lstAnnualDetailInDB.Where(m => m.ProfileID == Profile.ID && m.MonthYear == Month).FirstOrDefault(); bool isNew = false; if (AnnualDetail == null) { isNew = true; AnnualDetail = new Att_AnnualDetail(); } else { AnalyzeAnnualDetailPerProfile_Update.Add(AnnualDetail); } if (Profile.DateQuit != null) { DateTime MonthQuit = Profile.DateQuit.Value.Day >= 15 ? Profile.DateQuit.Value.AddMonths(1) : Profile.DateQuit.Value; MonthQuit = new DateTime(MonthQuit.Year, MonthQuit.Month, 1); if (Month >= MonthQuit) { AnnualDetail.IsDelete = true; } } AnnualDetail.Available = Availale; AnnualDetail.InitAvailable = AnnualLeave == null ? 0 : AnnualLeave.InitAnlValue; AnnualDetail.ProfileID = Profile.ID; //AnnualDetail.Hre_Profile = Profile.CopyData<Hre_Profile>(); AnnualDetail.Year = BeginYear.Year; AnnualDetail.MonthYear = Month; AnnualDetail.MonthBeginInYear = MonthStartAnl; //todo: Phần tử này để trong cấu hình chung if (AnnualLeave != null && AnnualLeave.MonthResetAnlOfBeforeYear != null) { AnnualDetail.MonthResetInitAvailable = AnnualLeave.MonthResetAnlOfBeforeYear.Value;//todo: MonthReset Này để trong chế độ lương } else { AnnualDetail.MonthResetInitAvailable = 12;//todo: MonthReset Này để trong chế độ lương } AnnualDetail.MonthStartProfile = AnnualLeave == null ? 1 : AnnualLeave.MonthStart; if (AnnualDetail.MonthResetInitAvailable != null && AnnualDetail.MonthResetInitAvailable != 12 && AnnualDetail.Year != null) { DateTime MonthReset = new DateTime(AnnualDetail.Year.Value, AnnualDetail.MonthResetInitAvailable.Value, 1); AnnualDetail.IsHaveResetInitAvailable = true; if (Month <= MonthReset) //Trong những tháng có { double delta = leaveBeginYearToMonth_Init + LeaveInMonth - AnnualDetail.InitAvailable.Value; if (delta > 0) { AnnualDetail.TotalLeaveBefFromInitAvailable = leaveBeginYearToMonth_Init; AnnualDetail.LeaveInMonthFromInitAvailable = LeaveInMonth - delta; AnnualDetail.LeaveInMonth = delta; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; leaveBeginYearToMonth += delta; leaveBeginYearToMonth_Init += LeaveInMonth - delta; } else { AnnualDetail.TotalLeaveBefFromInitAvailable = leaveBeginYearToMonth_Init; AnnualDetail.LeaveInMonthFromInitAvailable = LeaveInMonth; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = 0; leaveBeginYearToMonth_Init += LeaveInMonth; } } else //sau những tháng kho có { AnnualDetail.InitAvailable = 0; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = LeaveInMonth; leaveBeginYearToMonth += LeaveInMonth; } } else //Bình thường { AnnualDetail.IsHaveResetInitAvailable = false; AnnualDetail.TotalLeaveBef = leaveBeginYearToMonth; AnnualDetail.LeaveInMonth = LeaveInMonth; leaveBeginYearToMonth += LeaveInMonth; } double remain = (AnnualDetail.Available ?? 0) + (AnnualDetail.InitAvailable ?? 0) - (AnnualDetail.TotalLeaveBefFromInitAvailable ?? 0) - (AnnualDetail.LeaveInMonthFromInitAvailable ?? 0) - (AnnualDetail.TotalLeaveBef ?? 0) - (AnnualDetail.LeaveInMonth ?? 0); AnnualDetail.Remain = remain; AnnualDetail.Type = AnnualLeaveDetailType.E_ANNUAL_LEAVE.ToString(); if (isNew) { lstResult.Add(AnnualDetail); } } return(lstResult); } }