private void MapBenefit(Benefit Benefitobject, BenefitFormViewModel model, OptionsViewModel moreInfo)
        {
            model.Branches         = model.IBranches == null ? null : string.Join(",", model.IBranches.ToArray());
            model.Jobs             = model.IJobs == null ? null : string.Join(",", model.IJobs.ToArray());
            model.Employments      = model.IEmployments == null ? null : string.Join(",", model.IEmployments.ToArray());
            model.PeopleGroups     = model.IPeopleGroups == null ? null : string.Join(",", model.IPeopleGroups.ToArray());
            model.Payrolls         = model.IPayrolls == null ? null : string.Join(",", model.IPayrolls.ToArray());
            model.PayrollGrades    = model.IPayrollGrades == null ? null : string.Join(",", model.IPayrollGrades.ToArray());
            model.CompanyStuctures = model.ICompanyStuctures == null ? null : string.Join(",", model.ICompanyStuctures.ToArray());
            model.Positions        = model.IPositions == null ? null : string.Join(",", model.IPositions.ToArray());
            moreInfo.VisibleColumns.Add("Branches");
            moreInfo.VisibleColumns.Add("Jobs");
            moreInfo.VisibleColumns.Add("Employments");
            moreInfo.VisibleColumns.Add("PeopleGroups");
            moreInfo.VisibleColumns.Add("Payrolls");
            moreInfo.VisibleColumns.Add("PayrollGrades");
            moreInfo.VisibleColumns.Add("CompanyStuctures");
            moreInfo.VisibleColumns.Add("Positions");
            _hrUnitOfWork.BenefitsRepository.AddLName(Language, Benefitobject.Name, model.Name, model.LocalName);

            AutoMapper(new Models.AutoMapperParm
            {
                Destination = Benefitobject,
                Source      = model,
                ObjectName  = "Benefit",
                Options     = moreInfo
            });
        }
        public BenefitFormViewModel ReadBenefit(int Id, string culture)
        {
            var obj = context.Benefits.Where(a => a.Id == Id).Select(a => new { a, LocalName = HrContext.TrlsName(a.Name, culture) }).FirstOrDefault();
            var mod = new BenefitFormViewModel()
            {
                Id                = obj.a.Id,
                Name              = obj.a.Name,
                StartDate         = obj.a.StartDate,
                IsLocal           = obj.a.IsLocal,
                Code              = obj.a.Code,
                EndDate           = obj.a.EndDate,
                Coverage          = obj.a.Coverage,
                Description       = obj.a.Description,
                EmpAccural        = obj.a.EmpAccural,
                MaxFamilyCnt      = obj.a.MaxFamilyCnt,
                MonthFees         = obj.a.MonthFees,
                WaitMonth         = obj.a.WaitMonth,
                LocalName         = obj.LocalName,
                CreatedTime       = obj.a.CreatedTime,
                CreatedUser       = obj.a.CreatedUser,
                ModifiedTime      = obj.a.ModifiedTime,
                ModifiedUser      = obj.a.ModifiedUser,
                CalenderId        = obj.a.CalenderId,
                BenefitClass      = obj.a.BenefitClass,
                IPayrolls         = obj.a.Payrolls == null ? null : obj.a.Payrolls.Split(',').Select(int.Parse).ToList(),
                ICompanyStuctures = obj.a.CompanyStuctures == null ? null : obj.a.CompanyStuctures.Split(',').Select(int.Parse).ToList(),
                IEmployments      = obj.a.Employments == null ? null : obj.a.Employments.Split(',').Select(int.Parse).ToList(),
                IPayrollGrades    = obj.a.PayrollGrades == null ? null : obj.a.PayrollGrades.Split(',').Select(int.Parse).ToList(),
                IPositions        = obj.a.Positions == null ? null : obj.a.Positions.Split(',').Select(int.Parse).ToList(),
                IBranches         = obj.a.Branches == null ? null : obj.a.Branches.Split(',').Select(int.Parse).ToList(),
                IJobs             = obj.a.Jobs == null ? null : obj.a.Jobs.Split(',').Select(int.Parse).ToList(),
                IPeopleGroups     = obj.a.PeopleGroups == null ? null : obj.a.PeopleGroups.Split(',').Select(int.Parse).ToList(),
                PlanLimit         = obj.a.PlanLimit
            };

            return(mod);
        }
        public ActionResult Details(BenefitFormViewModel model, OptionsViewModel moreInfo, BenefitPlanVM grid1)
        {
            List <Error> errors = new List <Error>();

            if (ModelState.IsValid)
            {
                if (ServerValidationEnabled)
                {
                    errors = _hrUnitOfWork.SiteRepository.CheckForm(new CheckParm
                    {
                        CompanyId    = CompanyId,
                        ObjectName   = "Benefit",
                        TableName    = "Benefits",
                        ParentColumn = "CompanyId",
                        Columns      = Models.Utils.GetColumnViews(ModelState.Where(a => !a.Key.Contains('.'))),
                        Culture      = Language
                    });

                    if (errors.Count() > 0)
                    {
                        foreach (var e in errors)
                        {
                            foreach (var errorMsg in e.errors)
                            {
                                ModelState.AddModelError(errorMsg.field, errorMsg.message);
                            }
                        }

                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }
                var sequence = _hrUnitOfWork.Repository <Benefit>().Select(a => a.Code).DefaultIfEmpty(0).Max();
                // var MaxCode

                Benefit record;
                //insert
                if (model.Id == 0)
                {
                    record = new Benefit();

                    AutoMapper(new Models.AutoMapperParm
                    {
                        Destination = record,
                        Source      = model,
                        ObjectName  = "Benefit",
                        Options     = moreInfo,
                        Transtype   = TransType.Insert
                    });
                    MapBenefit(record, model, moreInfo);
                    record.CreatedUser = UserName;
                    record.CreatedTime = DateTime.Now;
                    if (model.EmpAccural != 3)
                    {
                        record.WaitMonth = null;
                    }
                    record.CompanyId = model.IsLocal ? CompanyId : (int?)null;
                    if (record.StartDate > record.EndDate)
                    {
                        ModelState.AddModelError("EndDate", MsgUtils.Instance.Trls("EndDateGthanStartDate"));
                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                    _hrUnitOfWork.BenefitsRepository.Add(record);
                }
                //update
                else
                {
                    record = _hrUnitOfWork.Repository <Benefit>().FirstOrDefault(a => a.Id == model.Id);
                    AutoMapper(new Models.AutoMapperParm
                    {
                        Destination = record,
                        Source      = model,
                        ObjectName  = "Benefit",
                        Options     = moreInfo,
                        Transtype   = TransType.Update
                    });
                    MapBenefit(record, model, moreInfo);
                    record.ModifiedTime = DateTime.Now;
                    record.ModifiedUser = UserName;
                    if (model.EmpAccural != 3)
                    {
                        record.WaitMonth = null;
                    }
                    record.CompanyId = model.IsLocal ? CompanyId : (int?)null;
                    if (record.StartDate > record.EndDate)
                    {
                        ModelState.AddModelError("EndDate", MsgUtils.Instance.Trls("EndDateGthanStartDate"));
                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                    _hrUnitOfWork.BenefitsRepository.Attach(record);
                    _hrUnitOfWork.BenefitsRepository.Entry(record).State = EntityState.Modified;
                }

                // Save grid1
                errors = SaveGrid(grid1, ModelState.Where(a => a.Key.Contains("grid")), record);

                if (errors.Count > 0)
                {
                    return(Json(errors.First().errors.First().message));
                }

                errors = SaveChanges(Language);

                var message = "OK";
                if (errors.Count > 0)
                {
                    message = errors.First().errors.First().message;
                }

                return(Json(message));
            }

            return(Json(Models.Utils.ParseFormErrors(ModelState)));
        }