public ActionResult AjaxAddEmployeeMandatoryInsurance(MandatoryInsuranceAddFormView insuranceaddFormView) { if (ModelState.IsValid) { MandatoryInsuranceAddRequest request = new MandatoryInsuranceAddRequest() { EmployeeId = insuranceaddFormView.EployeeId, DateOfIssue = insuranceaddFormView.DateOfIssue, InssuranceNo = insuranceaddFormView.InsuranceNo, Amount = insuranceaddFormView.InsuranceAmount, IsDefined = insuranceaddFormView.IsDefined }; MandatoryInsuranceAddResponse response = insuranceService.AddMandatoryInsurance(request); if (response.Success) { return Json(new { status = "Success", message = response.Message }); } else { return Json(new { status = "Fail", message = response.Message }); } } return Json(new { status = "Fail", message ="Dữ liệu nhập không hợp lệ" }); }
public MandatoryInsuranceAddResponse AddMandatoryInsurance(MandatoryInsuranceAddRequest request) { MandatoryInsuranceAddResponse response = new MandatoryInsuranceAddResponse(); Employee emp = employeeRepository.GetById(request.EmployeeId); if (emp != null) { double rateCompanyInsurance = emp.Company.CompanyInsuranceRatePercent; double rateLabaratorInsurance = emp.Company.LabaratorInsuranceRatePercent; double valuePositionInsurance = emp.Position.InsuranceRate; double companyValue = rateCompanyInsurance * valuePositionInsurance; double labaratorValue = rateLabaratorInsurance * valuePositionInsurance; emp.MadatoryInsurance = companyValue; emp.MadotoryInsuranceDate = request.DateOfIssue; Insurance insertedInsurance ; if (!request.IsDefined) { insertedInsurance = new Insurance() { InsuranceNo = request.InssuranceNo, DateOfIssue = request.DateOfIssue, IsMandatory = true, IsHistory = false, CompanyRatePercent = rateCompanyInsurance, LabaratorRatePercent = rateLabaratorInsurance, Values = labaratorValue, CompanyValue = companyValue, Amount = valuePositionInsurance, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, EmployeeId = request.EmployeeId }; } else { insertedInsurance = new Insurance() { InsuranceNo = request.InssuranceNo, DateOfIssue = request.DateOfIssue, IsMandatory = true, IsHistory = false, CompanyRatePercent = rateCompanyInsurance, LabaratorRatePercent = rateLabaratorInsurance, Values = rateLabaratorInsurance * request.Amount, CompanyValue = rateCompanyInsurance * request.Amount, Amount = request.Amount, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, EmployeeId = request.EmployeeId }; } try { insuranceRepository.Add(insertedInsurance); employeeRepository.Update(emp); SaveInsurance(); response.Success = true; response.Message = insertedInsurance.Id.ToString(); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } } else { response.Success = false; response.Message = "No Employee Id Exits"; } return response; }