1: | <?php
2: | namespace Opencart\Admin\Model\Sale;
3: | /**
4: | * Class Voucher
5: | *
6: | * @package Opencart\Admin\Model\Sale
7: | */
8: | class Voucher extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Voucher
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addVoucher(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "voucher` SET `code` = '" . $this->db->escape((string)$data['code']) . "', `from_name` = '" . $this->db->escape((string)$data['from_name']) . "', `from_email` = '" . $this->db->escape((string)$data['from_email']) . "', `to_name` = '" . $this->db->escape((string)$data['to_name']) . "', `to_email` = '" . $this->db->escape((string)$data['to_email']) . "', `voucher_theme_id` = '" . (int)$data['voucher_theme_id'] . "', `message` = '" . $this->db->escape((string)$data['message']) . "', `amount` = '" . (float)$data['amount'] . "', `status` = '" . (bool)$data['status'] . "', `date_added` = NOW()");
18: |
19: | return $this->db->getLastId();
20: | }
21: |
22: | /**
23: | * Edit Voucher
24: | *
25: | * @param int $voucher_id
26: | * @param array<string, mixed> $data
27: | *
28: | * @return void
29: | */
30: | public function editVoucher(int $voucher_id, array $data): void {
31: | $this->db->query("UPDATE `" . DB_PREFIX . "voucher` SET `code` = '" . $this->db->escape((string)$data['code']) . "', `from_name` = '" . $this->db->escape((string)$data['from_name']) . "', `from_email` = '" . $this->db->escape((string)$data['from_email']) . "', `to_name` = '" . $this->db->escape((string)$data['to_name']) . "', `to_email` = '" . $this->db->escape((string)$data['to_email']) . "', `voucher_theme_id` = '" . (int)$data['voucher_theme_id'] . "', `message` = '" . $this->db->escape((string)$data['message']) . "', `amount` = '" . (float)$data['amount'] . "', `status` = '" . (bool)$data['status'] . "' WHERE `voucher_id` = '" . (int)$voucher_id . "'");
32: | }
33: |
34: | /**
35: | * Delete Voucher
36: | *
37: | * @param int $voucher_id
38: | *
39: | * @return void
40: | */
41: | public function deleteVoucher(int $voucher_id): void {
42: | $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher` WHERE `voucher_id` = '" . (int)$voucher_id . "'");
43: |
44: | $this->deleteHistories($voucher_id);
45: | }
46: |
47: | /**
48: | * Get Voucher
49: | *
50: | * @param int $voucher_id
51: | *
52: | * @return array<string, mixed>
53: | */
54: | public function getVoucher(int $voucher_id): array {
55: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "voucher` WHERE `voucher_id` = '" . (int)$voucher_id . "'");
56: |
57: | return $query->row;
58: | }
59: |
60: | /**
61: | * Get Voucher By Code
62: | *
63: | * @param string $code
64: | *
65: | * @return array<string, mixed>
66: | */
67: | public function getVoucherByCode(string $code): array {
68: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "voucher` WHERE `code` = '" . $this->db->escape($code) . "'");
69: |
70: | return $query->row;
71: | }
72: |
73: | /**
74: | * Get Vouchers
75: | *
76: | * @param array<string, mixed> $data
77: | *
78: | * @return array<int, array<string, mixed>>
79: | */
80: | public function getVouchers(array $data = []): array {
81: | $sql = "SELECT `v`.`voucher_id`, `v`.`order_id`, `v`.`code`, `v`.`from_name`, `v`.`from_email`, `v`.`to_name`, `v`.`to_email`, (SELECT `vtd`.`name` FROM `" . DB_PREFIX . "voucher_theme_description` `vtd` WHERE `vtd`.`voucher_theme_id` = `v`.`voucher_theme_id` AND `vtd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "') AS `theme`, `v`.`amount`, `v`.`status`, `v`.`date_added` FROM `" . DB_PREFIX . "voucher` `v`";
82: |
83: | $sort_data = [
84: | 'v.code',
85: | 'v.from_name',
86: | 'v.to_name',
87: | 'theme',
88: | 'v.amount',
89: | 'v.status',
90: | 'v.date_added'
91: | ];
92: |
93: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
94: | $sql .= " ORDER BY " . $data['sort'];
95: | } else {
96: | $sql .= " ORDER BY `v`.`date_added`";
97: | }
98: |
99: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
100: | $sql .= " DESC";
101: | } else {
102: | $sql .= " ASC";
103: | }
104: |
105: | if (isset($data['start']) || isset($data['limit'])) {
106: | if ($data['start'] < 0) {
107: | $data['start'] = 0;
108: | }
109: |
110: | if ($data['limit'] < 1) {
111: | $data['limit'] = 20;
112: | }
113: |
114: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
115: | }
116: |
117: | $query = $this->db->query($sql);
118: |
119: | return $query->rows;
120: | }
121: |
122: | /**
123: | * Get Total Vouchers
124: | *
125: | * @return int
126: | */
127: | public function getTotalVouchers(): int {
128: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "voucher`");
129: |
130: | return (int)$query->row['total'];
131: | }
132: |
133: | /**
134: | * Get Total Vouchers By Voucher Theme ID
135: | *
136: | * @param int $voucher_theme_id
137: | *
138: | * @return int
139: | */
140: | public function getTotalVouchersByVoucherThemeId(int $voucher_theme_id): int {
141: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "voucher` WHERE `voucher_theme_id` = '" . (int)$voucher_theme_id . "'");
142: |
143: | return (int)$query->row['total'];
144: | }
145: |
146: | /**
147: | * Delete Voucher Histories
148: | *
149: | * @param int $voucher_id
150: | *
151: | * @return void
152: | */
153: | public function deleteHistories(int $voucher_id): void {
154: | $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_history` WHERE `voucher_id` = '" . (int)$voucher_id . "'");
155: | }
156: |
157: | /**
158: | * Delete Voucher Histories By Order ID
159: | *
160: | * @param int $order_id
161: | *
162: | * @return void
163: | */
164: | public function deleteHistoriesByOrderId(int $order_id): void {
165: | $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_history` WHERE `order_id` = '" . (int)$order_id . "'");
166: | }
167: |
168: | /**
169: | * Get Histories
170: | *
171: | * @param int $voucher_id
172: | * @param int $start
173: | * @param int $limit
174: | *
175: | * @return array<int, array<string, mixed>>
176: | */
177: | public function getHistories(int $voucher_id, int $start = 0, int $limit = 10): array {
178: | if ($start < 0) {
179: | $start = 0;
180: | }
181: |
182: | if ($limit < 1) {
183: | $limit = 10;
184: | }
185: |
186: | $query = $this->db->query("SELECT `vh`.`order_id`, CONCAT(`o`.`firstname`, ' ', `o`.`lastname`) AS `customer`, `vh`.`amount`, `vh`.`date_added` FROM `" . DB_PREFIX . "voucher_history` `vh` LEFT JOIN `" . DB_PREFIX . "order` `o` ON (`vh`.`order_id` = `o`.`order_id`) WHERE `vh`.`voucher_id` = '" . (int)$voucher_id . "' ORDER BY `vh`.`date_added` ASC LIMIT " . (int)$start . "," . (int)$limit);
187: |
188: | return $query->rows;
189: | }
190: |
191: | /**
192: | * Get Total Histories
193: | *
194: | * @param int $voucher_id
195: | *
196: | * @return int
197: | */
198: | public function getTotalHistories(int $voucher_id): int {
199: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "voucher_history` WHERE `voucher_id` = '" . (int)$voucher_id . "'");
200: |
201: | return (int)$query->row['total'];
202: | }
203: | }
204: |

OpenCart API API documentation generated by ApiGen dev-master