Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Enabled option for taxrates #3798

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
- Order conditions can now have a “Coupon Code” rule. ([#3776](https://github.com/craftcms/commerce/discussions/3776))
- Order conditions can now have a “Payment Gateway” rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722))
- Variant conditions can now have a “Product” rule.
- Tax rates now have statuses. ([#3790](https://github.com/craftcms/commerce/discussions/3790))

### Administration
- Added support for `to`, `bcc`, and `cc` email fields to support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738))
- The `to`, `bcc`, and `cc` email fields now support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738))

### Development
- Added the `couponCode` order query param.
- Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430))
- Added a new "Payment Gateway" order condition rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722))

### Extensibility
- Added `craft\commerce\base\InventoryItemTrait`.
Expand All @@ -22,6 +24,7 @@
- Added `craft\commerce\elements\db\OrderQuery::couponCode()`.
- Added `craft\commerce\services\Inventory::updateInventoryLevel()`.
- Added `craft\commerce\services\Inventory::updatePurchasableInventoryLevel()`.
- Added `craft\commerce\services\TaxRates::getAllEnabledTaxRates()`.

### System
- Craft Commerce now requires Craft CMS 5.5 or later.
637 changes: 343 additions & 294 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/adjusters/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ private function _adjustInternal(): array
$adjustments = [];

foreach ($this->_taxRates as $rate) {
if (!$rate->enabled) {
continue;
}
$newAdjustments = $this->_getAdjustments($rate);
if ($newAdjustments) {
$adjustments[] = $newAdjustments;
Expand Down Expand Up @@ -286,7 +289,7 @@ private function _getAdjustments(TaxRate $taxRate): array
*/
protected function getTaxRates(?int $storeId = null): Collection
{
return Plugin::getInstance()->getTaxRates()->getAllTaxRates($storeId);
return Plugin::getInstance()->getTaxRates()->getAllEnabledTaxRates($storeId);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/controllers/TaxRatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function actionSave(): void
$taxRate->taxCategoryId = (int)$this->request->getBodyParam('taxCategoryId') ?: null;
$taxRate->taxZoneId = (int)$this->request->getBodyParam('taxZoneId') ?: null;
$taxRate->rate = Localization::normalizePercentage($this->request->getBodyParam('rate'));
$taxRate->enabled = (bool)($this->request->getBodyParam('enabled'));

// Save it
if (Plugin::getInstance()->getTaxRates()->saveTaxRate($taxRate)) {
Expand Down Expand Up @@ -247,4 +248,34 @@ public function actionDelete(): Response
Plugin::getInstance()->getTaxRates()->deleteTaxRateById($id);
return $this->asSuccess();
}

/**
* @throws BadRequestHttpException
* @throws Exception
* @since 5.x
*/
public function actionUpdateStatus(): void
{
$this->requirePostRequest();
$ids = $this->request->getRequiredBodyParam('ids');
$status = $this->request->getRequiredBodyParam('status');

if (empty($ids)) {
$this->setFailFlash(Craft::t('commerce', 'Couldn’t update status.'));
}

$transaction = Craft::$app->getDb()->beginTransaction();
$taxRates = TaxRateRecord::find()
->where(['id' => $ids])
->all();

/** @var TaxRateRecord $taxRate */
foreach ($taxRates as $taxRate) {
$taxRate->enabled = ($status == 'enabled');
$taxRate->save();
}
$transaction->commit();

$this->setSuccessFlash(Craft::t('commerce', 'Tax rates updated.'));
}
}
33 changes: 33 additions & 0 deletions src/migrations/m241204_045158_enable_tax_rate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace craft\commerce\migrations;

use craft\db\Migration;

/**
* m241204_045158_enable_tax_rate migration.
*/
class m241204_045158_enable_tax_rate extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// if column doesnt exist
if (!$this->db->columnExists('{{%commerce_taxrates}}', 'enabled')) {
$this->addColumn('{{%commerce_taxrates}}', 'enabled', $this->boolean()->notNull()->defaultValue(true));
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m241204_045158_enable_tax_rate cannot be reverted.\n";
return false;
}
}
6 changes: 6 additions & 0 deletions src/models/TaxRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class TaxRate extends Model implements HasStoreInterface
*/
public ?DateTime $dateUpdated = null;

/**
* @var bool Whether the tax rate is enabled
*/
public bool $enabled = true;

/**
* @var TaxCategory|null
*/
Expand Down Expand Up @@ -144,6 +149,7 @@ protected function defineRules(): array
'taxable',
'taxCategoryId',
'taxZoneId',
'enabled',
], 'safe'];

return $rules;
Expand Down
1 change: 1 addition & 0 deletions src/records/TaxRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property TaxZone $taxZone
* @property bool $isEverywhere
* @property int $taxZoneId
* @property bool $enabled
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
*/
Expand Down
18 changes: 18 additions & 0 deletions src/services/TaxRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public function getAllTaxRates(?int $storeId = null): Collection
return $this->_allTaxRates[$storeId] ?? collect();
}

