public virtual IActionResult Create(MaterialModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(XrmsPermissionProvider.ManageMaterials))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                //a vendor should have access only to his materials

                /*if (_workContext.CurrentVendor != null)
                 * {
                 *  model.VendorId = _workContext.CurrentVendor.Id;
                 * }*/

                //material
                var material = model.ToEntity();
                material.CreatedOnUtc = DateTime.UtcNow;
                material.UpdatedOnUtc = DateTime.UtcNow;
                _materialService.InsertMaterial(material);

                // quantity change history
                _materialService.AddStockQuantityHistoryEntry(material, material.StockQuantity, material.StockQuantity, material.WarehouseId,
                                                              _localizationService.GetResource("Xrms.Admin.Catalog.Materials.StockQuantityHistory.Messages.Edit"));

                // activity log
                _customerActivityService.InsertActivity("AddNewMaterial", _localizationService.GetResource("Xrms.ActivityLog.AddNewMaterial"), material.Name);

                SuccessNotification(_localizationService.GetResource("Xrms.Admin.Catalog.Materials.Notifications.Added"));

                if (continueEditing)
                {
                    //selected tab
                    SaveSelectedTabName();

                    return(RedirectToAction("Edit", new { id = material.Id }));
                }

                return(RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            var viewModel = new MaterialDetailsPageViewModel();

            model.ToDetailsViewModel(viewModel);
            //viewModel.MaterialGroupInfo = model;

            // groups
            PrepareAvailableMaterialGroups(viewModel);
            // warehouses
            PrepareAvailableWarehouses(viewModel);

            return(View("~/Plugins/Xrms.MaterialAdmin/Areas/Admin/Views/MaterialGroup/Create.cshtml", viewModel));
        }
        public virtual IActionResult Edit(int id, MaterialModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(XrmsPermissionProvider.ManageMaterials))
            {
                return(AccessDeniedView());
            }

            var material = _materialService.GetMaterialById(id);

            if (material == null || material.Deleted)
            {
                //No material found with the specified id
                return(RedirectToAction("List"));
            }

            //check if the material quantity has been changed while we were editing the material
            //and if it has been changed then we show error notification
            //and redirect on the editing page without data saving

            /*if (material.StockQuantity != model.LastStockQuantity)
             * {
             *  ErrorNotification(_localizationService.GetResource("Admin.Catalog.Materials.Fields.StockQuantity.ChangedWarning"));
             *  return RedirectToAction("Edit", new { id = material.Id });
             * }*/

            if (ModelState.IsValid)
            {
                //some previously used values
                var previousWarehouseId   = material.WarehouseId;
                var previousStockQuantity = material.StockQuantity;

                //material
                material = model.ToEntity(material);

                material.UpdatedOnUtc = DateTime.UtcNow;
                _materialService.UpdateMaterial(material);

                // quantity change history
                if (previousWarehouseId != material.WarehouseId)
                {
                    //warehouse is changed
                    //compose a message
                    var oldWarehouseMessage = string.Empty;
                    if (previousWarehouseId > 0)
                    {
                        var oldWarehouse = _shippingService.GetWarehouseById(previousWarehouseId);
                        if (oldWarehouse != null)
                        {
                            oldWarehouseMessage = string.Format(_localizationService.GetResource("Xrms.Admin.Catalog.Materials.StockQuantityHistory.Messages.EditWarehouse.Old"), oldWarehouse.Name);
                        }
                    }
                    var newWarehouseMessage = string.Empty;
                    if (material.WarehouseId > 0)
                    {
                        var newWarehouse = _shippingService.GetWarehouseById(material.WarehouseId);
                        if (newWarehouse != null)
                        {
                            newWarehouseMessage = string.Format(_localizationService.GetResource("Xrms.Admin.Catalog.Materials.StockQuantityHistory.Messages.EditWarehouse.New"), newWarehouse.Name);
                        }
                    }
                    var message = string.Format(_localizationService.GetResource("Xrms.Admin.Catalog.Materials.StockQuantityHistory.Messages.EditWarehouse"), oldWarehouseMessage, newWarehouseMessage);

                    //record history
                    _materialService.AddStockQuantityHistoryEntry(material, -previousStockQuantity, 0, previousWarehouseId, message);
                    _materialService.AddStockQuantityHistoryEntry(material, material.StockQuantity, material.StockQuantity, material.WarehouseId, message);
                }
                else
                {
                    _materialService.AddStockQuantityHistoryEntry(material, material.StockQuantity - previousStockQuantity, material.StockQuantity,
                                                                  material.WarehouseId, _localizationService.GetResource("Xrms.Admin.Catalog.Materials.StockQuantityHistory.Messages.Edit"));
                }

                //activity log
                _customerActivityService.InsertActivity("EditMaterial", _localizationService.GetResource("Xrms.ActivityLog.EditMaterial"), material.Name);

                SuccessNotification(_localizationService.GetResource("Xrms.Admin.Catalog.Materials.Notifications.Updated"));

                if (continueEditing)
                {
                    //selected tab
                    SaveSelectedTabName();

                    return(RedirectToAction("Edit", new { id = material.Id }));
                }
                return(RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            var viewModel = new MaterialDetailsPageViewModel();

            model.ToDetailsViewModel(viewModel);
            // groups
            PrepareAvailableMaterialGroups(viewModel);
            // warehouses
            PrepareAvailableWarehouses(viewModel);

            return(View("~/Plugins/Xrms.MaterialAdmin/Areas/Admin/Views/MaterialGroup/Edit.cshtml", viewModel));
        }