1: <?php
2: namespace Opencart\Catalog\Model\Extension\Opencart\Total;
3: /**
4: * Class Voucher
5: *
6: * @package Opencart\Catalog\Model\Extension\Opencart\Total
7: */
8: class Voucher extends \Opencart\System\Engine\Model {
9: /**
10: * Get Total
11: *
12: * @param array<int, array<string, mixed>> $totals
13: * @param array<int, float> $taxes
14: * @param float $total
15: *
16: * @return void
17: */
18: public function getTotal(array &$totals, array &$taxes, float &$total): void {
19: if (isset($this->session->data['voucher'])) {
20: $this->load->language('extension/opencart/total/voucher', 'voucher');
21:
22: $this->load->model('checkout/voucher');
23:
24: $voucher_info = $this->model_checkout_voucher->getVoucher($this->session->data['voucher']);
25:
26: if ($voucher_info) {
27: $amount = min($voucher_info['amount'], $total);
28:
29: if ($amount > 0) {
30: $totals[] = [
31: 'extension' => 'opencart',
32: 'code' => 'voucher',
33: 'title' => sprintf($this->language->get('voucher_text_voucher'), $this->session->data['voucher']),
34: 'value' => -$amount,
35: 'sort_order' => (int)$this->config->get('total_voucher_sort_order')
36: ];
37:
38: $total -= $amount;
39: } else {
40: unset($this->session->data['voucher']);
41: }
42: } else {
43: unset($this->session->data['voucher']);
44: }
45: }
46: }
47:
48: /**
49: * Confirm
50: *
51: * @param array<string, mixed> $order_info
52: * @param array<string, mixed> $order_total
53: *
54: * @return int
55: */
56: public function confirm(array $order_info, array $order_total): int {
57: $code = '';
58:
59: $start = strpos($order_total['title'], '(');
60: $end = strrpos($order_total['title'], ')');
61:
62: if ($start !== false && $end !== false) {
63: $code = substr($order_total['title'], $start + 1, $end - ($start + 1));
64: }
65:
66: if ($code) {
67: $this->load->model('checkout/voucher');
68:
69: $voucher_info = $this->model_checkout_voucher->getVoucher($code);
70:
71: if ($voucher_info) {
72: $this->model_checkout_voucher->addHistory($voucher_info['voucher_id'], $order_info['order_id'], $order_total['value']);
73: } else {
74: return $this->config->get('config_fraud_status_id');
75: }
76: }
77:
78: return 0;
79: }
80:
81: /**
82: * Unconfirm
83: *
84: * @param array<string, mixed> $order_info
85: *
86: * @return void
87: */
88: public function unconfirm(array $order_info): void {
89: $this->load->model('checkout/voucher');
90:
91: $this->model_checkout_voucher->deleteHistoryByOrderId($order_info['order_id']);
92: }
93: }
94: