// GET: LeaveApplication/CreateLeaveForm public ActionResult CreateLeaveList(DateTime start, DateTime end, _leaveType leaveType) { // Try to fetch Leaveapplication from DB if it exists LeaveApplicationViewModel applicationVM = new LeaveApplicationViewModel(); List <TimeRecord> newTimeRecords = new List <TimeRecord>(); ViewBag.LeaveType = LeaveApplication.GetLeaveTypeItems(); for (int i = 0; i <= (end - start).Days; i++) { // Fetch each timerecord in DB if it exists DateTime currentDate = start.AddDays(i); var newTimeRecord = new TimeRecord(currentDate.Date); newTimeRecord.SetAttendence(null, null, 0); newTimeRecord.UserID = User.Identity.Name; newTimeRecord.LeaveType = leaveType; newTimeRecord.LeaveTime = (leaveType == 0) ? 0 : 7.5; PayPeriod.SetPublicHoliday(newTimeRecord); if (!newTimeRecord.IsHoliday) { newTimeRecords.Add(newTimeRecord); } } applicationVM.TimeRecords = newTimeRecords; if (applicationVM.TimeRecords.Count == 0) { return(Content("No working days were found.")); } return(PartialView(@"~/Views/LeaveApplication/_LeaveList.cshtml", applicationVM)); }
/// <summary> /// Create a list of <see cref="TimeRecord"/> for the applicant to edit details /// on each day of an HR application. /// </summary> /// <param name="start">The start day of the HR application</param> /// <param name="end">The end day of the HR application</param> /// <param name="leaveType">The main leave type of the application. It is also the default /// leave type of each <see cref="TimeRecord"/> in the period.</param> /// <returns>A partial view with a list of <see cref="TimeRecord"/> to edit leave details.</returns> public ActionResult CreateLeaveList(DateTime start, DateTime end, _leaveType leaveType) { // Create new Leaveapplication LeaveApplicationViewModel model = new LeaveApplicationViewModel(); List <TimeRecord> newTimeRecords = new List <TimeRecord>(); //get leave type for holidays IEnumerable <SelectListItem> items = new List <SelectListItem>() { new SelectListItem { Text = "Flexi Hours (earned)", Value = _leaveType.flexiHours.ToString(), Selected = true }, new SelectListItem { Text = "Additional Hours", Value = _leaveType.additionalHours.ToString() } }; ViewBag.HolidayLeaveTypeItems = items; for (int i = 0; i <= (end - start).Days; i++) { // Create new timerecords DateTime currentDate = start.AddDays(i); var newTimeRecord = new TimeRecord(currentDate.Date); PayPeriod.SetPublicHoliday(newTimeRecord); newTimeRecord.LeaveTime = (newTimeRecord.IsHoliday) ? 0 : 7.60; newTimeRecord.SetAttendence(null, null, 0); newTimeRecord.UserID = User.Identity.Name; newTimeRecord.LeaveType = leaveType; newTimeRecords.Add(newTimeRecord); } model.TimeRecords = newTimeRecords; return(PartialView("_LeaveList", model)); }
/// <summary> /// Gets the <see cref="DataTable"/> of payroll summary in a specific period. /// </summary> /// <param name="year">The year of pay period.</param> /// <param name="period">The number of a pay period.</param> /// <returns>A <see cref="DataTable"/> of payroll summary in a specific period.</returns> private DataTable GetDataTable(string year, string period) { int y = Convert.ToInt32(year); int p = Convert.ToInt32(period); DateTime startPeriod = PayPeriod.GetStartDay(y, p); DateTime endPeriod = PayPeriod.GetEndDay(y, p); ViewBag.Period = String.Format("{0:dd/MM/yy}", startPeriod) + " - " + String.Format("{0:dd/MM/yy}", endPeriod); //Select applications that's in this period, or has been approved in this period - old version List <LeaveApplication> applications = (from a in timesheetDb.LeaveApplications where a.status != _status.rejected && (!(a.EndTime <startPeriod || a.StartTime> endPeriod) || (a.ApprovedTime >= startPeriod && a.ApprovedTime <= endPeriod) && a.StartTime <= endPeriod) select a).ToList(); //select timesheet record forms that's in this period, or has been approved in this period List <TimeRecordForm> timeRecordForms = (from a in timesheetDb.TimeRecordForms where a.status != _status.rejected && (a.Year == y && a.Period == p) select a).ToList(); DataTable dt = new DataTable(); // Initialise columns dt.Columns.Add(new DataColumn("Application ID", typeof(string))); dt.Columns.Add(new DataColumn("Employee Card ID", typeof(string))); dt.Columns.Add(new DataColumn("Surname", typeof(string))); dt.Columns.Add(new DataColumn("First Name", typeof(string))); dt.Columns.Add(new DataColumn("Position", typeof(string))); dt.Columns.Add(new DataColumn("Date or Period", typeof(string))); dt.Columns.Add(new DataColumn("Total Hours", typeof(double))); dt.Columns.Add(new DataColumn("Leave Type / Additional Hours", typeof(string))); dt.Columns.Add(new DataColumn("Approved By", typeof(string))); dt.Columns.Add(new DataColumn("Note", typeof(string))); // Insert rows if (applications != null) { foreach (var application in applications) { ADUser user = timesheetDb.ADUsers.Find(application.UserID); string[] words = user.UserName.Split(' '); List <TimeRecord> records; if (application.ApprovedTime >= startPeriod && application.ApprovedTime <= endPeriod) { records = application.GetTimeRecords() .Where(r => r.RecordDate <= endPeriod) .OrderBy(r => r.RecordDate).ToList(); } else { records = application.GetTimeRecords() .Where(r => r.RecordDate >= startPeriod && r.RecordDate <= endPeriod) .OrderBy(r => r.RecordDate).ToList(); } if (records.Count > 0) { _leaveType previousType = records.FirstOrDefault().LeaveType.Value; string startDate = String.Format("{0:dd/MM/yy}", records.First().RecordDate); string endDate = string.Empty; double totalHours = 0.0; for (int i = 0; i < records.Count && records[i].RecordDate <= endPeriod; i++) { if (records[i].LeaveType == previousType) { totalHours += records[i].LeaveTime; endDate = String.Format("{0:dd/MM/yy}", records[i].RecordDate); } if (records[i].LeaveType != previousType || i == records.Count - 1) { DataRow dr = dt.NewRow(); dr["Employee Card ID"] = user.EmployeeID; dr["Surname"] = words[1]; dr["First Name"] = words[0]; dr["Application ID"] = application.id; dr["Position"] = user.JobCode; dr["Leave Type / Additional Hours"] = previousType.GetDisplayName(); dr["Date or Period"] = startDate; if (startDate != endDate) { dr["Date or Period"] += " - " + endDate; } dr["Total Hours"] = totalHours; if (application.status == _status.approved) { string[] managerName = timesheetDb.ADUsers.Find(application.ApprovedBy).UserName.Split(' '); dr["Approved By"] = managerName[1] + ", " + managerName[0]; dr["Note"] = ""; if (PayPeriod.GetPeriodNum(application.EndTime) < PayPeriod.GetPeriodNum(application.ApprovedTime.Value)) { if (PayPeriod.GetPeriodNum(application.ApprovedTime.Value) == p) { dr["Note"] = "Approved in this pay period"; } else { var approvedDate = application.ApprovedTime != null?application.ApprovedTime.Value.ToString("dd/MM/yy") : ""; dr["Note"] = "Not approved within this period, Approved on " + approvedDate; } } } else { dr["Note"] = "Not approved yet"; } dt.Rows.Add(dr); // Initialise variables for comparison totalHours = records[i].LeaveTime; previousType = records[i].LeaveType.Value; startDate = String.Format("{0:dd/MM/yy}", records[i].RecordDate); endDate = String.Format("{0:dd/MM/yy}", records[i].RecordDate); } } } } //for time record forms foreach (var form in timeRecordForms) { ADUser user = timesheetDb.ADUsers.Find(form.UserID); string[] words = user.UserName.Split(' '); DataRow dr = dt.NewRow(); dr["Employee Card ID"] = user.EmployeeID; dr["Surname"] = words[1]; dr["First Name"] = words[0]; dr["Application ID"] = form.TimeRecordFormId; dr["Position"] = user.JobCode; dr["Date or Period"] = form.Year + " - " + form.Period; dr["Total Hours"] = form.TotalWorkingHours; dr["Leave Type / Additional Hours"] = "Casual"; if (form.status == _status.approved) { string[] managerName = timesheetDb.ADUsers.Find(form.ApprovedBy).UserName.Split(' '); dr["Approved By"] = managerName[1] + ", " + managerName[0]; dr["Note"] = "Approved in this pay period"; } else { dr["Approved By"] = ""; dr["Note"] = "Not approved yet"; } dt.Rows.Add(dr); } } dt.DefaultView.Sort = "Surname"; dt = dt.DefaultView.ToTable(true); return(dt); }