1: <?php
2: namespace Opencart\Admin\Model\Localisation;
3: /**
4: * Class TaxRate
5: *
6: * @package Opencart\Admin\Model\Localisation
7: */
8: class TaxRate extends \Opencart\System\Engine\Model {
9: /**
10: * Add Tax Rate
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addTaxRate(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "tax_rate` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `rate` = '" . (float)$data['rate'] . "', `type` = '" . $this->db->escape((string)$data['type']) . "', `geo_zone_id` = '" . (int)$data['geo_zone_id'] . "'");
18:
19: $tax_rate_id = $this->db->getLastId();
20:
21: if (isset($data['tax_rate_customer_group'])) {
22: foreach ($data['tax_rate_customer_group'] as $customer_group_id) {
23: $this->addCustomerGroup($tax_rate_id, $customer_group_id);
24: }
25: }
26:
27: return $tax_rate_id;
28: }
29:
30: /**
31: * Edit Tax Rate
32: *
33: * @param int $tax_rate_id
34: * @param array<string, mixed> $data
35: *
36: * @return void
37: */
38: public function editTaxRate(int $tax_rate_id, array $data): void {
39: $this->db->query("UPDATE `" . DB_PREFIX . "tax_rate` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `rate` = '" . (float)$data['rate'] . "', `type` = '" . $this->db->escape((string)$data['type']) . "', `geo_zone_id` = '" . (int)$data['geo_zone_id'] . "' WHERE `tax_rate_id` = '" . (int)$tax_rate_id . "'");
40:
41: $this->deleteCustomerGroups($tax_rate_id);
42:
43: if (isset($data['tax_rate_customer_group'])) {
44: foreach ($data['tax_rate_customer_group'] as $customer_group_id) {
45: $this->addCustomerGroup($tax_rate_id, $customer_group_id);
46: }
47: }
48: }
49:
50: /**
51: * Delete Tax Rate
52: *
53: * @param int $tax_rate_id
54: *
55: * @return void
56: */
57: public function deleteTaxRate(int $tax_rate_id): void {
58: $this->db->query("DELETE FROM `" . DB_PREFIX . "tax_rate` WHERE `tax_rate_id` = '" . (int)$tax_rate_id . "'");
59:
60: $this->deleteCustomerGroups($tax_rate_id);
61: }
62:
63: /**
64: * Get Tax Rate
65: *
66: * @param int $tax_rate_id
67: *
68: * @return array<string, mixed>
69: */
70: public function getTaxRate(int $tax_rate_id): array {
71: $query = $this->db->query("SELECT `tr`.`tax_rate_id`, `tr`.`name` AS name, `tr`.`rate`, `tr`.`type`, `tr`.`geo_zone_id`, `gz`.`name` AS `geo_zone` FROM `" . DB_PREFIX . "tax_rate` `tr` LEFT JOIN `" . DB_PREFIX . "geo_zone` `gz` ON (`tr`.`geo_zone_id` = `gz`.`geo_zone_id`) WHERE `tr`.`tax_rate_id` = '" . (int)$tax_rate_id . "'");
72:
73: return $query->row;
74: }
75:
76: /**
77: * Get Tax Rates
78: *
79: * @param array<string, mixed> $data
80: *
81: * @return array<int, array<string, mixed>>
82: */
83: public function getTaxRates(array $data = []): array {
84: $sql = "SELECT `tr`.`tax_rate_id`, `tr`.`name` AS `name`, `tr`.`rate`, `tr`.`type`, `gz`.`name` AS `geo_zone` FROM `" . DB_PREFIX . "tax_rate` `tr` LEFT JOIN `" . DB_PREFIX . "geo_zone` `gz` ON (`tr`.`geo_zone_id` = `gz`.`geo_zone_id`)";
85:
86: $sort_data = [
87: 'tr.name',
88: 'tr.rate',
89: 'tr.type',
90: 'gz.name'
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 `tr`.`name`";
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 Tax Rates
124: *
125: * @return int
126: */
127: public function getTotalTaxRates(): int {
128: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "tax_rate`");
129:
130: return (int)$query->row['total'];
131: }
132:
133: /**
134: * Get Total Tax Rates By Geo Zone ID
135: *
136: * @param int $geo_zone_id
137: *
138: * @return int
139: */
140: public function getTotalTaxRatesByGeoZoneId(int $geo_zone_id): int {
141: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "tax_rate` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
142:
143: return (int)$query->row['total'];
144: }
145:
146: /**
147: * Add Customer Group
148: *
149: * @param int $tax_rate_id
150: * @param int $customer_group_id
151: *
152: * @return void
153: */
154: public function addCustomerGroup(int $tax_rate_id, int $customer_group_id): void {
155: $this->db->query("INSERT INTO `" . DB_PREFIX . "tax_rate_to_customer_group` SET `tax_rate_id` = '" . (int)$tax_rate_id . "', `customer_group_id` = '" . (int)$customer_group_id . "'");
156: }
157:
158: /**
159: * Delete Customer Groups
160: *
161: * @param int $tax_rate_id
162: *
163: * @return void
164: */
165: public function deleteCustomerGroups(int $tax_rate_id): void {
166: $this->db->query("DELETE FROM `" . DB_PREFIX . "tax_rate_to_customer_group` WHERE `tax_rate_id` = '" . (int)$tax_rate_id . "'");
167: }
168:
169: /**
170: * Delete Customer Groups By Customer Group ID
171: *
172: * @param int $customer_group_id
173: *
174: * @return void
175: */
176: public function deleteCustomerGroupsByCustomerGroupId(int $customer_group_id): void {
177: $this->db->query("DELETE FROM `" . DB_PREFIX . "tax_rate_to_customer_group` WHERE `customer_group_id` = '" . (int)$customer_group_id . "'");
178: }
179:
180: /**
181: * Get Customer Groups
182: *
183: * @param int $tax_rate_id
184: *
185: * @return array<int, int>
186: */
187: public function getCustomerGroups(int $tax_rate_id): array {
188: $tax_customer_group_data = [];
189:
190: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "tax_rate_to_customer_group` WHERE `tax_rate_id` = '" . (int)$tax_rate_id . "'");
191:
192: foreach ($query->rows as $result) {
193: $tax_customer_group_data[] = $result['customer_group_id'];
194: }
195:
196: return $tax_customer_group_data;
197: }
198: }
199: