/// <summary> /// Gets selected checkout attributes /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the selected checkout attributes /// </returns> public virtual async Task <IList <CheckoutAttribute> > ParseCheckoutAttributesAsync(string attributesXml) { var result = new List <CheckoutAttribute>(); if (string.IsNullOrEmpty(attributesXml)) { return(result); } var ids = ParseCheckoutAttributeIds(attributesXml); foreach (var id in ids) { var attribute = await _checkoutAttributeService.GetCheckoutAttributeByIdAsync(id); if (attribute != null) { result.Add(attribute); } } return(result); }
/// <summary> /// Handle model received event /// </summary> /// <param name="eventMessage">Event message</param> /// <returns>A task that represents the asynchronous operation</returns> public async Task HandleEventAsync(ModelReceivedEvent <BaseNopModel> eventMessage) { //get entity by received model var entity = eventMessage.Model switch { CustomerModel customerModel => (BaseEntity)await _customerService.GetCustomerByIdAsync(customerModel.Id), CustomerRoleModel customerRoleModel => await _customerService.GetCustomerRoleByIdAsync(customerRoleModel.Id), ProductModel productModel => await _productService.GetProductByIdAsync(productModel.Id), CheckoutAttributeModel checkoutAttributeModel => await _checkoutAttributeService.GetCheckoutAttributeByIdAsync(checkoutAttributeModel.Id), _ => null }; if (entity == null) { return; } //ensure that Avalara tax provider is active if (!await _taxPluginManager.IsPluginActiveAsync(AvalaraTaxDefaults.SystemName)) { return; } if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageTaxSettings)) { 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; await _genericAttributeService.SaveAttributeAsync(entity, AvalaraTaxDefaults.EntityUseCodeAttribute, entityUseCode); } }
/// <summary> /// Invoke the widget view component /// </summary> /// <param name="widgetZone">Widget zone</param> /// <param name="additionalData">Additional parameters</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the view component result /// </returns> public async Task <IViewComponentResult> InvokeAsync(string widgetZone, object additionalData) { //ensure that model is passed if (additionalData is not BaseNopEntityModel entityModel) { return(Content(string.Empty)); } //ensure that Avalara tax provider is active if (!await _taxPluginManager.IsPluginActiveAsync(AvalaraTaxDefaults.SystemName)) { return(Content(string.Empty)); } if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageTaxSettings)) { return(Content(string.Empty)); } //ensure that it's a proper widget zone if (!widgetZone.Equals(AdminWidgetZones.CustomerDetailsBlock) && !widgetZone.Equals(AdminWidgetZones.CustomerRoleDetailsTop) && !widgetZone.Equals(AdminWidgetZones.ProductDetailsBlock) && !widgetZone.Equals(AdminWidgetZones.CheckoutAttributeDetailsBlock)) { return(Content(string.Empty)); } //get Avalara pre-defined entity use codes var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(AvalaraTaxDefaults.EntityUseCodesCacheKey); var cachedEntityUseCodes = await _staticCacheManager.GetAsync(cacheKey, async() => await _avalaraTaxManager.GetEntityUseCodesAsync()); var entityUseCodes = cachedEntityUseCodes?.Select(useCode => new SelectListItem { Value = useCode.code, Text = $"{useCode.name} ({string.Join(", ", useCode.validCountries)})" }).ToList() ?? new List <SelectListItem>(); //add the special item for 'undefined' with empty guid value var defaultValue = Guid.Empty.ToString(); entityUseCodes.Insert(0, new SelectListItem { Value = defaultValue, Text = await _localizationService.GetResourceAsync("Plugins.Tax.Avalara.Fields.EntityUseCode.None") }); //prepare model var model = new EntityUseCodeModel { Id = entityModel.Id, EntityUseCodes = entityUseCodes }; //get entity by the model identifier BaseEntity entity = null; if (widgetZone.Equals(AdminWidgetZones.CustomerDetailsBlock)) { model.PrecedingElementId = nameof(CustomerModel.IsTaxExempt); entity = await _customerService.GetCustomerByIdAsync(entityModel.Id); } if (widgetZone.Equals(AdminWidgetZones.CustomerRoleDetailsTop)) { model.PrecedingElementId = nameof(CustomerRoleModel.TaxExempt); entity = await _customerService.GetCustomerRoleByIdAsync(entityModel.Id); } if (widgetZone.Equals(AdminWidgetZones.ProductDetailsBlock)) { model.PrecedingElementId = nameof(ProductModel.IsTaxExempt); entity = await _productService.GetProductByIdAsync(entityModel.Id); } if (widgetZone.Equals(AdminWidgetZones.CheckoutAttributeDetailsBlock)) { model.PrecedingElementId = nameof(CheckoutAttributeModel.IsTaxExempt); entity = await _checkoutAttributeService.GetCheckoutAttributeByIdAsync(entityModel.Id); } //try to get previously saved entity use code model.AvalaraEntityUseCode = entity == null ? defaultValue : await _genericAttributeService.GetAttributeAsync <string>(entity, AvalaraTaxDefaults.EntityUseCodeAttribute); return(View("~/Plugins/Tax.Avalara/Views/EntityUseCode/EntityUseCode.cshtml", model)); }
protected virtual async Task SaveConditionAttributesAsync(CheckoutAttribute checkoutAttribute, CheckoutAttributeModel model) { string attributesXml = null; if (model.ConditionModel.EnableCondition) { var attribute = await _checkoutAttributeService.GetCheckoutAttributeByIdAsync(model.ConditionModel.SelectedAttributeId); if (attribute != null) { switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: { var selectedAttribute = model.ConditionModel.ConditionAttributes .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId); var selectedValue = selectedAttribute?.SelectedValueId; //for conditions we should empty values save even when nothing is selected //otherwise "attributesXml" will be empty //hence we won't be able to find a selected attribute attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(null, attribute, string.IsNullOrEmpty(selectedValue) ? string.Empty : selectedValue); } break; case AttributeControlType.Checkboxes: { var selectedAttribute = model.ConditionModel.ConditionAttributes .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId); var selectedValues = selectedAttribute?.Values .Where(x => x.Selected) .Select(x => x.Value) .ToList(); if (selectedValues?.Any() ?? false) { foreach (var value in selectedValues) { attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, value); } } else { attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(null, attribute, string.Empty); } } break; case AttributeControlType.ReadonlyCheckboxes: case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: case AttributeControlType.Datepicker: case AttributeControlType.FileUpload: default: //these attribute types are not supported as conditions break; } } } checkoutAttribute.ConditionAttributeXml = attributesXml; }