//Get a list of time cards for an employee public HttpResponseMessage <IEnumerable <TimeCardData> > GetTimeCardHistory(string id) { using (var db = new TimeClockContext()) { var timecards = db.Timecards.Where(tc => tc.EmployeeID.Equals(id)); timecards.OrderBy(tc => tc.PayPeriod); int i = 1; List <TimeCardData> retVal = new List <TimeCardData>(); foreach (Timecard timec in timecards) { TimeCardData temp = new TimeCardData() { LineNumber = i.ToString(), StartDate = timec.PayPeriod.ToString(@"MM\/dd\/yy"), EndDate = timec.PayPeriod.AddDays(timec.Employee.department.PayPeriodInterval - 1).ToString(@"MM\/dd\/yy"), TimecardID = timec.TimecardID }; retVal.Add(temp); } return(new HttpResponseMessage <IEnumerable <TimeCardData> >(retVal)); } }
public TimeClockUnitOfWork(TimeClockContext context) { _context = context; Employees = new EmployeeRepository(_context); Shifts = new ShiftRepository(_context); IncompleteShifts = new IncompleteShiftRepository(_context); }
/* * this split up the punch to the different days that it happen, this is done recursively, and call allLinesHelper on the corresponding days * */ private static bool addLinesDaily(TimeClockContext db, Punch punch, Timecard tc, DateTime splitStart, DateTime splitEnd, double weeklyWorked, DateTime dayStartTime) { // Split on days if (splitStart.Subtract(dayStartTime).TotalMinutes < 0) // the punch started on the previous day, split and call recursively { addLinesDaily(db, punch, tc, splitStart, dayStartTime, weeklyWorked, dayStartTime.AddDays(-1)); splitStart = dayStartTime; // To continue adding the rest of the punch } if (splitEnd.Subtract(dayStartTime.AddDays(1)).TotalMinutes < 0) // the punch ended today, we can safely add it { double dailyworked = punch.employee.minutsWorkedDate(db, dayStartTime); bool seventhDay = punch.employee.workedSixPreviousDays(db); addLinesHelper(db, punch, tc, punch.employee.department.DefaultPayType, weeklyWorked, dailyworked, splitStart, splitEnd, seventhDay); } else // The punch ends on the next day { double dailyworked = punch.employee.minutsWorkedDate(db, dayStartTime); bool seventhDay = punch.employee.workedSixPreviousDays(db); addLinesHelper(db, punch, tc, punch.employee.department.DefaultPayType, weeklyWorked, dailyworked, splitStart, dayStartTime.AddDays(1), seventhDay); addLinesDaily(db, punch, tc, dayStartTime.AddDays(1), splitEnd, weeklyWorked, dayStartTime.AddDays(1)); } return(true); }
/** * Test function **/ public HttpResponseMessage RebuildLines(string employeeId, string timecardDate = null) { using (var db = new TimeClockContext()) { var employee = db.Employees.SingleOrDefault(e => e.EmployeeID == employeeId); if (employee == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } PayPeriod payperiod = PayPeriodTools.LookupPayPeriod(db, employee.DepartmentID); if (timecardDate != null) { payperiod = PayPeriodTools.LookupPayPeriod(db, employee.DepartmentID, DateTime.Parse(timecardDate)); } var timecard = employee.Timecards.SingleOrDefault(tc => tc.PayPeriod == payperiod.Start); employee.rebuildTimecardLines(db, timecard); return(new HttpResponseMessage(HttpStatusCode.OK)); } }
public String getAllPayroll(TimeClockContext db, PayPeriod payp) { var employees = db.Employees; String retString = ""; foreach (Employee emp in employees) { Timecard tc = db.Timecards.SingleOrDefault(time => time.PayPeriod.Equals(payp.Start) && time.EmployeeID.Equals(emp.EmployeeID)); var lines = db.Lines.Where(l => l.TimecardID == tc.TimecardID); Dictionary<String, Double> lineInformation = new Dictionary<String, Double>(); foreach (Line line in lines) { string key = line.Punch.DepartmentID.ToString() + ", " + line.PayType.ExportValue; if (lineInformation.ContainsKey(key)) lineInformation[key] += line.getMinutDuration(); else lineInformation.Add(key, line.getMinutDuration()); } foreach (String key in lineInformation.Keys) { String payRollLine = emp.EmployeeID + ", " + key + ", " + lineInformation[key].ToString("F2"); retString += payRollLine + "\n"; } } return retString; }
public ActionResult Timecard(string id) { using (var db = new TimeClockContext()) { int lineNumberCounter = 1; List <TimeCardView> timecard = new List <TimeCardView>(); PayPeriod payPeriod = PayPeriodTools.LookupPayPeriod(db, 1); var empTC = db.Timecards.SingleOrDefault(t => t.EmployeeID == id && t.PayPeriod == payPeriod.Start); var lines = db.Lines.Where(l => l.TimecardID == empTC.TimecardID); lines.OrderBy(l => l.SplitStart); foreach (Line line in lines) { int last = timecard.Count - 1; if (last > 0 && timecard[last].PunchID == line.PunchID) { timecard[last].Out = line.SplitEnd; if (line.PayType.Description == "Overtime") { timecard[last].Overtime = line.SplitEnd.Subtract(line.SplitStart).TotalHours; } else if (line.PayType.Description == "Regular") { timecard[last].Regular = line.SplitEnd.Subtract(line.SplitStart).TotalHours; } else { ; } timecard[last].updateEntry(); } // Otherwise we create a new line and add it to the timecard. else { TimeCardView temp = new TimeCardView(lineNumberCounter, line.SplitStart.Date, line.SplitStart, line.SplitEnd, line.PunchID); if (line.PayType.Description == "Regular") { temp.Regular = line.SplitStart.Subtract(line.SplitEnd).TotalHours; } else if (line.PayType.Description == "Overtime") { temp.Overtime = line.SplitStart.Subtract(line.SplitEnd).TotalHours; } else { ;// What should we do if it is neither of the two? } timecard.Add(temp); } } return(PartialView(timecard)); } }
private static bool addLinesHelper(TimeClockContext db, Punch punch, Timecard tc, PayType pt, double weeklyWorked, double dailyWorked, DateTime splitStart, DateTime splitEnd, bool seventhDay) { // a simple base case, to stop spawing extra lines -- Not sure if this is needed if (splitStart.Subtract(splitEnd).TotalMinutes == 0) { return(true); } // Determin the correct pay type for this line. while (weeklyWorked > pt.getWeeklyMax(seventhDay)) { pt = pt.NextPayType; } while (dailyWorked > pt.getDailyMax(seventhDay)) { pt = pt.NextPayType; } // Calculate dailyLeft and weeklyLeft double dailyLeft = (double)pt.getDailyMax(seventhDay) - dailyWorked; double weeklyLeft = (double)pt.getWeeklyMax(seventhDay) - dailyWorked; double splitLength = splitEnd.Subtract(splitStart).TotalMinutes; // Check if we reach over the weekly -- if if (weeklyWorked + splitLength > pt.getWeeklyMax(seventhDay)) { addLinesHelper(db, punch, tc, pt, weeklyWorked, dailyWorked, splitStart, splitStart.AddMinutes(weeklyLeft), seventhDay); addLinesHelper(db, punch, tc, pt.NextPayType, weeklyWorked + weeklyLeft, dailyWorked + weeklyLeft, splitStart.AddMinutes(weeklyLeft), splitEnd, seventhDay); } // Check if we reached over the daily limit else if (dailyWorked + splitLength > pt.getDailyMax(seventhDay)) { addLinesHelper(db, punch, tc, pt, weeklyWorked, dailyWorked, splitStart, splitStart.AddMinutes(dailyLeft), seventhDay); addLinesHelper(db, punch, tc, pt.NextPayType, weeklyWorked + dailyLeft, dailyWorked + dailyLeft, splitStart.AddMinutes(dailyLeft), splitEnd, seventhDay); } // we can safely add the line to the DB else { db.Lines.Add(new Line() { TimecardID = tc.TimecardID, PunchID = punch.PunchID, SplitStart = splitStart, SplitEnd = splitEnd, PayTypeID = pt.PayTypeID }); db.SaveChanges(); } return(true); }
/* This functions determine the weekly minuts worked, as well as the time that the current payday started. * * This information is then used to call addLinesDaily(...); */ private static bool addLinesTimecard(TimeClockContext db, Punch punch, Timecard tc, DateTime splitStart, DateTime splitEnd) { // Calculate weeklyWorked, and dailyWorked var lines = db.Lines.Where(l => l.TimecardID == tc.TimecardID); double weeklyMinuts = punch.employee.minutesWorkedWeek(db, splitStart); TimeSpan dayBeginTime = punch.employee.department.PayPeriodSeed.TimeOfDay; DateTime currentDayStart = DateTime.Now.Date.Add(dayBeginTime); return(addLinesDaily(db, punch, tc, splitStart, splitEnd, weeklyMinuts, currentDayStart)); }
public HttpResponseMessage MessageViewed(MessageRead request) { using (var db = new TimeClockContext()) { /****************************************************************************** TODO ITEM *********************************/ //Need to mark the corresponding message read. MessageViewed temp = db.MessagesViewed.SingleOrDefault(mv => mv.EmployeeID.Equals(request.employeeID) && mv.MessageID.Equals(request.messageID)); temp.DateViewed = request.time; return(new HttpResponseMessage(HttpStatusCode.Created)); } }
//Should return identical to time card lines public HttpResponseMessage <IEnumerable <TimeCardView> > GetTimeCardDetails(string empId, int tcId) { //List<TimeCardView>() {} using (var db = new TimeClockContext()) { Timecard curTimeCard = db.Timecards.SingleOrDefault(tc => tc.TimecardID == tcId); var timeCardData = db.Lines.Where(l => l.TimecardID == curTimeCard.TimecardID).OrderBy(l => l.SplitStart).ToList(); var lines = TimeCardView.LinesToTimeCardView(timeCardData); return(new HttpResponseMessage <IEnumerable <TimeCardView> >(lines)); } }
/* Looks up the employees current status */ public ActionResult Status(string id) { using (var db = new TimeClockContext()) { Employee emp = db.Employees.FirstOrDefault(e => e.EmployeeID == id); int empIn = emp.isWorking(db); if (empIn > 0) ViewBag.PunchDirection = "In"; else ViewBag.PunchDirection = "Out"; return PartialView(); } }
public ActionResult Timecard(string id) { using (var db = new TimeClockContext()) { int lineNumberCounter = 1; List<TimeCardView> timecard = new List<TimeCardView>(); PayPeriod payPeriod = PayPeriodTools.LookupPayPeriod(db, 1); var empTC = db.Timecards.SingleOrDefault(t => t.EmployeeID == id && t.PayPeriod == payPeriod.Start); var lines = db.Lines.Where(l => l.TimecardID == empTC.TimecardID); lines.OrderBy(l => l.SplitStart); foreach (Line line in lines) { int last = timecard.Count - 1; if (last > 0 && timecard[last].PunchID == line.PunchID) { timecard[last].Out = line.SplitEnd; if (line.PayType.Description == "Overtime") timecard[last].Overtime = line.SplitEnd.Subtract(line.SplitStart).TotalHours; else if (line.PayType.Description == "Regular") timecard[last].Regular = line.SplitEnd.Subtract(line.SplitStart).TotalHours; else ; timecard[last].updateEntry(); } // Otherwise we create a new line and add it to the timecard. else { TimeCardView temp = new TimeCardView(lineNumberCounter, line.SplitStart.Date, line.SplitStart, line.SplitEnd, line.PunchID); if (line.PayType.Description == "Regular") temp.Regular = line.SplitStart.Subtract(line.SplitEnd).TotalHours; else if (line.PayType.Description == "Overtime") temp.Overtime = line.SplitStart.Subtract(line.SplitEnd).TotalHours; else ;// What should we do if it is neither of the two? timecard.Add(temp); } } return PartialView(timecard); } }
public static PayPeriod LookupPayPeriod(TimeClockContext db, int DepartmentID, DateTime time) { var seed = db.Departments.SingleOrDefault(d => d.DepartmentID == DepartmentID); DateTime seedDate = seed.PayPeriodSeed; int interval = seed.PayPeriodInterval; TimeSpan span = time.Subtract(seedDate); int count = (int)Math.Floor(span.TotalDays / (double)interval); PayPeriod payPeriod = new PayPeriod(); payPeriod.Start = seedDate.AddDays(count * interval); payPeriod.End = seedDate.AddDays(((count + 1) * interval) - 1); return payPeriod; }
public static PayPeriod LookupPayPeriod(TimeClockContext db, int DepartmentID, DateTime time) { var seed = db.Departments.SingleOrDefault(d => d.DepartmentID == DepartmentID); DateTime seedDate = seed.PayPeriodSeed; int interval = seed.PayPeriodInterval; TimeSpan span = time.Subtract(seedDate); int count = (int)Math.Floor(span.TotalDays / (double)interval); PayPeriod payPeriod = new PayPeriod(); payPeriod.Start = seedDate.AddDays(count * interval); payPeriod.End = seedDate.AddDays(((count + 1) * interval) - 1); return(payPeriod); }
public ActionResult PunchGrid(string EID, PayPeriod pay = null) { using (var db = new TimeClockContext()) { if (pay == null) { pay = PayPeriodTools.LookupPayPeriod(db, 1); } // How are we suppose to handle punches that extend over the end of a payperiod? // // Get all punches from p payperiod - into a list List <Punch> punches = db.Punches.Where(p => p.EmployeeID == EID && pay.Start < p.InTime && p.InTime < pay.End).ToList(); // return partial view return(PartialView("PunchData", punches)); } }
/* Looks up the employees current status */ public ActionResult Status(string id) { using (var db = new TimeClockContext()) { Employee emp = db.Employees.FirstOrDefault(e => e.EmployeeID == id); int empIn = emp.isWorking(db); if (empIn > 0) { ViewBag.PunchDirection = "In"; } else { ViewBag.PunchDirection = "Out"; } return(PartialView()); } }
public HttpResponseMessage <MessageData> GetMessageDetails(int id, String empId) { using (var db = new TimeClockContext()) { Message mes = db.Messages.SingleOrDefault(m => m.MessageID == id); Employee emp = db.Employees.SingleOrDefault(e => e.EmployeeID == empId); MessageData retVal = new MessageData() { Date = mes.DateCreated.ToString(@"MM\/dd\/yy"), Subject = mes.Subject, From = mes.Manager.FirstName + " " + mes.Manager.LastName, To = emp.FirstName + " " + emp.LastName, Message = mes.Body }; return(new HttpResponseMessage <MessageData>(retVal)); } }
public static bool addLines(TimeClockContext db, Punch punch) { // determine payperiod PayPeriod currentPayP = PayPeriodTools.LookupPayPeriod(db, punch.employee.department.DepartmentID, punch.OutTime.Value); Timecard currentTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(currentPayP.Start)); // check if we reach over payperiods if (punch.InTime.Subtract(currentPayP.Start).TotalMinutes < 0) // We started in the previous payperiod , solit the punch into two. { PayPeriod previousPayP = new PayPeriod() { Start = currentPayP.Start.Subtract(TimeSpan.FromDays(punch.employee.department.PayPeriodInterval)), End = currentPayP.Start }; Timecard previousTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(previousPayP.Start)); bool first = addLinesTimecard(db, punch, previousTC, punch.InTime, previousPayP.End); bool second = addLinesTimecard(db, punch, currentTC, currentPayP.Start, punch.OutTime.Value); return first && second; } else if (punch.OutTime.Value.Subtract(currentPayP.End).TotalMinutes > 0) // We ended in the next payperiod - Not sure this will ever happen { PayPeriod nextPayP = new PayPeriod() { Start = currentPayP.End, End = currentPayP.End.Add(TimeSpan.FromDays(punch.employee.department.PayPeriodInterval)) }; Timecard nextTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(nextPayP.Start)); bool first = addLinesTimecard(db, punch, currentTC, punch.InTime, currentPayP.End); bool second = addLinesTimecard(db, punch, nextTC, nextPayP.Start, punch.OutTime.Value); return first && second; } else // No over lap, we just add the whole punch to the current Time card { return addLinesTimecard(db, punch, currentTC, punch.InTime, punch.OutTime.Value); } }
//pull list of messages needs a response type public HttpResponseMessage <IEnumerable <Messages> > GetMessages(string id) { using (var db = new TimeClockContext()) { Employee emp = db.Employees.SingleOrDefault(e => e.EmployeeID.Equals(id)); var messages = emp.Messages; messages.OrderBy(m => m.DateCreated); List <Messages> returnMessages = new List <Messages>(); foreach (Message mes in messages) { bool isViewed = true; MessageViewed tempMV = db.MessagesViewed.SingleOrDefault(mv => mv.EmployeeID.Equals(id) && mv.MessageID.Equals(mes.MessageID)); if (!tempMV.DateViewed.HasValue) { isViewed = false; } Messages temp = new Messages() { MessegeID = mes.MessageID.ToString(), Date = mes.DateCreated.ToString(@"MM\/dd\/yy"), From = mes.Manager.FirstName + " " + mes.Manager.LastName, Subject = mes.Subject, isViewed = isViewed }; returnMessages.Add(temp); } return(new HttpResponseMessage <IEnumerable <Messages> >(returnMessages)); } /* new two new types messageData and messageList * messageData() { messages = List<messageList>() { messageId , Date, From, Subject } } */ }
public ActionResult Index() { using (var db = new TimeClockContext()) { List <Employee> WorkingEmp = new List <Employee>(); var Emp = db.Employees.Where(e => e.Terminated == false); foreach (Employee employees in Emp) { if (employees.isWorking(db) > 0) { WorkingEmp.Add(employees); } } return(View(WorkingEmp)); } }
public static bool addLines(TimeClockContext db, Punch punch) { // determine payperiod PayPeriod currentPayP = PayPeriodTools.LookupPayPeriod(db, punch.employee.department.DepartmentID, punch.OutTime.Value); Timecard currentTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(currentPayP.Start)); // check if we reach over payperiods if (punch.InTime.Subtract(currentPayP.Start).TotalMinutes < 0) // We started in the previous payperiod , solit the punch into two. { PayPeriod previousPayP = new PayPeriod() { Start = currentPayP.Start.Subtract(TimeSpan.FromDays(punch.employee.department.PayPeriodInterval)), End = currentPayP.Start }; Timecard previousTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(previousPayP.Start)); bool first = addLinesTimecard(db, punch, previousTC, punch.InTime, previousPayP.End); bool second = addLinesTimecard(db, punch, currentTC, currentPayP.Start, punch.OutTime.Value); return(first && second); } else if (punch.OutTime.Value.Subtract(currentPayP.End).TotalMinutes > 0) // We ended in the next payperiod - Not sure this will ever happen { PayPeriod nextPayP = new PayPeriod() { Start = currentPayP.End, End = currentPayP.End.Add(TimeSpan.FromDays(punch.employee.department.PayPeriodInterval)) }; Timecard nextTC = db.Timecards.SingleOrDefault(tc => tc.EmployeeID == punch.EmployeeID && tc.PayPeriod.Equals(nextPayP.Start)); bool first = addLinesTimecard(db, punch, currentTC, punch.InTime, currentPayP.End); bool second = addLinesTimecard(db, punch, nextTC, nextPayP.Start, punch.OutTime.Value); return(first && second); } else // No over lap, we just add the whole punch to the current Time card { return(addLinesTimecard(db, punch, currentTC, punch.InTime, punch.OutTime.Value)); } }
/* * private IEnumerable<PayType> PayTypes = new TimeClockContext().PayTypes.Where(pt => * pt.Description.Equals("Regular") || * pt.Description.Equals("Overtime") * ).AsEnumerable(); */ // [GET] /REST/clock/getlist /** * Returns the list of active employees who are able to use the time clock. * * This function is meant for use by embedded devices. The web application will * be accessing this list from the MVC controller (Timeclock/Index) when inially * loading to cut down on open sockets. * * iOS/Android => watch out! * **/ public IEnumerable <ClockInitialItem> List() { using (var db = new TimeClockContext()) { List <ClockInitialItem> employeeList = new List <ClockInitialItem>(); var activeEmployees = db.Employees.Where(e => e.Terminated == false).OrderBy(e => e.DepartmentID); foreach (Employee e in activeEmployees) { employeeList.Add(new ClockInitialItem() { employeeID = e.EmployeeID, employeeName = e.FirstName + " " + e.LastName, departmentID = e.DepartmentID }); } return(employeeList); } }
// [GET] /REST/status/10-Jed /** * Returns the employee's status along with any new/pending messages for them. * The receiving web application should only display the messages after the employee has authenticated. * * Status Code: 200 - Have results * 204 - No matching EmployeeID in Employee table * **/ public EmployeeStatus Status(string id) { using (var db = new TimeClockContext()) { Employee employee = db.Employees.SingleOrDefault(e => e.EmployeeID == id); //PayPeriod payPeriod = PayPeriodTools.LookupPayPeriod(db, Constants.DEFAULT_DEPARTMENT); if (employee == null) { throw new HttpResponseException(HttpStatusCode.NoContent); } var isWorking = employee.isWorking(db); EmployeeStatus status = new EmployeeStatus() { EmployeeID = id, punchDirection = (isWorking >= 0 ? "Punch Out" : "Punch In"), openPunch = isWorking, }; return(status); } }
public String getAllPayroll(TimeClockContext db, PayPeriod payp) { var employees = db.Employees; String retString = ""; foreach (Employee emp in employees) { Timecard tc = db.Timecards.SingleOrDefault(time => time.PayPeriod.Equals(payp.Start) && time.EmployeeID.Equals(emp.EmployeeID)); var lines = db.Lines.Where(l => l.TimecardID == tc.TimecardID); Dictionary <String, Double> lineInformation = new Dictionary <String, Double>(); foreach (Line line in lines) { string key = line.Punch.DepartmentID.ToString() + ", " + line.PayType.ExportValue; if (lineInformation.ContainsKey(key)) { lineInformation[key] += line.getMinutDuration(); } else { lineInformation.Add(key, line.getMinutDuration()); } } foreach (String key in lineInformation.Keys) { String payRollLine = emp.EmployeeID + ", " + key + ", " + lineInformation[key].ToString("F2"); retString += payRollLine + "\n"; } } return(retString); }
public HttpResponseMessage <PunchResponse> Punch(PunchRequest request) { using (var db = new TimeClockContext()) { /****************************************************************************** TODO ITEM *********************************/ //This is where we need to insert the new punch for the employee //If it is an out punch, we should recalculate their timecard lines. Employee emp = db.Employees.FirstOrDefault(e => e.EmployeeID.Equals(request.ID)); if (!emp.Pin.Equals(request.pin)) { PunchResponse response = new PunchResponse() { isSuccess = false, pinError = "Pin and user did not match.", lines = null, generalError = null }; return(new HttpResponseMessage <PunchResponse>(response)); } else { var payPeriod = PayPeriodTools.LookupPayPeriod(db, emp.DepartmentID); var curTimeCard = emp.Timecards.SingleOrDefault(tc => tc.PayPeriod == payPeriod.Start); PunchResponse retVal = new PunchResponse() { isSuccess = true, pinError = "", generalError = null }; if (request.closesPunch == -1) // Create a new punch in the DB { Punch temp = new Punch() { EmployeeID = emp.EmployeeID, InTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0), OutTime = null, DepartmentID = emp.DepartmentID, PunchTypeID = Constants.DEFAULT_PUNCH_TYPE // Make this equal to the Regular punch type. //OutTime = null; }; db.Punches.Add(temp); db.SaveChanges(); var timeCardData = new List <TimeCardView>(); timeCardData.Add(new TimeCardView() { DateText = DateTime.Now.ToString(@"MM\/dd\/yy"), InText = temp.InTime.ToString(@"hh\:mm"), OutText = "", RegularText = "", OvertimeText = "", DoubletimeText = "" }); retVal.lines = timeCardData; } else // We need to close the last punch and make lines -- Make it split the lines over the payperiod seed { // Set the ending time of the punch Punch currentPunch = db.Punches.First(p => p.PunchID == request.closesPunch); currentPunch.OutTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0); if (currentPunch.OutTime.Value.Subtract(currentPunch.InTime).TotalMinutes < 1) { db.Punches.Remove(currentPunch); } db.SaveChanges(); Calculations.addLines(db, currentPunch); var timeCardData = db.Lines.Where(l => l.TimecardID == curTimeCard.TimecardID).OrderBy(l => l.SplitStart).ToList(); retVal.lines = TimeCardView.LinesToTimeCardView(timeCardData); } /* * PunchResponse retVal = new PunchResponse() * { * isSuccess = true, * pinError = "", * lines = TimeCardView.LinesToTimeCardView(timeCardData), * generalError = null * }; */ return(new HttpResponseMessage <PunchResponse>(retVal)); } } }
/* just whipped this up... need to test the logic and make sure we get the first day of the current pay period! */ public static PayPeriod LookupPayPeriod(TimeClockContext db, int DepartmentID) { return LookupPayPeriod(db, DepartmentID, DateTime.Now); }
/* This functions determine the weekly minuts worked, as well as the time that the current payday started. * * This information is then used to call addLinesDaily(...); */ private static bool addLinesTimecard(TimeClockContext db, Punch punch, Timecard tc, DateTime splitStart, DateTime splitEnd) { // Calculate weeklyWorked, and dailyWorked var lines = db.Lines.Where(l => l.TimecardID == tc.TimecardID); double weeklyMinuts = punch.employee.minutesWorkedWeek(db, splitStart); TimeSpan dayBeginTime = punch.employee.department.PayPeriodSeed.TimeOfDay; DateTime currentDayStart = DateTime.Now.Date.Add(dayBeginTime); return addLinesDaily(db, punch, tc, splitStart, splitEnd, weeklyMinuts, currentDayStart); }
/* * this split up the punch to the different days that it happen, this is done recursively, and call allLinesHelper on the corresponding days * */ private static bool addLinesDaily(TimeClockContext db, Punch punch, Timecard tc, DateTime splitStart, DateTime splitEnd, double weeklyWorked, DateTime dayStartTime) { // Split on days if(splitStart.Subtract(dayStartTime).TotalMinutes < 0) // the punch started on the previous day, split and call recursively { addLinesDaily(db, punch, tc, splitStart, dayStartTime, weeklyWorked, dayStartTime.AddDays(-1)); splitStart = dayStartTime; // To continue adding the rest of the punch } if(splitEnd.Subtract(dayStartTime.AddDays(1)).TotalMinutes < 0) // the punch ended today, we can safely add it { double dailyworked = punch.employee.minutsWorkedDate(db, dayStartTime); bool seventhDay = punch.employee.workedSixPreviousDays(db); addLinesHelper(db, punch, tc, punch.employee.department.DefaultPayType, weeklyWorked, dailyworked, splitStart, splitEnd, seventhDay); } else // The punch ends on the next day { double dailyworked = punch.employee.minutsWorkedDate(db, dayStartTime); bool seventhDay = punch.employee.workedSixPreviousDays(db); addLinesHelper(db, punch, tc, punch.employee.department.DefaultPayType, weeklyWorked, dailyworked, splitStart, dayStartTime.AddDays(1), seventhDay); addLinesDaily(db, punch, tc, dayStartTime.AddDays(1), splitEnd, weeklyWorked, dayStartTime.AddDays(1)); } return true; }
private static bool addLinesHelper(TimeClockContext db, Punch punch, Timecard tc, PayType pt, double weeklyWorked, double dailyWorked, DateTime splitStart, DateTime splitEnd, bool seventhDay) { // a simple base case, to stop spawing extra lines -- Not sure if this is needed if (splitStart.Subtract(splitEnd).TotalMinutes == 0) return true; // Determin the correct pay type for this line. while (weeklyWorked > pt.getWeeklyMax(seventhDay)) pt = pt.NextPayType; while (dailyWorked > pt.getDailyMax(seventhDay)) pt = pt.NextPayType; // Calculate dailyLeft and weeklyLeft double dailyLeft = (double) pt.getDailyMax(seventhDay) - dailyWorked; double weeklyLeft = (double) pt.getWeeklyMax(seventhDay) - dailyWorked; double splitLength = splitEnd.Subtract(splitStart).TotalMinutes; // Check if we reach over the weekly -- if if (weeklyWorked + splitLength > pt.getWeeklyMax(seventhDay)) { addLinesHelper(db, punch, tc, pt, weeklyWorked, dailyWorked, splitStart, splitStart.AddMinutes(weeklyLeft), seventhDay); addLinesHelper(db, punch, tc, pt.NextPayType, weeklyWorked + weeklyLeft, dailyWorked + weeklyLeft, splitStart.AddMinutes(weeklyLeft), splitEnd, seventhDay); } // Check if we reached over the daily limit else if (dailyWorked + splitLength > pt.getDailyMax(seventhDay)) { addLinesHelper(db, punch, tc, pt, weeklyWorked, dailyWorked, splitStart, splitStart.AddMinutes(dailyLeft), seventhDay); addLinesHelper(db, punch, tc, pt.NextPayType, weeklyWorked + dailyLeft, dailyWorked + dailyLeft, splitStart.AddMinutes(dailyLeft), splitEnd, seventhDay); } // we can safely add the line to the DB else { db.Lines.Add(new Line() { TimecardID = tc.TimecardID, PunchID = punch.PunchID, SplitStart = splitStart, SplitEnd = splitEnd, PayTypeID = pt.PayTypeID }); db.SaveChanges(); } return true; }
/* just whipped this up... need to test the logic and make sure we get the first day of the current pay period! */ public static PayPeriod LookupPayPeriod(TimeClockContext db, int DepartmentID) { return(LookupPayPeriod(db, DepartmentID, DateTime.Now)); }