Subversion Repositories web.creative

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
1 mjordaan 1
<?php
2
class ControllerLocalisationTaxRate extends Controller {
3
	private $error = array();
4
 
5
	public function index() {
6
		$this->load->language('localisation/tax_rate');
7
 
8
		$this->document->setTitle($this->language->get('heading_title'));
9
 
10
		$this->load->model('localisation/tax_rate');
11
 
12
		$this->getList();
13
	}
14
 
15
	public function add() {
16
		$this->load->language('localisation/tax_rate');
17
 
18
		$this->document->setTitle($this->language->get('heading_title'));
19
 
20
		$this->load->model('localisation/tax_rate');
21
 
22
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
23
			$this->model_localisation_tax_rate->addTaxRate($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_rate', '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_rate');
49
 
50
		$this->document->setTitle($this->language->get('heading_title'));
51
 
52
		$this->load->model('localisation/tax_rate');
53
 
54
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
55
			$this->model_localisation_tax_rate->editTaxRate($this->request->get['tax_rate_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_rate', '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_rate');
81
 
82
		$this->document->setTitle($this->language->get('heading_title'));
83
 
84
		$this->load->model('localisation/tax_rate');
85
 
86
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
87
			foreach ($this->request->post['selected'] as $tax_rate_id) {
88
				$this->model_localisation_tax_rate->deleteTaxRate($tax_rate_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_rate', '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 = 'tr.name';
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_rate', 'user_token=' . $this->session->data['user_token'] . $url, true)
156
		);
157
 
158
		$data['add'] = $this->url->link('localisation/tax_rate/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
159
		$data['delete'] = $this->url->link('localisation/tax_rate/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
160
 
161
		$data['tax_rates'] = 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_rate_total = $this->model_localisation_tax_rate->getTotalTaxRates();
171
 
172
		$results = $this->model_localisation_tax_rate->getTaxRates($filter_data);
173
 
174
		foreach ($results as $result) {
175
			$data['tax_rates'][] = array(
176
				'tax_rate_id'   => $result['tax_rate_id'],
177
				'name'          => $result['name'],
178
				'rate'          => $result['rate'],
179
				'type'          => ($result['type'] == 'F' ? $this->language->get('text_amount') : $this->language->get('text_percent')),
180
				'geo_zone'      => $result['geo_zone'],
181
				'date_added'    => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
182
				'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
183
				'edit'          => $this->url->link('localisation/tax_rate/edit', 'user_token=' . $this->session->data['user_token'] . '&tax_rate_id=' . $result['tax_rate_id'] . $url, true)
184
			);
185
		}
186
 
187
		if (isset($this->error['warning'])) {
188
			$data['error_warning'] = $this->error['warning'];
189
		} else {
190
			$data['error_warning'] = '';
191
		}
192
 
193
		if (isset($this->session->data['success'])) {
194
			$data['success'] = $this->session->data['success'];
195
 
196
			unset($this->session->data['success']);
197
		} else {
198
			$data['success'] = '';
199
		}
200
 
201
		if (isset($this->request->post['selected'])) {
202
			$data['selected'] = (array)$this->request->post['selected'];
203
		} else {
204
			$data['selected'] = array();
205
		}
206
 
207
		$url = '';
208
 
209
		if ($order == 'ASC') {
210
			$url .= '&order=DESC';
211
		} else {
212
			$url .= '&order=ASC';
213
		}
214
 
215
		if (isset($this->request->get['page'])) {
216
			$url .= '&page=' . $this->request->get['page'];
217
		}
218
 
219
		$data['sort_name'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.name' . $url, true);
220
		$data['sort_rate'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.rate' . $url, true);
221
		$data['sort_type'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.type' . $url, true);
222
		$data['sort_geo_zone'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=gz.name' . $url, true);
223
		$data['sort_date_added'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.date_added' . $url, true);
224
		$data['sort_date_modified'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.date_modified' . $url, true);
225
 
226
		$url = '';
227
 
228
		if (isset($this->request->get['sort'])) {
229
			$url .= '&sort=' . $this->request->get['sort'];
230
		}
231
 
232
		if (isset($this->request->get['order'])) {
233
			$url .= '&order=' . $this->request->get['order'];
234
		}
235
 
236
		$pagination = new Pagination();
237
		$pagination->total = $tax_rate_total;
238
		$pagination->page = $page;
239
		$pagination->limit = $this->config->get('config_limit_admin');
240
		$pagination->url = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
241
 
242
		$data['pagination'] = $pagination->render();
243
 
244
		$data['results'] = sprintf($this->language->get('text_pagination'), ($tax_rate_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($tax_rate_total - $this->config->get('config_limit_admin'))) ? $tax_rate_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $tax_rate_total, ceil($tax_rate_total / $this->config->get('config_limit_admin')));
245
 
246
		$data['sort'] = $sort;
247
		$data['order'] = $order;
248
 
249
		$data['header'] = $this->load->controller('common/header');
250
		$data['column_left'] = $this->load->controller('common/column_left');
251
		$data['footer'] = $this->load->controller('common/footer');
252
 
253
		$this->response->setOutput($this->load->view('localisation/tax_rate_list', $data));
254
	}
255
 
256
	protected function getForm() {
257
		$data['text_form'] = !isset($this->request->get['tax_rate_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
258
 
259
		if (isset($this->error['warning'])) {
260
			$data['error_warning'] = $this->error['warning'];
261
		} else {
262
			$data['error_warning'] = '';
263
		}
264
 
265
		if (isset($this->error['name'])) {
266
			$data['error_name'] = $this->error['name'];
267
		} else {
268
			$data['error_name'] = '';
269
		}
270
 
271
		if (isset($this->error['rate'])) {
272
			$data['error_rate'] = $this->error['rate'];
273
		} else {
274
			$data['error_rate'] = '';
275
		}
276
 
277
		$url = '';
278
 
279
		if (isset($this->request->get['sort'])) {
280
			$url .= '&sort=' . $this->request->get['sort'];
281
		}
282
 
283
		if (isset($this->request->get['order'])) {
284
			$url .= '&order=' . $this->request->get['order'];
285
		}
286
 
287
		if (isset($this->request->get['page'])) {
288
			$url .= '&page=' . $this->request->get['page'];
289
		}
290
 
291
		$data['breadcrumbs'] = array();
292
 
293
		$data['breadcrumbs'][] = array(
294
			'text' => $this->language->get('text_home'),
295
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
296
		);
297
 
298
		$data['breadcrumbs'][] = array(
299
			'text' => $this->language->get('heading_title'),
300
			'href' => $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url, true)
301
		);
302
 
303
		if (!isset($this->request->get['tax_rate_id'])) {
304
			$data['action'] = $this->url->link('localisation/tax_rate/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
305
		} else {
306
			$data['action'] = $this->url->link('localisation/tax_rate/edit', 'user_token=' . $this->session->data['user_token'] . '&tax_rate_id=' . $this->request->get['tax_rate_id'] . $url, true);
307
		}
308
 
309
		$data['cancel'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url, true);
310
 
311
		if (isset($this->request->get['tax_rate_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
312
			$tax_rate_info = $this->model_localisation_tax_rate->getTaxRate($this->request->get['tax_rate_id']);
313
		}
314
 
315
		if (isset($this->request->post['name'])) {
316
			$data['name'] = $this->request->post['name'];
317
		} elseif (!empty($tax_rate_info)) {
318
			$data['name'] = $tax_rate_info['name'];
319
		} else {
320
			$data['name'] = '';
321
		}
322
 
323
		if (isset($this->request->post['rate'])) {
324
			$data['rate'] = $this->request->post['rate'];
325
		} elseif (!empty($tax_rate_info)) {
326
			$data['rate'] = $tax_rate_info['rate'];
327
		} else {
328
			$data['rate'] = '';
329
		}
330
 
331
		if (isset($this->request->post['type'])) {
332
			$data['type'] = $this->request->post['type'];
333
		} elseif (!empty($tax_rate_info)) {
334
			$data['type'] = $tax_rate_info['type'];
335
		} else {
336
			$data['type'] = '';
337
		}
338
 
339
		if (isset($this->request->post['tax_rate_customer_group'])) {
340
			$data['tax_rate_customer_group'] = $this->request->post['tax_rate_customer_group'];
341
		} elseif (isset($this->request->get['tax_rate_id'])) {
342
			$data['tax_rate_customer_group'] = $this->model_localisation_tax_rate->getTaxRateCustomerGroups($this->request->get['tax_rate_id']);
343
		} else {
344
			$data['tax_rate_customer_group'] = array($this->config->get('config_customer_group_id'));
345
		}
346
 
347
		$this->load->model('customer/customer_group');
348
 
349
		$data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
350
 
351
		if (isset($this->request->post['geo_zone_id'])) {
352
			$data['geo_zone_id'] = $this->request->post['geo_zone_id'];
353
		} elseif (!empty($tax_rate_info)) {
354
			$data['geo_zone_id'] = $tax_rate_info['geo_zone_id'];
355
		} else {
356
			$data['geo_zone_id'] = '';
357
		}
358
 
359
		$this->load->model('localisation/geo_zone');
360
 
361
		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
362
 
363
		$data['header'] = $this->load->controller('common/header');
364
		$data['column_left'] = $this->load->controller('common/column_left');
365
		$data['footer'] = $this->load->controller('common/footer');
366
 
367
		$this->response->setOutput($this->load->view('localisation/tax_rate_form', $data));
368
	}
369
 
370
	protected function validateForm() {
371
		if (!$this->user->hasPermission('modify', 'localisation/tax_rate')) {
372
			$this->error['warning'] = $this->language->get('error_permission');
373
		}
374
 
375
		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
376
			$this->error['name'] = $this->language->get('error_name');
377
		}
378
 
379
		if (!$this->request->post['rate']) {
380
			$this->error['rate'] = $this->language->get('error_rate');
381
		}
382
 
383
		return !$this->error;
384
	}
385
 
386
	protected function validateDelete() {
387
		if (!$this->user->hasPermission('modify', 'localisation/tax_rate')) {
388
			$this->error['warning'] = $this->language->get('error_permission');
389
		}
390
 
391
		$this->load->model('localisation/tax_class');
392
 
393
		foreach ($this->request->post['selected'] as $tax_rate_id) {
394
			$tax_rule_total = $this->model_localisation_tax_class->getTotalTaxRulesByTaxRateId($tax_rate_id);
395
 
396
			if ($tax_rule_total) {
397
				$this->error['warning'] = sprintf($this->language->get('error_tax_rule'), $tax_rule_total);
398
			}
399
		}
400
 
401
		return !$this->error;
402
	}
403
}