Beispiel #1
0
        public async Task <IActionResult> Index(int n = 0, bool edit = false)
        {
            DateTime startDate = DateTime.Today.AddMonths(-1).GetMonthStartDate(n);
            DateTime endDate   = DateTime.Today.AddMonths(-1).GetMonthEndDate(n);

            ViewBag.Next      = n;
            ViewBag.StartDate = startDate.ToString("dd-MM-yyyy");
            ViewBag.EndDate   = endDate.ToString("dd-MM-yyyy");

            if (edit)
            {
                List <SalarySheet> salarySheet = await salarySheetRepository.GetSalarySheet(endDate);

                return(View("Edit", salarySheet));
            }

            if (endDate < DateTime.Today)
            {
                List <SalarySheetRecord> salarySheet = await salarySheetRepository.GetSalarySheetRecords(endDate, false);

                if (!salarySheet.Any())
                {
                    IEnumerable <ApplicationUser> allUsers = await applicationUserRepository.GetActiveUsers();

                    foreach (ApplicationUser user in allUsers)
                    {
                        string userName         = user.FullName;
                        int    salary           = (int)user.Salary;
                        int    penaltiesPercent = await offenseRepository.SumOfPenaltyPercent(user.Id, startDate, endDate);

                        int dailySalary = await applicationUserRepository.GetDailySalary(user.Id);

                        int    attendancePenalties = (int)((dailySalary * penaltiesPercent) / 100.0);
                        double unpaidVacationDays  = await vacationRepository.GetUnpaidUserVacationsInRange(user.Id, startDate, endDate);

                        int  unpaidVacationDeduction = (int)unpaidVacationDays * dailySalary;
                        int  insuranceDeduction      = (int)((user.InsuranceDeduction * salary) / 100.0);
                        Loan loan = await loanRepository.GetActiveLoanByUserId(user.Id);

                        int loanDeduction = loan != null?Math.Min(loan.MonthlyPayment, loan.RemainingAmount) : 0;

                        int housingAllowance        = user.HousingAllowance;
                        int transportationAllowance = user.TransportationAllowance;
                        int finalSalary             = (salary + housingAllowance + transportationAllowance) - (attendancePenalties + unpaidVacationDeduction + insuranceDeduction + loanDeduction);
                        salarySheet.Add(new SalarySheetRecord(user.Id, userName, salary, attendancePenalties, unpaidVacationDeduction, insuranceDeduction, loanDeduction, housingAllowance, transportationAllowance, finalSalary, endDate, true));
                    }
                }
                return(View("Index", salarySheet));
            }
            return(View("Index", new List <SalarySheetRecord>()));
        }