// GET: TimesheetApproval/TimeSheetOutput
        public ActionResult TimeSheetOutput(int id)
        {
            TimeRecordForm form = contextDb.TimeRecordForms.Find(id);

            // Get manager name
            ViewBag.ManagerName = contextDb.ADUsers.Find(form.ApprovedBy).UserName ?? string.Empty;
            // Format period with dates
            DateTime start = PayPeriod.GetStartDay(form.Year, form.Period);
            DateTime end   = PayPeriod.GetEndDay(form.Year, form.Period);

            ViewBag.Period = form.Period.ToString() + String.Format(" ({0:dd/MM} - {1:dd/MM})", start, end);
            return(View(form));
        }
        // GET: TimesheetApproval/ApprovalDetail
        public ActionResult ApprovalDetail(string id)
        {
            int formID = Convert.ToInt32(id);
            TimeSheetContainer model = new TimeSheetContainer();
            TimeRecordForm     form  = contextDb.TimeRecordForms.Find(formID);

            if (form != null)
            {
                // Format period with dates
                DateTime start = PayPeriod.GetStartDay(form.Year, form.Period);
                DateTime end   = PayPeriod.GetEndDay(form.Year, form.Period);
                ViewBag.Period = form.Period.ToString() + String.Format(" ({0:dd/MM} - {1:dd/MM})", start, end);

                // Get manager names
                List <string> managerNames = new List <string>();
                foreach (var managerId in form._managerIDs)
                {
                    managerNames.Add(contextDb.ADUsers.Find(managerId).UserName);
                }
                ViewBag.Managers = managerNames;

                // Get TimeRecords
                List <TimeRecord> timeRecords = (from t in contextDb.TimeRecords
                                                 where t.UserID == form.UserID &&
                                                 t.RecordDate >= start && t.RecordDate <= end
                                                 select t).ToList();

                model.TimeRecordForm = form;
                model.TimeRecords    = timeRecords.Where(t => t.WorkHours != 0).ToList();
                return(View(model));
            }
            else
            {
                return(HttpNotFound("Cannot find the timesheet in database. Please contact our IT support."));
            }
        }
Beispiel #3
0
        /// <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);
        }