Mage_Adminhtml_Tax_RateController Class Reference

Inheritance diagram for Mage_Adminhtml_Tax_RateController:

Mage_Adminhtml_Controller_Action Mage_Core_Controller_Varien_Action

List of all members.

Public Member Functions

 indexAction ()
 addAction ()
 saveAction ()
 editAction ()
 deleteAction ()
 exportCsvAction ()
 exportXmlAction ()
 importExportAction ()
 importPostAction ()
 exportPostAction ()

Protected Member Functions

 _initAction ()
 _importRates ()
 _isAllowed ()


Detailed Description

Definition at line 35 of file RateController.php.


Member Function Documentation

_importRates (  )  [protected]

checks columns

Definition at line 240 of file RateController.php.

00241     {
00242         $fileName   = $_FILES['import_rates_file']['tmp_name'];
00243         $csvObject  = new Varien_File_Csv();
00244         $csvData = $csvObject->getData($fileName);
00245 
00246         /** checks columns */
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                 //end of file has more then one empty lines
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     }

_initAction (  )  [protected]

Initialize action

Returns:
Mage_Adminhtml_Controller_Action

Definition at line 194 of file RateController.php.

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     }

_isAllowed (  )  [protected]

Reimplemented from Mage_Adminhtml_Controller_Action.

Definition at line 386 of file RateController.php.

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     }

addAction (  ) 

Show Add Form

Definition at line 58 of file RateController.php.

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     }

deleteAction (  ) 

Delete Rate and Data

Returns:
bool

Definition at line 133 of file RateController.php.

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     }

editAction (  ) 

Show Edit Form

Definition at line 107 of file RateController.php.

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     }

exportCsvAction (  ) 

Export rates grid to CSV format

Definition at line 168 of file RateController.php.

00169     {
00170         $fileName   = 'rates.csv';
00171         $content    = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
00172             ->getCsv();
00173 
00174         $this->_prepareDownloadResponse($fileName, $content);
00175     }

exportPostAction (  ) 

export action from import/export tax

get rate types

start csv content and set template

Definition at line 352 of file RateController.php.

00353     {
00354         /** get rate types */
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         /** start csv content and set template */
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     }

exportXmlAction (  ) 

Export rates grid to XML format

Definition at line 180 of file RateController.php.

00181     {
00182         $fileName   = 'rates.xml';
00183         $content    = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
00184             ->getXml();
00185 
00186         $this->_prepareDownloadResponse($fileName, $content);
00187     }

importExportAction (  ) 

Import and export Page

Definition at line 207 of file RateController.php.

00208     {
00209         $this->loadLayout()
00210             ->_setActiveMenu('sales/tax_importExport')
00211             ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rate_importExport'))
00212             ->renderLayout();
00213     }

importPostAction (  ) 

import action from import/export tax

Definition at line 219 of file RateController.php.

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     }

indexAction (  ) 

Show Main Grid

Definition at line 41 of file RateController.php.

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     }

saveAction (  ) 

Save Rate and Data

Returns:
bool

Definition at line 78 of file RateController.php.

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                 //Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Error while saving this rate. Please try again later.'));
00096                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00097             }
00098 
00099             $this->_redirectReferer();
00100         }
00101     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:30 2009 for Magento by  doxygen 1.5.8