1: <?php
2: namespace Opencart\Catalog\Model\Account;
3: /**
4: * Class Reward
5: *
6: * @package Opencart\Catalog\Model\Account
7: */
8: class Reward extends \Opencart\System\Engine\Model {
9: /**
10: * Add Reward
11: *
12: * @param int $customer_id
13: * @param int $order_id
14: * @param string $description
15: * @param int $points
16: *
17: * @return void
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: * Delete Reward
25: *
26: * @param int $customer_id
27: * @param int $order_id
28: *
29: * @return void
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: * Delete Reward By Order ID
43: *
44: * @param int $order_id
45: *
46: * @return void
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: * Get Rewards
54: *
55: * @param int $customer_id
56: * @param array<string, mixed> $data
57: *
58: * @return array<int, array<string, mixed>>
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: * Get Total Rewards
100: *
101: * @param int $customer_id
102: *
103: * @return int
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: * Get Reward Total
113: *
114: * @param int $customer_id
115: *
116: * @return int
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: