public ActionResult Create()
 {
     CodeCombinitionCreateViewModel model =  new CodeCombinitionCreateViewModel();
     model.SegmentList = AccountHelper.GetSegmentListForCodeCombination(SessionHelper.SOBId.ToString());
     model.SOBId = Convert.ToInt64(SessionHelper.SOBId.ToString());
     return PartialView("_Create", model);
 }
        public ActionResult UpdateCodeCombinition(long id, string segmentValues, bool allowPosting, string startDate, string endDate)
        {
            try
            {
                var segmentList = segmentValues.Split(new char[] { '±' }).ToList<string>();
                CodeCombinitionCreateViewModel model = new CodeCombinitionCreateViewModel();
                for (var i = 1; i <= segmentList.Count; i++)
                {
                    string segmentValue = segmentList[i - 1];
                    if (string.IsNullOrEmpty(segmentValue))
                        continue;
                    else
                        segmentValue = segmentValue.Substring(2);
                    switch (i)
                    {
                        case 1:
                            model.Segment1 = segmentValue;
                            break;
                        case 2:
                            model.Segment2 = segmentValue;
                            break;
                        case 3:
                            model.Segment3 = segmentValue;
                            break;
                        case 4:
                            model.Segment4 = segmentValue;
                            break;
                        case 5:
                            model.Segment5 = segmentValue;
                            break;
                        case 6:
                            model.Segment6 = segmentValue;
                            break;
                        case 7:
                            model.Segment7 = segmentValue;
                            break;
                        case 8:
                            model.Segment8 = segmentValue;
                            break;
                    }
                }

                model.AllowedPosting = allowPosting;
                model.CompanyId = AuthenticationHelper.CompanyId.Value;
                model.EndDate = endDate == "" ? null : (DateTime?)Convert.ToDateTime(endDate);
                model.StartDate = startDate == "" ? null : (DateTime?)Convert.ToDateTime(startDate);
                model.Id = id;
                model.SOBId = SessionHelper.SOBId;

                string result = CodeCombinationHelper.SaveCodeCombination(model);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }
        private static CodeCombinition getEntityByModel(CodeCombinitionCreateViewModel model)
        {
            if (model == null) return null;

            var entity = new CodeCombinition();

            entity.AllowedPosting = model.AllowedPosting;
            entity.EndDate = model.EndDate;
            entity.Id = model.Id;
            entity.Segment1 = model.Segment1;
            entity.Segment2 = model.Segment2;
            entity.Segment3 = model.Segment3;
            entity.Segment4 = model.Segment4;
            entity.Segment5 = model.Segment5;
            entity.Segment6 = model.Segment6;
            entity.Segment7 = model.Segment7;
            entity.Segment8 = model.Segment8;
            entity.SOBId = model.SOBId;
            entity.StartDate = model.StartDate;

            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.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;

            return entity;
        }
 public static string SaveCodeCombination(CodeCombinitionCreateViewModel model)
 {
     List<CodeCombinition> codeCombinitions = service.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
     if (model.Id > 0)
     {
         if (codeCombinitions.Where(rec => rec.Id != model.Id && rec.Segment1 == model.Segment1 && rec.Segment2 == model.Segment2 && rec.Segment3 == model.Segment3 && rec.Segment4 == model.Segment4 && rec.Segment5 == model.Segment5 && rec.Segment6 == model.Segment6 && rec.Segment7 == model.Segment7 && rec.Segment8 == model.Segment8).Any())
         {
             return "Combinition already exists";
         }
         return service.Update(getEntityByModel(model));
     }
     else
     {
         if (codeCombinitions.Any())
         {
             if (codeCombinitions.Where(rec => rec.Segment1 == model.Segment1 && rec.Segment2 == model.Segment2 && rec.Segment3 == model.Segment3 && rec.Segment4 == model.Segment4 && rec.Segment5 == model.Segment5 && rec.Segment6 == model.Segment6 && rec.Segment7 == model.Segment7 && rec.Segment8 == model.Segment8).Any())
             {
                 return "Combinition already exists";
             }
         }
         return service.Insert(getEntityByModel(model));
     }
 }