1: | <?php
2: | namespace Opencart\Admin\Model\Extension\Opencart\Report;
3: | /**
4: | * Class Subscription
5: | *
6: | * @package Opencart\Admin\Model\Extension\Opencart\Report
7: | */
8: | class Subscription extends \Opencart\System\Engine\Model {
9: | /**
10: | * Get Subscriptions
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return array<int, array<string, mixed>>
15: | */
16: | public function getSubscriptions(array $data = []): array {
17: | $sql = "SELECT MIN(`s`.`date_added`) AS date_start, MAX(`s`.`date_added`) AS date_end, COUNT(*) AS `subscriptions`, SUM((SELECT SUM(`ot`.`value`) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.`order_id` = `s`.`order_id` AND ot.`code` = 'tax' GROUP BY ot.`order_id`)) AS tax, SUM(`s`.`quantity`) AS `products`, SUM(`s`.`price`) AS `total` FROM `" . DB_PREFIX . "subscription` `s`";
18: |
19: | if (!empty($data['filter_subscription_status_id'])) {
20: | $sql .= " WHERE `s`.`subscription_status_id` = '" . (int)$data['filter_subscription_status_id'] . "'";
21: | } else {
22: | $sql .= " WHERE `s`.`subscription_status_id` > '0'";
23: | }
24: |
25: | if (!empty($data['filter_date_start'])) {
26: | $sql .= " AND DATE(`s`.`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_start']) . "')";
27: | }
28: |
29: | if (!empty($data['filter_date_end'])) {
30: | $sql .= " AND DATE(`s`.`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_end']) . "')";
31: | }
32: |
33: | if (!empty($data['filter_group'])) {
34: | $group = $data['filter_group'];
35: | } else {
36: | $group = 'week';
37: | }
38: |
39: | switch ($group) {
40: | case 'day':
41: | $sql .= " GROUP BY YEAR(`s`.`date_added`), MONTH(`s`.`date_added`), DAY(`s`.`date_added`)";
42: | break;
43: | default:
44: | case 'week':
45: | $sql .= " GROUP BY YEAR(`s`.`date_added`), WEEK(`s`.`date_added`)";
46: | break;
47: | case 'month':
48: | $sql .= " GROUP BY YEAR(`s`.`date_added`), MONTH(`s`.`date_added`)";
49: | break;
50: | case 'year':
51: | $sql .= " GROUP BY YEAR(`s`.`date_added`)";
52: | break;
53: | }
54: |
55: | $sql .= " ORDER BY `s`.`date_added` DESC";
56: |
57: | if (isset($data['start']) || isset($data['limit'])) {
58: | if ($data['start'] < 0) {
59: | $data['start'] = 0;
60: | }
61: |
62: | if ($data['limit'] < 1) {
63: | $data['limit'] = 20;
64: | }
65: |
66: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
67: | }
68: |
69: | $query = $this->db->query($sql);
70: |
71: | return $query->rows;
72: | }
73: |
74: | /**
75: | * Get Total Subscriptions
76: | *
77: | * @param array<string, mixed> $data
78: | *
79: | * @return int
80: | */
81: | public function getTotalSubscriptions(array $data = []): int {
82: | if (!empty($data['filter_group'])) {
83: | $group = $data['filter_group'];
84: | } else {
85: | $group = 'week';
86: | }
87: |
88: | switch ($group) {
89: | case 'day':
90: | $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), MONTH(`date_added`), DAY(`date_added`)) AS `total` FROM `" . DB_PREFIX . "subscription`";
91: | break;
92: | default:
93: | case 'week':
94: | $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), WEEK(`date_added`)) AS `total` FROM `" . DB_PREFIX . "subscription`";
95: | break;
96: | case 'month':
97: | $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), MONTH(`date_added`)) AS `total` FROM `" . DB_PREFIX . "subscription`";
98: | break;
99: | case 'year':
100: | $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`)) AS `total` FROM `" . DB_PREFIX . "subscription`";
101: | break;
102: | }
103: |
104: | if (!empty($data['filter_subscription_status_id'])) {
105: | $sql .= " WHERE `subscription_status_id` = '" . (int)$data['filter_subscription_status_id'] . "'";
106: | } else {
107: | $sql .= " WHERE `subscription_status_id` > '0'";
108: | }
109: |
110: | if (!empty($data['filter_date_start'])) {
111: | $sql .= " AND DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_start']) . "')";
112: | }
113: |
114: | if (!empty($data['filter_date_end'])) {
115: | $sql .= " AND DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_end']) . "')";
116: | }
117: |
118: | $query = $this->db->query($sql);
119: |
120: | return (int)$query->row['total'];
121: | }
122: | }
123: |

OpenCart API API documentation generated by ApiGen dev-master