This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 17a66c5
Showing
8 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 schoene neue kinder GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SNK Tax Updater Magento 2 | ||
|
||
**Extension for Magento 2** | ||
|
||
## Overview | ||
|
||
The module allows changing tax rates through magento console. | ||
|
||
## Requirements | ||
|
||
Magento 2.2+, PHP >7.1 | ||
|
||
## Usage | ||
|
||
``` | ||
bin/magento tax:rate:update [--id [TAX RATE ID]] [--country [COUNTRY ISO2 CODE]] [--old-rate OLD RATE] [--new-rate [NEW RATE]] [--dry-run [DRY RUN BOOL]] | ||
``` | ||
|
||
## Authors | ||
|
||
Oleh Kravets <a href="mailto:[email protected]">[email protected]</a> | ||
|
||
## Lisence | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "snk/magento2-module_tax-updater", | ||
"description": "Magento 2 Console Command for Updating Tax Rates", | ||
"require": { | ||
"magento/magento2-base": "^2.2", | ||
"php": "^7.1" | ||
}, | ||
"type": "magento2-module", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Oleh Kravets", | ||
"email": "[email protected]", | ||
"homepage": "https://www.snk.de", | ||
"role": "Technical Support" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"keywords": [ | ||
"magento 2", | ||
"snk" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"src/registration.php" | ||
], | ||
"psr-4": { | ||
"Snk\\TaxUpdater\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
/** | ||
* @author Oleh Kravets <[email protected]> | ||
* @copyright Copyright (c) 2020 schoene neue kinder GmbH (https://www.snk.de) | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
*/ | ||
namespace Snk\TaxUpdater\Console; | ||
|
||
use Magento\Framework\{ | ||
Api\SearchCriteriaBuilder, | ||
Api\SearchCriteriaBuilderFactory, | ||
Exception\InputException, | ||
Exception\NoSuchEntityException | ||
}; | ||
use Magento\Tax\{ | ||
Api\Data\TaxRateInterface, | ||
Api\TaxRateRepositoryInterface | ||
}; | ||
use Symfony\Component\{ | ||
Console\Command\Command, | ||
Console\Input\InputInterface, | ||
Console\Input\InputOption, | ||
Console\Output\OutputInterface | ||
}; | ||
|
||
class UpdateTaxRateCommand extends Command | ||
{ | ||
const OPTION_ID = 'id'; | ||
const OPTION_COUNTRY_CODE = 'country'; | ||
const OPTION_OLD_RATE = 'old-rate'; | ||
const OPTION_NEW_RATE = 'new-rate'; | ||
const OPTION_DRY_RUN = 'dry-run'; | ||
|
||
/** | ||
* @var TaxRateRepositoryInterface | ||
*/ | ||
private $taxRateRepository; | ||
/** | ||
* @var SearchCriteriaBuilderFactory | ||
*/ | ||
private $criteriaBuilderFactory; | ||
|
||
public function __construct( | ||
TaxRateRepositoryInterface $taxRateRepository, | ||
SearchCriteriaBuilderFactory $criteriaBuilderFactory | ||
) { | ||
parent::__construct(); | ||
$this->taxRateRepository = $taxRateRepository; | ||
$this->criteriaBuilderFactory = $criteriaBuilderFactory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('tax:rate:update') | ||
->setDescription('Updates the rate of existing tax rates.') | ||
->addOption( | ||
self::OPTION_ID, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Tax rate ID' | ||
) | ||
->addOption( | ||
self::OPTION_COUNTRY_CODE, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Tax rate country code.' | ||
) | ||
->addOption( | ||
self::OPTION_NEW_RATE, | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'New tax rate.' | ||
) | ||
->addOption( | ||
self::OPTION_OLD_RATE, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Old tax rate.' | ||
) | ||
->addOption( | ||
self::OPTION_DRY_RUN, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Dry run: do not save the tax rate.' | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$taxRateId = $input->getOption(self::OPTION_ID); | ||
$newRate = (float) $input->getOption(self::OPTION_NEW_RATE); | ||
|
||
if (!$newRate) { | ||
$output->writeln('<error>New tax rate must be specified</error>'); | ||
return 1; | ||
} | ||
|
||
if ($taxRateId) { | ||
try { | ||
$taxRate = $this->taxRateRepository->get($taxRateId); | ||
$this->saveNewRate($output, $taxRate, $newRate); | ||
} catch (NoSuchEntityException $exception) { | ||
$output->writeln(sprintf('<warning>No Tax Rate with ID %s found </warning>', $taxRateId)); | ||
return 1; | ||
} | ||
} else { | ||
$countryCode = $input->getOption(self::OPTION_COUNTRY_CODE); | ||
$oldRate = (float) $input->getOption(self::OPTION_OLD_RATE); | ||
|
||
if (!($countryCode && $oldRate)) { | ||
$output->writeln('<error>Tax rate ID or either country code and rate must be specified</error>'); | ||
return 1; | ||
} | ||
|
||
$taxRates = $this->getRatesByCountryAndRate($countryCode, $oldRate); | ||
|
||
if (count($taxRates)) { | ||
foreach ($taxRates as $taxRate) { | ||
$this->saveNewRate($output, $taxRate, $newRate); | ||
} | ||
} else { | ||
$output->writeln(sprintf( | ||
'<comment>No tax rates for %s with rate %g%% have been found</comment>', | ||
$countryCode, | ||
$oldRate | ||
)); | ||
} | ||
} | ||
|
||
if ($input->getOption(self::OPTION_DRY_RUN)) { | ||
$output->writeln('<comment>Dry Run Mode. No data has been modified</comment>'); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* @param TaxRateInterface $taxRate | ||
* @param $newRate | ||
* @param bool $dryRun | ||
* @return void | ||
*/ | ||
private function saveNewRate(OutputInterface $output, TaxRateInterface $taxRate, $newRate, $dryRun = false) | ||
{ | ||
// try to update tax code. should work wor most simple cases | ||
$taxRate->setCode(str_replace( | ||
sprintf('%g%%', $taxRate->getRate()), | ||
sprintf('%g%%', $newRate), | ||
$taxRate->getCode() | ||
)); | ||
|
||
$taxRate->setRate($newRate); | ||
|
||
if (!$dryRun) { | ||
try { | ||
$this->taxRateRepository->save($taxRate); | ||
} catch (\Exception $exception) { | ||
$output->writeln(sprintf( | ||
'<error>Cannot save Tax Rate with ID %d. Error message: %s</error>', | ||
$taxRate->getId(), | ||
$exception->getMessage() | ||
)); | ||
} | ||
} | ||
|
||
$output->writeln(sprintf( | ||
'<info>Tax Rate with ID %d for country %s has been saved with new percent rate %g%%</info>', | ||
$taxRate->getId(), | ||
$taxRate->getTaxCountryId(), | ||
$taxRate->getRate() | ||
)); | ||
} | ||
|
||
/** | ||
* @param string $countryCode | ||
* @param float $oldRate | ||
* @return TaxRateInterface[] | ||
* @throws InputException | ||
*/ | ||
private function getRatesByCountryAndRate($countryCode, float $oldRate) | ||
{ | ||
/** @var SearchCriteriaBuilder $criteriaBuilder */ | ||
$criteriaBuilder = $this->criteriaBuilderFactory->create(); | ||
if ($countryCode) { | ||
$criteriaBuilder->addFilter('tax_country_id', $countryCode); | ||
} | ||
|
||
if ($oldRate) { | ||
$criteriaBuilder->addFilter('rate', $oldRate); | ||
} | ||
|
||
return $this->taxRateRepository->getList($criteriaBuilder->create())->getItems(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* @author Oleh Kravets <[email protected]> | ||
* @copyright Copyright (c) 2020 schoene neue kinder GmbH (https://www.snk.de) | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Framework\Console\CommandListInterface"> | ||
<arguments> | ||
<argument name="commands" xsi:type="array"> | ||
<item name="snk-tax-rate-update" xsi:type="object">Snk\TaxUpdater\Console\UpdateTaxRateCommand</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* @author Oleh Kravets <[email protected]> | ||
* @copyright Copyright (c) 2020 schoene neue kinder GmbH (https://www.snk.de) | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Snk_TaxUpdater" setup_version="0.1.0" > | ||
<sequence> | ||
<module name="Magento_Tax"/> | ||
</sequence> | ||
</module> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
/** | ||
* @author Oleh Kravets <[email protected]> | ||
* @copyright Copyright (c) 2020 schoene neue kinder GmbH (https://www.snk.de) | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
*/ | ||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Snk_TaxUpdater', __DIR__); |