{% import "_includes/forms" as forms %} {% macro lazyCreateSelect(selectParams, newOptionLabel, modalHeading, html, js, action, successText) %} {% from "_includes/forms" import selectField %} {{ selectField(selectParams) }} {% js %} (function() { var $select = $('#{{ selectParams.id }}').selectize({ dropdownParent: 'body' }); var selectize = $select.get(0).selectize; selectize.addOption({ value: 'new', text: '{{ newOptionLabel|e("js") }}' }); selectize.on('item_add', function(value, $item) { if (value == 'new') { // Not a real option selectize.clear(); var $form = $( '
'); var modal = new Garnish.Modal($form, { onHide: function() { modal.destroy(); delete modal; } }); eval('{{ js|e("js") }}'); setTimeout(function() { $form.find('.text:first').focus(); modal.updateSizeAndPosition(); $form.find('.body:first').on('resize', function() { modal.updateSizeAndPosition(); }); }, 100); $form.on('submit', function(ev) { ev.preventDefault(); $form.find('.spinner').removeClass('hidden'); var namespacedData = Garnish.getPostData($form), data = {}; for (var name in namespacedData) { data[name.replace(/^new\[([^\]]+)\]/, '$1')] = namespacedData[name]; } Craft.postActionRequest('{{ action|e("js") }}', data, function(response, textStatus) { $form.find('.spinner').addClass('hidden'); if (textStatus == 'success') { if (response.success) { Craft.cp.displayNotice('{{ successText|e("js") }}'); selectize.addOption({ value: response.id, text: response.name, $order: selectize.order - 1 }); selectize.setValue(response.id); modal.hide(); } else { Craft.ui.clearErrorsFromField($form.find('.field')); for (var attribute in response.errors) { Craft.ui.addErrorsToField($('#new-'+attribute+'-field'), response.errors[attribute]); } modal.updateSizeAndPosition(); Garnish.shake($form); } } }); }); $form.find('.cancel-btn').on('click', function() { modal.hide(); }); } }); })(); {% endjs %} {% endmacro %} {% from _self import lazyCreateSelect %} {{ forms.textField({ first: true, label: "Name"|t('commerce'), instructions: "What this tax rate will be called in the CP."|t('commerce'), name: 'name', value: taxRate.name, errors: taxRate.getErrors('name'), autofocus: true, required: true, translatable: true }) }} {{ forms.selectField({ label: 'Taxable Subject'|t('commerce'), instructions: "What this tax rate should apply to."|t('commerce'), id: 'taxable', name: 'taxable', options: taxables, value: taxRate.taxable, }) }} {{ forms.textField({ label: "Rate"|t('commerce'), instructions: "Tax rates are a percentages, (i.e. if the tax rate is 5% then enter ‘5%’. You could also enter ‘10.5%’)."|t('commerce'), name: 'rate', value: taxRate.rateAsPercent, errors: taxRate.getErrors('rate'), required: true, }) }} {% set taxZoneSelectParams = { label: "Tax Zone"|t('commerce'), instructions: "Select a tax zone. If no zone is selected, this rate will match anywhere."|t('commerce'), id: 'taxZoneId', name: 'taxZoneId', value: taxRate.taxZoneId, errors: taxRate.getErrors('taxZoneId'), required: false, options: taxZones, class: 'selectize fullwidth', } %} {{ lazyCreateSelect( taxZoneSelectParams, 'New tax zone'|t('commerce'), 'Create a new tax zone'|t('commerce'), newTaxZoneFields, newTaxZoneJs, 'commerce/tax-zones/save', 'Tax zone saved.'|t ) }} {% set taxCategorySelectParams = { label: "Tax Category"|t('commerce'), instructions: "Select a tax category."|t('commerce'), id: 'taxCategoryId', name: 'taxCategoryId', value: taxRate.taxCategoryId, errors: taxRate.getErrors('taxCategoryId'), required: true, options: taxCategories, class: 'selectize fullwidth', } %} {{ lazyCreateSelect( taxCategorySelectParams, 'New tax category'|t('commerce'), 'Create a new tax category'|t('commerce'), newTaxCategoryFields, newTaxCategoryJs, 'commerce/tax-categories/save', 'Tax category saved.'|t ) }} {{ forms.lightSwitchField({ label: "Is this tax is already included in the taxable subject?"|t('commerce'), name: 'include', instructions: "If enabled, and this tax rate does not match the order, this included tax rate amount will be removed from the order."|t('commerce'), on: taxRate.include, checked: taxRate.include, errors: taxRate.getErrors('include'), }) }} {{ forms.lightSwitchField({ label: "Don’t apply this tax when the address has a valid VAT ID"|t('commerce'), name: 'isVat', on: taxRate.isVat, errors: taxRate.getErrors('isVat'), toggle: '#isVat' }) }}{% if craft.commerce.settings.useBillingAddressForTax %} {{ "This tax rate will not be applied if a valid businessTaxId is entered into the billing address."|t('commerce') }} {% else %} {{ "This tax rate will not be applied if a valid businessTaxId is entered into the shipping address."|t('commerce') }} {% endif %}