Example #1
0
 public void can_create_payroll_of_active_employees()
 {
     using (var uow = new NHUnitOfWork()) {
         var ees    = _employees.FetchAllActive();
         var actual = ees.Count;
         var pr     = PayrollReport.Create(ees, Date.Now);
         EventBroker.getInstance().Command(new CommandIncludeSalaryDeductionInReport(pr));
         Assert.True(ees.Count > 0);
         Assert.Equal(pr.Records.Count, actual);
         _payrolls.Save(pr);
         uow.Commit();
     }
 }
Example #2
0
        public PayrollReport GetPayrollReport()
        {
            var uploadedFiles = PayrollContext.UploadedFiles.ToList();

            var orderedRows = uploadedFiles.OrderBy(p => p.EmployeeID).ThenBy(x => x.Date).ToList();

            var idGroups = orderedRows.GroupBy(x => x.EmployeeID).ToList();

            var employeeReports = new List <EmpolyeeReport>();
            var payrollReport   = new PayrollReport(employeeReports);


            foreach (var idGroup in idGroups)
            {
                foreach (var id in idGroup)
                {
                    var payPeriodStartDate = new DateTime(id.Date.Year, id.Date.Month, 1).ToString("yyyy-MM-dd");
                    var payPeriodEndDate   = new DateTime(id.Date.Year, id.Date.Month, 15).ToString("yyyy-MM-dd");

                    if (id.Date.Day > 15)
                    {
                        payPeriodStartDate = new DateTime(id.Date.Year, id.Date.Month, 16).ToString("yyyy-MM-dd");
                        var lastDayOfMonth = DateTime.DaysInMonth(id.Date.Year, id.Date.Month);
                        payPeriodEndDate = new DateTime(id.Date.Year, id.Date.Month, lastDayOfMonth).ToString("yyyy-MM-dd");
                    }

                    var employeeReport = employeeReports.FirstOrDefault(
                        x => x.EmployeeId.Equals(id.EmployeeID.ToString()) &&
                        x.PayPeriod.StartDate.Equals(payPeriodStartDate) &&
                        x.PayPeriod.EndDate.Equals(payPeriodEndDate));

                    if (employeeReport != null)
                    {
                        double totalPaid = GetAmountPaidFromFormattedTotal(employeeReport.AmountPaid) +
                                           GetAmountPaidFromFormattedTotal(CalculateAmountPaidPerDay(id.JobGroup, id.HoursWorked));
                        employeeReport.AmountPaid = totalPaid.ToString("C", CultureInfo.CreateSpecificCulture("en-US"));
                    }
                    else
                    {
                        employeeReports.Add(new EmpolyeeReport(id.EmployeeID.ToString(),
                                                               new PayPeriod(payPeriodStartDate, payPeriodEndDate),
                                                               CalculateAmountPaidPerDay(id.JobGroup, id.HoursWorked)));
                    }
                }
            }

            return(payrollReport);
        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            PayrollReport pr = new PayrollReport();

            pr.ShowDialog();
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var data = await FileHelpers.ProcessFormFile(FileUpload.Timesheet, ModelState);

            var fileName = WebUtility.HtmlEncode(Path.GetFileName(FileUpload.Timesheet.FileName));

            DataTable dt = FileHelpers.Csv2Table(FileUpload.Timesheet, ",");

            const string COL_EMPID       = "employee id";
            const string COL_DATE        = "date";
            const string COL_HOURSWORKED = "hours worked";
            const string COL_JOBCODE     = "job group";

            var reportId = int.Parse(dt.Rows[dt.Rows.Count - 1][COL_HOURSWORKED].ToString());

            dt.Rows.RemoveAt(dt.Rows.Count - 1);
            dt.AcceptChanges();

            var report = _context.Report.FirstOrDefault(r => r.ReportId == reportId);

            if (report == null)
            {
                _context.Report.Add(new Report {
                    ReportId = reportId
                });
                _context.SaveChanges();
            }
            else
            {
                ErrorMessage = string.Format("Report with ID: {0} already exists in database.", reportId);
            }

            DataView dv = new DataView(dt);

            dv.Sort = COL_EMPID;
            DataTable distinctEmployees = dv.ToTable(true, COL_EMPID);

            _context.Database.EnsureCreated();
            var employeedToAdd = new List <Employee>();

            foreach (DataRow dr in distinctEmployees.Rows)
            {
                var employeeId = int.Parse(dr[COL_EMPID].ToString());

                if (_context.Employee.FirstOrDefault(e => e.EmployeeId == employeeId) == null)
                {
                    employeedToAdd.Add(new Employee {
                        EmployeeId = employeeId
                    });
                }
            }

            if (employeedToAdd.Count > 0)
            {
                _context.Employee.AddRange(employeedToAdd.ToArray());
                _context.SaveChanges();
            }

            foreach (DataRow dr in dt.Rows)
            {
                var date        = DateTime.ParseExact(dr[COL_DATE].ToString(), "d/m/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                var employeeId  = int.Parse(dr[COL_EMPID].ToString());
                var hoursWorked = decimal.Parse(dr[COL_HOURSWORKED].ToString());
                var jobCode     = dr[COL_JOBCODE].ToString();
                var jobId       = _context.Job.FirstOrDefault(j => j.JobCode == jobCode).JobId;
                _context.Timesheet.Add(new Timesheet {
                    Date = date, EmployeeId = employeeId, HoursWorked = hoursWorked, ReportId = reportId, JobId = jobId
                });
            }

            _context.SaveChanges();


            PayrollReport = new PayrollReport[]
            {
                new PayrollReport {
                    EmployeeId = 1, StartDate = DateTime.Parse("1/1/2011"), EndDate = DateTime.Parse("1/15,2011"), Amount = 100
                },
                new PayrollReport {
                    EmployeeId = 1, StartDate = DateTime.Parse("1/16/2011"), EndDate = DateTime.Parse("1/31,2011"), Amount = 100
                }
            };

            //return new OkObjectResult(data);
            return(RedirectToPage("./Update"));;
        }