Ejemplo n.º 1
0
        public OperationResult <Payroll> ProcessPayrollForWeek(DateTime date, int payrollidNotUsed, string FranchiseId, bool persistToDataBase, Guid userKey)
        {
            // The date is not getting the end date for the week right now, but we can change that
            while (date.DayOfWeek != DayOfWeek.Saturday)
            {
                date = date.AddDays(1.0);
            }

            // NOTE doesn't make sense to put in a payrollidNotUsed if we have the week, so ignore the variable for now.  Kept there to maintain the parameter signature
            var result = new OperationResult <Payroll>();

            int      franchiseid = Convert.ToInt32(FranchiseId);
            DateTime startDate   = date.AddDays(-7);
            DateTime holddate    = date.AddDays(1);

            // Forget about what's in the database, just perform the function to generate a payroll for this week since we re-process it anyways
            // [Step 1] - Get everything we should need so we don't have to make anymore database calls
            List <tbl_Employee>     employeelist  = (from e in db.tbl_Employee where e.ActiveYN == true && e.FranchiseID == franchiseid select e).ToList <tbl_Employee>();
            List <tbl_HR_TimeSheet> timeSheetList = (from ts in db.tbl_HR_TimeSheet
                                                     join e in db.tbl_Employee on ts.EmployeeID equals e.EmployeeID
                                                     where
                                                     e.FranchiseID == franchiseid &&
                                                     ts.WeekEndingDateOn == date
                                                     select ts).ToList <tbl_HR_TimeSheet>();

            var reportingService = AbstractBusinessService.Create <ReportingService>(userKey);
            // Query Jobs within dates from our reporting layer
            KeyPerformanceIndicator kpiAllFranchise = reportingService.GetKeyPerformanceIndicators(
                franchiseid, startDate, holddate);
            List <Reporting.Job>          jobList          = kpiAllFranchise.AllJobs.ToList <Reporting.Job>();
            List <Reporting.JobTask>      taskList         = kpiAllFranchise.AllTasks.ToList();
            List <Reporting.PayrollSetup> payrollsetuplist = reportingService.GetPayrollSetupData(franchiseid).ToList <Reporting.PayrollSetup>();

            // Existing Payroll
            List <tbl_Payroll>        existingPayrollList = (from p in db.tbl_Payroll where p.PayrollDate == date select p).ToList <tbl_Payroll>();
            tbl_Payroll               existingPayroll     = (existingPayrollList.Count == 0) ? null : existingPayrollList[0];
            List <tbl_Job_Payroll>    jobPayrollListForExistingPayroll = new List <tbl_Job_Payroll>();
            List <tbl_PayrollDetails> payrollDetailsForExistingPayroll = new List <tbl_PayrollDetails>();

            if (existingPayroll != null)
            {
                jobPayrollListForExistingPayroll = (from j in db.tbl_Job_Payroll where j.PayrollID == existingPayroll.PayrollID select j).ToList <tbl_Job_Payroll>();
                payrollDetailsForExistingPayroll = (from pd in db.tbl_PayrollDetails where pd.PayrollID == existingPayroll.PayrollID select pd).ToList <tbl_PayrollDetails>();
            }

            // [Step 2] - Process a new Payroll
            Payroll payrollResult = new Payroll(date, franchiseid);

            ProcessEmployeesForWeek(payrollResult, employeelist, timeSheetList, jobList, taskList, payrollsetuplist);


            // [Step 3] - Now do the database stuff
            tbl_Payroll payrollToUse = null;

            if (existingPayroll != null)                // There IS an existing payroll
            {
                payrollToUse = existingPayroll;

                // blow away any existing JobPayrolls
                foreach (tbl_Job_Payroll existingJobPayroll in jobPayrollListForExistingPayroll)
                {
                    db.DeleteObject(existingJobPayroll);
                }

                // Blow away any existing PayrollDetails
                foreach (tbl_PayrollDetails existingPayrollDetail in payrollDetailsForExistingPayroll)
                {
                    db.DeleteObject(existingPayrollDetail);
                }
            }
            else
            {
                // Create a new payroll
                payrollToUse             = new tbl_Payroll();
                payrollToUse.LockDate    = null;
                payrollToUse.PayrollDate = date;
                payrollToUse.FranchiseID = franchiseid;
                db.tbl_Payroll.AddObject(payrollToUse);
            }

            // add all the payroll details
            foreach (PayrollDetail pd in payrollResult.PayrollDetails)
            {
                tbl_PayrollDetails newPayrollDetail = new tbl_PayrollDetails()
                {
                    PayrollID = payrollToUse.PayrollID,

                    EmployeeID = pd.EmployeeID,

                    SundayHours    = (float)pd.SundayHours,
                    MondayHours    = (float)pd.MondayHours,
                    TuesdayHours   = (float)pd.TuesdayHours,
                    WednesdayHours = (float)pd.WednesdayHours,
                    ThursdayHours  = (float)pd.ThursdayHours,
                    FridayHours    = (float)pd.FridayHours,
                    SaturdayHours  = (float)pd.SaturdayHours,

                    OTHours      = (float)pd.OTHours,
                    OTPay        = (float)pd.OTPay,
                    OTRate       = (float)pd.OTRate,
                    RegularHours = (float)pd.RegularHours,
                    RegularPay   = (float)pd.RegularPay,
                    RegularRate  = (float)pd.RegularRate,
                    WeeklySalary = (float)pd.WeeklySalary
                };
                db.tbl_PayrollDetails.AddObject(newPayrollDetail);

                // now add the job_payroll records
                foreach (JobPayroll jobPayroll in pd.JobPayrolls)
                {
                    tbl_Job_Payroll newJobPayroll = new tbl_Job_Payroll()
                    {
                        JobID          = jobPayroll.JobID
                        , ServiceProID = pd.EmployeeID   // TODO: Get rid of this after we add the foreign key to Payroll Detail
                        ,
                        JobSubTotal = (float)jobPayroll.JobSubTotal
                        ,
                        TotalCommissionPartsAndLabor = jobPayroll.TotalCommissionPartsAndLabor
                        ,
                        TotalCommissionSpifs = (float)jobPayroll.TotalCommissionSpifs
                        ,
                        PayrollID = payrollToUse.PayrollID
                    };
                    db.tbl_Job_Payroll.AddObject(newJobPayroll);
                }
            }

            db.SaveChanges();

            //[Step 4] - Communicate results
            payrollResult.PayrollID = payrollToUse.PayrollID;   // Doesn't make perfect sense but the UI needs this
            result.ResultData       = payrollResult;
            result.Success          = true;
            if (!result.Success)
            {
                result.Message = "Run Payroll Failed";
            }

            return(result);
        }