Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created August 9, 2013 07:47
Show Gist options
  • Save peterjaap/6191845 to your computer and use it in GitHub Desktop.
Save peterjaap/6191845 to your computer and use it in GitHub Desktop.
Generate tax rules for Magento
<?php
chdir(dirname(__FILE__));
require_once '../app/Mage.php';
Mage::app();
umask(0);
require_once 'countries.php'; // import lists with all countries
// UK Island shipping rates; http://www.crearegroup-ecommerce.co.uk/blog/magento-advice/shipping-rates-to-the-uk-islands.php
$truncateTables = false;
if($truncateTables) {
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core/write');
$db->query('TRUNCATE tax_calculation');
$db->query('TRUNCATE tax_calculation_rate');
$db->query('TRUNCATE tax_calculation_rate_title');
$db->query('TRUNCATE tax_rule');
}
foreach(array('B2C','B2B') as $natureCustomer) {
$attributeSets = array(
'Hardcopy Publications',
'Elec.copy Publications',
'Downloads of Publications',
'Advertising',
'Merchandise Goods',
);
foreach($attributeSets as $tableName) {
$rate = 0;
foreach($world as $country) {
$rate = 0;
if($tableName == 'Hardcopy Publications') {
// everything 0%
} elseif($tableName == 'Elec.copy Publications') {
if($natureCustomer == 'B2B') {
if($country == 'GB') {
$rate = 20;
}
}
if($natureCustomer == 'B2C') {
if(in_array($country,$EU)) {
$rate = 20;
}
}
} elseif($tableName == 'Downloads of Publications') {
if($natureCustomer == 'B2B') {
if($country == 'GB') {
$rate = 20;
} elseif($country == 'NL') {
$rate = 21;
}
} elseif($natureCustomer == 'B2C') {
if($country == 'GB') {
$rate = 20;
} elseif($country == 'NL') {
$rate = 21;
} else {
if(in_array($country,$EU)) {
$rate = 21; // rest of the EU
}
}
}
} elseif($tableName == 'Advertising') {
if($natureCustomer == 'B2B') {
if($country == 'GB') {
$rate = 20;
} elseif($country == 'NL') {
$rate = 21;
}
} elseif($natureCustomer == 'B2C') {
if($country == 'GB') {
$rate = 20;
} elseif($country == 'NL') {
$rate = 21;
} else {
if(in_array($country,$EU)) {
$rate = 21; // rest of the EU
}
}
}
} elseif($tableName == 'Merchandise Goods') {
if($natureCustomer == 'B2B') {
if($country == 'GB') {
$rate = 20;
}
} elseif($natureCustomer == 'B2C') {
if(in_array($country,$EU)) {
$rate = 20;
} else {
$rate = 0;
}
}
}
$taxCalculationRate = Mage::getModel('tax/calculation_rate');
$taxCalculationRate->setTaxCountryId($country);
$taxCalculationRate->setTaxRegionId(0);
$taxCalculationRate->setTaxPostcode('*');
$taxCalculationRate->setCode($country . ' ' . $rate . '%');
$taxCalculationRate->setRate($rate);
try {
$taxCalculationRate->save();
echo 'Tax calculation rate for ' . $country . ':' . $rate . ' has been saved' . "\n";
} catch(Exception $e) {
echo $country.':'.$rate.' '.$e->getMessage()."\n";
$taxCalculationRate = Mage::getModel('tax/calculation_rate')->getCollection()->addFieldToFilter('code',$country . ' ' . $rate . '%')->getFirstItem();
}
$productTaxClassId = Mage::getModel('tax/class')->getCollection()->addFieldToFilter('class_type','PRODUCT')->addFieldToFilter('class_name',$tableName)->getFirstItem()->getId();
if(!$productTaxClassId) {
echo 'No product tax class ID found.'."\n"; exit;
}
$customerTaxClassId = Mage::getModel('tax/class')->getCollection()->addFieldToFilter('class_type','CUSTOMER')->addFieldToFilter('class_name',$natureCustomer)->getFirstItem()->getId();
if(!$customerTaxClassId) {
echo 'No customer tax class ID found.'."\n"; exit;
}
$taxCalculationRule = Mage::getModel('tax/calculation_rule');
$code = $tableName . ' ' . $natureCustomer . ' ' . $country . ' ' . $rate . '%';
$taxCalculationRule->setCode($code);
try {
$taxCalculationRule->save();
echo 'Tax calculation rule '.$code.' has been saved.'."\n";
} catch(Exception $e) {
echo $tableName.':'.$natureCustomer.': '.$e->getMessage()."\n";
$taxCalculationRule = Mage::getModel('tax/calculation_rule')->getCollection()->addFieldToFilter('code',$code)->getFirstItem();
}
$taxCalculation = Mage::getModel('tax/calculation');
$taxCalculation->setTaxCalculationRateId($taxCalculationRate->getId());
$taxCalculation->setTaxCalculationRuleId($taxCalculationRule->getId());
$taxCalculation->setCustomerTaxClassId($customerTaxClassId);
$taxCalculation->setProductTaxClassId($productTaxClassId);
try {
$taxCalculation->save();
} catch(Exception $e) {
echo $e->getMessage()."\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment