1: <?php
2: namespace Opencart\Catalog\Model\Extension\Opencart\Total;
3: /**
4: * Class Reward
5: *
6: * @package Opencart\Catalog\Model\Extension\Opencart\Total
7: */
8: class Reward 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['reward'])) {
20: $this->load->language('extension/opencart/total/reward', 'reward');
21:
22: $points = $this->customer->getRewardPoints();
23:
24: if ($this->session->data['reward'] <= $points) {
25: $discount_total = 0;
26:
27: $points_total = 0;
28:
29: foreach ($this->cart->getProducts() as $product) {
30: if ($product['points']) {
31: $points_total += $product['points'];
32: }
33: }
34:
35: $points = min($points, $points_total);
36:
37: foreach ($this->cart->getProducts() as $product) {
38: $discount = 0;
39:
40: if ($product['points']) {
41: $discount = $product['total'] * ($this->session->data['reward'] / $points_total);
42:
43: if ($product['tax_class_id']) {
44: $tax_rates = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']);
45:
46: foreach ($tax_rates as $tax_rate) {
47: if ($tax_rate['type'] == 'P') {
48: $taxes[$tax_rate['tax_rate_id']] -= $tax_rate['amount'];
49: }
50: }
51: }
52: }
53:
54: $discount_total += $discount;
55: }
56:
57: $totals[] = [
58: 'extension' => 'opencart',
59: 'code' => 'reward',
60: 'title' => sprintf($this->language->get('reward_text_reward'), -$this->session->data['reward']),
61: 'value' => -$discount_total,
62: 'sort_order' => (int)$this->config->get('total_reward_sort_order')
63: ];
64:
65: $total -= $discount_total;
66: }
67: }
68: }
69:
70: /**
71: * Confirm
72: *
73: * @param array<string, mixed> $order_info
74: * @param array<string, mixed> $order_total
75: *
76: * @return int
77: */
78: public function confirm(array $order_info, array $order_total): int {
79: $this->load->language('extension/opencart/total/reward');
80:
81: $points = 0.0;
82:
83: $start = strpos($order_total['title'], '(');
84: $end = strrpos($order_total['title'], ')');
85:
86: if ($start !== false && $end !== false) {
87: $points = (float)substr($order_total['title'], $start + 1, $end - ($start + 1));
88: }
89:
90: $this->load->model('account/reward');
91:
92: if ($order_info['customer_id'] && $this->model_account_reward->getRewardTotal($order_info['customer_id']) >= $points) {
93: $this->model_account_reward->addReward($order_info['customer_id'], $order_info['order_id'], sprintf($this->language->get('text_order_id'), (int)$order_info['order_id']), (int)$points);
94: } else {
95: return $this->config->get('config_fraud_status_id');
96: }
97:
98: return 0;
99: }
100:
101: /**
102: * Unconfirm
103: *
104: * @param array<string, mixed> $order_info
105: *
106: * @return void
107: */
108: public function unconfirm(array $order_info): void {
109: $this->load->model('account/reward');
110:
111: $this->model_account_reward->deleteRewardByOrderId($order_info['order_id']);
112: }
113: }
114: