58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Transformers;
|
|
|
|
use App\Integrations\Whmcs\DTOs\OrderData;
|
|
use App\Integrations\Whmcs\Support\CustomerIdResolver;
|
|
|
|
class OrderTransformer
|
|
{
|
|
public function __construct(private CustomerIdResolver $customers)
|
|
{
|
|
}
|
|
|
|
public function transform(array|object $row): OrderData
|
|
{
|
|
$row = (array) $row;
|
|
|
|
return new OrderData(
|
|
legacyId: (int) $row['id'],
|
|
orderNum: (string) $row['ordernum'],
|
|
customerId: $this->customers->resolveFor('order', $row['ordernum'] ?? $row['id'], (int) $row['userid']),
|
|
date: $this->nullableDate($row['date'] ?? null),
|
|
total: (int) round((float) $row['amount']),
|
|
status: $this->mapStatus($row['status']),
|
|
paymentMethod: $row['paymentmethod'] ?: null,
|
|
note: $row['notes'] ?: null,
|
|
clientIp: $row['ipaddress'] ?: null,
|
|
createdAt: $this->nullableDate($row['date'] ?? null),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* orders.status: 0=pending, 1=completed, 2=cancelled, 3=processing.
|
|
*/
|
|
private function mapStatus(string $status): int
|
|
{
|
|
return match ($status) {
|
|
'Active', 'Completed' => 1,
|
|
'Cancelled', 'Fraud', 'Failed' => 2,
|
|
'Pending Activation', 'In Progress' => 3,
|
|
default => 0,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* WHMCS has zero-dates ('0000-00-00...') and occasional bogus years, both
|
|
* invalid for MySQL strict mode, so nullify anything before 1970.
|
|
*/
|
|
private function nullableDate(?string $value): ?string
|
|
{
|
|
if (! $value || (int) substr($value, 0, 4) < 1970) {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|