/**
* @param int|null $storeId
* @return Collection
* @throws InvalidConfigException
* @throws StoreNotFoundException
* @since 5.3.0
*/
public function getAllEnabledTaxRates(?int $storeId = null): Collection
{
return $this->getAllTaxRates($storeId)->where('enabled', true);
}

/**
* Returns an array of all rates belonging to the specified zone.
*
Expand Down Expand Up @@ -143,6 +155,7 @@ public function saveTaxRate(TaxRate $model, bool $runValidation = true): bool
$record->taxCategoryId = $model->taxCategoryId;
$record->taxZoneId = $model->taxZoneId ?: null;
$record->isEverywhere = $model->getIsEverywhere();
$record->enabled = $model->enabled;

if (!$record->isEverywhere && $record->taxZoneId && empty($record->getErrors('taxZoneId'))) {
$taxZone = Plugin::getInstance()->getTaxZones()->getTaxZoneById($record->taxZoneId, $record->storeId);
Expand Down Expand Up @@ -211,6 +224,11 @@ private function _createTaxRatesQuery(): Query
->orderBy(['include' => SORT_DESC, 'isVat' => SORT_DESC])
->from([Table::TAXRATES]);

// if enabled column exists add the select
if (Craft::$app->getDb()->columnExists(Table::TAXRATES, 'enabled')) {
$query->addSelect(['enabled']);
}

return $query;
}

Expand Down
11 changes: 11 additions & 0 deletions src/templates/store-management/tax/taxrates/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
{ label: "Tax Rates"|t('commerce'), url: store.getStoreSettingsUrl('taxrates') },
] %}

{% import "_includes/forms" as forms %}
{% set selectedSubnavItem = 'tax' %}

{% set fullPageForm = true %}

{% block details %}
<div class="meta">
{{ forms.lightSwitchField({
label: "Enable this tax rate"|t('commerce'),
id: 'enabled',
name: 'enabled',
on: taxRate.enabled,
errors: taxRate.getErrors('enabled')
}) }}
</div>

{% if taxRate and taxRate.id %}
<div class="meta read-only">
<div class="data">
Expand Down
27 changes: 26 additions & 1 deletion src/templates/store-management/tax/taxrates/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{% for taxRate in taxRates -%}
{%- set tableData = tableData|merge([{
id: taxRate.id,
status: taxRate.enabled,
title: taxRate.name|t('site'),
url: taxRate.getCpEditUrl(),
rate: taxRate.rateAsPercent,
Expand Down Expand Up @@ -70,11 +71,35 @@ var columns = [
if (value) {
return '<a href="'+value.url+'">'+value.label+'</a>';
}
} },
} }
];

var actions = [
{
label: Craft.t('commerce', 'Set status'),
actions: [
{
label: Craft.t('commerce', 'Enabled'),
action: 'commerce/tax-rates/update-status',
param: 'status',
value: 'enabled',
status: 'enabled'
},
{
label: Craft.t('commerce', 'Disabled'),
action: 'commerce/tax-rates/update-status',
param: 'status',
value: 'disabled',
status: 'disabled'
}
]
}
];

new Craft.VueAdminTable({
columns: columns,
actions: actions,
checkboxes: true,
container: '#taxrate-vue-admin-table',
deleteAction: {{ craft.commerce.taxes.deleteTaxRates() ? 'commerce/tax-rates/delete'|json_encode|raw : 'null' }},
tableData: {{ tableData|json_encode|raw }},
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en/commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
'Enable this sale' => 'Enable this sale',
'Enable this shipping method on the front end' => 'Enable this shipping method on the front end',
'Enable this shipping rule' => 'Enable this shipping rule',
'Enable this tax rate' => 'Enable this tax rate',
'Enable' => 'Enable',
'Enabled for customers to select during checkout?' => 'Enabled for customers to select during checkout?',
'Enabled for customers to select?' => 'Enabled for customers to select?',
Expand Down Expand Up @@ -1010,7 +1011,6 @@
'Shipping costs added to the order as a whole before percentage, item, and weight rates are applied. Set to zero to disable this rate. The whole rule, including this base rate, will not match and apply if the cart only contains non-shippable items like digital products.' => 'Shipping costs added to the order as a whole before percentage, item, and weight rates are applied. Set to zero to disable this rate. The whole rule, including this base rate, will not match and apply if the cart only contains non-shippable items like digital products.',
'Shipping method saved.' => 'Shipping method saved.',
'Shipping methods and rules deleted.' => 'Shipping methods and rules deleted.',
'Shipping methods updated.' => 'Shipping methods updated.',
'Shipping rule saved.' => 'Shipping rule saved.',
'Shipping zone saved.' => 'Shipping zone saved.',
'Shipping' => 'Shipping',
Expand Down Expand Up @@ -1098,6 +1098,7 @@
'Tax category saved.' => 'Tax category saved.',
'Tax category updated.' => 'Tax category updated.',
'Tax rate saved.' => 'Tax rate saved.',
'Tax rates updated.' => 'Tax rates updated.',
'Tax zone saved.' => 'Tax zone saved.',
'Tax' => 'Tax',
'Taxable Subject' => 'Taxable Subject',
Expand Down
Loading