// GET: /ProductCategorySettingMaster/Create

        public ActionResult Create(int id)//ProductCategoryId
        {
            if (!UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            ProductCategoryProcessSettingsViewModel vm = new ProductCategoryProcessSettingsViewModel();

            vm.ProductCategoryId = id;
            PrepareViewBag(vm);
            return(View("Create", vm));
        }
        public ActionResult Edit(int id)//ProductCategoryProcessSettingsId
        {
            if (!UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            var settings = new ProductCategoryProcessSettingsService(_unitOfWork).Find(id);
            ProductCategoryProcessSettingsViewModel temp = AutoMapper.Mapper.Map <ProductCategoryProcessSettings, ProductCategoryProcessSettingsViewModel>(settings);

            PrepareViewBag(temp);
            return(View("Create", temp));
        }
        public ActionResult Post(ProductCategoryProcessSettingsViewModel vm)
        {
            ProductCategoryProcessSettings pt = AutoMapper.Mapper.Map <ProductCategoryProcessSettingsViewModel, ProductCategoryProcessSettings>(vm);


            if (ModelState.IsValid)
            {
                ProductCategory ProductCategory = new ProductCategoryService(_unitOfWork).Find(vm.ProductCategoryId);
                if (vm.ProductCategoryProcessSettingsId <= 0)
                {
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _ProductCategoryProcessSettingsService.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.ProductCategoryProcessSettingsId,
                        DocTypeId    = DocTypeId,
                        ActivityType = (int)ActivityTypeContants.SettingsAdded,
                    }));



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

                    ProductCategoryProcessSettings temp = _ProductCategoryProcessSettingsService.Find(pt.ProductCategoryProcessSettingsId);

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

                    temp.ProcessId    = vm.ProcessId;
                    temp.QAGroupId    = vm.QAGroupId;
                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    _ProductCategoryProcessSettingsService.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.ProductCategoryProcessSettingsId,
                        ActivityType    = (int)ActivityTypeContants.SettingsModified,
                        DocTypeId       = DocTypeId,
                        xEModifications = Modifications,
                    }));

                    return(RedirectToAction("Index", "ProductCategoryProcessSettings", new { id = ProductCategory.ProductCategoryId }).Success("Data saved successfully"));
                }
            }
            PrepareViewBag(vm);
            return(View("Create", vm));
        }
 private void PrepareViewBag(ProductCategoryProcessSettingsViewModel s)
 {
     ViewBag.Name = new ProductCategoryService(_unitOfWork).Find(s.ProductCategoryId).ProductCategoryName;
     ViewBag.id   = s.ProductCategoryId;
 }