| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace Yatra\Repositories; |
| 6 | 6 | |
| 7 | +use Yatra\Database\Tables\BookingsTable; | |
| 7 | 8 | use Yatra\Database\Tables\DiscountsTable; |
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * Discount Repository |
| @@ -16,8 +17,47 @@ | ||
| 16 | 17 | { |
| 17 | 18 | return DiscountsTable::getTableName(); |
| 18 | 19 | } |
| 19 | 20 | |
| 21 | + /** | |
| 22 | + * Strip unknown keys (e.g. created_by_name) before wpdb writes. | |
| 23 | + * Never allow updating the primary key from payloads. | |
| 24 | + * | |
| 25 | + * @param array<string, mixed> $data | |
| 26 | + * @return array<string, mixed> | |
| 27 | + */ | |
| 28 | + private function filterToWritableDiscountColumnsForInsert(array $data): array | |
| 29 | + { | |
| 30 | + unset($data['id']); | |
| 31 | + | |
| 32 | + return array_intersect_key($data, array_flip(DiscountsTable::getWritableColumnNames())); | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * @param array<string, mixed> $data | |
| 37 | + * @return array<string, mixed> | |
| 38 | + */ | |
| 39 | + private function filterToWritableDiscountColumnsForUpdate(array $data): array | |
| 40 | + { | |
| 41 | + unset($data['id']); | |
| 42 | + $allowed = array_values(array_diff( | |
| 43 | + DiscountsTable::getWritableColumnNames(), | |
| 44 | + ['created_at', 'created_by'] | |
| 45 | + )); | |
| 46 | + | |
| 47 | + return array_intersect_key($data, array_flip($allowed)); | |
| 48 | + } | |
| 49 | + | |
| 50 | + public function update(int $id, array $data): bool | |
| 51 | + { | |
| 52 | + return parent::update($id, $this->filterToWritableDiscountColumnsForUpdate($data)); | |
| 53 | + } | |
| 54 | + | |
| 55 | + public function create(array $data): int | |
| 56 | + { | |
| 57 | + return parent::create($this->filterToWritableDiscountColumnsForInsert($data)); | |
| 58 | + } | |
| 59 | + | |
| 20 | 60 | public function findByCode(string $code): ?\stdClass |
| 21 | 61 | { |
| 22 | 62 | $table = esc_sql($this->table); |
| 23 | 63 | $result = $this->wpdb->get_row( |
| @@ -81,11 +121,12 @@ | ||
| 81 | 121 | $today = date('Y-m-d'); |
| 82 | 122 | |
| 83 | 123 | // Query for active group discounts applicable to this trip |
| 84 | 124 | // Check both is_group_discount=1 OR discount_mode IN ('group', 'both') for backward compatibility |
| 125 | + // Status: admin UI uses "publish"; legacy rows may use "active" as live (see DiscountService::isLive). | |
| 85 | 126 | $query = "SELECT * FROM `{$table}` |
| 86 | 127 | WHERE (is_group_discount = 1 OR discount_mode IN ('group', 'both')) |
| 87 | - AND status = 'publish'"; | |
| 128 | + AND status IN ('publish', 'active')"; | |
| 88 | 129 | |
| 89 | 130 | return $wpdb->get_results($query) ?: []; |
| 90 | 131 | } |
| 91 | 132 | |
| @@ -97,17 +138,14 @@ | ||
| 97 | 138 | */ |
| 98 | 139 | public function countUsage(string $code): int |
| 99 | 140 | { |
| 100 | 141 | 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 | - | |
| 142 | + | |
| 143 | + // Canonical bookings table — post 3.0.5 rename. Previous code had | |
| 144 | + // a fallback probe for `yatra_new_bookings`; that's no longer | |
| 145 | + // needed since the migration guarantees the canonical name. | |
| 146 | + $bookingsTable = BookingsTable::getTableName(); | |
| 147 | + | |
| 110 | 148 | $count = $wpdb->get_var( |
| 111 | 149 | $wpdb->prepare( |
| 112 | 150 | "SELECT COUNT(*) FROM `{$bookingsTable}` |
| 113 | 151 | WHERE discount_code = %s |
| @@ -119,9 +157,9 @@ | ||
| 119 | 157 | return (int) ($count ?? 0); |
| 120 | 158 | } |
| 121 | 159 | |
| 122 | 160 | /** |
| 123 | - * Status counts for admin toolbar (matches wp_yatra_new_discounts.status values). | |
| 161 | + * Status counts for admin toolbar (matches wp_yatra_discounts.status values). | |
| 124 | 162 | * |
| 125 | 163 | * @return array{all: int, publish: int, draft: int, trash: int, expired: int} |
| 126 | 164 | */ |
| 127 | 165 | public function getAdminStatusCounts(): array |