| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Database\Tables; |
| 4 |
|
| 5 |
/** |
| 6 |
* Discounts Table Class |
| 7 |
* |
| 8 |
* Represents the discounts table (wp_yatra_discounts) containing comprehensive |
| 9 |
* discount management data including promo codes, group discounts, |
| 10 |
* usage tracking, and advanced discount configurations. |
| 11 |
* |
| 12 |
* Static API: {@see getTableName()}, {@see getWritableColumnNames()}, |
| 13 |
* {@see getRestRequestBodyColumnNames()}, {@see getSchema()}. |
| 14 |
* |
| 15 |
* Usage: |
| 16 |
* DiscountsTable::getTableName() // Returns 'wp_yatra_discounts' |
| 17 |
* DiscountsTable::getSchema() // Returns complete SQL schema |
| 18 |
* |
| 19 |
* @package Yatra\Database\Tables |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class DiscountsTable extends BaseTable |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Table name without prefix |
| 26 |
* |
| 27 |
* @var string The base table name without WordPress prefix |
| 28 |
*/ |
| 29 |
protected static string $table = 'yatra_discounts'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Columns that exist on {@see getSchema()} (excluding `id`) for repository mass-assignment. |
| 33 |
* |
| 34 |
* @return list<string> |
| 35 |
*/ |
| 36 |
public static function getWritableColumnNames(): array |
| 37 |
{ |
| 38 |
return [ |
| 39 |
'code', |
| 40 |
'description', |
| 41 |
'type', |
| 42 |
'amount', |
| 43 |
'max_discount_amount', |
| 44 |
'usage_limit', |
| 45 |
'usage_limit_per_customer', |
| 46 |
'usage_count', |
| 47 |
'valid_from', |
| 48 |
'expiry_date', |
| 49 |
'status', |
| 50 |
'applicable_to', |
| 51 |
'trip_ids', |
| 52 |
'min_amount', |
| 53 |
'first_time_customer_only', |
| 54 |
'is_group_discount', |
| 55 |
'discount_mode', |
| 56 |
'min_group_size', |
| 57 |
'max_group_size', |
| 58 |
'group_discount_type', |
| 59 |
'group_discount_amount', |
| 60 |
'group_discount_mode', |
| 61 |
'group_discount_ranges', |
| 62 |
'category_discounts', |
| 63 |
'created_at', |
| 64 |
'updated_at', |
| 65 |
'created_by', |
| 66 |
'updated_by', |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* REST request body keys allowed through {@see \Yatra\Controllers\DiscountController}. |
| 72 |
* Omits display-only keys (e.g. created_by_name) and server-controlled timestamps. |
| 73 |
* Updates do not accept `created_by` from the client. |
| 74 |
* |
| 75 |
* @return list<string> |
| 76 |
*/ |
| 77 |
public static function getRestRequestBodyColumnNames(bool $forCreate): array |
| 78 |
{ |
| 79 |
$base = array_values(array_diff( |
| 80 |
self::getWritableColumnNames(), |
| 81 |
['created_at', 'updated_at'] |
| 82 |
)); |
| 83 |
|
| 84 |
if (!$forCreate) { |
| 85 |
$base = array_values(array_diff($base, ['created_by'])); |
| 86 |
} |
| 87 |
|
| 88 |
return $base; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the complete table schema as raw SQL CREATE TABLE statement. |
| 93 |
* |
| 94 |
* Returns the full SQL schema for the discounts table using heredoc syntax |
| 95 |
* for proper IDE syntax highlighting. Includes all columns, indexes, |
| 96 |
* and constraints from the original Database.php schema. |
| 97 |
* |
| 98 |
* @return string Complete CREATE TABLE SQL statement |
| 99 |
*/ |
| 100 |
public static function getSchema(): string |
| 101 |
{ |
| 102 |
$tableName = static::getTableName(); |
| 103 |
$charsetCollate = static::getCharsetCollate(); |
| 104 |
|
| 105 |
return <<<SQL |
| 106 |
CREATE TABLE IF NOT EXISTS `{$tableName}` ( |
| 107 |
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 108 |
`code` varchar(100) NOT NULL, |
| 109 |
`description` text, |
| 110 |
`type` varchar(20) NOT NULL DEFAULT 'percentage', |
| 111 |
`amount` decimal(10,2) NOT NULL DEFAULT 0.00, |
| 112 |
`max_discount_amount` decimal(10,2) DEFAULT NULL, |
| 113 |
`usage_limit` int(11) NOT NULL DEFAULT 0, |
| 114 |
`usage_limit_per_customer` int(11) DEFAULT 0, |
| 115 |
`usage_count` int(11) NOT NULL DEFAULT 0, |
| 116 |
`valid_from` datetime DEFAULT NULL, |
| 117 |
`expiry_date` datetime DEFAULT NULL, |
| 118 |
`status` varchar(20) DEFAULT 'draft', |
| 119 |
`applicable_to` varchar(20) DEFAULT 'all', |
| 120 |
`trip_ids` text DEFAULT NULL, |
| 121 |
`min_amount` decimal(10,2) DEFAULT NULL, |
| 122 |
`first_time_customer_only` tinyint(1) DEFAULT 0, |
| 123 |
`is_group_discount` tinyint(1) DEFAULT 0, |
| 124 |
`discount_mode` varchar(20) DEFAULT 'both' COMMENT 'promo, group, or both', |
| 125 |
`min_group_size` int(11) DEFAULT NULL, |
| 126 |
`max_group_size` int(11) DEFAULT NULL, |
| 127 |
`group_discount_type` varchar(20) DEFAULT 'percentage', |
| 128 |
`group_discount_amount` decimal(10,2) DEFAULT NULL, |
| 129 |
`group_discount_mode` varchar(20) DEFAULT 'total', |
| 130 |
`group_discount_ranges` longtext DEFAULT NULL, |
| 131 |
`category_discounts` longtext DEFAULT NULL, |
| 132 |
`created_at` datetime DEFAULT CURRENT_TIMESTAMP, |
| 133 |
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 134 |
`created_by` bigint(20) UNSIGNED NOT NULL DEFAULT 0, |
| 135 |
`updated_by` bigint(20) UNSIGNED NOT NULL DEFAULT 0, |
| 136 |
|
| 137 |
PRIMARY KEY (`id`), |
| 138 |
UNIQUE KEY `code` (`code`), |
| 139 |
KEY `type` (`type`), |
| 140 |
KEY `status` (`status`), |
| 141 |
KEY `valid_from` (`valid_from`), |
| 142 |
KEY `expiry_date` (`expiry_date`), |
| 143 |
KEY `created_by` (`created_by`) |
| 144 |
) {$charsetCollate} COMMENT='Discount codes and group discounts'; |
| 145 |
SQL; |
| 146 |
} |
| 147 |
} |
| 148 |
|