Exemplo n.º 1
0
        public ActionResult Save(WorkingProcessViewModel model)
        {
            model = MapperHelper.ConvertModel(model);
            var param = new BasicParamModel()
            {
                FilterField = string.Empty,
                PageNumber  = 1,
                PageSize    = int.MaxValue,
                LanguageId  = _languageId,
                RoleId      = _roleId,
                UserId      = _userId,
                DbName      = CurrentUser.DbName
            };
            var  paramEntity = MapperHelper.Map <BasicParamModel, BasicParamType>(param);
            var  validations = new List <ValidationModel>();
            bool check       = false;
            //var checkDecisionNo = this._workingProcessService.CheckDecisionNoExisted(paramEntity, model.WorkingProcess.DecisionNo, model.WorkingProcess.Id, out check);
            //if (checkDecisionNo != null)
            //{
            //    var user = JsonConvert.DeserializeObject<HrmResultModel<bool>>(checkDecisionNo);
            //    if (!CheckPermission(user))
            //    {
            //        //return to Access Denied
            //    }
            //    else
            //    {
            //        if (user.Results != null && check)
            //        {
            //            validations.Add(new ValidationModel { ColumnName = "WorkingProcess.DecisionNo", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.DecisionNoExists") });
            //        }
            //    }
            //}
            var checkTimeApply = this._workingProcessService.CheckWorkingProcessDate(model.WorkingProcess.StartDate, model.WorkingProcess.EndDate, model.WorkingProcess.StaffId, !model.IsPossition && model.WorkingProcess.Id == 0 ? 0 : model.WorkingProcess.OrganizationId, model.WorkingProcess.Id, out check);

            if (checkTimeApply != null)
            {
                var checkTime = JsonConvert.DeserializeObject <HrmResultModel <bool> >(checkTimeApply);
                if (!CheckPermission(checkTime))
                {
                    //return to Access Denied
                }
                else
                {
                    if (checkTime.Results != null && check)
                    {
                        validations.Add(new ValidationModel {
                            ColumnName = "WorkingProcess.StartDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.WorkingProcessTimeApplyCannotDuplicate")
                        });
                        validations.Add(new ValidationModel {
                            ColumnName = "WorkingProcess.EndDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.WorkingProcessTimeApplyCannotDuplicate")
                        });
                    }
                }
            }
            validations.AddRange(ValidationHelper.Validation(model.WorkingProcess, "WorkingProcess"));
            if (model.IsContract || model.Contract.Id > 0)
            {
                validations.AddRange(ValidationHelper.Validation(model.Contract, "Contract"));
            }
            if (model.WorkingProcess.StartDate > model.WorkingProcess.EndDate)
            {
                validations.Add(new ValidationModel {
                    ColumnName = "WorkingProcess.EndDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.EndDateMustBeGreaterStarddate")
                });
            }
            if (model.Contract.ContractStartDate > model.Contract.ContractEndDate)
            {
                validations.Add(new ValidationModel {
                    ColumnName = "Contract.ContractEndDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.EndDateMustBeGreaterStarddate")
                });
            }
            var checkContractCode = this._workingProcessService.CheckContractCodeExisted(paramEntity, model.Contract.ContractCode, model.Contract.Id, out check);

            if (checkContractCode != null)
            {
                var user = JsonConvert.DeserializeObject <HrmResultModel <bool> >(checkContractCode);
                if (!CheckPermission(user))
                {
                    //return to Access Denied
                }
                else
                {
                    if (user.Results != null && check)
                    {
                        validations.Add(new ValidationModel {
                            ColumnName = "Contract.ContractCode", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.ContractCodeExists")
                        });
                    }
                }
            }
            var checkPipelineStep = this._staffService.CheckNextPipeLineStep(paramEntity, model.StaffOnboardInfo.PipelineStepId, model.WorkingProcess.StaffId, out check);

            if (checkPipelineStep != null)
            {
                var user = JsonConvert.DeserializeObject <HrmResultModel <bool> >(checkPipelineStep);
                if (!CheckPermission(user))
                {
                    //return to Access Denied
                }
                else
                {
                    if (user.Results != null && check)
                    {
                        validations.Add(new ValidationModel {
                            ColumnName = "StaffOnboardInfo.PipelineStepId", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.PipelineStepIsInValid")
                        });
                    }
                }
            }
            if (model.StaffAllowancePopupWorkingprocess != null && model.StaffAllowancePopupWorkingprocess.Count > 0)
            {
                validations.AddRange(ValidationHelper.ListValidation(model.StaffAllowancePopupWorkingprocess, "StaffAllowancePopupWorkingprocess"));
                for (int i = 0; i < model.StaffAllowancePopupWorkingprocess.Count; i++)
                {
                    if (model.StaffAllowancePopupWorkingprocess[i].StartDate > model.StaffAllowancePopupWorkingprocess[i].EndDate)
                    {
                        validations.Add(new ValidationModel {
                            ColumnName = "StaffAllowancePopupWorkingprocess[" + i.ToString() + "].EndDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.EndDateMustBeGreaterStarddate")
                        });
                    }
                }
            }
            if (model.StaffBenefitPopupWorkingprocess != null && model.StaffBenefitPopupWorkingprocess.Count > 0)
            {
                validations.AddRange(ValidationHelper.ListValidation(model.StaffBenefitPopupWorkingprocess, "StaffBenefitPopupWorkingprocess"));
                for (int i = 0; i < model.StaffBenefitPopupWorkingprocess.Count; i++)
                {
                    if (model.StaffBenefitPopupWorkingprocess[i].StartDate > model.StaffBenefitPopupWorkingprocess[i].EndDate)
                    {
                        validations.Add(new ValidationModel {
                            ColumnName = "StaffBenefitPopupWorkingprocess[" + i.ToString() + "].EndDate", ErrorMessage = _localizationService.GetResources("ErrorMessage.Validation.EndDateMustBeGreaterStarddate")
                        });
                    }
                }
            }
            if (model.StaffOnboardInfo.IsOnboarding)
            {
                validations.AddRange(ValidationHelper.Validation(model.StaffOnboardInfo, "StaffOnboardInfo"));
            }
            if (validations.Count > 0)
            {
                return(Json(new { Result = validations, Invalid = true }, JsonRequestBehavior.AllowGet));
            }

            long contractId = 0;

            if (!model.IsSalary && model.WorkingProcess.Id == 0)
            {
                model.StaffAllowancePopupWorkingprocess = new List <StaffAllowanceModel>();
            }
            if (!model.IsBennefit && model.WorkingProcess.Id == 0)
            {
                model.StaffBenefitPopupWorkingprocess = new List <StaffBenefitsModel>();
            }
            var  workingProcessResult = false;
            var  msg                = "";
            long id                 = 0;
            var  contract           = MapperHelper.Map <StaffContractModel, StaffContractType>(model.Contract);
            var  workingProcess     = MapperHelper.Map <WorkingProcessModel, WorkingProcessType>(model.WorkingProcess);
            var  staffAllowance     = MapperHelper.MapList <StaffAllowanceModel, StaffAllowanceType>(MapperHelper.ConvertModel(model.StaffAllowancePopupWorkingprocess));
            var  staffBenefit       = MapperHelper.MapList <StaffBenefitsModel, StaffBenefitsType>(MapperHelper.ConvertModel(model.StaffBenefitPopupWorkingprocess));
            var  staffOnboardInfo   = MapperHelper.Map <StaffOnboardInfoModel, StaffOnboardInfoType>(MapperHelper.ConvertModel(model.StaffOnboardInfo));
            var  listContract       = new List <StaffContractType>();
            var  listWorkingProcess = new List <WorkingProcessType>();

            listContract.Add(contract);
            listWorkingProcess.Add(workingProcess);

            workingProcess.ContractId = contractId;
            var wpResponse = this._workingProcessService.SaveWorkingProcess(listWorkingProcess, listContract, staffAllowance, staffBenefit, staffOnboardInfo, model.IsSalary, model.IsPossition, model.IsContract, paramEntity);

            if (wpResponse != null)
            {
                var result = JsonConvert.DeserializeObject <HrmResultModel <WorkingProcessEntity> >(wpResponse);
                if (!CheckPermission(result))
                {
                    //return to Access Denied
                }
                else
                {
                    id = result.Results.FirstOrDefault().Id;
                    if (id > 0)
                    {
                        msg = model.WorkingProcess.Id == 0 ? _localizationService.GetResources("Message.Create.Successful") : _localizationService.GetResources("Message.Update.Successful");
                        workingProcessResult = true;
                    }
                    else
                    {
                        msg = model.WorkingProcess.Id == 0 ? _localizationService.GetResources("Message.Create.UnSuccessful") : _localizationService.GetResources("Message.Update.UnSuccessful");
                        workingProcessResult = false;
                    }
                }
            }

            Thread thread = new Thread(() => UpdateStaffParent())
            {
                Name = "UpdateStaffParent"
            };

            thread.Start();
            return(Json(new { Result = workingProcessResult, Message = msg, Id = id }, JsonRequestBehavior.AllowGet));
        }