1: | <?php
2: | namespace Opencart\Admin\Model\Customer;
3: | /**
4: | * Class Customer Group
5: | *
6: | * @package Opencart\Admin\Model\Customer
7: | */
8: | class CustomerGroup extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Customer Group
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addCustomerGroup(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_group` SET `approval` = '" . (isset($data['approval']) ? (bool)$data['approval'] : 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
18: |
19: | $customer_group_id = $this->db->getLastId();
20: |
21: | foreach ($data['customer_group_description'] as $language_id => $value) {
22: | $this->addDescription($customer_group_id, $language_id, $value);
23: | }
24: |
25: | return $customer_group_id;
26: | }
27: |
28: | /**
29: | * Edit Customer Group
30: | *
31: | * @param int $customer_group_id
32: | * @param array<string, mixed> $data
33: | *
34: | * @return void
35: | */
36: | public function editCustomerGroup(int $customer_group_id, array $data): void {
37: | $this->db->query("UPDATE `" . DB_PREFIX . "customer_group` SET `approval` = '" . (isset($data['approval']) ? (bool)$data['approval'] : 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `customer_group_id` = '" . (int)$customer_group_id . "'");
38: |
39: | $this->deleteDescriptions($customer_group_id);
40: |
41: | foreach ($data['customer_group_description'] as $language_id => $value) {
42: | $this->addDescription($customer_group_id, $language_id, $value);
43: | }
44: | }
45: |
46: | /**
47: | * Delete Customer Group
48: | *
49: | * @param int $customer_group_id
50: | *
51: | * @return void
52: | */
53: | public function deleteCustomerGroup(int $customer_group_id): void {
54: | $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_group` WHERE `customer_group_id` = '" . (int)$customer_group_id . "'");
55: |
56: | $this->deleteDescriptions($customer_group_id);
57: |
58: | $this->load->model('catalog/product');
59: |
60: | $this->model_catalog_product->deleteDiscountsByCustomerGroupId($customer_group_id);
61: | $this->model_catalog_product->deleteSpecialsByCustomerGroupId($customer_group_id);
62: | $this->model_catalog_product->deleteRewardsByCustomerGroupId($customer_group_id);
63: |
64: | $this->load->model('localisation/tax_rate');
65: |
66: | $this->model_localisation_tax_rate->deleteCustomerGroupsByCustomerGroupId($customer_group_id);
67: | }
68: |
69: | /**
70: | * Get Customer Group
71: | *
72: | * @param int $customer_group_id
73: | *
74: | * @return array<string, mixed>
75: | */
76: | public function getCustomerGroup(int $customer_group_id): array {
77: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "customer_group` `cg` LEFT JOIN `" . DB_PREFIX . "customer_group_description` `cgd` ON (`cg`.`customer_group_id` = `cgd`.`customer_group_id`) WHERE `cg`.`customer_group_id` = '" . (int)$customer_group_id . "' AND `cgd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
78: |
79: | return $query->row;
80: | }
81: |
82: | /**
83: | * Get Customer Groups
84: | *
85: | * @param array<string, mixed> $data
86: | *
87: | * @return array<int, array<string, mixed>>
88: | */
89: | public function getCustomerGroups(array $data = []): array {
90: | $sql = "SELECT * FROM `" . DB_PREFIX . "customer_group` `cg` LEFT JOIN `" . DB_PREFIX . "customer_group_description` `cgd` ON (`cg`.`customer_group_id` = `cgd`.`customer_group_id`) WHERE `cgd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
91: |
92: | $sort_data = [
93: | 'cgd.name',
94: | 'cg.sort_order'
95: | ];
96: |
97: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
98: | $sql .= " ORDER BY " . $data['sort'];
99: | } else {
100: | $sql .= " ORDER BY `cgd`.`name`";
101: | }
102: |
103: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
104: | $sql .= " DESC";
105: | } else {
106: | $sql .= " ASC";
107: | }
108: |
109: | if (isset($data['start']) || isset($data['limit'])) {
110: | if ($data['start'] < 0) {
111: | $data['start'] = 0;
112: | }
113: |
114: | if ($data['limit'] < 1) {
115: | $data['limit'] = 20;
116: | }
117: |
118: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
119: | }
120: |
121: | $query = $this->db->query($sql);
122: |
123: | return $query->rows;
124: | }
125: |
126: | /**
127: | * Add Description
128: | *
129: | * @param int $customer_group_id
130: | * @param int $language_id
131: | * @param array<string, mixed> $data
132: | *
133: | * @return void
134: | */
135: | public function addDescription(int $customer_group_id, int $language_id, array $data): void {
136: | $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_group_description` SET `customer_group_id` = '" . (int)$customer_group_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "', `description` = '" . $this->db->escape($data['description']) . "'");
137: | }
138: |
139: | /**
140: | * Delete Descriptions
141: | *
142: | * @param int $customer_group_id
143: | *
144: | * @return void
145: | */
146: | public function deleteDescriptions(int $customer_group_id): void {
147: | $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_group_description` WHERE `customer_group_id` = '" . (int)$customer_group_id . "'");
148: | }
149: |
150: | /**
151: | * Delete Descriptions By Language ID
152: | *
153: | * @param int $language_id
154: | *
155: | * @return void
156: | */
157: | public function deleteDescriptionsByLanguageId(int $language_id): void {
158: | $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_group_description` WHERE `language_id` = '" . (int)$language_id . "'");
159: | }
160: |
161: | /**
162: | * Get Descriptions
163: | *
164: | * @param int $customer_group_id
165: | *
166: | * @return array<int, array<string, string>>
167: | */
168: | public function getDescriptions(int $customer_group_id): array {
169: | $customer_group_data = [];
170: |
171: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_group_description` WHERE `customer_group_id` = '" . (int)$customer_group_id . "'");
172: |
173: | foreach ($query->rows as $result) {
174: | $customer_group_data[$result['language_id']] = [
175: | 'name' => $result['name'],
176: | 'description' => $result['description']
177: | ];
178: | }
179: |
180: | return $customer_group_data;
181: | }
182: |
183: | /**
184: | * Get Descriptions By Language ID
185: | *
186: | * @param int $language_id
187: | *
188: | * @return array<int, array<string, string>>
189: | */
190: | public function getDescriptionsByLanguageId(int $language_id): array {
191: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_group_description` WHERE `language_id` = '" . (int)$language_id . "'");
192: |
193: | return $query->rows;
194: | }
195: |
196: | /**
197: | * Get Total Customer Groups
198: | *
199: | * @return int
200: | */
201: | public function getTotalCustomerGroups(): int {
202: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_group`");
203: |
204: | return (int)$query->row['total'];
205: | }
206: | }
207: |

OpenCart API API documentation generated by ApiGen dev-master