public int CreateApplyLeave(LeaveOfficeApply newApply, bool isActiveApply, string loginName, string leaveName) { using (var trans = _DbContext.Database.BeginTransaction()){ var count = 0; try { _DbContext.leaveofficeapplys.Add(newApply); count = _DbContext.SaveChanges(); if (count == 1) { if (leaveName == specialName) { refreshEmployeeAnnualLeave(newApply, 2); //減時數 } else if (leaveName == otRestName) { refreshEmployeeOvertimeRest(newApply, 2); } if (isActiveApply) { systemSendMessage(loginName, newApply.accountID, "leave"); } else { punchLogWithTakeLeave(newApply); } } trans.Commit(); } catch (Exception ex) { count = catchErrorProcess(ex, count); } return(count); } }
public bool chkEmployeeAnnualLeave(LeaveOfficeApply context, int leaveStatus, string leaveName) { if (leaveStatus != 2) //0:無動作 1:加時數 2:減時數 { return(true); } else //若減時數 需確認還有多餘的時可以以扣 { var applyHours = getApplyHours(context); //申請請假時數 var haveHours = 0.0f; //剩餘請假時數 if (leaveName == specialName) //特休 { var query = _DbContext.employeeannualleaves .Where(b => b.employeeID == context.accountID && definePara.dtNow() < b.deadLine); foreach (var spDays in query) { haveHours += spDays.remainHours; } } else if (leaveName == otRestName) //補休 { var query = _DbContext.overTimeRest.FirstOrDefault(b => b.accountID == context.accountID); haveHours = query == null? haveHours : query.canRestTime / 60.0f; } else { return(false); } return(haveHours >= applyHours? true : false); } }
public bool ChkLeaveHadSameTimeData(LeaveOfficeApply data) { var query = _DbContext.leaveofficeapplys.FirstOrDefault( b => b.accountID == data.accountID && b.startTime == data.startTime && b.endTime == data.endTime && b.applyStatus < 2); return(query == null? false : true); }
public void refreshEmployeeOvertimeRest(LeaveOfficeApply context, int status) { var applyHours = getApplyHours(context); var tmpOvertimeApply = new OvertimeApply { accountID = context.accountID, timeLength = Convert.ToInt32(applyHours * 60), lastOperaAccID = context.lastOperaAccID, }; refreshOvertimeRest(tmpOvertimeApply, status); }
public void IsAgreeApplyLeave_convertToDic(ref Dictionary <string, string> Dic, LeaveOfficeApply leave, String leaveName) { if (leave == null) { leave = new LeaveOfficeApply(); } Dic.Add("leaveName", leaveName); Dic.Add("applyStatus", leave.applyStatus == 0? "待審核" : leave.applyStatus == 1? "通過" : "未通過"); Dic.Add("applyDate", leave.createTime.ToString("yyyy-MM-dd HH:mm")); Dic.Add("startTime", leave.startTime.ToString("yyyy-MM-dd HH:mm")); Dic.Add("endTime", leave.endTime.ToString("yyyy-MM-dd HH:mm")); }
public void refreshEmployeeAnnualLeave(LeaveOfficeApply context, int leaveStatus) { var DayToHour = definePara.dayToHour(); var applyHours = getApplyHours(context); var spLeaves = _DbContext.employeeannualleaves .Where(b => b.employeeID == context.accountID && definePara.dtNow() < b.deadLine) .OrderBy(b => b.deadLine).ToList(); if (leaveStatus == 2) //減時數 { for (int i = 0; i < spLeaves.Count; i++) { var remainHours = spLeaves[i].remainHours; if (remainHours >= applyHours) { remainHours -= applyHours; applyHours = 0; } else { applyHours -= remainHours; remainHours = 0; } spLeaves[i].remainHours = remainHours; spLeaves[i].updateTime = definePara.dtNow(); } } else if (leaveStatus == 1) //加回時數 { for (int i = spLeaves.Count - 1; i >= 0; i--) { var remainHours = spLeaves[i].remainHours; if ((remainHours + applyHours) > (spLeaves[i].specialDays) * DayToHour) { applyHours -= ((spLeaves[i].specialDays) * DayToHour - remainHours); remainHours = (spLeaves[i].specialDays) * DayToHour; } else { remainHours += applyHours; applyHours = 0; } spLeaves[i].remainHours = remainHours; spLeaves[i].updateTime = definePara.dtNow(); } } _DbContext.SaveChanges(); }
public float getApplyHours(LeaveOfficeApply context) { var DayToHour = definePara.dayToHour(); var applyHours = 0.0f; switch (context.unit) { case 1: applyHours = (context.unitVal) * DayToHour; break; case 2: applyHours = (context.unitVal) * (DayToHour / 2.0f); break; case 3: applyHours = (context.unitVal); break; } return(applyHours); }
public bool ChkApplyLeaveData(LeaveOfficeApply data, bool isActiveApply) { var query = _DbContext.leavenames.FirstOrDefault(b => b.ID == data.leaveID); if (query == null || (data.unit > query.timeUnit)) { return(false); } var result = true; result = data.unitVal <= 0 ? false : result; result = (data.unit == 1 && (data.unitVal - (int)data.unitVal) != 0)? false : result; //單位天 值不可小數 result = (data.unit == 2 && data.unitVal > 2)? false : result; //單位半天 1:上半天 2:下半天 if (data.unit == 3) //單位小時 { var halfVal = query.halfVal; //可否0.5小時 result = (!halfVal && (data.unitVal - (int)data.unitVal) != 0)? false : result; //不可小數 result = (halfVal && (data.unitVal % 0.5) != 0)? false : result; //值不為0.5倍數 } result = (!isActiveApply && data.unit == 1 && data.unitVal != 1)? false : result; //被請假 只能請當天(1天) return(result); }
public int UpdateApplyLeave(LeaveOfficeApply updateApply) //暫無使用到 { int count = 0; var context = _DbContext.leaveofficeapplys.FirstOrDefault(b => b.ID == updateApply.ID); if (context != null && context.applyStatus == 0) { context.leaveID = updateApply.leaveID; context.note = updateApply.note; context.startTime = updateApply.startTime; context.endTime = updateApply.endTime; context.unitVal = updateApply.unitVal; context.unit = updateApply.unit; context.lastOperaAccID = updateApply.lastOperaAccID; context.updateTime = updateApply.updateTime; count = _DbContext.SaveChanges(); } if (count == 1) { //之後有使用到此更新功能 須在這新增更新後的特休刷新處理 } return(count); }
public int getStatusCode(PunchCardNeedParam logParam, LeaveOfficeApply leave = null) { WorkDateTime wt = logParam.workDateTime; PunchCardLog processLog = logParam.punchCardLog; var fullDayRest = false; //請整天假 var statusCode = 0; //statusCode: 0x01:正常 0x02:遲到 0x04:早退 0x08:加班 0x10:缺卡 0x20:曠職 0x40:請假 if (wt.workAllTime) { return(psCode.normal); } var elasticityMin = wt.elasticityMin; //彈性時間 List <LeaveOfficeApply> thisLeave = new List <LeaveOfficeApply>(); //if(leave == null){ thisLeave = Repository.GetThisTakeLeave(processLog.accountID, wt.sWorkDt, wt.eWorkDt); //}else{ // thisLeave.Add(leave); //} if (thisLeave.Count > 0) { var allWorkLen = (wt.eWorkDt - wt.sWorkDt).TotalMinutes; var restLen = (wt.eRestDt - wt.sRestDt).TotalMinutes; var workLen = allWorkLen - restLen; var sum = 0.0; foreach (var tmp in thisLeave) { sum += (tmp.endTime - tmp.startTime).TotalMinutes; fullDayRest = (tmp.startTime <= wt.sWorkDt && tmp.endTime >= wt.eWorkDt)? true : false; } fullDayRest = sum >= workLen? true : fullDayRest; statusCode |= psCode.takeLeave; } if (processLog.onlineTime.Year == 1 && processLog.offlineTime.Year == 1) { if (definePara.dtNow() >= wt.ePunchDT) { statusCode = fullDayRest? statusCode : (statusCode | psCode.noWork); } else { if (definePara.dtNow() >= wt.eWorkDt) { statusCode = fullDayRest? statusCode : (statusCode | psCode.hadLost); } } } else if (processLog.onlineTime.Year > 1 && processLog.offlineTime.Year > 1) { var newStartWt = wt.sWorkDt.AddMinutes(elasticityMin + 1); //+1因遲到以時分為主 09:00:59 也不算遲到 statusCode = processLog.onlineTime >= newStartWt? (statusCode | psCode.lateIn) : statusCode; var timeLen = (int)((processLog.onlineTime - wt.sWorkDt).TotalMinutes); timeLen = (timeLen <0 || timeLen> elasticityMin)? 0: timeLen; //上班打卡超過彈性時間 下班就已下班時間為準 statusCode = processLog.offlineTime < wt.eWorkDt.AddMinutes(timeLen)? (statusCode | psCode.earlyOut):statusCode; if (statusCode == psCode.takeLeave && (statusCode & psCode.lateIn) == 0 && (statusCode & psCode.earlyOut) == 0) { statusCode = (statusCode | psCode.normal); } } else { if (processLog.onlineTime.Year > 1) //只有填上班 { var newStartWt = wt.sWorkDt.AddMinutes(elasticityMin + 1); statusCode = processLog.onlineTime >= newStartWt? (statusCode | psCode.lateIn) : statusCode; statusCode = definePara.dtNow() >= wt.ePunchDT ? (statusCode | psCode.hadLost) : statusCode; //打不到下班卡了 } else //只有填下班 { statusCode |= psCode.hadLost; statusCode = processLog.offlineTime < wt.eWorkDt ? (statusCode | psCode.earlyOut) : statusCode; } } return(statusCode == 0? (psCode.normal) : statusCode); }
public dynamic addUpApplyLeave(LeaveOfficeApply data) { var applyData = new LeaveOfficeApply() { ID = data.ID, accountID = data.accountID, principalID = data.principalID, leaveID = data.leaveID, optionType = data.optionType, note = data.note, startTime = data.startTime, endTime = data.endTime, applyStatus = data.applyStatus, unitVal = data.unitVal, unit = data.unit, lastOperaAccID = data.lastOperaAccID, createTime = data.createTime, updateTime = data.updateTime }; var isActiveApply = data.accountID == 0? true : false; //是否個人申請請假 還是主管幫申請 if (!isActiveApply) //被申請 { applyData.lastOperaAccID = applyData.principalID = (int)loginID; applyData.applyStatus = 1; //申請狀態直接通過 } else { applyData.accountID = applyData.lastOperaAccID = (int)loginID; applyData.principalID = (int)principalID; if (!Repository.ChkHasPrincipal(applyData.accountID)) //沒有代理人不可請假 { return(cudAjaxResult(MessageCode.No_Principal)); } } if (!Repository.ChkApplyLeaveData(applyData, isActiveApply)) { return(cudAjaxResult(MessageCode.LeaveData_Illegal)); } applyData.endTime = getLeaveEndTime(applyData); var isOverEndWorkTime = (applyData.note == "note_overEndWorkTime"? true : false); applyData.note = data.note; //須改回原本的note if (!isActiveApply) { applyData.note += $"({loginName}主管申請)"; //被主管申請的請假進行備註 } if (Repository.ChkLeaveHadSameTimeData(applyData)) { return(cudAjaxResult(MessageCode.HadSameLeave)); } var leaveName = Repository.getApplyLeaveName(applyData.leaveID); if (leaveName == definePara.annualName() || leaveName == definePara.otRestName()) { if (!Repository.chkEmployeeAnnualLeave(applyData, 2, leaveName)) //特休/補休 不足不可請假 { return(cudAjaxResult(MessageCode.CanLeaveTime_NotEnough)); } } int result = 0; if (applyData.ID == 0) { applyData.createTime = definePara.dtNow(); result = Repository.CreateApplyLeave(applyData, isActiveApply, loginName, leaveName); } else //編輯更新 未使用 //applyData.updateTime = definePara.dtNow(); //result = Repository.UpdateApplyLeave(applyData); { } if (isOverEndWorkTime && applyData.unit == 3 && result == 1) { return(cudAjaxResult(MessageCode.OverEndWorkTime)); } return(cudAjaxResult(result)); }
public DateTime getLeaveEndTime(LeaveOfficeApply data) //計算請假結束時間 { var workTime = RepositoryPunch.GetThisWorkTime(data.accountID); WorkDateTime wdt = punchCardFn.workTimeProcess(workTime); var offsetDay = (data.startTime.Date - wdt.sWorkDt.Date).TotalDays; var workLengthMinute = (wdt.eWorkDt - wdt.sWorkDt).TotalMinutes; workLengthMinute = workLengthMinute < 0? workLengthMinute + 24 * 60 : workLengthMinute; var restLengthMinute = (wdt.eRestDt - wdt.sRestDt).TotalMinutes; restLengthMinute = restLengthMinute < 0? restLengthMinute + 24 * 60 : restLengthMinute; var useHalfVal = data.unit == 3? Repository.IsUseHourHalfVal(data.leaveID) : false; var myDepartClass = (Repository.GetMyDepartClass(data.accountID)).Split(","); var totalMin = 0.0; if (data.unit == 3) //小時 { totalMin = data.unitVal * 60; } else if (data.unit == 2) //半天 { data.startTime = wdt.sWorkDt.AddDays(offsetDay); totalMin = (workLengthMinute - restLengthMinute) * 0.5; //半天時長 if (data.unitVal == 2) //1=上半天 2=下半天 { for (int i = 1; i <= totalMin; i++) { data.startTime = data.startTime.AddMinutes(1); if (data.startTime.Hour == wdt.sRestDt.Hour && data.startTime.Minute == wdt.sRestDt.Minute) { data.startTime = data.startTime.AddMinutes(restLengthMinute); } } } data.unitVal = 1; //須改為1 一個半天 } else //天 { data.startTime = wdt.sWorkDt.AddDays(offsetDay); totalMin = (workLengthMinute - restLengthMinute) * data.unitVal; } var eTime = data.startTime; var iTmp = 1; for (; iTmp <= totalMin; iTmp++) { if (eTime.Hour == wdt.sRestDt.Hour && eTime.Minute == wdt.sRestDt.Minute) { eTime = eTime.AddMinutes(restLengthMinute); //跳過休息時間 } if (eTime.Hour == wdt.eWorkDt.Hour && eTime.Minute == wdt.eWorkDt.Minute) { //若接下來時間超過下班時間 跳到隔天的上班時間繼續加 eTime = eTime.AddDays(1).AddMinutes(-workLengthMinute); var flag = wdt.type == 1? false : true; //1:排休制(都要上班) 0:固定制 while (flag) //判斷是否休假日 有就略過 { var spDate = RepositoryPunch.GetThisSpecialDate(eTime); //是否有特殊日期 if (spDate == null) //DayOfWeek.ToString("d") 轉換成星期幾 { if ((eTime.DayOfWeek.ToString("d") == "0" || eTime.DayOfWeek.ToString("d") == "6")) { eTime = eTime.AddDays(1); } else { flag = false; } } else //1:休假 2:上班 { if ((spDate.status == 2 && Array.IndexOf(myDepartClass, spDate.departClass) > -1) || (spDate.status == 1 && Array.IndexOf(myDepartClass, spDate.departClass) == -1) ) { flag = false; } else { eTime = eTime.AddDays(1); } } } } eTime = eTime.AddMinutes(1); if (eTime.Hour == wdt.eWorkDt.Hour && eTime.Minute == wdt.eWorkDt.Minute && data.unit == 3) { break; } }//for(; iTmp<=totalMin; iTmp++) if (data.unit == 3) //小時 { if (iTmp < totalMin) //若超過下班時間 以下班時間為底 { var hour = iTmp / 60; //unitVal為實際請假時數(開始時間到下班時間) data.unitVal = (float)(iTmp % 60 > 30? (++hour) : iTmp % 60 > 0? hour + 0.5 : hour); data.note = "note_overEndWorkTime"; //用此判斷是否有超過下班時間 } if (useHalfVal) //可以有0.5小時 { var dot = data.unitVal - ((int)data.unitVal); if (dot > 0) { dot = (float)(dot > 0.5 ? 1 : 0.5); data.unitVal = (int)data.unitVal + dot; } } else { data.unitVal = (float)(Math.Ceiling(data.unitVal)); //無條件進位 } } return(eTime); }
public void punchLogWithTakeLeave(LeaveOfficeApply restLog) { var restST = restLog.startTime; var restET = restLog.endTime; var wtLength = 720; //工作時間長度 unit:minute //因工作時間可能跨日 不能直接用restST.Date EX: 2300-0800 休下半天0400-0800 var workDate = restST.Date; //工作天日期(logDate) var wtRule = (from a in _DbContext.accounts join b in _DbContext.worktimerules on a.timeRuleID equals b.ID where a.ID == restLog.accountID select b).FirstOrDefault(); if (wtRule != null) { if (wtRule.endTime < wtRule.startTime) { wtLength = (int)(wtRule.endTime.Add(new TimeSpan(24, 0, 0)) - wtRule.startTime).TotalMinutes; } else { wtLength = (int)(wtRule.endTime - wtRule.startTime).TotalMinutes; } var restSTime = new TimeSpan(restST.Hour, restST.Minute, restST.Second); workDate = (wtRule.startTime > restSTime)? workDate.AddDays(-1) : workDate; } do { var log = _DbContext.punchcardlogs.FirstOrDefault(b => b.accountID == restLog.accountID && b.logDate == workDate); if (log != null) { log.lastOperaAccID = restLog.lastOperaAccID; log.updateTime = definePara.dtNow(); WorkDateTime wt = punchCardFn.workTimeProcess(wtRule, log); log.punchStatus = punchCardFn.getStatusCode(wt, log); if (restLog.applyStatus != 1) { if (wtRule != null) { var logEndTime = log.logDate + wtRule.endTime; logEndTime = (wtRule.endTime <= wtRule.startTime)? logEndTime.AddDays(1) : logEndTime; if (definePara.dtNow() <= logEndTime && log.onlineTime.Year == 1 && log.offlineTime.Year == 1) { _DbContext.Remove(log); } } else { if (log.logDate > definePara.dtNow()) { _DbContext.Remove(log); } } } _DbContext.SaveChanges(); } else { if (restLog.applyStatus == 1) { var applyAcc = _DbContext.accounts.FirstOrDefault(b => b.ID == restLog.accountID); var departID = applyAcc == null? 0 : applyAcc.departmentID; PunchCardLog newLog = new PunchCardLog { accountID = restLog.accountID, departmentID = departID, logDate = workDate, createTime = definePara.dtNow(), }; WorkDateTime wt = punchCardFn.workTimeProcess(wtRule, newLog); newLog.punchStatus = punchCardFn.getStatusCode(wt, newLog, restLog); _DbContext.punchcardlogs.Add(newLog); _DbContext.SaveChanges(); } } if (restST.AddMinutes(wtLength) < restET) { restST = restST.AddDays(1); workDate = workDate.AddDays(1); } else { restST = restST.AddMinutes(wtLength); } }while(restST < restET); }
public void AddUpApplyLeave_convertToDic(ref Dictionary <string, string> Dic, LeaveOfficeApply data) { var query = _DbContext.leavenames.FirstOrDefault(b => b.ID == data.leaveID); if (query == null) { query = new LeaveName(); } data = (data == null? new LeaveOfficeApply() : data); Dic.Add("note", data.note == null? "無" : data.note); Dic.Add("sDate", data.startTime.ToString("yyyy-MM-dd HH:mm")); Dic.Add("eDate", data.endTime.ToString("yyyy-MM-dd HH:mm")); Dic.Add("applyDate", data.createTime.ToString("yyyy-MM-dd HH:mm")); Dic.Add("leaveName", query.leaveName); }