1: <?php
2: namespace Opencart\Admin\Model\Customer;
3: /**
4: * Class Customer Approval
5: *
6: * @package Opencart\Admin\Model\Customer
7: */
8: class CustomerApproval extends \Opencart\System\Engine\Model {
9: /**
10: * Delete Approvals By Customer ID
11: *
12: * @param int $customer_id
13: *
14: * @return void
15: */
16: public function deleteApprovalsByCustomerId(int $customer_id): void {
17: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_id` = '" . (int)$customer_id . "'");
18: }
19:
20: /**
21: * Get Customer Approvals
22: *
23: * @param array<string, mixed> $data
24: *
25: * @return array<int, array<string, mixed>>
26: */
27: public function getCustomerApprovals(array $data = []): array {
28: $sql = "SELECT *, CONCAT(`c`.`firstname`, ' ', `c`.`lastname`) AS customer, `cgd`.`name` AS customer_group, `ca`.`type` FROM `" . DB_PREFIX . "customer_approval` `ca` LEFT JOIN `" . DB_PREFIX . "customer` `c` ON (`ca`.`customer_id` = `c`.`customer_id`) LEFT JOIN `" . DB_PREFIX . "customer_group_description` `cgd` ON (`c`.`customer_group_id` = `cgd`.`customer_group_id`) WHERE `cgd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
29:
30: if (!empty($data['filter_customer'])) {
31: $sql .= " AND LCASE(CONCAT(`c`.`firstname`, ' ', `c`.`lastname`)) LIKE '" . $this->db->escape('%' . oc_strtolower($data['filter_customer']) . '%') . "'";
32: }
33:
34: if (!empty($data['filter_email'])) {
35: $sql .= " AND LCASE(`c`.`email`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_email']) . '%') . "'";
36: }
37:
38: if (!empty($data['filter_customer_group_id'])) {
39: $sql .= " AND `c`.`customer_group_id` = '" . (int)$data['filter_customer_group_id'] . "'";
40: }
41:
42: if (!empty($data['filter_type'])) {
43: $sql .= " AND `ca`.`type` = '" . $this->db->escape((string)$data['filter_type']) . "'";
44: }
45:
46: if (!empty($data['filter_date_from'])) {
47: $sql .= " AND DATE(`c`.`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
48: }
49:
50: if (!empty($data['filter_date_to'])) {
51: $sql .= " AND DATE(`c`.`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
52: }
53:
54: $sql .= " ORDER BY `c`.`date_added` DESC";
55:
56: if (isset($data['start']) || isset($data['limit'])) {
57: if ($data['start'] < 0) {
58: $data['start'] = 0;
59: }
60:
61: if ($data['limit'] < 1) {
62: $data['limit'] = 20;
63: }
64:
65: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
66: }
67:
68: $query = $this->db->query($sql);
69:
70: return $query->rows;
71: }
72:
73: /**
74: * Get Customer Approval
75: *
76: * @param int $customer_approval_id
77: *
78: * @return array<string, mixed>
79: */
80: public function getCustomerApproval(int $customer_approval_id): array {
81: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_approval_id` = '" . (int)$customer_approval_id . "'");
82:
83: return $query->row;
84: }
85:
86: /**
87: * Get Total Customer Approvals
88: *
89: * @param array<string, mixed> $data
90: *
91: * @return int
92: */
93: public function getTotalCustomerApprovals(array $data = []): int {
94: $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_approval` `ca` LEFT JOIN `" . DB_PREFIX . "customer` `c` ON (`ca`.`customer_id` = `c`.`customer_id`)";
95:
96: $implode = [];
97:
98: if (!empty($data['filter_customer'])) {
99: $implode[] = "LCASE(CONCAT(`c`.`firstname`, ' ', `c`.`lastname`)) LIKE '" . $this->db->escape(oc_strtolower($data['filter_customer']) . '%') . "'";
100: }
101:
102: if (!empty($data['filter_email'])) {
103: $implode[] = "LCASE(`c`.`email`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_email']) . '%') . "'";
104: }
105:
106: if (!empty($data['filter_customer_group_id'])) {
107: $implode[] = "`c`.`customer_group_id` = '" . (int)$data['filter_customer_group_id'] . "'";
108: }
109:
110: if (!empty($data['filter_type'])) {
111: $implode[] = "`ca`.`type` = '" . $this->db->escape((string)$data['filter_type']) . "'";
112: }
113:
114: if (!empty($data['filter_date_from'])) {
115: $implode[] = "DATE(`c`.`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
116: }
117:
118: if (!empty($data['filter_date_to'])) {
119: $implode[] = "DATE(`c`.`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
120: }
121:
122: if ($implode) {
123: $sql .= " WHERE " . implode(" AND ", $implode);
124: }
125:
126: $query = $this->db->query($sql);
127:
128: return (int)$query->row['total'];
129: }
130:
131: /**
132: * Approve Customer
133: *
134: * @param int $customer_id
135: *
136: * @return void
137: */
138: public function approveCustomer(int $customer_id): void {
139: $this->db->query("UPDATE `" . DB_PREFIX . "customer` SET `status` = '1' WHERE `customer_id` = '" . (int)$customer_id . "'");
140: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_id` = '" . (int)$customer_id . "' AND `type` = 'customer'");
141: }
142:
143: /**
144: * Deny Customer
145: *
146: * @param int $customer_id
147: *
148: * @return void
149: */
150: public function denyCustomer(int $customer_id): void {
151: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_id` = '" . (int)$customer_id . "' AND `type` = 'customer'");
152: }
153:
154: /**
155: * Approve Affiliate
156: *
157: * @param int $customer_id
158: *
159: * @return void
160: */
161: public function approveAffiliate(int $customer_id): void {
162: $this->db->query("UPDATE `" . DB_PREFIX . "customer_affiliate` SET `status` = '1' WHERE `customer_id` = '" . (int)$customer_id . "'");
163: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_id` = '" . (int)$customer_id . "' AND `type` = 'affiliate'");
164: }
165:
166: /**
167: * Deny Affiliate
168: *
169: * @param int $customer_id
170: *
171: * @return void
172: */
173: public function denyAffiliate(int $customer_id): void {
174: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_approval` WHERE `customer_id` = '" . (int)$customer_id . "' AND `type` = 'affiliate'");
175: }
176: }
177: