Example #1
0
        /// <summary>
        /// Execute a mapping from the model to a new entity
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="model">Model to map from</param>
        /// <returns>Mapped entity</returns>
        public static TEntity ToEntity <TEntity>(this BaseQNetEntityModel model) where TEntity : BaseEntity
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(model.Map <TEntity>());
        }
        /// <summary>
        /// Save entity use code
        /// </summary>
        /// <param name="model">Base QNet entity model</param>
        private void SaveEntityUseCode(BaseQNetEntityModel model)
        {
            //ensure that received model is BaseQNetEntityModel
            if (model == null)
            {
                return;
            }

            //get entity by received model
            var entity = model is CustomerModel?_customerService.GetCustomerById(model.Id)
                             : model is CustomerRoleModel?_customerService.GetCustomerRoleById(model.Id)
                                 : model is ProductModel?_productService.GetProductById(model.Id)
                                     : model is CheckoutAttributeModel ? (BaseEntity)_checkoutAttributeService.GetCheckoutAttributeById(model.Id)
                : null;

            if (entity == null)
            {
                return;
            }

            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return;
            }

            //ensure that Avalara tax provider is active
            if (!_taxPluginManager.IsPluginActive(AvalaraTaxDefaults.SystemName))
            {
                return;
            }

            //whether there is a form value for the entity use code
            if (_httpContextAccessor.HttpContext.Request.Form.TryGetValue(AvalaraTaxDefaults.EntityUseCodeAttribute, out var entityUseCodeValue) &&
                !StringValues.IsNullOrEmpty(entityUseCodeValue))
            {
                //save attribute
                var entityUseCode = !entityUseCodeValue.ToString().Equals(Guid.Empty.ToString()) ? entityUseCodeValue.ToString() : null;
                _genericAttributeService.SaveAttribute(entity, AvalaraTaxDefaults.EntityUseCodeAttribute, entityUseCode);
            }
        }