Skip to content

Instantly share code, notes, and snippets.

@borriglione
Last active June 8, 2020 17:44
Show Gist options
  • Save borriglione/22e79c47f2b5cb6bb053e96fa5e80fa5 to your computer and use it in GitHub Desktop.
Save borriglione/22e79c47f2b5cb6bb053e96fa5e80fa5 to your computer and use it in GitHub Desktop.
Magento1 tax rate changing script (originally developed for Corona-caused tax rate change in Germany)
<?php
require_once 'abstract.php';
class TaxrateChange extends Mage_Shell_Abstract
{
public function run()
{
$this->validateInput();
$this->changeTaxRate();
}
protected function changeTaxRate()
{
/** @var $resource Mage_Core_Model_Resource $resource */
$resource = Mage::getSingleton('core/resource');
/** @var $connection Magento_Db_Adapter_Pdo_Mysql */
$connection = $resource->getConnection('read_write');
$tblTaxCalculationRate = $resource->getTableName('tax_calculation_rate');
$query = implode("\n", [
"UPDATE `{$tblTaxCalculationRate}`",
"SET rate = :new_tax_rate",
"WHERE rate = :current_tax_rate",
"AND tax_country_id = :county_id",
]);
$statement = $connection->prepare($query);
$statement->bindValue('new_tax_rate', $this->getArgumentNewRate());
$statement->bindValue('current_tax_rate', $this->getArgumentCurrentRate());
$statement->bindValue('county_id', $this->getArgumentCountryId());
$result = $statement->execute();
if (true === $result && $statement->rowCount() == 1) {
echo "Successfully changed tax rate\n";
} else if (true === $result && $statement->rowCount() == 0) {
echo "Query executed but no tax rate changed\n";
} else {
echo "Changing tax rate failed\n";
}
}
protected function validateInput()
{
if (!$this->getArgumentCountryId()
|| !$this->getArgumentCountryId()
|| !$this->getArgumentNewRate()) {
Mage::throwException('Missing arguments');
}
}
protected function getArgumentCountryId()
{
return trim($this->getArg('country_id'));
}
protected function getArgumentCurrentRate()
{
return trim(
$this->formatInputRate($this->getArg('current_rate'))
);
}
protected function getArgumentNewRate()
{
return trim(
$this->formatInputRate($this->getArg('new_rate'))
);
}
protected function formatInputRate($inputRate)
{
return number_format(
(float) $inputRate,
4
);
}
public function usageHelp()
{
return <<<USAGE
Usage: php -f change_taxrate.php -- [options]
Example: php -f change_taxrate.php --country_id "DE" --current_rate "19" --new_rate "16"
h This help
country_id ISO-2 Country code
current_rate Current tax rate to change
new_rate Taxrate change to
USAGE;
}
}
$shell = new TaxrateChange();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment