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: class Mage_Adminhtml_Tax_RuleController extends Mage_Adminhtml_Controller_Action
35: {
36: public function indexAction()
37: {
38: $this->_title($this->__('Sales'))
39: ->_title($this->__('Tax'))
40: ->_title($this->__('Manage Tax Rules'));
41:
42: $this->_initAction()
43: ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rule'))
44: ->renderLayout();
45: return $this;
46: }
47:
48: public function newAction()
49: {
50: $this->_forward('edit');
51: }
52:
53: public function editAction()
54: {
55: $this->_title($this->__('Sales'))
56: ->_title($this->__('Tax'))
57: ->_title($this->__('Manage Tax Rules'));
58:
59: $taxRuleId = $this->getRequest()->getParam('rule');
60: $ruleModel = Mage::getModel('tax/calculation_rule');
61: if ($taxRuleId) {
62: $ruleModel->load($taxRuleId);
63: if (!$ruleModel->getId()) {
64: Mage::getSingleton('adminhtml/session')->unsRuleData();
65: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('This rule no longer exists.'));
66: $this->_redirect('*/*/');
67: return;
68: }
69: }
70:
71: $data = Mage::getSingleton('adminhtml/session')->getRuleData(true);
72: if (!empty($data)) {
73: $ruleModel->setData($data);
74: }
75:
76: $this->_title($ruleModel->getId() ? sprintf("%s", $ruleModel->getCode()) : $this->__('New Rule'));
77:
78: Mage::register('tax_rule', $ruleModel);
79:
80: $this->_initAction()
81: ->_addBreadcrumb($taxRuleId ? Mage::helper('tax')->__('Edit Rule') : Mage::helper('tax')->__('New Rule'), $taxRuleId ? Mage::helper('tax')->__('Edit Rule') : Mage::helper('tax')->__('New Rule'))
82: ->_addContent($this->getLayout()->createBlock('adminhtml/tax_rule_edit')->setData('action', $this->getUrl('*/tax_rule/save')))
83: ->renderLayout();
84: }
85:
86: public function saveAction()
87: {
88: if ($postData = $this->getRequest()->getPost()) {
89:
90: $ruleModel = Mage::getSingleton('tax/calculation_rule');
91: $ruleModel->setData($postData);
92:
93: try {
94: $ruleModel->save();
95:
96: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('The tax rule has been saved.'));
97:
98: if ($this->getRequest()->getParam('back')) {
99: $this->_redirect('*/*/edit', array('rule' => $ruleModel->getId()));
100: return;
101: }
102:
103: $this->_redirect('*/*/');
104: return;
105: }
106: catch (Mage_Core_Exception $e) {
107: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
108: }
109: catch (Exception $e) {
110: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('An error occurred while saving this tax rule.'));
111: }
112:
113: Mage::getSingleton('adminhtml/session')->setRuleData($postData);
114: $this->_redirectReferer();
115: return;
116: }
117: $this->getResponse()->setRedirect($this->getUrl('*/tax_rule'));
118: }
119:
120: public function deleteAction()
121: {
122: $ruleId = (int)$this->getRequest()->getParam('rule');
123: $ruleModel = Mage::getSingleton('tax/calculation_rule')
124: ->load($ruleId);
125: if (!$ruleModel->getId()) {
126: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('This rule no longer exists'));
127: $this->_redirect('*/*/');
128: return;
129: }
130:
131: try {
132: $ruleModel->delete();
133:
134: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('tax')->__('The tax rule has been deleted.'));
135: $this->_redirect('*/*/');
136:
137: return;
138: }
139: catch (Mage_Core_Exception $e) {
140: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
141: }
142: catch (Exception $e) {
143: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('An error occurred while deleting this tax rule.'));
144: }
145:
146: $this->_redirectReferer();
147: }
148:
149: 150: 151: 152: 153:
154: protected function _initAction()
155: {
156: $this->loadLayout()
157: ->_setActiveMenu('sales/tax/rule')
158: ->_addBreadcrumb(Mage::helper('tax')->__('Tax'), Mage::helper('tax')->__('Tax'))
159: ->_addBreadcrumb(Mage::helper('tax')->__('Tax Rules'), Mage::helper('tax')->__('Tax Rules'))
160: ;
161: return $this;
162: }
163:
164: protected function _isAllowed()
165: {
166: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/rules');
167: }
168: }
169: