1: | <?php
|
2: | namespace Opencart\Admin\Model\Localisation;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class TaxClass extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
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: | |
34: | |
35: | |
36: | |
37: | |
38: | |
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: | |
56: | |
57: | |
58: | |
59: | |
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: | |
71: | |
72: | |
73: | |
74: | |
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: | |
84: | |
85: | |
86: | |
87: | |
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: | |
127: | |
128: | |
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: | |
138: | |
139: | |
140: | |
141: | |
142: | |
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: | |
150: | |
151: | |
152: | |
153: | |
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: | |
161: | |
162: | |
163: | |
164: | |
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: | |
174: | |
175: | |
176: | |
177: | |
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: | |