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

OpenCart API API documentation generated by ApiGen dev-master