Exemplo n.º 1
0
        public Result <ComputeSalaryResponse> CalculateSalary(ComputeSalaryRequest model)
        {
            if (string.IsNullOrEmpty(model.Month))
            {
                return(Result <ComputeSalaryResponse> .Failure("Month is required"));
            }
            if (model.EmployeeId == Guid.Empty)
            {
                return(Result <ComputeSalaryResponse> .Failure("EmployeeId is required"));
            }

            var emp = _employeeContext.GetById(model.EmployeeId);

            if (emp == null)
            {
                return(Result <ComputeSalaryResponse> .Failure($"Sorry, employee  not found"));
            }

            var sal = new Result <ComputeSalaryResponse>();

            switch (emp.EmployeeType.SalaryType)
            {
            case SalaryType.RegularStaff:
                sal = _salaryCalculator.ComputeForRegularStaff(emp.EmployeeType, model.DaysAbsent, model.Month);
                break;

            case SalaryType.ContractualStaff:
                sal = _salaryCalculator.ComputeForContractualStaff(emp.EmployeeType, model.DaysWorked, model.Month);
                break;

            default:
                return(Result <ComputeSalaryResponse> .Failure(null, "Salary Type Not Found"));
            }

            //save to paymeent
            _ = _paymentManager.Create(new Payment
            {
                Month      = sal.Data.Month.ToUpper(),
                Days       = sal.Data.Days,
                Employee   = emp,
                EmployeeId = emp.Id,
                SalaryPaid = sal.Data.Salary,
                Tax        = sal.Data.Tax
            });

            return(sal);
        }
Exemplo n.º 2
0
        public IActionResult CalculateSalary(DetailsViewModel model)
        {
            if (StaticCache.User == null)
            {
                AddNotification(NotificationViewModel.GetWarning("Page Not Accessable", $"Login to access page"));
                return(RedirectToAction("Index", "Home"));
            }



            if (ModelState.IsValid)
            {
                var request = new ComputeSalaryRequest
                {
                    Month      = model.Month,
                    EmployeeId = model.EmployeeId,
                    DaysAbsent = model.DaysAbsence,
                    DaysWorked = model.DaysWorked
                };

                var res = _employeeManager.CalculateSalary(request);
                if (res.IsSuccess)
                {
                    StaticCache.Employees = _employeeManager.GetAll().Take(20);

                    TempData["MsgSuccess"] = $"Employee Salary for the month of {model.Month} is {res.Data.Salary}";
                    TempData["MsgError"]   = null;

                    return(RedirectToAction(nameof(Details), new { employeeId = model.EmployeeId }));
                }
                else
                {
                    TempData["MsgError"]   = $"{res.Message ?? "Unable to Compute Salary at the moment"}";
                    TempData["MsgSuccess"] = null;

                    return(RedirectToAction(nameof(Details), new { employeeId = model.EmployeeId }));
                }
            }

            TempData["MsgError"]   = $"Unable to Compute Salary at the moment";
            TempData["MsgSuccess"] = null;

            return(RedirectToAction(nameof(Details), new { employeeId = model.EmployeeId }));
        }