Ejemplo n.º 1
0
        public ActionResult ApplicationOutput(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            LeaveApplicationViewModel applicationVM = new LeaveApplicationViewModel();
            LeaveApplication          application   = contextDb.LeaveApplications.Find(id);

            if (application != null)
            {
                applicationVM.LeaveApplication = application;
                applicationVM.TimeRecords      = application.GetTimeRecords();
                // Get leave balance
                List <LeaveBalance> LeaveBalances = new List <LeaveBalance>();
                if (application.OriginalBalances != null)
                {
                    ViewBag.OriginalBalances = application.OriginalBalances.Split('/');
                }
                else
                {
                    ViewBag.OriginalBalances = new string[] { "", "", "" }
                };

                if (application.CloseBalances != null)
                {
                    ViewBag.CloseBalances = application.CloseBalances.Split('/');
                }
                else
                {
                    ViewBag.CloseBalances = new string[] { "", "", "" }
                };

                // Get manager name
                ViewBag.ManagerName = contextDb.ADUsers.Find(application.ApprovedBy).UserName ?? string.Empty;

                // Get pay period and format it
                int    payPeriod1 = PayPeriod.GetPeriodNum(application.StartTime);
                int    payPeriod2 = PayPeriod.GetPeriodNum(application.EndTime);
                string period     = "Pay Period " + payPeriod1;
                if (payPeriod1 != payPeriod2)
                {
                    period += " - " + payPeriod2;
                }
                ViewBag.PayPeriod = period;

                return(View(applicationVM));
            }
            else
            {
                return(HttpNotFound("Cannot find the application in database. Please contact our IT support."));
            }
        }
Ejemplo n.º 2
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);
        }