public async Task <ApiResponse> Handle(EditPensionRateCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { var lst = await _dbContext.EmployeePensionRate.FirstOrDefaultAsync(x => x.IsDefault == true); if (request.IsDefault == true) { if (lst != null) { lst.IsDefault = false; _dbContext.EmployeePensionRate.Update(lst); await _dbContext.SaveChangesAsync(); } } if (request.IsDefault == false) { if (lst == null) { request.IsDefault = true; } if (lst.IsDefault == true && lst.FinancialYearId == request.FinancialYearId) { request.IsDefault = true; } else { request.IsDefault = false; } } EmployeePensionRate obj = await _dbContext.EmployeePensionRate.FirstOrDefaultAsync(x => x.FinancialYearId == request.FinancialYearId); obj.FinancialYearId = request.FinancialYearId; obj.PensionRate = request.PensionRate; obj.IsDefault = (request.IsDefault == false && lst == null) ? true : request.IsDefault; obj.IsDefault = true; obj.ModifiedById = request.ModifiedById; obj.ModifiedDate = DateTime.UtcNow; await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = ex.Message; } return(response); }
public async Task <ApiResponse> Handle(AddPensionRateCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { if (request != null) { // check for financial year is present or not if (!(await _dbContext.FinancialYearDetail.AnyAsync(x => x.FinancialYearId == request.FinancialYearId))) { throw new Exception(StaticResource.FinancialYearNotFound); } // check if pension rate is already assigned to fianacial year if (!(await _dbContext.EmployeePensionRate.AnyAsync(x => x.FinancialYearId == request.FinancialYearId))) { EmployeePensionRate obj = new EmployeePensionRate(); // check if default pension rate is already saved or not var lst = await _dbContext.EmployeePensionRate.FirstOrDefaultAsync(x => x.IsDefault == true); if (request.IsDefault == true) { if (lst != null) { lst.IsDefault = false; _dbContext.EmployeePensionRate.Update(lst); await _dbContext.SaveChangesAsync(); } } obj.FinancialYearId = request.FinancialYearId; obj.PensionRate = request.PensionRate; obj.IsDefault = (request.IsDefault == false && lst == null) ? true : request.IsDefault; obj.CreatedById = request.CreatedById; obj.CreatedDate = DateTime.UtcNow; obj.IsDeleted = false; await _dbContext.EmployeePensionRate.AddAsync(obj); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } else { response.StatusCode = 700; response.Message = StaticResource.FinancialYearAlreadyExists; } } } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = ex.Message; } return(response); }
public async Task <ApiResponse> Handle(GetEmployeePensionReportQuery request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); EmployeePensionRate pensionRateDetail = _dbContext.EmployeePensionRate.FirstOrDefault(x => x.IsDefault == true && x.IsDeleted == false); double pensionRate = pensionRateDetail != null ? (double)pensionRateDetail.PensionRate : 4.5; try { EmployeePensionModel epm = new EmployeePensionModel(); List <EmployeePensionReportModel> lst = new List <EmployeePensionReportModel>(); var financialYearList = await _dbContext.FinancialYearDetail.Where(x => request.FinancialYearId.Contains(x.FinancialYearId)).ToListAsync(); var empList = await _dbContext.EmployeePaymentTypes.Where(x => financialYearList.Select(y => y.StartDate.Year).Contains(x.PayrollYear.Value) && x.OfficeId == request.OfficeId && x.EmployeeID == request.EmployeeId && x.IsDeleted == false).ToListAsync(); EmployeeDetail xEmployeeDetail = await _dbContext.EmployeeDetail.FirstOrDefaultAsync(x => x.EmployeeID == request.EmployeeId && x.IsDeleted == false); var previousPensionList = await _dbContext.EmployeePaymentTypes.Where(x => x.PayrollYear < financialYearList.OrderByDescending(y => y.StartDate).FirstOrDefault().StartDate.Year&& x.IsDeleted == false).ToListAsync(); epm.PreviousPensionRate = previousPensionList.Average(x => x.PensionRate); foreach (var item in previousPensionList) { if (item.CurrencyId == request.CurrencyId) { epm.PreviousPensionDeduction += item.PensionAmount; epm.PreviousProfit += Math.Round(Convert.ToDouble(item.PensionAmount * item.PensionRate), 2); epm.PreviousTotal += Math.Round(Convert.ToDouble((item.PensionAmount * item.PensionRate) + item.PensionAmount), 2); } else { ExchangeRateDetail exchangeRate = await _dbContext.ExchangeRateDetail.OrderByDescending(x => x.Date).FirstOrDefaultAsync(x => x.IsDeleted == false && x.FromCurrency == item.CurrencyId && x.ToCurrency == request.CurrencyId); epm.PreviousPensionDeduction += item.PensionAmount * (double)exchangeRate.Rate; epm.PreviousProfit += Math.Round(Convert.ToDouble(item.PensionAmount * pensionRate), 2) * (double)exchangeRate.Rate; epm.PreviousTotal += Math.Round(Convert.ToDouble((item.PensionAmount * pensionRate) + item.PensionAmount), 2) * (double)exchangeRate.Rate; } } foreach (var item in empList) { ExchangeRateDetail exchangeRate = await _dbContext.ExchangeRateDetail.OrderByDescending(x => x.Date).FirstOrDefaultAsync(x => x.IsDeleted == false && x.FromCurrency == item.CurrencyId && x.ToCurrency == request.CurrencyId); EmployeePensionReportModel obj = new EmployeePensionReportModel(); obj.CurrencyId = item.CurrencyId.Value; obj.Date = new DateTime(item.PayrollYear.Value, item.PayrollMonth.Value, 1); obj.GrossSalary = Math.Round(Convert.ToDouble(item.GrossSalary), 2) * (double)exchangeRate.Rate; obj.PensionRate = pensionRate; obj.PensionDeduction = Math.Round(Convert.ToDouble((item.GrossSalary * pensionRate) / 100), 2) * (double)exchangeRate.Rate; obj.Profit = Math.Round(Convert.ToDouble((obj.PensionDeduction * pensionRate)) / 100, 2); obj.Total = obj.Profit + obj.PensionDeduction; lst.Add(obj); } epm.EmployeePensionReportList = lst.OrderBy(x => x.Date.Date).ToList(); epm.PensionTotal = lst.Sum(x => x.Total); epm.PensionProfitTotal = lst.Sum(x => x.Profit); epm.PensionDeductionTotal = Math.Round(Convert.ToDouble(lst.Sum(x => x.GrossSalary * pensionRate / 100)), 2); epm.EmployeeCode = xEmployeeDetail.EmployeeCode; response.data.EmployeePensionModel = epm; response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = ex.Message; } return(response); }