00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Tax_RateController extends Mage_Adminhtml_Controller_Action
00036 {
00037
00038
00039
00040
00041 public function indexAction()
00042 {
00043 $this->_initAction()
00044 ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'))
00045 ->_addContent(
00046 $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_add', 'tax_rate_toolbar')
00047 ->assign('createUrl', $this->getUrl('*/tax_rate/add'))
00048 ->assign('header', Mage::helper('tax')->__('Manage Tax Rates'))
00049 )
00050 ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rate_grid', 'tax_rate_grid'))
00051 ->renderLayout();
00052 }
00053
00054
00055
00056
00057
00058 public function addAction()
00059 {
00060 $rateModel = Mage::getSingleton('tax/calculation_rate')
00061 ->load(null);
00062 $this->_initAction()
00063 ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'), $this->getUrl('*/tax_rate'))
00064 ->_addBreadcrumb(Mage::helper('tax')->__('New Tax Rate'), Mage::helper('tax')->__('New Tax Rate'))
00065 ->_addContent(
00066 $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_save')
00067 ->assign('header', Mage::helper('tax')->__('Add New Tax Rate'))
00068 ->assign('form', $this->getLayout()->createBlock('adminhtml/tax_rate_form'))
00069 )
00070 ->renderLayout();
00071 }
00072
00073
00074
00075
00076
00077
00078 public function saveAction()
00079 {
00080 if ($ratePost = $this->getRequest()->getPost()) {
00081 $ratePostData = $this->getRequest()->getPost('rate_data');
00082 $rateModel = Mage::getModel('tax/calculation_rate')->setData($ratePost);
00083
00084 try {
00085 $rateModel->save();
00086
00087 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('Tax rate was successfully saved'));
00088 $this->getResponse()->setRedirect($this->getUrl("*/*/"));
00089 return true;
00090 }
00091 catch (Mage_Core_Exception $e) {
00092 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00093 }
00094 catch (Exception $e) {
00095
00096 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00097 }
00098
00099 $this->_redirectReferer();
00100 }
00101 }
00102
00103
00104
00105
00106
00107 public function editAction()
00108 {
00109 $rateId = (int)$this->getRequest()->getParam('rate');
00110 $rateModel = Mage::getSingleton('tax/calculation_rate')
00111 ->load($rateId);
00112 if (!$rateModel->getId()) {
00113 $this->getResponse()->setRedirect($this->getUrl("*/*/"));
00114 return ;
00115 }
00116
00117 $this->_initAction()
00118 ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'), $this->getUrl('*/tax_rate'))
00119 ->_addBreadcrumb(Mage::helper('tax')->__('Edit Tax Rate'), Mage::helper('tax')->__('Edit Tax Rate'))
00120 ->_addContent(
00121 $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_save')
00122 ->assign('header', Mage::helper('tax')->__('Edit Tax Rate'))
00123 ->assign('form', $this->getLayout()->createBlock('adminhtml/tax_rate_form'))
00124 )
00125 ->renderLayout();
00126 }
00127
00128
00129
00130
00131
00132
00133 public function deleteAction()
00134 {
00135 if ($rateId = $this->getRequest()->getParam('rate')) {
00136 $rateModel = Mage::getModel('tax/calculation_rate')->load($rateId);
00137 if ($rateModel->getId()) {
00138 try {
00139 $rateModel->delete();
00140
00141 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('Tax rate was successfully deleted'));
00142 $this->getResponse()->setRedirect($this->getUrl("*/*/"));
00143 return true;
00144 }
00145 catch (Mage_Core_Exception $e) {
00146 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00147 }
00148 catch (Exception $e) {
00149 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Error while deleting this rate. Please try again later.'));
00150 }
00151 if ($referer = $this->getRequest()->getServer('HTTP_REFERER')) {
00152 $this->getResponse()->setRedirect($referer);
00153 }
00154 else {
00155 $this->getResponse()->setRedirect($this->getUrl("*/*/"));
00156 }
00157 } else {
00158 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Error while deleting this rate. Incorrect rate ID'));
00159 $this->getResponse()->setRedirect($this->getUrl('*/*/'));
00160 }
00161 }
00162 }
00163
00164
00165
00166
00167
00168 public function exportCsvAction()
00169 {
00170 $fileName = 'rates.csv';
00171 $content = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
00172 ->getCsv();
00173
00174 $this->_prepareDownloadResponse($fileName, $content);
00175 }
00176
00177
00178
00179
00180 public function exportXmlAction()
00181 {
00182 $fileName = 'rates.xml';
00183 $content = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
00184 ->getXml();
00185
00186 $this->_prepareDownloadResponse($fileName, $content);
00187 }
00188
00189
00190
00191
00192
00193
00194 protected function _initAction()
00195 {
00196 $this->loadLayout()
00197 ->_setActiveMenu('sales/tax_rates')
00198 ->_addBreadcrumb(Mage::helper('tax')->__('Sales'), Mage::helper('tax')->__('Sales'))
00199 ->_addBreadcrumb(Mage::helper('tax')->__('Tax'), Mage::helper('tax')->__('Tax'));
00200 return $this;
00201 }
00202
00203
00204
00205
00206
00207 public function importExportAction()
00208 {
00209 $this->loadLayout()
00210 ->_setActiveMenu('sales/tax_importExport')
00211 ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rate_importExport'))
00212 ->renderLayout();
00213 }
00214
00215
00216
00217
00218
00219 public function importPostAction()
00220 {
00221 if ($this->getRequest()->isPost() && !empty($_FILES['import_rates_file']['tmp_name'])) {
00222 try {
00223 $this->_importRates();
00224
00225 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('Tax rate was successfully imported'));
00226 }
00227 catch (Mage_Core_Exception $e) {
00228 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00229 }
00230 catch (Exception $e) {
00231 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
00232 }
00233 }
00234 else {
00235 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
00236 }
00237 $this->_redirect('*/*/importExport');
00238 }
00239
00240 protected function _importRates()
00241 {
00242 $fileName = $_FILES['import_rates_file']['tmp_name'];
00243 $csvObject = new Varien_File_Csv();
00244 $csvData = $csvObject->getData($fileName);
00245
00246
00247 $csvFields = array(
00248 0 => Mage::helper('tax')->__('Code'),
00249 1 => Mage::helper('tax')->__('Country'),
00250 2 => Mage::helper('tax')->__('State'),
00251 3 => Mage::helper('tax')->__('Zip/Post Code'),
00252 4 => Mage::helper('tax')->__('Rate')
00253 );
00254
00255
00256 $stores = array();
00257 $unset = array();
00258 $storeCollection = Mage::getModel('core/store')->getCollection()->setLoadDefault(false);
00259 for ($i=5; $i<count($csvData[0]); $i++) {
00260 $header = $csvData[0][$i];
00261 $found = false;
00262 foreach ($storeCollection as $store) {
00263 if ($header == $store->getCode()) {
00264 $csvFields[$i] = $store->getCode();
00265 $stores[$i] = $store->getId();
00266 $found = true;
00267 }
00268 }
00269 if (!$found) {
00270 $unset[] = $i;
00271 }
00272
00273 }
00274
00275 $regions = array();
00276
00277 if ($unset) {
00278 foreach ($unset as $u) {
00279 unset($csvData[0][$u]);
00280 }
00281 }
00282
00283
00284 if ($csvData[0] == $csvFields) {
00285 Mage::getModel('tax/calculation_rate')->deleteAllRates();
00286
00287 foreach ($csvData as $k => $v) {
00288 if ($k == 0) {
00289 continue;
00290 }
00291
00292
00293 if (count($v) <= 1 && !strlen($v[0])) {
00294 continue;
00295 }
00296 if ($unset) {
00297 foreach ($unset as $u) {
00298 unset($v[$u]);
00299 }
00300 }
00301
00302 if (count($csvFields) != count($v)) {
00303 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
00304 }
00305
00306 $country = Mage::getModel('directory/country')->loadByCode($v[1], 'iso2_code');
00307 if (!$country->getId()) {
00308 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('One of the country has invalid code.'));
00309 continue;
00310 }
00311
00312 if (!isset($regions[$v[1]])) {
00313 $regions[$v[1]]['*'] = '*';
00314 $regionCollection = Mage::getModel('directory/region')->getCollection()
00315 ->addCountryFilter($v[1]);
00316 if ($regionCollection->getSize()) {
00317 foreach ($regionCollection as $region) {
00318 $regions[$v[1]][$region->getCode()] = $region->getRegionId();
00319 }
00320 }
00321 }
00322
00323 if (!empty($regions[$v[1]][$v[2]])) {
00324 $rateData = array(
00325 'code'=>$v[0],
00326 'tax_country_id' => $v[1],
00327 'tax_region_id' => ($regions[$v[1]][$v[2]] == '*') ? 0 : $regions[$v[1]][$v[2]],
00328 'tax_postcode' => (empty($v[3]) || $v[3]=='*') ? null : $v[3],
00329 'rate'=>$v[4],
00330 );
00331
00332 $rateModel = Mage::getModel('tax/calculation_rate')
00333 ->setData($rateData);
00334 $titles = array();
00335 foreach ($stores as $field=>$id) {
00336 $titles[$id]=$v[$field];
00337 }
00338 $rateModel->setTitle($titles);
00339 $rateModel->save();
00340 }
00341 }
00342 }
00343 else {
00344 Mage::throwException(Mage::helper('tax')->__('Invalid file format upload attempt'));
00345 }
00346 }
00347
00348
00349
00350
00351
00352 public function exportPostAction()
00353 {
00354
00355 $stores = array();
00356 $storeCollection = Mage::getModel('core/store')->getCollection()->setLoadDefault(false);
00357 foreach ($storeCollection as $store) {
00358 $stores[$store->getId()] = $store->getCode();
00359 }
00360
00361
00362 $content = '"'.Mage::helper('tax')->__('Code').'","'.Mage::helper('tax')->__('Country').'","'.Mage::helper('tax')->__('State').'","'.Mage::helper('tax')->__('Zip/Post Code').'","'.Mage::helper('tax')->__('Rate').'"';
00363 $template = '"{{code}}","{{country_name}}","{{region_name}}","{{tax_postcode}}","{{rate}}"';
00364 foreach ($stores as $id => $name) {
00365 $content .= ',"'.$name.'"';
00366 $template .= ',"{{title_'.$id.'}}"';
00367 }
00368 $content .= "\n";
00369
00370 $rateCollection = Mage::getModel('tax/calculation_rate')->getCollection()
00371 ->joinStoreTitles()
00372 ->joinCountryTable()
00373 ->joinRegionTable();
00374 foreach ($rateCollection as $rate) {
00375 if ($rate->getTaxRegionId() == 0) {
00376 $rate->setRegionName('*');
00377 }
00378 $content .= $rate->toString($template)."\n";
00379 }
00380
00381 $fileName = 'tax_rates.csv';
00382
00383 $this->_prepareDownloadResponse($fileName, $content);
00384 }
00385
00386 protected function _isAllowed()
00387 {
00388
00389 switch ($this->getRequest()->getActionName()) {
00390 case 'importExport':
00391 return Mage::getSingleton('admin/session')->isAllowed('sales/tax/import_export');
00392 break;
00393 case 'index':
00394 return Mage::getSingleton('admin/session')->isAllowed('sales/tax/rates');
00395 break;
00396 default:
00397 return Mage::getSingleton('admin/session')->isAllowed('sales/tax/rates');
00398 break;
00399 }
00400 }
00401 }