public ActionResult Create()
 {
     if (AccountHelper.GetAccountBySOBId(SessionHelper.SOBId.ToString()) != null)
     {
         ViewBag.ErrorMessage = "One book can have maximum one chart of account!";
         return RedirectToAction("Index");
     }
     AccountCreateViewModel model = new AccountCreateViewModel();
     model.SOBId = SessionHelper.SOBId;
     return View("Edit", model);
 }
        private static Account getEntityByModel(AccountCreateViewModel model)
        {
            if (model == null) return null;

            Account entity = new Account();
            entity.Id = model.Id;
            entity.SegmentChar1 = model.SegmentChar1;
            entity.SegmentChar2 = model.SegmentChar2;
            entity.SegmentChar3 = model.SegmentChar3;
            entity.SegmentChar4 = model.SegmentChar4;
            entity.SegmentChar5 = model.SegmentChar5;
            entity.SegmentChar6 = model.SegmentChar6;
            entity.SegmentChar7 = model.SegmentChar7;
            entity.SegmentChar8 = model.SegmentChar8;
            entity.SegmentEnabled1 = model.SegmentEnabled1;
            entity.SegmentEnabled2 = model.SegmentEnabled2;
            entity.SegmentEnabled3 = model.SegmentEnabled3;
            entity.SegmentEnabled4 = model.SegmentEnabled4;
            entity.SegmentEnabled5 = model.SegmentEnabled5;
            entity.SegmentEnabled6 = model.SegmentEnabled6;
            entity.SegmentEnabled7 = model.SegmentEnabled7;
            entity.SegmentEnabled8 = model.SegmentEnabled8;
            entity.SegmentName1 = model.SegmentName1;
            entity.SegmentName2 = model.SegmentName2;
            entity.SegmentName3 = model.SegmentName3;
            entity.SegmentName4 = model.SegmentName4;
            entity.SegmentName5 = model.SegmentName5;
            entity.SegmentName6 = model.SegmentName6;
            entity.SegmentName7 = model.SegmentName7;
            entity.SegmentName8 = model.SegmentName8;
            entity.SOBId = model.SOBId;
            if (model.Id == 0)
            {
                entity.CompanyId = AuthenticationHelper.CompanyId.Value;
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
            }
            else
            {
                entity.CompanyId = model.CompanyId;
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
            }
            entity.UpdateDate = DateTime.Now;
            return entity;
        }
        public ActionResult Edit(AccountCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.SegmentEnabled1 == false)
                {
                    ModelState.AddModelError("Error", "First segment should be enabled.");
                    return View(model);
                }
                model.SOBId = SessionHelper.SOBId;
                if (model.Id > 0)
                {
                    string result = AccountHelper.SaveChartOfAccount(model);
                    if (result.Contains("can not be marked as disabled"))
                    {
                        ModelState.AddModelError("Error", result);
                    }
                    else
                        return RedirectToAction("Index");
                }
                else
                {
                    if (AccountHelper.GetAccountBySOBId(model.SOBId.ToString()) == null)
                    {
                        string result = AccountHelper.SaveChartOfAccount(model);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Account Already exists.");
                    }
                }
            }

            return View(model);
        }
 public static string SaveChartOfAccount(AccountCreateViewModel model)
 {
     if (model.Id > 0)
     {
         if (!string.IsNullOrEmpty(model.SegmentName1) && !model.SegmentEnabled1)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName1);
             if (accountValues.Any())
                 return "Segment 1 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName2) && !model.SegmentEnabled2)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName2);
             if (accountValues.Any())
                 return "Segment 2 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName3) && !model.SegmentEnabled3)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName3);
             if (accountValues.Any())
                 return "Segment 3 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName4) && !model.SegmentEnabled4)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName4);
             if (accountValues.Any())
                 return "Segment 4 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName5) && !model.SegmentEnabled5)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName5);
             if (accountValues.Any())
                 return "Segment 5 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName6) && !model.SegmentEnabled6)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName6);
             if (accountValues.Any())
                 return "Segment 6 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName7) && !model.SegmentEnabled7)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName7);
             if (accountValues.Any())
                 return "Segment 7 can not be marked as disabled";
         }
         else if (!string.IsNullOrEmpty(model.SegmentName8) && !model.SegmentEnabled8)
         {
             List<AccountValueViewModel> accountValues = AccountValueHelper.GetAccountValues(model.Id, SessionHelper.SOBId, model.SegmentName8);
             if (accountValues.Any())
                 return "Segment 8 can not be marked as disabled";
         }
         return service.Update(getEntityByModel(model));
     }
     else
     {
         return service.Insert(getEntityByModel(model));
     }
 }
 public static AccountCreateViewModel GetAccount(string id)
 {
     AccountCreateViewModel account = new AccountCreateViewModel
         (service.GetSingle(id, AuthenticationHelper.CompanyId.Value));
     return account;
 }