1: <?php
2: namespace Opencart\Catalog\Controller\Api\Sale;
3: /**
4: * Class Reward
5: *
6: * @package Opencart\Catalog\Controller\Api\Sale
7: */
8: class Reward extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('api/sale/reward');
14:
15: $json = [];
16:
17: if (isset($this->request->post['reward'])) {
18: $reward = abs((int)$this->request->post['reward']);
19: } else {
20: $reward = 0;
21: }
22:
23: $available = $this->customer->getRewardPoints();
24:
25: $points_total = 0;
26:
27: foreach ($this->cart->getProducts() as $product) {
28: if ($product['points']) {
29: $points_total += $product['points'];
30: }
31: }
32:
33: if ($reward) {
34: if ($reward > $available) {
35: $json['error'] = sprintf($this->language->get('error_points'), $this->request->post['reward']);
36: }
37:
38: if ($reward > $points_total) {
39: $json['error'] = sprintf($this->language->get('error_maximum'), $points_total);
40: }
41: }
42:
43: if (!$json) {
44: if ($reward) {
45: $json['success'] = $this->language->get('text_success');
46:
47: $this->session->data['reward'] = $reward;
48: } else {
49: $json['success'] = $this->language->get('text_remove');
50:
51: unset($this->session->data['reward']);
52: }
53: }
54:
55: $this->response->addHeader('Content-Type: application/json');
56: $this->response->setOutput(json_encode($json));
57: }
58:
59: /**
60: * Maximum
61: *
62: * @return void
63: */
64: public function maximum(): void {
65: $this->load->language('api/sale/reward');
66:
67: $json = [];
68:
69: $json['maximum'] = 0;
70:
71: foreach ($this->cart->getProducts() as $product) {
72: if ($product['points']) {
73: $json['maximum'] += $product['points'];
74: }
75: }
76:
77: $this->response->addHeader('Content-Type: application/json');
78: $this->response->setOutput(json_encode($json));
79: }
80:
81: /**
82: * Available
83: *
84: * @return void
85: */
86: public function available(): void {
87: $this->response->addHeader('Content-Type: application/json');
88: $this->response->setOutput(json_encode(['points' => $this->customer->getRewardPoints()]));
89: }
90: }
91: