tax_class.php (12462B)
1 <?php 2 class ControllerLocalisationTaxClass extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/tax_class'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/tax_class'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/tax_class'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/tax_class'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_tax_class->addTaxClass($this->request->post); 24 25 $this->session->data['success'] = $this->language->get('text_success'); 26 27 $url = ''; 28 29 if (isset($this->request->get['sort'])) { 30 $url .= '&sort=' . $this->request->get['sort']; 31 } 32 33 if (isset($this->request->get['order'])) { 34 $url .= '&order=' . $this->request->get['order']; 35 } 36 37 if (isset($this->request->get['page'])) { 38 $url .= '&page=' . $this->request->get['page']; 39 } 40 41 $this->response->redirect($this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true)); 42 } 43 44 $this->getForm(); 45 } 46 47 public function edit() { 48 $this->load->language('localisation/tax_class'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/tax_class'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_tax_class->editTaxClass($this->request->get['tax_class_id'], $this->request->post); 56 57 $this->session->data['success'] = $this->language->get('text_success'); 58 59 $url = ''; 60 61 if (isset($this->request->get['sort'])) { 62 $url .= '&sort=' . $this->request->get['sort']; 63 } 64 65 if (isset($this->request->get['order'])) { 66 $url .= '&order=' . $this->request->get['order']; 67 } 68 69 if (isset($this->request->get['page'])) { 70 $url .= '&page=' . $this->request->get['page']; 71 } 72 73 $this->response->redirect($this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true)); 74 } 75 76 $this->getForm(); 77 } 78 79 public function delete() { 80 $this->load->language('localisation/tax_class'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/tax_class'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $tax_class_id) { 88 $this->model_localisation_tax_class->deleteTaxClass($tax_class_id); 89 } 90 91 $this->session->data['success'] = $this->language->get('text_success'); 92 93 $url = ''; 94 95 if (isset($this->request->get['sort'])) { 96 $url .= '&sort=' . $this->request->get['sort']; 97 } 98 99 if (isset($this->request->get['order'])) { 100 $url .= '&order=' . $this->request->get['order']; 101 } 102 103 if (isset($this->request->get['page'])) { 104 $url .= '&page=' . $this->request->get['page']; 105 } 106 107 $this->response->redirect($this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true)); 108 } 109 110 $this->getList(); 111 } 112 113 protected function getList() { 114 if (isset($this->request->get['sort'])) { 115 $sort = $this->request->get['sort']; 116 } else { 117 $sort = 'title'; 118 } 119 120 if (isset($this->request->get['order'])) { 121 $order = $this->request->get['order']; 122 } else { 123 $order = 'ASC'; 124 } 125 126 if (isset($this->request->get['page'])) { 127 $page = $this->request->get['page']; 128 } else { 129 $page = 1; 130 } 131 132 $url = ''; 133 134 if (isset($this->request->get['sort'])) { 135 $url .= '&sort=' . $this->request->get['sort']; 136 } 137 138 if (isset($this->request->get['order'])) { 139 $url .= '&order=' . $this->request->get['order']; 140 } 141 142 if (isset($this->request->get['page'])) { 143 $url .= '&page=' . $this->request->get['page']; 144 } 145 146 $data['breadcrumbs'] = array(); 147 148 $data['breadcrumbs'][] = array( 149 'text' => $this->language->get('text_home'), 150 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 151 ); 152 153 $data['breadcrumbs'][] = array( 154 'text' => $this->language->get('heading_title'), 155 'href' => $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/tax_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/tax_class/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['tax_classes'] = array(); 162 163 $filter_data = array( 164 'sort' => $sort, 165 'order' => $order, 166 'start' => ($page - 1) * $this->config->get('config_limit_admin'), 167 'limit' => $this->config->get('config_limit_admin') 168 ); 169 170 $tax_class_total = $this->model_localisation_tax_class->getTotalTaxClasses(); 171 172 $results = $this->model_localisation_tax_class->getTaxClasses($filter_data); 173 174 foreach ($results as $result) { 175 $data['tax_classes'][] = array( 176 'tax_class_id' => $result['tax_class_id'], 177 'title' => $result['title'], 178 'edit' => $this->url->link('localisation/tax_class/edit', 'user_token=' . $this->session->data['user_token'] . '&tax_class_id=' . $result['tax_class_id'] . $url, true) 179 ); 180 } 181 182 if (isset($this->error['warning'])) { 183 $data['error_warning'] = $this->error['warning']; 184 } else { 185 $data['error_warning'] = ''; 186 } 187 188 if (isset($this->session->data['success'])) { 189 $data['success'] = $this->session->data['success']; 190 191 unset($this->session->data['success']); 192 } else { 193 $data['success'] = ''; 194 } 195 196 if (isset($this->request->post['selected'])) { 197 $data['selected'] = (array)$this->request->post['selected']; 198 } else { 199 $data['selected'] = array(); 200 } 201 202 $url = ''; 203 204 if ($order == 'ASC') { 205 $url .= '&order=DESC'; 206 } else { 207 $url .= '&order=ASC'; 208 } 209 210 if (isset($this->request->get['page'])) { 211 $url .= '&page=' . $this->request->get['page']; 212 } 213 214 $data['sort_title'] = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url, true); 215 216 $url = ''; 217 218 if (isset($this->request->get['sort'])) { 219 $url .= '&sort=' . $this->request->get['sort']; 220 } 221 222 if (isset($this->request->get['order'])) { 223 $url .= '&order=' . $this->request->get['order']; 224 } 225 226 $pagination = new Pagination(); 227 $pagination->total = $tax_class_total; 228 $pagination->page = $page; 229 $pagination->limit = $this->config->get('config_limit_admin'); 230 $pagination->url = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true); 231 232 $data['pagination'] = $pagination->render(); 233 234 $data['results'] = sprintf($this->language->get('text_pagination'), ($tax_class_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($tax_class_total - $this->config->get('config_limit_admin'))) ? $tax_class_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $tax_class_total, ceil($tax_class_total / $this->config->get('config_limit_admin'))); 235 236 $data['sort'] = $sort; 237 $data['order'] = $order; 238 239 $data['header'] = $this->load->controller('common/header'); 240 $data['column_left'] = $this->load->controller('common/column_left'); 241 $data['footer'] = $this->load->controller('common/footer'); 242 243 $this->response->setOutput($this->load->view('localisation/tax_class_list', $data)); 244 } 245 246 protected function getForm() { 247 $data['text_form'] = !isset($this->request->get['tax_class_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); 248 249 if (isset($this->error['warning'])) { 250 $data['error_warning'] = $this->error['warning']; 251 } else { 252 $data['error_warning'] = ''; 253 } 254 255 if (isset($this->error['title'])) { 256 $data['error_title'] = $this->error['title']; 257 } else { 258 $data['error_title'] = ''; 259 } 260 261 if (isset($this->error['description'])) { 262 $data['error_description'] = $this->error['description']; 263 } else { 264 $data['error_description'] = ''; 265 } 266 267 $url = ''; 268 269 if (isset($this->request->get['sort'])) { 270 $url .= '&sort=' . $this->request->get['sort']; 271 } 272 273 if (isset($this->request->get['order'])) { 274 $url .= '&order=' . $this->request->get['order']; 275 } 276 277 if (isset($this->request->get['page'])) { 278 $url .= '&page=' . $this->request->get['page']; 279 } 280 281 $data['breadcrumbs'] = array(); 282 283 $data['breadcrumbs'][] = array( 284 'text' => $this->language->get('text_home'), 285 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 286 ); 287 288 $data['breadcrumbs'][] = array( 289 'text' => $this->language->get('heading_title'), 290 'href' => $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true) 291 ); 292 293 if (!isset($this->request->get['tax_class_id'])) { 294 $data['action'] = $this->url->link('localisation/tax_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 295 } else { 296 $data['action'] = $this->url->link('localisation/tax_class/edit', 'user_token=' . $this->session->data['user_token'] . '&tax_class_id=' . $this->request->get['tax_class_id'] . $url, true); 297 } 298 299 $data['cancel'] = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true); 300 301 if (isset($this->request->get['tax_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 302 $tax_class_info = $this->model_localisation_tax_class->getTaxClass($this->request->get['tax_class_id']); 303 } 304 305 if (isset($this->request->post['title'])) { 306 $data['title'] = $this->request->post['title']; 307 } elseif (!empty($tax_class_info)) { 308 $data['title'] = $tax_class_info['title']; 309 } else { 310 $data['title'] = ''; 311 } 312 313 if (isset($this->request->post['description'])) { 314 $data['description'] = $this->request->post['description']; 315 } elseif (!empty($tax_class_info)) { 316 $data['description'] = $tax_class_info['description']; 317 } else { 318 $data['description'] = ''; 319 } 320 321 $this->load->model('localisation/tax_rate'); 322 323 $data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates(); 324 325 if (isset($this->request->post['tax_rule'])) { 326 $data['tax_rules'] = $this->request->post['tax_rule']; 327 } elseif (isset($this->request->get['tax_class_id'])) { 328 $data['tax_rules'] = $this->model_localisation_tax_class->getTaxRules($this->request->get['tax_class_id']); 329 } else { 330 $data['tax_rules'] = array(); 331 } 332 333 $data['header'] = $this->load->controller('common/header'); 334 $data['column_left'] = $this->load->controller('common/column_left'); 335 $data['footer'] = $this->load->controller('common/footer'); 336 337 $this->response->setOutput($this->load->view('localisation/tax_class_form', $data)); 338 } 339 340 protected function validateForm() { 341 if (!$this->user->hasPermission('modify', 'localisation/tax_class')) { 342 $this->error['warning'] = $this->language->get('error_permission'); 343 } 344 345 if ((utf8_strlen($this->request->post['title']) < 3) || (utf8_strlen($this->request->post['title']) > 32)) { 346 $this->error['title'] = $this->language->get('error_title'); 347 } 348 349 if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) { 350 $this->error['description'] = $this->language->get('error_description'); 351 } 352 353 return !$this->error; 354 } 355 356 protected function validateDelete() { 357 if (!$this->user->hasPermission('modify', 'localisation/tax_class')) { 358 $this->error['warning'] = $this->language->get('error_permission'); 359 } 360 361 $this->load->model('catalog/product'); 362 363 foreach ($this->request->post['selected'] as $tax_class_id) { 364 $product_total = $this->model_catalog_product->getTotalProductsByTaxClassId($tax_class_id); 365 366 if ($product_total) { 367 $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total); 368 } 369 } 370 371 return !$this->error; 372 } 373 }