1: <?php
2: namespace Opencart\Catalog\Model\Account;
3: /**
4: * Class Affiliate
5: *
6: * @package Opencart\Catalog\Model\Account
7: */
8: class Affiliate extends \Opencart\System\Engine\Model {
9: /**
10: * Add Affiliate
11: *
12: * @param int $customer_id
13: * @param array<string, mixed> $data
14: *
15: * @return void
16: */
17: public function addAffiliate(int $customer_id, array $data): void {
18: $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_affiliate` SET `customer_id` = '" . (int)$customer_id . "', `company` = '" . $this->db->escape($data['company']) . "', `website` = '" . $this->db->escape($data['website']) . "', `tracking` = '" . $this->db->escape(oc_token(10)) . "', `commission` = '" . (float)$this->config->get('config_affiliate_commission') . "', `tax` = '" . $this->db->escape($data['tax']) . "', `payment_method` = '" . $this->db->escape($data['payment_method']) . "', `cheque` = '" . $this->db->escape($data['cheque']) . "', `paypal` = '" . $this->db->escape($data['paypal']) . "', `bank_name` = '" . $this->db->escape($data['bank_name']) . "', `bank_branch_number` = '" . $this->db->escape($data['bank_branch_number']) . "', `bank_swift_code` = '" . $this->db->escape($data['bank_swift_code']) . "', `bank_account_name` = '" . $this->db->escape($data['bank_account_name']) . "', `bank_account_number` = '" . $this->db->escape($data['bank_account_number']) . "', `custom_field` = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', `status` = '" . (int)!$this->config->get('config_affiliate_approval') . "', `date_added` = NOW()");
19:
20: if ($this->config->get('config_affiliate_approval')) {
21: $this->load->model('account/approval');
22:
23: $this->model_account_approval->addApproval($customer_id, 'affiliate');
24: }
25: }
26:
27: /**
28: * Edit Affiliate
29: *
30: * @param int $customer_id
31: * @param array<string, mixed> $data
32: *
33: * @return void
34: */
35: public function editAffiliate(int $customer_id, array $data): void {
36: $this->db->query("UPDATE `" . DB_PREFIX . "customer_affiliate` SET `company` = '" . $this->db->escape($data['company']) . "', `website` = '" . $this->db->escape($data['website']) . "', `commission` = '" . (float)$this->config->get('config_affiliate_commission') . "', `tax` = '" . $this->db->escape($data['tax']) . "', `payment_method` = '" . $this->db->escape($data['payment_method']) . "', `cheque` = '" . $this->db->escape($data['cheque']) . "', `paypal` = '" . $this->db->escape($data['paypal']) . "', `bank_name` = '" . $this->db->escape($data['bank_name']) . "', `bank_branch_number` = '" . $this->db->escape($data['bank_branch_number']) . "', `bank_swift_code` = '" . $this->db->escape($data['bank_swift_code']) . "', `bank_account_name` = '" . $this->db->escape($data['bank_account_name']) . "', `bank_account_number` = '" . $this->db->escape($data['bank_account_number']) . "', `custom_field` = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "' WHERE `customer_id` = '" . (int)$customer_id . "'");
37: }
38:
39: /**
40: * Delete Affiliate
41: *
42: * @param int $customer_id
43: *
44: * @return void
45: */
46: public function deleteAffiliate(int $customer_id): void {
47: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_affiliate` WHERE `customer_id` = '" . (int)$customer_id . "'");
48:
49: $this->deleteReports($customer_id);
50: }
51:
52: /**
53: * Get Affiliate
54: *
55: * @param int $customer_id
56: *
57: * @return array<string, mixed>
58: */
59: public function getAffiliate(int $customer_id): array {
60: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_affiliate` WHERE `customer_id` = '" . (int)$customer_id . "'");
61:
62: if ($query->num_rows) {
63: return $query->row + ['custom_field' => json_decode($query->row['custom_field'], true)];
64: } else {
65: return [];
66: }
67: }
68:
69: /**
70: * Get Affiliate By Tracking
71: *
72: * @param string $code
73: *
74: * @return array<string, mixed>
75: */
76: public function getAffiliateByTracking(string $code): array {
77: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_affiliate` WHERE `tracking` = '" . $this->db->escape($code) . "'");
78:
79: if ($query->num_rows) {
80: return $query->row + ['custom_field' => json_decode($query->row['custom_field'], true)];
81: } else {
82: return [];
83: }
84: }
85:
86: /**
87: * Add Report
88: *
89: * @param int $customer_id
90: * @param string $ip
91: * @param string $country
92: *
93: * @return void
94: */
95: public function addReport(int $customer_id, string $ip, string $country = ''): void {
96: $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_affiliate_report` SET `customer_id` = '" . (int)$customer_id . "', `store_id` = '" . (int)$this->config->get('config_store_id') . "', `ip` = '" . $this->db->escape($ip) . "', `country` = '" . $this->db->escape($country) . "', `date_added` = NOW()");
97: }
98:
99: /**
100: * Delete Customer Affiliate Reports
101: *
102: * @param int $customer_id
103: *
104: * @return void
105: */
106: public function deleteReports(int $customer_id): void {
107: $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_affiliate_report` WHERE `customer_id` = '" . (int)$customer_id . "'");
108: }
109: }
110: