PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / database / Migrations / OrderTaxRateMigrator.php

OrderTaxRateMigrator.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at database/Migrations/OrderTaxRateMigrator.php

78 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Database\Migrations;
4
5 class OrderTaxRateMigrator extends Migrator
6 {
7
8 public static string $tableName = 'fct_order_tax_rate';
9
10 public static function getSqlSchema(): string
11 {
12 return "`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
13 `order_id` BIGINT(20) UNSIGNED NOT NULL,
14 `tax_rate_id` BIGINT(20) NOT NULL,
15 `shipping_tax` BIGINT NULL,
16 `order_tax` BIGINT NULL,
17 `total_tax` BIGINT NULL,
18 `meta` json DEFAULT NULL,
19 `filed_at` DATETIME NULL,
20 `created_at` DATETIME NULL,
21 `updated_at` DATETIME NULL";
22 }
23
24 public static function migrated()
25 {
26 static::addMetaColumn();
27 static::addFiledAtColumn();
28 static::allowVirtualTaxRateIds();
29 static::deduplicateOrderTaxRates();
30 static::addOrderTaxRateUniqueIndex();
31 }
32
33 public static function allowVirtualTaxRateIds()
34 {
35 // EU VAT registration rates are virtual and use negative IDs. Keep zero
36 // reserved for the no-tax sentinel while allowing those rows to persist.
37 static::modifyColumnIfExists('tax_rate_id', 'BIGINT(20) NOT NULL');
38 }
39
40 public static function deduplicateOrderTaxRates()
41 {
42 // Keep only the latest row per (order_id, tax_rate_id) before adding the unique
43 // index — duplicate keys from the old write path would cause ALTER TABLE to fail.
44 //
45 // "Latest" = highest id. Duplicates existed because the original prepareOtherData()
46 // path did INSERT without a uniqueness guard; the last write was always authoritative
47 // (it overwrote the in-memory tax_data), so keeping the highest id is correct.
48 // Any rows deleted here were already superseded by a later write in the same order.
49 global $wpdb;
50 $table = $wpdb->prefix . static::$tableName;
51 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
52 $wpdb->query("
53 DELETE t1 FROM `{$table}` t1
54 INNER JOIN `{$table}` t2
55 ON t1.order_id = t2.order_id
56 AND t1.tax_rate_id = t2.tax_rate_id
57 AND t1.id < t2.id
58 ");
59 }
60
61 public static function addOrderTaxRateUniqueIndex()
62 {
63 static::addIndexIfNotExists('uniq_order_tax_rate', ['order_id', 'tax_rate_id'], true);
64 }
65
66 public static function addMetaColumn()
67 {
68 // "ALTER TABLE %i ADD COLUMN `meta` JSON"
69 static::addColumnIfNotExists('meta', 'JSON');
70 }
71
72 public static function addFiledAtColumn()
73 {
74 // "ALTER TABLE %i ADD COLUMN `filed_at` DATETIME NULL AFTER `meta`"
75 static::addColumnIfNotExists('filed_at', 'DATETIME NULL', 'meta');
76 }
77 }
78