| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Migrations; |
| 4 |
|
| 5 |
class OrdersMigrator extends Migrator |
| 6 |
{ |
| 7 |
|
| 8 |
public static string $tableName = 'fct_orders'; |
| 9 |
|
| 10 |
|
| 11 |
public static function getSqlSchema(): string |
| 12 |
{ |
| 13 |
$indexPrefix = static::getDbPrefix() . 'fct_ord_'; |
| 14 |
|
| 15 |
return "`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 16 |
`status` VARCHAR(20) NOT NULL DEFAULT 'draft' COMMENT 'draft / pending / on-hold / processing / completed / failed / refunded / partial-refund', |
| 17 |
`parent_id` BIGINT UNSIGNED NULL, |
| 18 |
`receipt_number` BIGINT UNSIGNED NULL, |
| 19 |
`invoice_no` VARCHAR(192) NULL DEFAULT '', |
| 20 |
`fulfillment_type` VARCHAR(20) NULL DEFAULT 'physical', |
| 21 |
`type` VARCHAR(20) NOT NULL DEFAULT 'payment', |
| 22 |
`mode` ENUM('live', 'test') NOT NULL DEFAULT 'live' COMMENT 'live / test', |
| 23 |
`shipping_status` VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'unshipped / shipped / delivered / unshippable', |
| 24 |
`customer_id` BIGINT UNSIGNED NULL, |
| 25 |
`payment_method` VARCHAR(100) NOT NULL, |
| 26 |
`payment_status` VARCHAR(20) NOT NULL DEFAULT '', |
| 27 |
`payment_method_title` VARCHAR(100) NOT NULL, |
| 28 |
`currency` VARCHAR(10) NOT NULL, |
| 29 |
`subtotal` BIGINT NOT NULL DEFAULT '0', |
| 30 |
`discount_tax` BIGINT NOT NULL DEFAULT '0', |
| 31 |
`manual_discount_total` BIGINT NOT NULL DEFAULT '0', |
| 32 |
`coupon_discount_total` BIGINT NOT NULL DEFAULT '0', |
| 33 |
`shipping_tax` BIGINT NOT NULL DEFAULT '0', |
| 34 |
`shipping_total` BIGINT NOT NULL DEFAULT '0', |
| 35 |
`fee_total` BIGINT NOT NULL DEFAULT '0', |
| 36 |
`tax_total` BIGINT NOT NULL DEFAULT '0', |
| 37 |
`total_amount` BIGINT NOT NULL DEFAULT '0', |
| 38 |
`total_paid` BIGINT NOT NULL DEFAULT '0', |
| 39 |
`total_refund` BIGINT NOT NULL DEFAULT '0', |
| 40 |
`rate` DECIMAL(12,4) NOT NULL DEFAULT '1.0000', |
| 41 |
`tax_behavior` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '0 => no_tax, 1 => exclusive, 2 => inclusive', |
| 42 |
`note` TEXT NOT NULL DEFAULT '', |
| 43 |
`ip_address` TEXT NOT NULL DEFAULT '', |
| 44 |
`completed_at` DATETIME NULL DEFAULT NULL, |
| 45 |
`refunded_at` DATETIME NULL DEFAULT NULL, |
| 46 |
`uuid` VARCHAR(100) NOT NULL, |
| 47 |
`config` json DEFAULT NULL, |
| 48 |
`created_at` DATETIME NULL, |
| 49 |
`updated_at` DATETIME NULL, |
| 50 |
|
| 51 |
INDEX `{$indexPrefix}_invoice_no` (`invoice_no`(191) ASC), |
| 52 |
INDEX `{$indexPrefix}_status_type` (`type` ASC), |
| 53 |
INDEX `{$indexPrefix}_customer_id` (`customer_id` ASC), |
| 54 |
INDEX `{$indexPrefix}_date_created_completed` (`created_at` ASC, `completed_at` ASC), |
| 55 |
INDEX `{$indexPrefix}_uuid` (`uuid` ASC)"; |
| 56 |
} |
| 57 |
|
| 58 |
public static function migrated() |
| 59 |
{ |
| 60 |
static::addFeeTotalColumn(); |
| 61 |
static::addTaxBehaviorColumn(); |
| 62 |
static::addReceiptNumberColumn(); |
| 63 |
static::renameDiscountTotalColumn(); |
| 64 |
static::addPaymentStatusIndex(); |
| 65 |
static::addUuidIndex(); |
| 66 |
} |
| 67 |
|
| 68 |
public static function addFeeTotalColumn() |
| 69 |
{ |
| 70 |
// "ALTER TABLE %i ADD COLUMN `fee_total` BIGINT NOT NULL DEFAULT '0' AFTER `shipping_total`" |
| 71 |
static::addColumnIfNotExists('fee_total', "BIGINT NOT NULL DEFAULT '0'", 'shipping_total'); |
| 72 |
} |
| 73 |
|
| 74 |
public static function addTaxBehaviorColumn() |
| 75 |
{ |
| 76 |
// "ALTER TABLE %i ADD COLUMN `tax_behavior` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '...' AFTER `rate`" |
| 77 |
static::addColumnIfNotExists('tax_behavior', "TINYINT(1) NOT NULL DEFAULT 0 COMMENT '0 => no_tax, 1 => exclusive, 2 => inclusive'", 'rate'); |
| 78 |
} |
| 79 |
|
| 80 |
public static function addReceiptNumberColumn() |
| 81 |
{ |
| 82 |
// "ALTER TABLE %i ADD COLUMN `receipt_number` BIGINT NULL AFTER `parent_id`" |
| 83 |
static::addColumnIfNotExists('receipt_number', 'BIGINT NULL', 'parent_id'); |
| 84 |
} |
| 85 |
|
| 86 |
public static function renameDiscountTotalColumn() |
| 87 |
{ |
| 88 |
// "ALTER TABLE %i CHANGE `discount_total` `manual_discount_total` BIGINT NOT NULL DEFAULT '0'" |
| 89 |
static::renameColumnIfExists('discount_total', 'manual_discount_total', "BIGINT NOT NULL DEFAULT '0'"); |
| 90 |
} |
| 91 |
|
| 92 |
public static function addPaymentStatusIndex() |
| 93 |
{ |
| 94 |
// "ALTER TABLE %i ADD INDEX `fct_payment_status` (`payment_status`, `id`)" |
| 95 |
static::addIndexIfNotExists('fct_payment_status', ['payment_status', 'id']); |
| 96 |
} |
| 97 |
|
| 98 |
public static function addUuidIndex() |
| 99 |
{ |
| 100 |
// "ALTER TABLE %i ADD INDEX `{prefix}fct_ord__uuid` (`uuid`)" |
| 101 |
// Non-unique on purpose: speeds up the uuid collision check and every |
| 102 |
// by-value order lookup (receipts, webhooks, customer profile) without |
| 103 |
// enforcing a UNIQUE constraint. |
| 104 |
static::addIndexIfNotExists(static::getDbPrefix() . 'fct_ord__uuid', 'uuid'); |
| 105 |
} |
| 106 |
} |
| 107 |
|