1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Tax Rate
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class TaxRate extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/tax_rate');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['sort'])) {
22: $url .= '&sort=' . $this->request->get['sort'];
23: }
24:
25: if (isset($this->request->get['order'])) {
26: $url .= '&order=' . $this->request->get['order'];
27: }
28:
29: if (isset($this->request->get['page'])) {
30: $url .= '&page=' . $this->request->get['page'];
31: }
32:
33: $data['breadcrumbs'] = [];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_home'),
37: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: ];
39:
40: $data['breadcrumbs'][] = [
41: 'text' => $this->language->get('heading_title'),
42: 'href' => $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('localisation/tax_rate.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('localisation/tax_rate.delete', 'user_token=' . $this->session->data['user_token']);
47:
48: $data['list'] = $this->getList();
49:
50: $data['user_token'] = $this->session->data['user_token'];
51:
52: $data['header'] = $this->load->controller('common/header');
53: $data['column_left'] = $this->load->controller('common/column_left');
54: $data['footer'] = $this->load->controller('common/footer');
55:
56: $this->response->setOutput($this->load->view('localisation/tax_rate', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('localisation/tax_rate');
66:
67: $this->response->setOutput($this->getList());
68: }
69:
70: /**
71: * Get List
72: *
73: * @return string
74: */
75: protected function getList(): string {
76: if (isset($this->request->get['sort'])) {
77: $sort = (string)$this->request->get['sort'];
78: } else {
79: $sort = 'tr.name';
80: }
81:
82: if (isset($this->request->get['order'])) {
83: $order = (string)$this->request->get['order'];
84: } else {
85: $order = 'ASC';
86: }
87:
88: if (isset($this->request->get['page'])) {
89: $page = (int)$this->request->get['page'];
90: } else {
91: $page = 1;
92: }
93:
94: $url = '';
95:
96: if (isset($this->request->get['sort'])) {
97: $url .= '&sort=' . $this->request->get['sort'];
98: }
99:
100: if (isset($this->request->get['order'])) {
101: $url .= '&order=' . $this->request->get['order'];
102: }
103:
104: if (isset($this->request->get['page'])) {
105: $url .= '&page=' . $this->request->get['page'];
106: }
107:
108: $data['action'] = $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['tax_rates'] = [];
111:
112: $filter_data = [
113: 'sort' => $sort,
114: 'order' => $order,
115: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: 'limit' => $this->config->get('config_pagination_admin')
117: ];
118:
119: $this->load->model('localisation/tax_rate');
120:
121: $results = $this->model_localisation_tax_rate->getTaxRates($filter_data);
122:
123: foreach ($results as $result) {
124: $data['tax_rates'][] = [
125: 'tax_rate_id' => $result['tax_rate_id'],
126: 'name' => $result['name'],
127: 'rate' => $result['rate'],
128: 'type' => ($result['type'] == 'F' ? $this->language->get('text_amount') : $this->language->get('text_percent')),
129: 'geo_zone' => $result['geo_zone'],
130: 'edit' => $this->url->link('localisation/tax_rate.form', 'user_token=' . $this->session->data['user_token'] . '&tax_rate_id=' . $result['tax_rate_id'] . $url)
131: ];
132: }
133:
134: $url = '';
135:
136: if ($order == 'ASC') {
137: $url .= '&order=DESC';
138: } else {
139: $url .= '&order=ASC';
140: }
141:
142: $data['sort_name'] = $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.name' . $url);
143: $data['sort_rate'] = $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.rate' . $url);
144: $data['sort_type'] = $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=tr.type' . $url);
145: $data['sort_geo_zone'] = $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=gz.name' . $url);
146:
147: $url = '';
148:
149: if (isset($this->request->get['sort'])) {
150: $url .= '&sort=' . $this->request->get['sort'];
151: }
152:
153: if (isset($this->request->get['order'])) {
154: $url .= '&order=' . $this->request->get['order'];
155: }
156:
157: $tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRates();
158:
159: $data['pagination'] = $this->load->controller('common/pagination', [
160: 'total' => $tax_rate_total,
161: 'page' => $page,
162: 'limit' => $this->config->get('config_pagination_admin'),
163: 'url' => $this->url->link('localisation/tax_rate.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
164: ]);
165:
166: $data['results'] = sprintf($this->language->get('text_pagination'), ($tax_rate_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($tax_rate_total - $this->config->get('config_pagination_admin'))) ? $tax_rate_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $tax_rate_total, ceil($tax_rate_total / $this->config->get('config_pagination_admin')));
167:
168: $data['sort'] = $sort;
169: $data['order'] = $order;
170:
171: return $this->load->view('localisation/tax_rate_list', $data);
172: }
173:
174: /**
175: * Form
176: *
177: * @return void
178: */
179: public function form(): void {
180: $this->load->language('localisation/tax_rate');
181:
182: $this->document->setTitle($this->language->get('heading_title'));
183:
184: $data['text_form'] = !isset($this->request->get['tax_rate_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
185:
186: $url = '';
187:
188: if (isset($this->request->get['sort'])) {
189: $url .= '&sort=' . $this->request->get['sort'];
190: }
191:
192: if (isset($this->request->get['order'])) {
193: $url .= '&order=' . $this->request->get['order'];
194: }
195:
196: if (isset($this->request->get['page'])) {
197: $url .= '&page=' . $this->request->get['page'];
198: }
199:
200: $data['breadcrumbs'] = [];
201:
202: $data['breadcrumbs'][] = [
203: 'text' => $this->language->get('text_home'),
204: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
205: ];
206:
207: $data['breadcrumbs'][] = [
208: 'text' => $this->language->get('heading_title'),
209: 'href' => $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url)
210: ];
211:
212: $data['save'] = $this->url->link('localisation/tax_rate.save', 'user_token=' . $this->session->data['user_token']);
213: $data['back'] = $this->url->link('localisation/tax_rate', 'user_token=' . $this->session->data['user_token'] . $url);
214:
215: if (isset($this->request->get['tax_rate_id'])) {
216: $this->load->model('localisation/tax_rate');
217:
218: $tax_rate_info = $this->model_localisation_tax_rate->getTaxRate($this->request->get['tax_rate_id']);
219: }
220:
221: if (isset($this->request->get['tax_rate_id'])) {
222: $data['tax_rate_id'] = (int)$this->request->get['tax_rate_id'];
223: } else {
224: $data['tax_rate_id'] = 0;
225: }
226:
227: if (!empty($tax_rate_info)) {
228: $data['name'] = $tax_rate_info['name'];
229: } else {
230: $data['name'] = '';
231: }
232:
233: if (!empty($tax_rate_info)) {
234: $data['rate'] = $tax_rate_info['rate'];
235: } else {
236: $data['rate'] = '';
237: }
238:
239: if (!empty($tax_rate_info)) {
240: $data['type'] = $tax_rate_info['type'];
241: } else {
242: $data['type'] = '';
243: }
244:
245: $this->load->model('customer/customer_group');
246:
247: $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
248:
249: if (isset($this->request->get['tax_rate_id'])) {
250: $data['tax_rate_customer_group'] = $this->model_localisation_tax_rate->getCustomerGroups($this->request->get['tax_rate_id']);
251: } else {
252: $data['tax_rate_customer_group'] = [$this->config->get('config_customer_group_id')];
253: }
254:
255: $this->load->model('localisation/geo_zone');
256:
257: $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
258:
259: if (!empty($tax_rate_info)) {
260: $data['geo_zone_id'] = $tax_rate_info['geo_zone_id'];
261: } else {
262: $data['geo_zone_id'] = '';
263: }
264:
265: $data['header'] = $this->load->controller('common/header');
266: $data['column_left'] = $this->load->controller('common/column_left');
267: $data['footer'] = $this->load->controller('common/footer');
268:
269: $this->response->setOutput($this->load->view('localisation/tax_rate_form', $data));
270: }
271:
272: /**
273: * Save
274: *
275: * @return void
276: */
277: public function save(): void {
278: $this->load->language('localisation/tax_rate');
279:
280: $json = [];
281:
282: if (!$this->user->hasPermission('modify', 'localisation/tax_rate')) {
283: $json['error']['warning'] = $this->language->get('error_permission');
284: }
285:
286: if ((oc_strlen($this->request->post['name']) < 3) || (oc_strlen($this->request->post['name']) > 32)) {
287: $json['error']['name'] = $this->language->get('error_name');
288: }
289:
290: if (!$this->request->post['rate']) {
291: $json['error']['rate'] = $this->language->get('error_rate');
292: }
293:
294: if (!$json) {
295: $this->load->model('localisation/tax_rate');
296:
297: if (!$this->request->post['tax_rate_id']) {
298: $json['tax_rate_id'] = $this->model_localisation_tax_rate->addTaxRate($this->request->post);
299: } else {
300: $this->model_localisation_tax_rate->editTaxRate($this->request->post['tax_rate_id'], $this->request->post);
301: }
302:
303: $json['success'] = $this->language->get('text_success');
304: }
305:
306: $this->response->addHeader('Content-Type: application/json');
307: $this->response->setOutput(json_encode($json));
308: }
309:
310: /**
311: * Delete
312: *
313: * @return void
314: */
315: public function delete(): void {
316: $this->load->language('localisation/tax_rate');
317:
318: $json = [];
319:
320: if (isset($this->request->post['selected'])) {
321: $selected = $this->request->post['selected'];
322: } else {
323: $selected = [];
324: }
325:
326: if (!$this->user->hasPermission('modify', 'localisation/tax_rate')) {
327: $json['error'] = $this->language->get('error_permission');
328: }
329:
330: $this->load->model('localisation/tax_class');
331:
332: foreach ($this->request->post['selected'] as $tax_rate_id) {
333: $tax_rule_total = $this->model_localisation_tax_class->getTotalTaxRulesByTaxRateId($tax_rate_id);
334:
335: if ($tax_rule_total) {
336: $json['error'] = sprintf($this->language->get('error_tax_rule'), $tax_rule_total);
337: }
338: }
339:
340: if (!$json) {
341: $this->load->model('localisation/tax_rate');
342:
343: foreach ($selected as $tax_rate_id) {
344: $this->model_localisation_tax_rate->deleteTaxRate($tax_rate_id);
345: }
346:
347: $json['success'] = $this->language->get('text_success');
348: }
349:
350: $this->response->addHeader('Content-Type: application/json');
351: $this->response->setOutput(json_encode($json));
352: }
353: }
354: