// GET: /ProductBuyerSettingMaster/Create

        public ActionResult Create(int id)
        {
            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 ProductBuyerSettingsService(_unitOfWork).GetProductBuyerSettings(DivisionId, SiteId);
            int DocTypeId  = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.Carpet).DocumentTypeId;



            if (settings == null)
            {
                ProductBuyerSettingsViewModel vm = new ProductBuyerSettingsViewModel();
                vm.SiteId     = SiteId;
                vm.DivisionId = DivisionId;
                vm.ProductId  = id;
                PrepareViewBag(vm);
                ViewBag.DocTypeId = DocTypeId;
                return(View("Create", vm));
            }
            else
            {
                ProductBuyerSettingsViewModel temp = AutoMapper.Mapper.Map <ProductBuyerSettings, ProductBuyerSettingsViewModel>(settings);
                temp.ProductId = id;
                PrepareViewBag(temp);
                ViewBag.DocTypeId = DocTypeId;
                return(View("Create", temp));
            }
        }
        public ActionResult Post(ProductBuyerSettingsViewModel vm)
        {
            ProductBuyerSettings pt = AutoMapper.Mapper.Map <ProductBuyerSettingsViewModel, ProductBuyerSettings>(vm);


            if (ModelState.IsValid)
            {
                if (vm.ProductBuyerSettingsId <= 0)
                {
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _ProductBuyerSettingsService.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.ProductBuyerSettingsId,
                        DocTypeId    = DocTypeId,
                        ActivityType = (int)ActivityTypeContants.SettingsAdded,
                    }));



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

                    ProductBuyerSettings temp = _ProductBuyerSettingsService.Find(pt.ProductBuyerSettingsId);

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

                    temp.BuyerSpecificationDisplayName  = vm.BuyerSpecificationDisplayName;
                    temp.BuyerSpecification1DisplayName = vm.BuyerSpecification1DisplayName;
                    temp.BuyerSpecification2DisplayName = vm.BuyerSpecification2DisplayName;
                    temp.BuyerSpecification3DisplayName = vm.BuyerSpecification3DisplayName;
                    temp.BuyerSpecification4DisplayName = vm.BuyerSpecification4DisplayName;
                    temp.BuyerSpecification5DisplayName = vm.BuyerSpecification5DisplayName;
                    temp.BuyerSpecification6DisplayName = vm.BuyerSpecification6DisplayName;
                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    _ProductBuyerSettingsService.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.ProductBuyerSettingsId,
                        ActivityType    = (int)ActivityTypeContants.SettingsModified,
                        DocTypeId       = DocTypeId,
                        xEModifications = Modifications,
                    }));

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