1: | <?php
|
2: | namespace Opencart\Catalog\Model\Account;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Reward extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: |
|
19: | public function addReward(int $customer_id, int $order_id, string $description, int $points): void {
|
20: | $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_reward` SET `customer_id` = '" . (int)$customer_id . "', `order_id` = '" . (int)$order_id . "', `description` = '" . $this->db->escape($description) . "', `points` = '" . (int)$points . "', `date_added` = NOW()");
|
21: | }
|
22: |
|
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: |
|
31: | public function deleteReward(int $customer_id, int $order_id = 0): void {
|
32: | $sql = "DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'";
|
33: |
|
34: | if ($order_id) {
|
35: | $sql .= " AND `order_id` = '" . (int)$order_id . "'";
|
36: | }
|
37: |
|
38: | $this->db->query($sql);
|
39: | }
|
40: |
|
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: |
|
48: | public function deleteRewardByOrderId(int $order_id): void {
|
49: | $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `order_id` = '" . (int)$order_id . "' AND `points` < 0");
|
50: | }
|
51: |
|
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: |
|
60: | public function getRewards(int $customer_id, array $data = []): array {
|
61: | $sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'";
|
62: |
|
63: | $sort_data = [
|
64: | 'points',
|
65: | 'description',
|
66: | 'date_added'
|
67: | ];
|
68: |
|
69: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
70: | $sql .= " ORDER BY `" . $data['sort'] . "`";
|
71: | } else {
|
72: | $sql .= " ORDER BY `date_added`";
|
73: | }
|
74: |
|
75: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
76: | $sql .= " DESC";
|
77: | } else {
|
78: | $sql .= " ASC";
|
79: | }
|
80: |
|
81: | if (isset($data['start']) || isset($data['limit'])) {
|
82: | if ($data['start'] < 0) {
|
83: | $data['start'] = 0;
|
84: | }
|
85: |
|
86: | if ($data['limit'] < 1) {
|
87: | $data['limit'] = 20;
|
88: | }
|
89: |
|
90: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
91: | }
|
92: |
|
93: | $query = $this->db->query($sql);
|
94: |
|
95: | return $query->rows;
|
96: | }
|
97: |
|
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: |
|
105: | public function getTotalRewards(int $customer_id): int {
|
106: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'");
|
107: |
|
108: | return (int)$query->row['total'];
|
109: | }
|
110: |
|
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: |
|
118: | public function getRewardTotal(int $customer_id): int {
|
119: | $query = $this->db->query("SELECT SUM(`points`) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "' GROUP BY `customer_id`");
|
120: |
|
121: | if ($query->num_rows) {
|
122: | return (int)$query->row['total'];
|
123: | } else {
|
124: | return 0;
|
125: | }
|
126: | }
|
127: | }
|
128: | |