| 1 |
<?php |
| 2 |
/** |
| 3 |
* Value Transformer for Easy Invoice Migration |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration\Src; |
| 11 |
|
| 12 |
/** |
| 13 |
* Value transformer class. |
| 14 |
* |
| 15 |
* Provides shared transformation logic for all migration classes. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
class ValueTransformer { |
| 20 |
|
| 21 |
/** |
| 22 |
* Transform date value to MySQL format. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @param mixed $date_value Date value |
| 26 |
* @return string MySQL formatted date |
| 27 |
*/ |
| 28 |
public static function transform_date($date_value): string { |
| 29 |
if (empty($date_value)) { |
| 30 |
return current_time('mysql'); |
| 31 |
} |
| 32 |
|
| 33 |
// If it's already a MySQL date format, return as is |
| 34 |
if (preg_match('/^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$/', $date_value)) { |
| 35 |
return $date_value; |
| 36 |
} |
| 37 |
|
| 38 |
// Try to parse the date |
| 39 |
$timestamp = strtotime($date_value); |
| 40 |
if ($timestamp === false) { |
| 41 |
return current_time('mysql'); |
| 42 |
} |
| 43 |
|
| 44 |
return date('Y-m-d H:i:s', $timestamp); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Transform currency value to standard code. |
| 49 |
* |
| 50 |
* @since 2.0.0 |
| 51 |
* @param mixed $currency Currency value |
| 52 |
* @return string Currency code |
| 53 |
*/ |
| 54 |
public static function transform_currency($currency): string { |
| 55 |
if (empty($currency)) { |
| 56 |
return 'USD'; |
| 57 |
} |
| 58 |
|
| 59 |
$currency = strtoupper(trim($currency)); |
| 60 |
|
| 61 |
// Common currency mappings |
| 62 |
$mappings = [ |
| 63 |
'$' => 'USD', |
| 64 |
'£' => 'GBP', |
| 65 |
'€' => 'EUR', |
| 66 |
'¥' => 'JPY', |
| 67 |
'USD' => 'USD', |
| 68 |
'EUR' => 'EUR', |
| 69 |
'GBP' => 'GBP', |
| 70 |
'JPY' => 'JPY', |
| 71 |
'DOLLAR' => 'USD', |
| 72 |
'EURO' => 'EUR', |
| 73 |
'POUND' => 'GBP', |
| 74 |
'YEN' => 'JPY' |
| 75 |
]; |
| 76 |
|
| 77 |
return $mappings[$currency] ?? 'USD'; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Transform boolean value. |
| 82 |
* |
| 83 |
* @since 2.0.0 |
| 84 |
* @param mixed $value Boolean value |
| 85 |
* @return bool |
| 86 |
*/ |
| 87 |
public static function transform_boolean($value): bool { |
| 88 |
if (is_bool($value)) { |
| 89 |
return $value; |
| 90 |
} |
| 91 |
|
| 92 |
if (is_string($value)) { |
| 93 |
return in_array(strtolower($value), ['true', '1', 'yes', 'on'], true); |
| 94 |
} |
| 95 |
|
| 96 |
if (is_numeric($value)) { |
| 97 |
return (bool)$value; |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Transform payment method value. |
| 105 |
* |
| 106 |
* @since 2.0.0 |
| 107 |
* @param mixed $method Payment method value |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public static function transform_payment_method($method): string { |
| 111 |
if (empty($method)) { |
| 112 |
return 'bank_transfer'; |
| 113 |
} |
| 114 |
|
| 115 |
$method = strtolower(trim($method)); |
| 116 |
|
| 117 |
$mappings = [ |
| 118 |
'bank' => 'bank_transfer', |
| 119 |
'bank_transfer' => 'bank_transfer', |
| 120 |
'wire' => 'bank_transfer', |
| 121 |
'wire_transfer' => 'bank_transfer', |
| 122 |
'cash' => 'cash', |
| 123 |
'check' => 'check', |
| 124 |
'cheque' => 'check', |
| 125 |
'paypal' => 'paypal', |
| 126 |
'stripe' => 'stripe', |
| 127 |
'credit_card' => 'credit_card', |
| 128 |
'debit_card' => 'credit_card', |
| 129 |
'card' => 'credit_card', |
| 130 |
'other' => 'other' |
| 131 |
]; |
| 132 |
|
| 133 |
return $mappings[$method] ?? 'other'; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Transform payment methods array. |
| 138 |
* |
| 139 |
* @since 2.0.0 |
| 140 |
* @param mixed $methods Payment methods array |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public static function transform_payment_methods($methods): array { |
| 144 |
if (!is_array($methods)) { |
| 145 |
return []; |
| 146 |
} |
| 147 |
|
| 148 |
$transformed = []; |
| 149 |
foreach ($methods as $method) { |
| 150 |
if (is_array($method)) { |
| 151 |
$transformed[] = [ |
| 152 |
'id' => $method['id'] ?? '', |
| 153 |
'name' => $method['name'] ?? '', |
| 154 |
'description' => $method['description'] ?? '', |
| 155 |
'enabled' => isset($method['enabled']) ? self::transform_boolean($method['enabled']) : true, |
| 156 |
'settings' => $method['settings'] ?? [] |
| 157 |
]; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return $transformed; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Transform invoice/quote status. |
| 166 |
* |
| 167 |
* @since 2.0.0 |
| 168 |
* @param string $type Document type (invoice/quote) |
| 169 |
* @param mixed $status Status value |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public static function transform_status(string $type, $status): string { |
| 173 |
if (empty($status)) { |
| 174 |
return 'draft'; |
| 175 |
} |
| 176 |
|
| 177 |
$status = strtolower(trim($status)); |
| 178 |
|
| 179 |
// Status mappings |
| 180 |
$mappings = [ |
| 181 |
'invoice' => [ |
| 182 |
'draft' => 'draft', |
| 183 |
'pending' => 'pending', |
| 184 |
'published' => 'published', |
| 185 |
'paid' => 'paid', |
| 186 |
'partially_paid' => 'partially_paid', |
| 187 |
'overdue' => 'overdue', |
| 188 |
'cancelled' => 'cancelled', |
| 189 |
'unpaid' => 'pending', |
| 190 |
'complete' => 'paid', |
| 191 |
'partial' => 'partially_paid', |
| 192 |
'void' => 'cancelled' |
| 193 |
], |
| 194 |
'quote' => [ |
| 195 |
'draft' => 'draft', |
| 196 |
'pending' => 'pending', |
| 197 |
'sent' => 'sent', |
| 198 |
'accepted' => 'accepted', |
| 199 |
'declined' => 'declined', |
| 200 |
'expired' => 'expired', |
| 201 |
'cancelled' => 'cancelled', |
| 202 |
'approved' => 'accepted', |
| 203 |
'rejected' => 'declined', |
| 204 |
'void' => 'cancelled' |
| 205 |
], |
| 206 |
'payment' => [ |
| 207 |
'pending' => 'pending', |
| 208 |
'completed' => 'completed', |
| 209 |
'failed' => 'failed', |
| 210 |
'refunded' => 'refunded', |
| 211 |
'processing' => 'pending', |
| 212 |
'success' => 'completed', |
| 213 |
'error' => 'failed', |
| 214 |
'cancelled' => 'failed', |
| 215 |
'void' => 'failed' |
| 216 |
] |
| 217 |
]; |
| 218 |
|
| 219 |
return $mappings[$type][$status] ?? 'draft'; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Transform tax type value. |
| 224 |
* |
| 225 |
* @since 2.0.0 |
| 226 |
* @param mixed $tax_type Tax type value |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
public static function transform_tax_type($tax_type): string { |
| 230 |
if (empty($tax_type)) { |
| 231 |
return 'exclusive'; |
| 232 |
} |
| 233 |
|
| 234 |
$tax_type = strtolower(trim($tax_type)); |
| 235 |
|
| 236 |
$mappings = [ |
| 237 |
'inclusive' => 'inclusive', |
| 238 |
'exclusive' => 'exclusive', |
| 239 |
'none' => 'none', |
| 240 |
'included' => 'inclusive', |
| 241 |
'excluded' => 'exclusive', |
| 242 |
'disabled' => 'none', |
| 243 |
'yes' => 'exclusive', |
| 244 |
'no' => 'none', |
| 245 |
'1' => 'exclusive', |
| 246 |
'0' => 'none' |
| 247 |
]; |
| 248 |
|
| 249 |
return $mappings[$tax_type] ?? 'exclusive'; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Transform currency position value. |
| 254 |
* |
| 255 |
* @since 2.0.0 |
| 256 |
* @param mixed $position Currency position value |
| 257 |
* @return string |
| 258 |
*/ |
| 259 |
public static function transform_currency_position($position): string { |
| 260 |
if (empty($position)) { |
| 261 |
return 'before'; |
| 262 |
} |
| 263 |
|
| 264 |
$position = strtolower(trim($position)); |
| 265 |
|
| 266 |
$mappings = [ |
| 267 |
'left' => 'before', |
| 268 |
'right' => 'after', |
| 269 |
'left_space' => 'before_space', |
| 270 |
'right_space' => 'after_space', |
| 271 |
'before' => 'before', |
| 272 |
'after' => 'after', |
| 273 |
'before_space' => 'before_space', |
| 274 |
'after_space' => 'after_space' |
| 275 |
]; |
| 276 |
|
| 277 |
return $mappings[$position] ?? 'before'; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Transform amount value. |
| 282 |
* |
| 283 |
* @since 2.0.0 |
| 284 |
* @param mixed $amount Amount value |
| 285 |
* @return float |
| 286 |
*/ |
| 287 |
public static function transform_amount($amount): float { |
| 288 |
if (empty($amount)) { |
| 289 |
return 0.00; |
| 290 |
} |
| 291 |
|
| 292 |
// If it's already a float or int, just return it |
| 293 |
if (is_float($amount) || is_int($amount)) { |
| 294 |
return (float)$amount; |
| 295 |
} |
| 296 |
|
| 297 |
// If it's a string, clean it up first |
| 298 |
if (is_string($amount)) { |
| 299 |
// Remove any currency symbols and thousands separators |
| 300 |
$amount = preg_replace('/[^0-9.-]/', '', $amount); |
| 301 |
|
| 302 |
// Convert to float |
| 303 |
return (float)$amount; |
| 304 |
} |
| 305 |
|
| 306 |
return 0.00; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Transform template value. |
| 311 |
* |
| 312 |
* @since 2.0.0 |
| 313 |
* @param mixed $template Template value |
| 314 |
* @param string $type Document type (invoice/quote) |
| 315 |
* @return string |
| 316 |
*/ |
| 317 |
public static function transform_template($template, string $type = 'invoice'): string { |
| 318 |
if (empty($template)) { |
| 319 |
return 'minimal'; |
| 320 |
} |
| 321 |
|
| 322 |
$template = strtolower(trim($template)); |
| 323 |
|
| 324 |
$mappings = [ |
| 325 |
'invoice' => [ |
| 326 |
'minimal' => 'minimal', |
| 327 |
'modern' => 'modern', |
| 328 |
'classic' => 'classic', |
| 329 |
'professional' => 'professional', |
| 330 |
'business' => 'business', |
| 331 |
'simple' => 'minimal', |
| 332 |
'basic' => 'minimal', |
| 333 |
'standard' => 'classic', |
| 334 |
'default' => 'minimal' |
| 335 |
], |
| 336 |
'quote' => [ |
| 337 |
'minimal' => 'minimal', |
| 338 |
'modern' => 'modern', |
| 339 |
'classic' => 'classic', |
| 340 |
'professional' => 'professional', |
| 341 |
'simple' => 'minimal', |
| 342 |
'basic' => 'minimal', |
| 343 |
'standard' => 'classic', |
| 344 |
'default' => 'minimal' |
| 345 |
] |
| 346 |
]; |
| 347 |
|
| 348 |
return $mappings[$type][$template] ?? 'minimal'; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Transform watermark type value. |
| 353 |
* |
| 354 |
* @since 2.0.0 |
| 355 |
* @param mixed $type Watermark type value |
| 356 |
* @return string |
| 357 |
*/ |
| 358 |
public static function transform_watermark_type($type): string { |
| 359 |
if (empty($type)) { |
| 360 |
return 'text'; |
| 361 |
} |
| 362 |
|
| 363 |
$type = strtolower(trim($type)); |
| 364 |
|
| 365 |
$mappings = [ |
| 366 |
'text' => 'text', |
| 367 |
'image' => 'image', |
| 368 |
'none' => 'none', |
| 369 |
'disabled' => 'none', |
| 370 |
'off' => 'none', |
| 371 |
'on' => 'text' |
| 372 |
]; |
| 373 |
|
| 374 |
return $mappings[$type] ?? 'text'; |
| 375 |
} |
| 376 |
} |
| 377 |
|