docs/api/source-admin.controller.sale.voucher.html
| 1: | <?php | | 2: | namespace Opencart\Admin\Controller\Sale; | | 3: | /** | | 4: | * Class Voucher | | 5: | * | | 6: | * @package Opencart\Admin\Controller\Sale | | 7: | */ | | 8: | class Voucher extends \Opencart\System\Engine\Controller { | | 9: | /** | | 10: | * Index | | 11: | * | | 12: | * @return void | | 13: | */ | | 14: | public function index(): void { | | 15: | $this->load->language('sale/voucher'); | | 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('sale/voucher', 'user_token=' . $this->session->data['user_token'] . $url) | | 43: | ]; | | 44: | | | 45: | $data['add'] = $this->url->link('sale/voucher.form', 'user_token=' . $this->session->data['user_token'] . $url); | | 46: | $data['delete'] = $this->url->link('sale/voucher.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('sale/voucher', $data)); | | 57: | } | | 58: | | | 59: | /** | | 60: | * List | | 61: | * | | 62: | * @return void | | 63: | */ | | 64: | public function list(): void { | | 65: | $this->load->language('sale/voucher'); | | 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 = 'v.date_added'; | | 80: | } | | 81: | | | 82: | if (isset($this->request->get['order'])) { | | 83: | $order = (string)$this->request->get['order']; | | 84: | } else { | | 85: | $order = 'DESC'; | | 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('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . $url); | | 109: | | | 110: | $data['vouchers'] = []; | | 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('sale/voucher'); | | 120: | | | 121: | $results = $this->model_sale_voucher->getVouchers($filter_data); | | 122: | | | 123: | foreach ($results as $result) { | | 124: | if ($result['order_id']) { | | 125: | $order_href = $this->url->link('sale/order.info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $result['order_id'] . $url); | | 126: | } else { | | 127: | $order_href = ''; | | 128: | } | | 129: | | | 130: | $data['vouchers'][] = [ | | 131: | 'voucher_id' => $result['voucher_id'], | | 132: | 'code' => $result['code'], | | 133: | 'status' => $result['status'], | | 134: | 'from' => $result['from_name'], | | 135: | 'to' => $result['to_name'], | | 136: | 'theme' => $result['theme'], | | 137: | 'amount' => $this->currency->format($result['amount'], $this->config->get('config_currency')), | | 138: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])), | | 139: | 'edit' => $this->url->link('sale/voucher.form', 'user_token=' . $this->session->data['user_token'] . '&voucher_id=' . $result['voucher_id'] . $url), | | 140: | 'order' => $order_href | | 141: | ]; | | 142: | } | | 143: | | | 144: | $url = ''; | | 145: | | | 146: | if ($order == 'ASC') { | | 147: | $url .= '&order=DESC'; | | 148: | } else { | | 149: | $url .= '&order=ASC'; | | 150: | } | | 151: | | | 152: | $data['sort_code'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.code' . $url); | | 153: | $data['sort_from'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.from_name' . $url); | | 154: | $data['sort_to'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.to_name' . $url); | | 155: | $data['sort_theme'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=theme' . $url); | | 156: | $data['sort_amount'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.amount' . $url); | | 157: | $data['sort_status'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.status' . $url); | | 158: | $data['sort_date_added'] = $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . '&sort=v.date_added' . $url); | | 159: | | | 160: | $url = ''; | | 161: | | | 162: | if (isset($this->request->get['sort'])) { | | 163: | $url .= '&sort=' . $this->request->get['sort']; | | 164: | } | | 165: | | | 166: | if (isset($this->request->get['order'])) { | | 167: | $url .= '&order=' . $this->request->get['order']; | | 168: | } | | 169: | | | 170: | $voucher_total = $this->model_sale_voucher->getTotalVouchers(); | | 171: | | | 172: | $data['pagination'] = $this->load->controller('common/pagination', [ | | 173: | 'total' => $voucher_total, | | 174: | 'page' => $page, | | 175: | 'limit' => $this->config->get('config_pagination_admin'), | | 176: | 'url' => $this->url->link('sale/voucher.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}') | | 177: | ]); | | 178: | | | 179: | $data['results'] = sprintf($this->language->get('text_pagination'), ($voucher_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($voucher_total - $this->config->get('config_pagination_admin'))) ? $voucher_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $voucher_total, ceil($voucher_total / $this->config->get('config_pagination_admin'))); | | 180: | | | 181: | $data['sort'] = $sort; | | 182: | $data['order'] = $order; | | 183: | | | 184: | return $this->load->view('sale/voucher_list', $data); | | 185: | } | | 186: | | | 187: | /** | | 188: | * Form | | 189: | * | | 190: | * @return void | | 191: | */ | | 192: | public function form(): void { | | 193: | $this->load->language('sale/voucher'); | | 194: | | | 195: | $this->document->setTitle($this->language->get('heading_title')); | | 196: | | | 197: | $data['text_form'] = !isset($this->request->get['voucher_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); | | 198: | | | 199: | $url = ''; | | 200: | | | 201: | if (isset($this->request->get['sort'])) { | | 202: | $url .= '&sort=' . $this->request->get['sort']; | | 203: | } | | 204: | | | 205: | if (isset($this->request->get['order'])) { | | 206: | $url .= '&order=' . $this->request->get['order']; | | 207: | } | | 208: | | | 209: | if (isset($this->request->get['page'])) { | | 210: | $url .= '&page=' . $this->request->get['page']; | | 211: | } | | 212: | | | 213: | $data['breadcrumbs'] = []; | | 214: | | | 215: | $data['breadcrumbs'][] = [ | | 216: | 'text' => $this->language->get('text_home'), | | 217: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token']) | | 218: | ]; | | 219: | | | 220: | $data['breadcrumbs'][] = [ | | 221: | 'text' => $this->language->get('heading_title'), | | 222: | 'href' => $this->url->link('sale/voucher', 'user_token=' . $this->session->data['user_token'] . $url) | | 223: | ]; | | 224: | | | 225: | $data['save'] = $this->url->link('sale/voucher.save', 'user_token=' . $this->session->data['user_token']); | | 226: | $data['back'] = $this->url->link('sale/voucher', 'user_token=' . $this->session->data['user_token'] . $url); | | 227: | | | 228: | if (isset($this->request->get['voucher_id'])) { | | 229: | $this->load->model('sale/voucher'); | | 230: | | | 231: | $voucher_info = $this->model_sale_voucher->getVoucher($this->request->get['voucher_id']); | | 232: | } | | 233: | | | 234: | if (isset($this->request->get['voucher_id'])) { | | 235: | $data['voucher_id'] = (int)$this->request->get['voucher_id']; | | 236: | } else { | | 237: | $data['voucher_id'] = 0; | | 238: | } | | 239: | | | 240: | if (!empty($voucher_info)) { | | 241: | $data['code'] = $voucher_info['code']; | | 242: | } else { | | 243: | $data['code'] = ''; | | 244: | } | | 245: | | | 246: | if (!empty($voucher_info)) { | | 247: | $data['from_name'] = $voucher_info['from_name']; | | 248: | } else { | | 249: | $data['from_name'] = ''; | | 250: | } | | 251: | | | 252: | if (!empty($voucher_info)) { | | 253: | $data['from_email'] = $voucher_info['from_email']; | | 254: | } else { | | 255: | $data['from_email'] = ''; | | 256: | } | | 257: | | | 258: | if (!empty($voucher_info)) { | | 259: | $data['to_name'] = $voucher_info['to_name']; | | 260: | } else { | | 261: | $data['to_name'] = ''; | | 262: | } | | 263: | | | 264: | if (!empty($voucher_info)) { | | 265: | $data['to_email'] = $voucher_info['to_email']; | | 266: | } else { | | 267: | $data['to_email'] = ''; | | 268: | } | | 269: | | | 270: | $this->load->model('sale/voucher_theme'); | | 271: | | | 272: | $data['voucher_themes'] = $this->model_sale_voucher_theme->getVoucherThemes(); | | 273: | | | 274: | if (!empty($voucher_info)) { | | 275: | $data['voucher_theme_id'] = $voucher_info['voucher_theme_id']; | | 276: | } else { | | 277: | $data['voucher_theme_id'] = ''; | | 278: | } | | 279: | | | 280: | if (!empty($voucher_info)) { | | 281: | $data['message'] = $voucher_info['message']; | | 282: | } else { | | 283: | $data['message'] = ''; | | 284: | } | | 285: | | | 286: | if (!empty($voucher_info)) { | | 287: | $data['amount'] = $voucher_info['amount']; | | 288: | } else { | | 289: | $data['amount'] = ''; | | 290: | } | | 291: | | | 292: | if (!empty($voucher_info)) { | | 293: | $data['status'] = $voucher_info['status']; | | 294: | } else { | | 295: | $data['status'] = true; | | 296: | } | | 297: | | | 298: | $data['history'] = $this->getHistory(); | | 299: | | | 300: | $data['user_token'] = $this->session->data['user_token']; | | 301: | | | 302: | $data['header'] = $this->load->controller('common/header'); | | 303: | $data['column_left'] = $this->load->controller('common/column_left'); | | 304: | $data['footer'] = $this->load->controller('common/footer'); | | 305: | | | 306: | $this->response->setOutput($this->load->view('sale/voucher_form', $data)); | | 307: | } | | 308: | | | 309: | /** | | 310: | * Save | | 311: | * | | 312: | * @return void | | 313: | */ | | 314: | public function save(): void { | | 315: | $this->load->language('sale/voucher'); | | 316: | | | 317: | $json = []; | | 318: | | | 319: | if (!$this->user->hasPermission('modify', 'sale/voucher')) { | | 320: | $json['error']['warning'] = $this->language->get('error_permission'); | | 321: | } | | 322: | | | 323: | if ((oc_strlen($this->request->post['code']) < 3) || (oc_strlen($this->request->post['code']) > 10)) { | | 324: | $json['error']['code'] = $this->language->get('error_code'); | | 325: | } | | 326: | | | 327: | $this->load->model('sale/voucher'); | | 328: | | | 329: | $voucher_info = $this->model_sale_voucher->getVoucherByCode($this->request->post['code']); | | 330: | | | 331: | if ($voucher_info) { | | 332: | if (!isset($this->request->post['voucher_id'])) { | | 333: | $json['error']['warning'] = $this->language->get('error_exists'); | | 334: | } elseif ($voucher_info['voucher_id'] != (int)$this->request->post['voucher_id']) { | | 335: | $json['error']['warning'] = $this->language->get('error_exists'); | | 336: | } | | 337: | } | | 338: | | | 339: | if (!oc_validate_length($this->request->post['to_name'], 1, 64)) { | | 340: | $json['error']['to_name'] = $this->language->get('error_to_name'); | | 341: | } | | 342: | | | 343: | if ((oc_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) { | | 344: | $json['error']['to_email'] = $this->language->get('error_email'); | | 345: | } | | 346: | | | 347: | if (!oc_validate_length($this->request->post['from_name'], 1, 64)) { | | 348: | $json['error']['from_name'] = $this->language->get('error_from_name'); | | 349: | } | | 350: | | | 351: | if ((oc_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) { | | 352: | $json['error']['from_email'] = $this->language->get('error_email'); | | 353: | } | | 354: | | | 355: | if ($this->request->post['amount'] < 1) { | | 356: | $json['error']['amount'] = $this->language->get('error_amount'); | | 357: | } | | 358: | | | 359: | if (!$json) { | | 360: | if (!$this->request->post['voucher_id']) { | | 361: | $json['voucher_id'] = $this->model_sale_voucher->addVoucher($this->request->post); | | 362: | } else { | | 363: | $this->model_sale_voucher->editVoucher($this->request->post['voucher_id'], $this->request->post); | | 364: | } | | 365: | | | 366: | $json['success'] = $this->language->get('text_success'); | | 367: | } | | 368: | | | 369: | $this->response->addHeader('Content-Type: application/json'); | | 370: | $this->response->setOutput(json_encode($json)); | | 371: | } | | 372: | | | 373: | /** | | 374: | * Delete | | 375: | * | | 376: | * @return void | | 377: | */ | | 378: | public function delete(): void { | | 379: | $this->load->language('sale/voucher'); | | 380: | | | 381: | $json = []; | | 382: | | | 383: | if (isset($this->request->post['selected'])) { | | 384: | $selected = $this->request->post['selected']; | | 385: | } else { | | 386: | $selected = []; | | 387: | } | | 388: | | | 389: | if (!$this->user->hasPermission('modify', 'sale/voucher')) { | | 390: | $json['error'] = $this->language->get('error_permission'); | | 391: | } | | 392: | | | 393: | $this->load->model('sale/order'); | | 394: | | | 395: | foreach ($selected as $voucher_id) { | | 396: | $order_voucher_info = $this->model_sale_order->getVoucherByVoucherId($voucher_id); | | 397: | | | 398: | if ($order_voucher_info) { | | 399: | $json['error'] = sprintf($this->language->get('error_order'), $this->url->link('sale/order.info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_voucher_info['order_id'])); | | 400: | | | 401: | break; | | 402: | } | | 403: | } | | 404: | | | 405: | if (!$json) { | | 406: | $this->load->model('sale/voucher'); | | 407: | | | 408: | foreach ($selected as $voucher_id) { | | 409: | $this->model_sale_voucher->deleteVoucher($voucher_id); | | 410: | } | | 411: | | | 412: | $json['success'] = $this->language->get('text_success'); | | 413: | } | | 414: | | | 415: | $this->response->addHeader('Content-Type: application/json'); | | 416: | $this->response->setOutput(json_encode($json)); | | 417: | } | | 418: | | | 419: | /** | | 420: | * History | | 421: | * | | 422: | * @return void | | 423: | */ | | 424: | public function history(): void { | | 425: | $this->load->language('sale/voucher'); | | 426: | | | 427: | $this->response->setOutput($this->getHistory()); | | 428: | } | | 429: | | | 430: | /** | | 431: | * Get History | | 432: | * | | 433: | * @return string | | 434: | */ | | 435: | public function getHistory(): string { | | 436: | if (isset($this->request->get['voucher_id'])) { | | 437: | $voucher_id = (int)$this->request->get['voucher_id']; | | 438: | } else { | | 439: | $voucher_id = 0; | | 440: | } | | 441: | | | 442: | if (isset($this->request->get['page']) && $this->request->get['route'] == 'sale/voucher.history') { | | 443: | $page = (int)$this->request->get['page']; | | 444: | } else { | | 445: | $page = 1; | | 446: | } | | 447: | | | 448: | $limit = 10; | | 449: | | | 450: | $data['histories'] = []; | | 451: | | | 452: | $this->load->model('sale/voucher'); | | 453: | | | 454: | $results = $this->model_sale_voucher->getHistories($voucher_id, ($page - 1) * $limit, $limit); | | 455: | | | 456: | foreach ($results as $result) { | | 457: | $data['histories'][] = [ | | 458: | 'order_id' => $result['order_id'], | | 459: | 'customer' => $result['customer'], | | 460: | 'amount' => $this->currency->format($result['amount'], $this->config->get('config_currency')), | | 461: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])) | | 462: | ]; | | 463: | } | | 464: | | | 465: | $history_total = $this->model_sale_voucher->getTotalHistories($voucher_id); | | 466: | | | 467: | $data['pagination'] = $this->load->controller('common/pagination', [ | | 468: | 'total' => $history_total, | | 469: | 'page' => $page, | | 470: | 'limit' => $limit, | | 471: | 'url' => $this->url->link('sale/voucher.history', 'user_token=' . $this->session->data['user_token'] . '&voucher_id=' . $voucher_id . '&page={page}') | | 472: | ]); | | 473: | | | 474: | $data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($history_total - $limit)) ? $history_total : ((($page - 1) * $limit) + $limit), $history_total, ceil($history_total / $limit)); | | 475: | | | 476: | return $this->load->view('sale/voucher_history', $data); | | 477: | } | | 478: | | | 479: | /** | | 480: | * Send | | 481: | * | | 482: | * @return void | | 483: | */ | | 484: | public function send(): void { | | 485: | $this->load->language('mail/voucher'); | | 486: | | | 487: | $json = []; | | 488: | | | 489: | if (!$this->user->hasPermission('modify', 'sale/voucher')) { | | 490: | $json['error'] = $this->language->get('error_permission'); | | 491: | } | | 492: | | | 493: | if (!$json) { | | 494: | $this->load->model('sale/voucher'); | | 495: | | | 496: | $vouchers = []; | | 497: | | | 498: | if (isset($this->request->post['selected'])) { | | 499: | $vouchers = $this->request->post['selected']; | | 500: | } | | 501: | | | 502: | if (isset($this->request->post['voucher_id'])) { | | 503: | $vouchers[] = $this->request->post['voucher_id']; | | 504: | } | | 505: | | | 506: | if ($vouchers) { | | 507: | foreach ($vouchers as $voucher_id) { | | 508: | $this->load->controller('mail/voucher', $voucher_id); | | 509: | } | | 510: | | | 511: | $json['success'] = $this->language->get('text_sent'); | | 512: | } | | 513: | } | | 514: | | | 515: | $this->response->addHeader('Content-Type: application/json'); | | 516: | $this->response->setOutput(json_encode($json)); | | 517: | } | | 518: | } | | 519: | |
OpenCart API API documentation generated by ApiGen dev-master