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_Class_CustomerController 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->__('Customer Tax Classes'));
46:
47: $this->_initAction()
48: ->_addContent(
49: $this->getLayout()
50: ->createBlock('adminhtml/tax_class')
51: ->setClassType(Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER)
52: )
53: ->renderLayout();
54: }
55:
56: 57: 58: 59:
60: public function newAction()
61: {
62: $this->_forward('edit');
63: }
64:
65: 66: 67: 68:
69: public function editAction()
70: {
71: $this->_title($this->__('Sales'))
72: ->_title($this->__('Tax'))
73: ->_title($this->__('Customer Tax Classes'));
74:
75: $classId = $this->getRequest()->getParam('id');
76: $model = Mage::getModel('tax/class');
77: if ($classId) {
78: $model->load($classId);
79: if (!$model->getId() || $model->getClassType() != Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER) {
80: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('This class no longer exists'));
81: $this->_redirect('*/*/');
82: return;
83: }
84: }
85:
86: $this->_title($model->getId() ? $model->getClassName() : $this->__('New Class'));
87:
88: $data = Mage::getSingleton('adminhtml/session')->getClassData(true);
89: if (!empty($data)) {
90: $model->setData($data);
91: }
92:
93: Mage::register('tax_class', $model);
94:
95: $this->_initAction()
96: ->_addBreadcrumb(
97: $classId ? Mage::helper('tax')->__('Edit Class') : Mage::helper('tax')->__('New Class'),
98: $classId ? Mage::helper('tax')->__('Edit Class') : Mage::helper('tax')->__('New Class')
99: )
100: ->_addContent(
101: $this->getLayout()
102: ->createBlock('adminhtml/tax_class_edit')
103: ->setData('action', $this->getUrl('*/tax_class/save'))
104: ->setClassType(Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER)
105: )
106: ->renderLayout();
107: }
108:
109: 110: 111: 112:
113: public function deleteAction()
114: {
115: $classId = $this->getRequest()->getParam('id');
116: $session = Mage::getSingleton('adminhtml/session');
117: $classModel = Mage::getModel('tax/class')
118: ->load($classId);
119:
120: if (!$classModel->getId() || $classModel->getClassType() != Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER) {
121: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('This class no longer exists'));
122: $this->_redirect('*/*/');
123: return;
124: }
125:
126: $ruleCollection = Mage::getModel('tax/calculation_rule')
127: ->getCollection()
128: ->setClassTypeFilter(Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER, $classId);
129:
130: if ($ruleCollection->getSize() > 0) {
131: $session->addError(Mage::helper('tax')->__('You cannot delete this tax class as it is used in Tax Rules. You have to delete the rules it is used in first.'));
132: $this->_redirect('*/*/edit/',array('id'=>$classId));
133: return;
134: }
135:
136: $customerGroupCollection = Mage::getModel('customer/group')
137: ->getCollection()
138: ->addFieldToFilter('tax_class_id', $classId);
139: $groupCount = $customerGroupCollection->getSize();
140:
141: if ($groupCount > 0) {
142: $session->addError(Mage::helper('tax')->__('You cannot delete this tax class as it is used for %d customer groups.', $groupCount));
143: $this->_redirect('*/*/edit/',array('id'=>$classId));
144: return;
145: }
146:
147: try {
148: $classModel->delete();
149:
150: $session->addSuccess(Mage::helper('tax')->__('The tax class has been deleted.'));
151: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
152: return ;
153: } catch (Mage_Core_Exception $e) {
154: $session->addError($e->getMessage());
155: } catch (Exception $e) {
156: $session->addException($e, Mage::helper('tax')->__('An error occurred while deleting this tax class.'));
157: }
158:
159: $this->_redirect('*/*/edit/',array('id'=>$classId));
160: }
161:
162: 163: 164: 165: 166:
167: protected function _initAction()
168: {
169: $this->loadLayout()
170: ->_setActiveMenu('sales/tax/tax_class_customer')
171: ->_addBreadcrumb(Mage::helper('tax')->__('Sales'), Mage::helper('tax')->__('Sales'))
172: ->_addBreadcrumb(Mage::helper('tax')->__('Tax'), Mage::helper('tax')->__('Tax'))
173: ->_addBreadcrumb(Mage::helper('tax')->__('Manage Customer Tax Classes'), Mage::helper('tax')->__('Manage Customer Tax Classes'))
174: ;
175: return $this;
176: }
177:
178: 179: 180: 181: 182:
183: protected function _isAllowed()
184: {
185: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/classes_customer');
186: }
187: }
188: