Example #1
0
        public void GenerateTotal(IList <EmployeeHours> employeeHoursList)
        {
            if (employeeHoursList != null && employeeHoursList.Any())
            {
                int      tempEmployeeId = 0;
                DateTime?tempDate       = null;
                RateType?tempRate       = null;

                TotalEmployeeHours totalEmployeeHours = null;

                EmployeeHours last = employeeHoursList.Last();

                foreach (EmployeeHours hours in employeeHoursList)
                {
                    // If not the same date, type and employee as the last entry should create new total employee hours
                    if (tempEmployeeId != hours.EmployeeId ||
                        tempDate != hours.Date || tempRate != hours.Type)
                    {
                        //Save previous entry if any
                        if (totalEmployeeHours != null)
                        {
                            //Check if there's an existing total entry for the employee, rate type and date
                            var existingTotalEmployeeHours = _totalEmployeeHoursRepository
                                                             .GetByEmployeeDateAndType(totalEmployeeHours.EmployeeId,
                                                                                       totalEmployeeHours.Date, totalEmployeeHours.Type);

                            //If yes update the value
                            if (existingTotalEmployeeHours != null)
                            {
                                _totalEmployeeHoursRepository.Update(existingTotalEmployeeHours);

                                existingTotalEmployeeHours.Hours = (existingTotalEmployeeHours.Hours + totalEmployeeHours.Hours);
                            }
                            else //Create new entry
                            {
                                totalEmployeeHours.Hours = (totalEmployeeHours.Hours);

                                _totalEmployeeHoursRepository.Add(totalEmployeeHours);
                            }
                        }

                        //Create new total employee hours obj
                        totalEmployeeHours = new TotalEmployeeHours
                        {
                            Date       = hours.Date,
                            EmployeeId = hours.EmployeeId,
                            Type       = hours.Type,
                            Hours      = hours.Hours
                        };
                    }
                    else //Same Employee, Date and Rate, Update the total Employee hours data
                    {
                        totalEmployeeHours.Hours = totalEmployeeHours.Hours + hours.Hours;
                    }

                    //If Last Iteration and New entry
                    //Save
                    if (hours.Equals(last) && totalEmployeeHours.TotalEmployeeHoursId <= 0)
                    {
                        //Check if there's an existing total entry for the employee, rate type and date
                        var existingTotalEmployeeHours = _totalEmployeeHoursRepository
                                                         .GetByEmployeeDateAndType(totalEmployeeHours.EmployeeId,
                                                                                   totalEmployeeHours.Date, totalEmployeeHours.Type);

                        //If yes update the value
                        if (existingTotalEmployeeHours != null)
                        {
                            _totalEmployeeHoursRepository.Update(existingTotalEmployeeHours);

                            existingTotalEmployeeHours.Hours = (existingTotalEmployeeHours.Hours + totalEmployeeHours.Hours);
                        }
                        else //Create new entry
                        {
                            totalEmployeeHours.Hours = (totalEmployeeHours.Hours);

                            _totalEmployeeHoursRepository.Add(totalEmployeeHours);
                        }
                    }

                    //Set Reference data
                    tempDate       = hours.Date;
                    tempEmployeeId = hours.EmployeeId;
                    tempRate       = hours.Type;

                    //Update employee hours IsIncludedInTotal value
                    _employeeHoursService.Update(hours);

                    hours.IsIncludedInTotal = true;
                }

                //Commit all change
                _unitOfWork.Commit();
            }
        }