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_Class_ProductController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39:
40: public function indexAction()
41: {
42: $this->_title($this->__('Sales'))
43: ->_title($this->__('Tax'))
44: ->_title($this->__('Product Tax Classes'));
45:
46: $this->_initAction()
47: ->_addContent(
48: $this->getLayout()->createBlock('adminhtml/tax_class')
49: ->setClassType(Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT)
50: )
51: ->renderLayout();
52: }
53:
54: 55: 56: 57:
58: public function newAction()
59: {
60: $this->_forward('edit');
61: }
62:
63: 64: 65: 66:
67: public function editAction()
68: {
69: $this->_title($this->__('Sales'))
70: ->_title($this->__('Tax'))
71: ->_title($this->__('Product Tax Classes'));
72:
73: $classId = $this->getRequest()->getParam('id');
74: $model = Mage::getModel('tax/class');
75: if ($classId) {
76: $model->load($classId);
77: if (!$model->getId() || $model->getClassType() != Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT) {
78: Mage::getSingleton('adminhtml/session')->addError(
79: Mage::helper('tax')->__('This class no longer exists')
80: );
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()->createBlock('adminhtml/tax_class_edit')
102: ->setData('action', $this->getUrl('*/tax_class/save'))
103: ->setClassType(Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT)
104: )
105: ->renderLayout();
106: }
107:
108: 109: 110: 111:
112: public function deleteAction()
113: {
114: $classId = $this->getRequest()->getParam('id');
115: $session = Mage::getSingleton('adminhtml/session');
116: $classModel = Mage::getModel('tax/class')
117: ->load($classId);
118:
119: if (!$classModel->getId() || $classModel->getClassType() != Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT) {
120: $session->addError(Mage::helper('tax')->__('This class no longer exists'));
121: $this->_redirect('*/*/');
122: return;
123: }
124:
125: $ruleCollection = Mage::getModel('tax/calculation_rule')
126: ->getCollection()
127: ->setClassTypeFilter(Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT, $classId);
128:
129: if ($ruleCollection->getSize() > 0) {
130: $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.'));
131: $this->_redirect('*/*/edit/', array('id' => $classId));
132: return;
133: }
134:
135: $productCollection = Mage::getModel('catalog/product')
136: ->getCollection()
137: ->addAttributeToFilter('tax_class_id', $classId);
138: $productCount = $productCollection->getSize();
139:
140: if ($productCount > 0) {
141: $session->addError(Mage::helper('tax')->__('You cannot delete this tax class as it is used for %d products.', $productCount));
142: $this->_redirect('*/*/edit/', array('id' => $classId));
143: return;
144: }
145:
146: try {
147: $classModel->delete();
148:
149: $session->addSuccess(Mage::helper('tax')->__('The tax class has been deleted.'));
150: $this->getResponse()->setRedirect($this->getUrl("*/*/"));
151: return;
152: } catch (Mage_Core_Exception $e) {
153: $session->addError($e->getMessage());
154: } catch (Exception $e) {
155: $session->addException($e, Mage::helper('tax')->__('An error occurred while deleting this tax class.'));
156: }
157:
158: $this->_redirect('*/*/edit/', array('id' => $classId));
159: }
160:
161: 162: 163: 164: 165:
166: protected function _initAction()
167: {
168: $this->loadLayout()
169: ->_setActiveMenu('sales/tax/tax_class_product')
170: ->_addBreadcrumb(Mage::helper('tax')->__('Sales'), Mage::helper('tax')->__('Sales'))
171: ->_addBreadcrumb(Mage::helper('tax')->__('Tax'), Mage::helper('tax')->__('Tax'))
172: ->_addBreadcrumb(Mage::helper('tax')->__('Manage Product Tax Classes'), Mage::helper('tax')->__('Manage Product Tax Classes'))
173: ;
174: return $this;
175: }
176:
177: 178: 179: 180: 181:
182: protected function _isAllowed()
183: {
184: return Mage::getSingleton('admin/session')->isAllowed('sales/tax/classes_product');
185: }
186:
187: }
188: