1: <?php
2: namespace Opencart\Catalog\Model\Account;
3: /**
4: * Class Subscription
5: *
6: * @package Opencart\Catalog\Model\Account
7: */
8: class Subscription extends \Opencart\System\Engine\Model {
9: /**
10: * Get Subscription
11: *
12: * @param int $subscription_id
13: *
14: * @return array<string, mixed>
15: */
16: public function getSubscription(int $subscription_id): array {
17: $subscription_data = [];
18:
19: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription` `s` WHERE `subscription_id` = '" . (int)$subscription_id . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");
20:
21: if ($query->num_rows) {
22: $subscription_data = $query->row;
23:
24: $subscription_data['option'] = ($query->row['option'] ? json_decode($query->row['option'], true) : '');
25: $subscription_data['payment_method'] = ($query->row['payment_method'] ? json_decode($query->row['payment_method'], true) : '');
26: $subscription_data['shipping_method'] = ($query->row['shipping_method'] ? json_decode($query->row['shipping_method'], true) : '');
27: }
28:
29: return $subscription_data;
30: }
31:
32: /**
33: * Get Subscription By Order Product ID
34: *
35: * @param int $order_id
36: * @param int $order_product_id
37: *
38: * @return array<string, mixed>
39: */
40: public function getSubscriptionByOrderProductId(int $order_id, int $order_product_id): array {
41: $subscription_data = [];
42:
43: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");
44:
45: if ($query->num_rows) {
46: $subscription_data = $query->row;
47:
48: $subscription_data['option'] = ($query->row['option'] ? json_decode($query->row['option'], true) : '');
49: $subscription_data['payment_method'] = ($query->row['payment_method'] ? json_decode($query->row['payment_method'], true) : '');
50: $subscription_data['shipping_method'] = ($query->row['shipping_method'] ? json_decode($query->row['shipping_method'], true) : '');
51: }
52:
53: return $subscription_data;
54: }
55:
56: /**
57: * Get Subscriptions
58: *
59: * @param int $start
60: * @param int $limit
61: *
62: * @return array<int, array<string, mixed>>
63: */
64: public function getSubscriptions(int $start = 0, int $limit = 20): array {
65: if ($start < 0) {
66: $start = 0;
67: }
68:
69: if ($limit < 1) {
70: $limit = 1;
71: }
72:
73: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `subscription_status_id` > '0' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' ORDER BY `subscription_id` DESC LIMIT " . (int)$start . "," . (int)$limit);
74:
75: return $query->rows;
76: }
77:
78: /**
79: * Get Total Subscriptions
80: *
81: * @return int
82: */
83: public function getTotalSubscriptions(): int {
84: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "subscription` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `subscription_status_id` > '0' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "'");
85:
86: if ($query->num_rows) {
87: return (int)$query->row['total'];
88: } else {
89: return 0;
90: }
91: }
92:
93: /**
94: * Get Total Subscription By Shipping Address ID
95: *
96: * @param int $address_id
97: *
98: * @return int
99: */
100: public function getTotalSubscriptionByShippingAddressId(int $address_id): int {
101: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "subscription` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `shipping_address_id` = '" . (int)$address_id . "'");
102:
103: return (int)$query->row['total'];
104: }
105:
106: /**
107: * Get Total Subscription By Payment Address ID
108: *
109: * @param int $address_id
110: *
111: * @return int
112: */
113: public function getTotalSubscriptionByPaymentAddressId(int $address_id): int {
114: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "subscription` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `payment_address_id` = '" . (int)$address_id . "'");
115:
116: return (int)$query->row['total'];
117: }
118:
119: /**
120: * Get Histories
121: *
122: * @param int $subscription_id
123: * @param int $start
124: * @param int $limit
125: *
126: * @return array<int, array<string, mixed>>
127: */
128: public function getHistories(int $subscription_id, int $start = 0, int $limit = 10): array {
129: if ($start < 0) {
130: $start = 0;
131: }
132:
133: if ($limit < 1) {
134: $limit = 10;
135: }
136:
137: $query = $this->db->query("SELECT `sh`.`date_added`, `ss`.`name` AS `status`, `sh`.`comment`, `sh`.`notify` FROM `" . DB_PREFIX . "subscription_history` `sh` LEFT JOIN `" . DB_PREFIX . "subscription_status` `ss` ON `sh`.`subscription_status_id` = `ss`.`subscription_status_id` WHERE `sh`.`subscription_id` = '" . (int)$subscription_id . "' AND `ss`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `sh`.`date_added` DESC LIMIT " . (int)$start . "," . (int)$limit);
138:
139: return $query->rows;
140: }
141:
142: /**
143: * Get Total Histories
144: *
145: * @param int $subscription_id
146: *
147: * @return int
148: */
149: public function getTotalHistories(int $subscription_id): int {
150: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "subscription_history` WHERE `subscription_id` = '" . (int)$subscription_id . "'");
151:
152: return (int)$query->row['total'];
153: }
154:
155: /**
156: * Edit Remaining
157: *
158: * @param int $subscription_id
159: * @param int $remaining
160: *
161: * @return void
162: */
163: public function editRemaining(int $subscription_id, int $remaining): void {
164: $this->db->query("UPDATE `" . DB_PREFIX . "subscription` SET `remaining` = '" . (int)$remaining . "' WHERE `subscription_id` = '" . (int)$subscription_id . "'");
165: }
166:
167: /**
168: * Edit Trial Remaining
169: *
170: * @param int $subscription_id
171: * @param int $trial_remaining
172: *
173: * @return void
174: */
175: public function editTrialRemaining(int $subscription_id, int $trial_remaining): void {
176: $this->db->query("UPDATE `" . DB_PREFIX . "subscription` SET `trial_remaining` = '" . (int)$trial_remaining . "' WHERE `subscription_id` = '" . (int)$subscription_id . "'");
177: }
178: }
179: