// GET: /ProductGroupSettingMaster/Create

        public ActionResult Create(int id)//ProductGroupId
        {
            if (!UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            var DivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            var SiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            var settings   = new ProductGroupSettingsService(_unitOfWork).GetProductGroupSettings(id);

            if (settings == null)
            {
                ProductGroupSettingsViewModel vm = new ProductGroupSettingsViewModel();
                vm.ProductGroupId = id;
                PrepareViewBag(vm);
                return(View("Create", vm));
            }
            else
            {
                ProductGroupSettingsViewModel temp = AutoMapper.Mapper.Map <ProductGroupSettings, ProductGroupSettingsViewModel>(settings);
                PrepareViewBag(temp);
                return(View("Create", temp));
            }
        }
        public ActionResult Post(ProductGroupSettingsViewModel vm)
        {
            ProductGroupSettings pt = AutoMapper.Mapper.Map <ProductGroupSettingsViewModel, ProductGroupSettings>(vm);


            if (ModelState.IsValid)
            {
                ProductGroup ProductGroup = new ProductGroupService(_unitOfWork).Find(vm.ProductGroupId);
                if (vm.ProductGroupSettingsId <= 0)
                {
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _ProductGroupSettingsService.Create(pt);

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag(vm);
                        return(View("Create", vm));
                    }

                    int DocTypeId = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.Carpet).DocumentTypeId;

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocId        = pt.ProductGroupSettingsId,
                        DocTypeId    = DocTypeId,
                        ActivityType = (int)ActivityTypeContants.SettingsAdded,
                    }));



                    return(RedirectToAction("Index", "ProductGroup", new { id = ProductGroup.ProductTypeId }).Success("Data saved successfully"));
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    ProductGroupSettings temp = _ProductGroupSettingsService.Find(pt.ProductGroupSettingsId);

                    ProductGroupSettings ExRec = Mapper.Map <ProductGroupSettings>(temp);

                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    _ProductGroupSettingsService.Update(temp);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = temp,
                    });

                    XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag(vm);
                        return(View("Create", pt));
                    }

                    int DocTypeId = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.Carpet).DocumentTypeId;

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocId           = temp.ProductGroupSettingsId,
                        ActivityType    = (int)ActivityTypeContants.SettingsModified,
                        DocTypeId       = DocTypeId,
                        xEModifications = Modifications,
                    }));

                    return(RedirectToAction("Index", "ProductGroup", new { id = ProductGroup.ProductTypeId }).Success("Data saved successfully"));
                }
            }
            PrepareViewBag(vm);
            return(View("Create", vm));
        }
 private void PrepareViewBag(ProductGroupSettingsViewModel s)
 {
     int SiteId = (int)System.Web.HttpContext.Current.Session["SiteId"];
 }