1: <?php
2: namespace Opencart\Catalog\Model\Checkout;
3: /**
4: * Class Voucher
5: *
6: * @package Opencart\Catalog\Model\Checkout
7: */
8: class Voucher extends \Opencart\System\Engine\Model {
9: /**
10: * Add Voucher
11: *
12: * @param int $order_id
13: * @param array<string, mixed> $data
14: *
15: * @return int
16: */
17: public function addVoucher(int $order_id, array $data): int {
18: $this->db->query("INSERT INTO `" . DB_PREFIX . "voucher` SET `order_id` = '" . (int)$order_id . "', `code` = '" . $this->db->escape($data['code']) . "', `from_name` = '" . $this->db->escape($data['from_name']) . "', `from_email` = '" . $this->db->escape($data['from_email']) . "', `to_name` = '" . $this->db->escape($data['to_name']) . "', `to_email` = '" . $this->db->escape($data['to_email']) . "', `voucher_theme_id` = '" . (int)$data['voucher_theme_id'] . "', `message` = '" . $this->db->escape($data['message']) . "', `amount` = '" . (float)$data['amount'] . "', `status` = '1', `date_added` = NOW()");
19:
20: return $this->db->getLastId();
21: }
22:
23: /**
24: * Delete Vouchers By Order ID
25: *
26: * @param int $order_id
27: *
28: * @return void
29: */
30: public function deleteVouchersByOrderId(int $order_id): void {
31: $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher` WHERE `order_id` = '" . (int)$order_id . "'");
32: }
33:
34: /**
35: * Disable Voucher
36: *
37: * @param int $order_id
38: *
39: * @return void
40: */
41: public function disableVoucher(int $order_id): void {
42: $this->db->query("UPDATE `" . DB_PREFIX . "voucher` SET `status` = '0' WHERE `order_id` = '" . (int)$order_id . "'");
43: }
44:
45: /**
46: * Get Voucher
47: *
48: * @param string $code
49: *
50: * @return array<string, mixed>
51: */
52: public function getVoucher(string $code): array {
53: $status = true;
54:
55: $this->load->model('checkout/voucher');
56:
57: $voucher_info = $this->model_checkout_voucher->getVoucherByCode($code);
58:
59: if ($voucher_info) {
60: if ($voucher_info['order_id']) {
61: $this->load->model('checkout/order');
62:
63: $order_info = $this->model_checkout_order->getOrder($voucher_info['order_id']);
64:
65: if (!$order_info || !in_array($order_info['order_status_id'], (array)$this->config->get('config_complete_status'))) {
66: $status = false;
67: }
68:
69: $order_info = $this->model_checkout_order->getVoucherByVoucherId($voucher_info['order_id'], $voucher_info['voucher_id']);
70:
71: if (!$order_info) {
72: $status = false;
73: }
74: }
75:
76: $remaining = $this->model_checkout_voucher->getVoucherTotal($voucher_info['voucher_id']);
77:
78: if ($remaining) {
79: $amount = $voucher_info['amount'] + $remaining;
80: } else {
81: $amount = $voucher_info['amount'];
82: }
83:
84: if ($amount <= 0) {
85: $status = false;
86: }
87: } else {
88: $status = false;
89: }
90:
91: if ($status) {
92: return [
93: 'voucher_id' => $voucher_info['voucher_id'],
94: 'code' => $voucher_info['code'],
95: 'from_name' => $voucher_info['from_name'],
96: 'from_email' => $voucher_info['from_email'],
97: 'to_name' => $voucher_info['to_name'],
98: 'to_email' => $voucher_info['to_email'],
99: 'voucher_theme_id' => $voucher_info['voucher_theme_id'],
100: 'theme' => $voucher_info['theme'],
101: 'message' => $voucher_info['message'],
102: 'image' => $voucher_info['image'],
103: 'amount' => $amount,
104: 'status' => $voucher_info['status'],
105: 'date_added' => $voucher_info['date_added']
106: ];
107: } else {
108: return [];
109: }
110: }
111:
112: /**
113: * Get Voucher By Code
114: *
115: * @param string $code
116: *
117: * @return array<string, mixed>
118: */
119: public function getVoucherByCode(string $code): array {
120: $query = $this->db->query("SELECT *, `vtd`.`name` AS theme FROM `" . DB_PREFIX . "voucher` `v` LEFT JOIN `" . DB_PREFIX . "voucher_theme` `vt` ON (`v`.`voucher_theme_id` = `vt`.`voucher_theme_id`) LEFT JOIN `" . DB_PREFIX . "voucher_theme_description` `vtd` ON (`vt`.`voucher_theme_id` = `vtd`.`voucher_theme_id`) WHERE `v`.`code` = '" . $this->db->escape($code) . "' AND `vtd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND `v`.`status` = '1'");
121:
122: return $query->row;
123: }
124:
125: /**
126: * Get Voucher Total
127: *
128: * @param int $voucher_id
129: *
130: * @return float
131: */
132: public function getVoucherTotal(int $voucher_id): float {
133: $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "voucher_history` `vh` WHERE `vh`.`voucher_id` = '" . (int)$voucher_id . "' GROUP BY `vh`.`voucher_id`");
134:
135: if ($query->num_rows) {
136: return (int)$query->row['total'];
137: } else {
138: return 0;
139: }
140: }
141:
142: /**
143: * Add History
144: *
145: * @param int $voucher_id
146: * @param int $order_id
147: * @param float $value
148: *
149: * @return int
150: */
151: public function addHistory(int $voucher_id, int $order_id, float $value): int {
152: $this->db->query("INSERT INTO `" . DB_PREFIX . "voucher_history` SET `voucher_id` = '" . (int)$voucher_id . "', `order_id` = '" . (int)$order_id . "', `amount` = '" . (float)$value . "', `date_added` = NOW()");
153:
154: return $this->db->getLastId();
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: