PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.8
Yatra – Travel Booking & Tour Operator Software v3.0.2.8
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / app / Repositories / DiscountRepository.php

DiscountRepository.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.8, at app/Repositories/DiscountRepository.php

160 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Repositories;
6
7 use Yatra\Database\Tables\DiscountsTable;
8
9 /**
10 * Discount Repository
11 * Handles database operations for discounts
12 */
13 class DiscountRepository extends BaseRepository
14 {
15 protected function getTableName(): string
16 {
17 return DiscountsTable::getTableName();
18 }
19
20 public function findByCode(string $code): ?\stdClass
21 {
22 $table = esc_sql($this->table);
23 $result = $this->wpdb->get_row(
24 $this->wpdb->prepare(
25 "SELECT * FROM `{$table}` WHERE code = %s",
26 $code
27 )
28 );
29 return $result ?: null;
30 }
31
32 public function getByStatus(string $status, array $args = []): array
33 {
34 $args['where']['status'] = $status;
35 return $this->all($args);
36 }
37
38 public function getByType(string $type, array $args = []): array
39 {
40 $args['where']['type'] = $type;
41 return $this->all($args);
42 }
43
44 public function search(string $search_term, array $args = []): array
45 {
46 $table = esc_sql($this->table);
47 $search_term = '%' . esc_like($search_term) . '%';
48
49 $where_conditions = ["(`code` LIKE %s OR `description` LIKE %s)"];
50 $where_values = [$search_term, $search_term];
51
52 if (isset($args['where'])) {
53 foreach ($args['where'] as $key => $value) {
54 $key = preg_replace('/[^a-zA-Z0-9_]/', '', $key); // Sanitize column name
55 $where_conditions[] = "`{$key}` = %s";
56 $where_values[] = $value;
57 }
58 }
59
60 $where_clause = 'WHERE ' . implode(' AND ', $where_conditions);
61 $order = $this->buildOrderClause($args);
62 $limit = $this->buildLimitClause($args);
63
64 $query = $this->wpdb->prepare(
65 "SELECT * FROM `{$table}` {$where_clause} {$order} {$limit}",
66 ...$where_values
67 );
68
69 return $this->wpdb->get_results($query) ?: [];
70 }
71
72 /**
73 * Get all active group discounts
74 *
75 * @return array Array of group discount objects
76 */
77 public function getActiveGroupDiscounts(): array
78 {
79 global $wpdb;
80 $table = $this->getTableName();
81 $today = date('Y-m-d');
82
83 // Query for active group discounts applicable to this trip
84 // Check both is_group_discount=1 OR discount_mode IN ('group', 'both') for backward compatibility
85 $query = "SELECT * FROM `{$table}`
86 WHERE (is_group_discount = 1 OR discount_mode IN ('group', 'both'))
87 AND status = 'publish'";
88
89 return $wpdb->get_results($query) ?: [];
90 }
91
92 /**
93 * Count how many bookings have used a specific discount code
94 *
95 * @param string $code Discount code
96 * @return int Number of bookings that used this code
97 */
98 public function countUsage(string $code): int
99 {
100 global $wpdb;
101
102 // Try new bookings table first, then fallback to old table
103 $bookingsTable = $wpdb->prefix . 'yatra_new_bookings';
104 $tableExists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $bookingsTable));
105
106 if (!$tableExists) {
107 $bookingsTable = $wpdb->prefix . 'yatra_bookings';
108 }
109
110 $count = $wpdb->get_var(
111 $wpdb->prepare(
112 "SELECT COUNT(*) FROM `{$bookingsTable}`
113 WHERE discount_code = %s
114 AND status NOT IN ('cancelled', 'failed')",
115 $code
116 )
117 );
118
119 return (int) ($count ?? 0);
120 }
121
122 /**
123 * Status counts for admin toolbar (matches wp_yatra_new_discounts.status values).
124 *
125 * @return array{all: int, publish: int, draft: int, trash: int, expired: int}
126 */
127 public function getAdminStatusCounts(): array
128 {
129 $table = esc_sql($this->table);
130 $all = (int) $this->wpdb->get_var("SELECT COUNT(*) FROM `{$table}`");
131
132 $rows = $this->wpdb->get_results(
133 "SELECT `status`, COUNT(*) AS c FROM `{$table}` GROUP BY `status`"
134 ) ?: [];
135
136 $map = [
137 'publish' => 0,
138 'draft' => 0,
139 'trash' => 0,
140 'expired' => 0,
141 ];
142
143 foreach ($rows as $row) {
144 $st = (string) ($row->status ?? '');
145 if (isset($map[$st])) {
146 $map[$st] = (int) $row->c;
147 }
148 }
149
150 return [
151 'all' => $all,
152 'publish' => $map['publish'],
153 'draft' => $map['draft'],
154 'trash' => $map['trash'],
155 'expired' => $map['expired'],
156 ];
157 }
158 }
159
160