private static AccountValue getEntityByModel(AccountValueViewModel model)
        {
            if (model == null) return null;
            AccountValue entity = new AccountValue();

            entity.AccountType = model.AccountType;
            entity.ChartId = model.ChartId;
            entity.EndDate = model.EndDate;
            entity.Id = model.Id;
            entity.Levl = model.Levl;
            entity.Segment = model.Segment;
            entity.StartDate = model.StartDate;
            if (model.Id == 0)
            {
                entity.CreateDate = DateTime.Now;
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CompanyId = AuthenticationHelper.CompanyId.Value;
            }
            else
            {
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
                entity.CompanyId = model.CompanyId;
            }
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            entity.Value = model.Value;
            entity.ValueName = model.ValueName;

            return entity;
        }
 public static string SaveChartOfAccountValue(AccountValueViewModel model)
 {
     if (model.Id > 0)
     {
         return service.Update(getEntityByModel(model));
     }
     else
     {
         return service.Insert(getEntityByModel(model));
     }
 }
        public ActionResult Create(string segment)
        {
            AccountValueViewModel model = new AccountValueViewModel();
            Account account = AccountHelper.GetAccountBySOBId(SessionHelper.SOBId.ToString());
            if (account != null)
            {
                model.ChartId = account.Id;
                model.SetOfBook = SetOfBookHelper.GetSetOfBook(SessionHelper.SOBId.ToString()).Name;
                model.Segment = segment;
                SessionHelper.SelectedValue = segment;
                model.ValueChar = AccountHelper.GetSegmentCharacters(segment, account);
                return View("Edit", model);
            }

            return RedirectToAction("Index");
        }
        public ActionResult Edit(AccountValueViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.Value.Length != model.ValueChar)
                        ModelState.AddModelError("Error", "Invalid Value character length.");
                    else if (model.StartDate != null && model.StartDate > model.EndDate)
                        ModelState.AddModelError("Error", "End Date should be greater than StartDate.");
                    else
                    {
                        string result = AccountValueHelper.SaveChartOfAccountValue(model);
                        return RedirectToAction("Index", new { id = SessionHelper.SOBId });
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.InnerException.InnerException.Message);
                }
            }

            return View(model);
        }