Automatically change the VAT rate on 4th January

Unless you are lucky enough to have a development agency that is willing to make changes to your store just after New Years Day, you might benefit from running this little script. With the proposed change to 20% on January 4th 2011, you can automate the rate change in your Magento installation.

You'll need to set a few variables, then set-up a cron to run the job. We made the script standalone as a not all people have set up their Magento crons.

First, go to "Admin > Sales > Tax > Manage Tax Zones & Rates", then select your UK VAT rate.

ratescreen

Then look in the URL at the top to identify the ID of the rule,

rateid

Then, create the file below (wherever you want), and set magento, tax_rule and admin_email with your own details. You can check if the script will execute correctly by setting the is_test_run flag to true.

Then create the cron job, we've set it to run every hour, every day - just in case!

cronjob

And finally, create the AutoTax.php script itself using the code below:

<?php

set_time_limit(86400); ini_set('memory_limit', '128M');

$op = array( 'magento' => dirname(FILE).'/../', // the relative directory to your base Magento directory 'tax_rule' => '3', // the id of the tax rule to change 'date_change' => '04-01-2011', // the date of the change. eg. 01-01-2011 'new_rate' => '20', // the new tax rate 'admin_email' => 'your@email.com', // the confirmation email recipient 'is_test_run' => false // run a test first eg. true/false );

Do not change anything below this line

require_once $op['magento'] . 'app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$data = new Varien_Object(); $data->setData($op);

$rateId = $data->getTaxRule(); if ($rateId) { $rateModel = Mage::getSingleton('tax/calculation_rate')->load($rateId); } else { Mage::throwException( Mage::helper('tax')->__('Could not load tax rule') ); }

$oldModel = $rateModel->getData(); $timeToChange = intval((strtotime($data->getDateChange())-time())/86400);

if ($data->getIsTestRun() === true || Mage::app()->getRequest()->getParam('is_test') ) {

$rateModel-&gt;setRate($data-&gt;getNewRate());

echo &quot;Current data is:nn&quot;;
var_dump($oldModel);
echo &quot;nn&quot;;

echo &quot;Proposed new data is:nn&quot;;
var_dump($rateModel-&gt;getData());
echo &quot;nnWith an effective change in &quot;.$timeToChange.&quot; days&quot;;

} else if (date("d-m-Y") == $data->getDateChange()) {

if ($rateModel-&gt;getRate() != $data-&gt;getNewRate()) {

  try {

      $rateModel-&gt;setRate($data-&gt;getNewRate());

      if ($rateModel-&gt;save()) {
        $emailData = new Varien_Object();
        $emailData-&gt;setData(array('comment'=&gt;&quot;AutoTax rate changed successfully!nn&quot;.print_r($rateModel-&gt;getData(),true),'subject'=&gt;&quot;AutoTax rate changed successfully!&quot;));

        Mage::getModel('core/email_template')
            -&gt;setTemplateSubject('AutoTax rate changed successfully!')
            -&gt;sendTransactional(Mage::getStoreConfig('contacts/email/email_template'), array('name'=&gt;'Sonassi AutoTax','email'=&gt;'contact@sonassi.com'), $data-&gt;getAdminEmail(), 'Customer', array('data'=&gt;$emailData));

        Mage::log('Tax rate changed successfully!');

      } else {
        echo &quot;Save failed&quot;;
      }

      // see https://www.sonassi.com/knowledge-base/magento-knowledge-base/catalog-search-index-refresh-running-slow-or-haltingfreezing/
      // 2. Product Prices Index product prices
      // 4. Product Flat Data Reorganize EAV product structure to flat structure

      if (version_compare(Mage::getVersion(), '1.4', '&gt;=')) {

        foreach ( array(2,4) as $id ) {
          $process = Mage::getModel('index/process')-&gt;load($id);
          $process-&gt;reindexAll();
        }

      }

  }
  catch (Exception $e) {
    echo $e;
    Mage::log($e);

  }

}

} else { echo "Change in ".$timeToChange." daysn"; }

[syntaxhighlighter]