1: <?php
2: namespace Opencart\Admin\Model\Localisation;
3: /**
4: * Class TaxClass
5: *
6: * @package Opencart\Admin\Model\Localisation
7: */
8: class TaxClass extends \Opencart\System\Engine\Model {
9: /**
10: * Add Tax Class
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addTaxClass(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "tax_class` SET `title` = '" . $this->db->escape((string)$data['title']) . "', `description` = '" . $this->db->escape((string)$data['description']) . "'");
18:
19: $tax_class_id = $this->db->getLastId();
20:
21: if (isset($data['tax_rule'])) {
22: foreach ($data['tax_rule'] as $tax_rule) {
23: $this->addTaxRule($tax_class_id, $tax_rule);
24: }
25: }
26:
27: $this->cache->delete('tax_class');
28:
29: return $tax_class_id;
30: }
31:
32: /**
33: * Edit Tax Class
34: *
35: * @param int $tax_class_id
36: * @param array<string, mixed> $data
37: *
38: * @return void
39: */
40: public function editTaxClass(int $tax_class_id, array $data): void {
41: $this->db->query("UPDATE `" . DB_PREFIX . "tax_class` SET `title` = '" . $this->db->escape((string)$data['title']) . "', `description` = '" . $this->db->escape((string)$data['description']) . "' WHERE `tax_class_id` = '" . (int)$tax_class_id . "'");
42:
43: $this->deleteTaxRules($tax_class_id);
44:
45: if (isset($data['tax_rule'])) {
46: foreach ($data['tax_rule'] as $tax_rule) {
47: $this->addTaxRule($tax_class_id, $tax_rule);
48: }
49: }
50:
51: $this->cache->delete('tax_class');
52: }
53:
54: /**
55: * Delete Tax Class
56: *
57: * @param int $tax_class_id
58: *
59: * @return void
60: */
61: public function deleteTaxClass(int $tax_class_id): void {
62: $this->db->query("DELETE FROM `" . DB_PREFIX . "tax_class` WHERE `tax_class_id` = '" . (int)$tax_class_id . "'");
63:
64: $this->deleteTaxRules($tax_class_id);
65:
66: $this->cache->delete('tax_class');
67: }
68:
69: /**
70: * Get Tax Class
71: *
72: * @param int $tax_class_id
73: *
74: * @return array<string, mixed>
75: */
76: public function getTaxClass(int $tax_class_id): array {
77: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "tax_class` WHERE `tax_class_id` = '" . (int)$tax_class_id . "'");
78:
79: return $query->row;
80: }
81:
82: /**
83: * Get Tax Classes
84: *
85: * @param array<string, mixed> $data
86: *
87: * @return array<int, array<string, mixed>>
88: */
89: public function getTaxClasses(array $data = []): array {
90: $sql = "SELECT * FROM `" . DB_PREFIX . "tax_class` ORDER BY `title`";
91:
92: if (isset($data['order']) && ($data['order'] == 'DESC')) {
93: $sql .= " DESC";
94: } else {
95: $sql .= " ASC";
96: }
97:
98: if (isset($data['start']) || isset($data['limit'])) {
99: if ($data['start'] < 0) {
100: $data['start'] = 0;
101: }
102:
103: if ($data['limit'] < 1) {
104: $data['limit'] = 20;
105: }
106:
107: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
108: }
109:
110: $key = md5($sql);
111:
112: $tax_class_data = $this->cache->get('tax_class.' . $key);
113:
114: if (!$tax_class_data) {
115: $query = $this->db->query($sql);
116:
117: $tax_class_data = $query->rows;
118:
119: $this->cache->set('tax_class.' . $key, $tax_class_data);
120: }
121:
122: return $tax_class_data;
123: }
124:
125: /**
126: * Get Total Tax Classes
127: *
128: * @return int
129: */
130: public function getTotalTaxClasses(): int {
131: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "tax_class`");
132:
133: return (int)$query->row['total'];
134: }
135:
136: /**
137: * Add Tax Rule
138: *
139: * @param int $tax_class_id
140: * @param array<string, mixed> $data
141: *
142: * @return void
143: */
144: public function addTaxRule(int $tax_class_id, array $data): void {
145: $this->db->query("INSERT INTO `" . DB_PREFIX . "tax_rule` SET `tax_class_id` = '" . (int)$tax_class_id . "', `tax_rate_id` = '" . (int)$data['tax_rate_id'] . "', `based` = '" . $this->db->escape($data['based']) . "', `priority` = '" . (int)$data['priority'] . "'");
146: }
147:
148: /**
149: * Delete Tax Rules
150: *
151: * @param int $tax_class_id
152: *
153: * @return void
154: */
155: public function deleteTaxRules(int $tax_class_id): void {
156: $this->db->query("DELETE FROM `" . DB_PREFIX . "tax_rule` WHERE `tax_class_id` = '" . (int)$tax_class_id . "'");
157: }
158:
159: /**
160: * Get Tax Rules
161: *
162: * @param int $tax_class_id
163: *
164: * @return array<int, array<string, mixed>>
165: */
166: public function getTaxRules(int $tax_class_id): array {
167: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "tax_rule` WHERE `tax_class_id` = '" . (int)$tax_class_id . "' ORDER BY `priority` ASC");
168:
169: return $query->rows;
170: }
171:
172: /**
173: * Get Total Tax Rules By Tax Rate ID
174: *
175: * @param int $tax_rate_id
176: *
177: * @return int
178: */
179: public function getTotalTaxRulesByTaxRateId(int $tax_rate_id): int {
180: $query = $this->db->query("SELECT COUNT(DISTINCT `tax_class_id`) AS `total` FROM `" . DB_PREFIX . "tax_rule` WHERE `tax_rate_id` = '" . (int)$tax_rate_id . "'");
181:
182: return (int)$query->row['total'];
183: }
184: }
185: