1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_Adminhtml_Tax_RateController extends Mage_Adminhtml_Controller_Action
36: {
37: 38: 39: 40:
41: public function indexAction()
42: {
43: $this->_title($this->__('Sales'))
44: ->_title($this->__('Tax'))
45: ->_title($this->__('Manage Tax Zones and Rates'));
46:
47: $this->_initAction()
48: ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'))
49: ->_addContent(
50: $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_add', 'tax_rate_toolbar')
51: ->assign('createUrl', $this->getUrl('*/tax_rate/add'))
52: ->assign('header', Mage::helper('tax')->__('Manage Tax Rates'))
53: )
54: ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rate_grid', 'tax_rate_grid'))
55: ->renderLayout();
56: }
57:
58: 59: 60: 61:
62: public function addAction()
63: {
64: $rateModel = Mage::getSingleton('tax/calculation_rate')
65: ->load(null);
66:
67: $this->_title($this->__('Sales'))
68: ->_title($this->__('Tax'))
69: ->_title($this->__('Manage Tax Zones and Rates'));
70:
71: $this->_title($this->__('New Rate'));
72:
73:
74: $rateModel->setData(Mage::getSingleton('adminhtml/session')->getFormData(true));
75:
76: $this->_initAction()
77: ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'), $this->getUrl('*/tax_rate'))
78: ->_addBreadcrumb(Mage::helper('tax')->__('New Tax Rate'), Mage::helper('tax')->__('New Tax Rate'))
79: ->_addContent(
80: $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_save')
81: ->assign('header', Mage::helper('tax')->__('Add New Tax Rate'))
82: ->assign('form', $this->getLayout()->createBlock('adminhtml/tax_rate_form'))
83: )
84: ->renderLayout();
85: }
86:
87: 88: 89: 90: 91:
92: public function saveAction()
93: {
94: $ratePost = $this->getRequest()->getPost();
95: if ($ratePost) {
96: $rateId = $this->getRequest()->getParam('tax_calculation_rate_id');
97: if ($rateId) {
98: $rateModel = Mage::getSingleton('tax/calculation_rate')->load($rateId);
99: if (!$rateModel->getId()) {
100: unset($ratePost['tax_calculation_rate_id']);
101: }
102: }
103:
104: $rateModel = Mage::getModel('tax/calculation_rate')->setData($ratePost);
105:
106: try {
107: $rateModel->save();
108:
109: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('The tax rate has been saved.'));
110: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
111: return true;
112: } catch (Mage_Core_Exception $e) {
113: Mage::getSingleton('adminhtml/session')->setFormData($ratePost);
114: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
115: } catch (Exception $e) {
116: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
117: }
118:
119: $this->_redirectReferer();
120: return;
121: }
122: $this->getResponse()->setRedirect($this->getUrl('*/tax_rate'));
123: }
124:
125: 126: 127: 128:
129: public function editAction()
130: {
131: $this->_title($this->__('Sales'))
132: ->_title($this->__('Tax'))
133: ->_title($this->__('Manage Tax Zones and Rates'));
134:
135: $rateId = (int)$this->getRequest()->getParam('rate');
136: $rateModel = Mage::getSingleton('tax/calculation_rate')->load($rateId);
137: if (!$rateModel->getId()) {
138: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
139: return ;
140: }
141:
142: $this->_title(sprintf("%s", $rateModel->getCode()));
143:
144: $this->_initAction()
145: ->_addBreadcrumb(Mage::helper('tax')->__('Manage Tax Rates'), Mage::helper('tax')->__('Manage Tax Rates'), $this->getUrl('*/tax_rate'))
146: ->_addBreadcrumb(Mage::helper('tax')->__('Edit Tax Rate'), Mage::helper('tax')->__('Edit Tax Rate'))
147: ->_addContent(
148: $this->getLayout()->createBlock('adminhtml/tax_rate_toolbar_save')
149: ->assign('header', Mage::helper('tax')->__('Edit Tax Rate'))
150: ->assign('form', $this->getLayout()->createBlock('adminhtml/tax_rate_form'))
151: )
152: ->renderLayout();
153: }
154:
155: 156: 157: 158: 159:
160: public function deleteAction()
161: {
162: if ($rateId = $this->getRequest()->getParam('rate')) {
163: $rateModel = Mage::getModel('tax/calculation_rate')->load($rateId);
164: if ($rateModel->getId()) {
165: try {
166: $rateModel->delete();
167:
168: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('The tax rate has been deleted.'));
169: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
170: return true;
171: }
172: catch (Mage_Core_Exception $e) {
173: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
174: }
175: catch (Exception $e) {
176: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('An error occurred while deleting this rate.'));
177: }
178: if ($referer = $this->getRequest()->getServer('HTTP_REFERER')) {
179: $this->getResponse()->setRedirect($referer);
180: }
181: else {
182: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
183: }
184: } else {
185: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('An error occurred while deleting this rate. Incorrect rate ID.'));
186: $this->getResponse()->setRedirect($this->getUrl('*/*/'));
187: }
188: }
189: }
190:
191: 192: 193: 194:
195: public function exportCsvAction()
196: {
197: $fileName = 'rates.csv';
198: $content = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
199: ->getCsvFile();
200:
201: $this->_prepareDownloadResponse($fileName, $content);
202: }
203:
204: 205: 206:
207: public function exportXmlAction()
208: {
209: $fileName = 'rates.xml';
210: $content = $this->getLayout()->createBlock('adminhtml/tax_rate_grid')
211: ->getExcelFile();
212:
213: $this->_prepareDownloadResponse($fileName, $content);
214: }
215:
216: 217: 218: 219: 220:
221: protected function _initAction()
222: {
223: $this->loadLayout()
224: ->_setActiveMenu('sales/tax_rates')
225: ->_addBreadcrumb(Mage::helper('tax')->__('Sales'), Mage::helper('tax')->__('Sales'))
226: ->_addBreadcrumb(Mage::helper('tax')->__('Tax'), Mage::helper('tax')->__('Tax'));
227: return $this;
228: }
229:
230: 231: 232: 233:
234: public function importExportAction()
235: {
236: $this->_title($this->__('Sales'))
237: ->_title($this->__('Tax'))
238: ->_title($this->__('Manage Tax Zones and Rates'));
239:
240: $this->_title($this->__('Import and Export Tax Rates'));
241:
242: $this->loadLayout()
243: ->_setActiveMenu('sales/tax_importExport')
244: ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rate_importExport'))
245: ->renderLayout();
246: }
247:
248: 249: 250: 251:
252: public function importPostAction()
253: {
254: if ($this->getRequest()->isPost() && !empty($_FILES['import_rates_file']['tmp_name'])) {
255: try {
256: $this->_importRates();
257:
258: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('The tax rate has been imported.'));
259: } catch (Mage_Core_Exception $e) {
260: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
261: } catch (Exception $e) {
262: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
263: }
264: }
265: else {
266: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
267: }
268: $this->_redirect('*/*/importExport');
269: }
270:
271: protected function _importRates()
272: {
273: $fileName = $_FILES['import_rates_file']['tmp_name'];
274: $csvObject = new Varien_File_Csv();
275: $csvData = $csvObject->getData($fileName);
276:
277:
278: $csvFields = array(
279: 0 => Mage::helper('tax')->__('Code'),
280: 1 => Mage::helper('tax')->__('Country'),
281: 2 => Mage::helper('tax')->__('State'),
282: 3 => Mage::helper('tax')->__('Zip/Post Code'),
283: 4 => Mage::helper('tax')->__('Rate'),
284: 5 => Mage::helper('tax')->__('Zip/Post is Range'),
285: 6 => Mage::helper('tax')->__('Range From'),
286: 7 => Mage::helper('tax')->__('Range To')
287: );
288:
289:
290: $stores = array();
291: $unset = array();
292: $storeCollection = Mage::getModel('core/store')->getCollection()->setLoadDefault(false);
293: $cvsFieldsNum = count($csvFields);
294: $cvsDataNum = count($csvData[0]);
295: for ($i = $cvsFieldsNum; $i < $cvsDataNum; $i++) {
296: $header = $csvData[0][$i];
297: $found = false;
298: foreach ($storeCollection as $store) {
299: if ($header == $store->getCode()) {
300: $csvFields[$i] = $store->getCode();
301: $stores[$i] = $store->getId();
302: $found = true;
303: }
304: }
305: if (!$found) {
306: $unset[] = $i;
307: }
308:
309: }
310:
311: $regions = array();
312:
313: if ($unset) {
314: foreach ($unset as $u) {
315: unset($csvData[0][$u]);
316: }
317: }
318: if ($csvData[0] == $csvFields) {
319:
320: $helper = Mage::helper('adminhtml');
321:
322: foreach ($csvData as $k => $v) {
323: if ($k == 0) {
324: continue;
325: }
326:
327:
328: if (count($v) <= 1 && !strlen($v[0])) {
329: continue;
330: }
331: if ($unset) {
332: foreach ($unset as $u) {
333: unset($v[$u]);
334: }
335: }
336:
337: if (count($csvFields) != count($v)) {
338: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Invalid file upload attempt'));
339: }
340:
341: $country = Mage::getModel('directory/country')->loadByCode($v[1], 'iso2_code');
342: if (!$country->getId()) {
343: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('One of the country has invalid code.'));
344: continue;
345: }
346:
347: if (!isset($regions[$v[1]])) {
348: $regions[$v[1]]['*'] = '*';
349: $regionCollection = Mage::getModel('directory/region')->getCollection()
350: ->addCountryFilter($v[1]);
351: if ($regionCollection->getSize()) {
352: foreach ($regionCollection as $region) {
353: $regions[$v[1]][$region->getCode()] = $region->getRegionId();
354: }
355: }
356: }
357:
358: if (!empty($regions[$v[1]][$v[2]])) {
359: $rateData = array(
360: 'code' => $v[0],
361: 'tax_country_id' => $v[1],
362: 'tax_region_id' => ($regions[$v[1]][$v[2]] == '*') ? 0 : $regions[$v[1]][$v[2]],
363: 'tax_postcode' => (empty($v[3]) || $v[3]=='*') ? null : $v[3],
364: 'rate' => $v[4],
365: 'zip_is_range' => $v[5],
366: 'zip_from' => $v[6],
367: 'zip_to' => $v[7]
368: );
369:
370: $rateModel = Mage::getModel('tax/calculation_rate')->loadByCode($rateData['code']);
371: foreach($rateData as $dataName => $dataValue) {
372: $rateModel->setData($dataName, $dataValue);
373: }
374:
375: $titles = array();
376: foreach ($stores as $field=>$id) {
377: $titles[$id] = $v[$field];
378: }
379:
380: $rateModel->setTitle($titles);
381: $rateModel->save();
382: }
383: }
384: } else {
385: Mage::throwException(Mage::helper('tax')->__('Invalid file format upload attempt'));
386: }
387: }
388:
389: 390: 391: 392:
393: public function exportPostAction()
394: {
395:
396: $headers = new Varien_Object(array(
397: 'code' => Mage::helper('tax')->__('Code'),
398: 'country_name' => Mage::helper('tax')->__('Country'),
399: 'region_name' => Mage::helper('tax')->__('State'),
400: 'tax_postcode' => Mage::helper('tax')->__('Zip/Post Code'),
401: 'rate' => Mage::helper('tax')->__('Rate'),
402: 'zip_is_range' => Mage::helper('tax')->__('Zip/Post is Range'),
403: 'zip_from' => Mage::helper('tax')->__('Range From'),
404: 'zip_to' => Mage::helper('tax')->__('Range To')
405: ));
406: $template = '"{{code}}","{{country_name}}","{{region_name}}","{{tax_postcode}}","{{rate}}"'
407: . ',"{{zip_is_range}}","{{zip_from}}","{{zip_to}}"';
408: $content = $headers->toString($template);
409:
410: $storeTaxTitleTemplate = array();
411: $taxCalculationRateTitleDict = array();
412:
413: foreach (Mage::getModel('core/store')->getCollection()->setLoadDefault(false) as $store) {
414: $storeTitle = 'title_' . $store->getId();
415: $content .= ',"' . $store->getCode() . '"';
416: $template .= ',"{{' . $storeTitle . '}}"';
417: $storeTaxTitleTemplate[$storeTitle] = null;
418: }
419: unset($store);
420:
421: $content .= "\n";
422:
423: foreach (Mage::getModel('tax/calculation_rate_title')->getCollection() as $title) {
424: $rateId = $title->getTaxCalculationRateId();
425:
426: if (! array_key_exists($rateId, $taxCalculationRateTitleDict)) {
427: $taxCalculationRateTitleDict[$rateId] = $storeTaxTitleTemplate;
428: }
429:
430: $taxCalculationRateTitleDict[$rateId]['title_' . $title->getStoreId()] = $title->getValue();
431: }
432: unset($title);
433:
434: $collection = Mage::getResourceModel('tax/calculation_rate_collection')
435: ->joinCountryTable()
436: ->joinRegionTable();
437:
438: while ($rate = $collection->fetchItem()) {
439: if ($rate->getTaxRegionId() == 0) {
440: $rate->setRegionName('*');
441: }
442:
443: if (array_key_exists($rate->getId(), $taxCalculationRateTitleDict)) {
444: $rate->addData($taxCalculationRateTitleDict[$rate->getId()]);
445: } else {
446: $rate->addData($storeTaxTitleTemplate);
447: }
448:
449: $content .= $rate->toString($template) . "\n";
450: }
451:
452: $this->_prepareDownloadResponse('tax_rates.csv', $content);
453: }
454:
455: protected function _isAllowed()
456: {
457:
458: switch ($this->getRequest()->getActionName()) {
459: case 'importExport':
460: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/import_export');
461: break;
462: case 'index':
463: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/rates');
464: break;
465: default:
466: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/rates');
467: break;
468: }
469: }
470: }
471: