1: <?php
2: namespace Opencart\Admin\Model\Extension\Opencart\Report;
3: /**
4: * Class Coupon
5: *
6: * @package Opencart\Admin\Model\Extension\Opencart\Report
7: */
8: class Coupon extends \Opencart\System\Engine\Model {
9: /**
10: * Get Coupons
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return array<int, array<string, mixed>>
15: */
16: public function getCoupons(array $data = []): array {
17: $sql = "SELECT ch.`coupon_id`, c.`name`, c.`code`, COUNT(DISTINCT ch.`order_id`) AS orders, SUM(ch.`amount`) AS `total` FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . "coupon` c ON (ch.`coupon_id` = c.`coupon_id`)";
18:
19: $implode = [];
20:
21: if (!empty($data['filter_date_start'])) {
22: $implode[] = "DATE(ch.`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_start']) . "')";
23: }
24:
25: if (!empty($data['filter_date_end'])) {
26: $implode[] = "DATE(ch.`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_end']) . "')";
27: }
28:
29: if ($implode) {
30: $sql .= " WHERE " . implode(" AND ", $implode);
31: }
32:
33: $sql .= " GROUP BY ch.`coupon_id` ORDER BY `total` DESC";
34:
35: if (isset($data['start']) || isset($data['limit'])) {
36: if ($data['start'] < 0) {
37: $data['start'] = 0;
38: }
39:
40: if ($data['limit'] < 1) {
41: $data['limit'] = 20;
42: }
43:
44: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
45: }
46:
47: $query = $this->db->query($sql);
48:
49: return $query->rows;
50: }
51:
52: /**
53: * Get Total Coupons
54: *
55: * @param array<string, mixed> $data
56: *
57: * @return int
58: */
59: public function getTotalCoupons(array $data = []): int {
60: $sql = "SELECT COUNT(DISTINCT `coupon_id`) AS `total` FROM `" . DB_PREFIX . "coupon_history`";
61:
62: $implode = [];
63:
64: if (!empty($data['filter_date_start'])) {
65: $implode[] = "DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_start']) . "')";
66: }
67:
68: if (!empty($data['filter_date_end'])) {
69: $implode[] = "DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_end']) . "')";
70: }
71:
72: if ($implode) {
73: $sql .= " WHERE " . implode(" AND ", $implode);
74: }
75:
76: $query = $this->db->query($sql);
77:
78: return (int)$query->row['total'];
79: }
80: }
81: