feat(portal): lead module, WHMCS sync report/contacts/mapping, service lifecycle email templates
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
3c7710f1de
commit
a443ec3bf6
@@ -4,16 +4,18 @@ namespace App\Integrations\Whmcs\Writers;
|
||||
|
||||
use App\Integrations\Whmcs\DTOs\CustomerDomainData;
|
||||
use App\Integrations\Whmcs\Support\DnsDomainLinker;
|
||||
use App\Integrations\Whmcs\Support\SyncReport;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CustomerDomainWriter
|
||||
{
|
||||
public function __construct(private DnsDomainLinker $dnsLinker)
|
||||
public function __construct(private DnsDomainLinker $dnsLinker, private SyncReport $report)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips domains whose WHMCS client couldn't be resolved to a local customer.
|
||||
* A domain name that already exists under another legacy id is merged into that row instead of duplicated.
|
||||
*/
|
||||
public function write(CustomerDomainData $data): bool
|
||||
{
|
||||
@@ -21,26 +23,62 @@ class CustomerDomainWriter
|
||||
return false;
|
||||
}
|
||||
|
||||
DB::table('module_domain')->updateOrInsert(
|
||||
['legacy_id' => $data->legacyId],
|
||||
[
|
||||
'name' => $data->name,
|
||||
'order_id' => $data->orderId,
|
||||
'first_payment' => $data->firstPayment,
|
||||
'module_dns_id' => 0,
|
||||
'next_payment' => $data->nextPayment,
|
||||
'next_due_date' => $data->nextDueDate,
|
||||
'next_invoice_date' => $data->nextInvoiceDate,
|
||||
'customer_id' => $data->customerId,
|
||||
'domain_type_id' => 0,
|
||||
'registrar_id' => $data->registrarId,
|
||||
'note' => $data->note,
|
||||
'status' => $data->status,
|
||||
]
|
||||
);
|
||||
$attributes = [
|
||||
'name' => $data->name,
|
||||
'order_id' => $data->orderId,
|
||||
'first_payment' => $data->firstPayment,
|
||||
'module_dns_id' => 0,
|
||||
'next_payment' => $data->nextPayment,
|
||||
'next_due_date' => $data->nextDueDate,
|
||||
'next_invoice_date' => $data->nextInvoiceDate,
|
||||
'customer_id' => $data->customerId,
|
||||
'domain_type_id' => 0,
|
||||
'registrar_id' => $data->registrarId,
|
||||
'note' => $data->note,
|
||||
'status' => $data->status,
|
||||
];
|
||||
|
||||
$this->dnsLinker->link($data->name, $data->customerId, $data->registrarId);
|
||||
$duplicate = DB::table('module_domain')->where('legacy_id', $data->legacyId)->doesntExist()
|
||||
? DB::table('module_domain')->whereRaw('LOWER(TRIM(name)) = ?', [mb_strtolower(trim($data->name))])->orderBy('id')->first()
|
||||
: null;
|
||||
|
||||
if ($duplicate !== null) {
|
||||
$this->mergeInto($duplicate, $attributes, $data);
|
||||
} else {
|
||||
DB::table('module_domain')->updateOrInsert(['legacy_id' => $data->legacyId], $attributes);
|
||||
}
|
||||
|
||||
$this->dnsLinker->link($data->name, $duplicate->customer_id ?? $data->customerId, $data->registrarId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function mergeInto(object $existing, array $incoming, CustomerDomainData $data): void
|
||||
{
|
||||
$update = [];
|
||||
foreach (['order_id', 'registrar_id', 'note', 'first_payment', 'next_payment'] as $column) {
|
||||
if (in_array($existing->{$column}, [null, '', 0], true) && ! in_array($incoming[$column], [null, '', 0], true)) {
|
||||
$update[$column] = $incoming[$column];
|
||||
}
|
||||
}
|
||||
if (($incoming['next_due_date'] ?? 0) > ($existing->next_due_date ?? 0)) {
|
||||
$update += [
|
||||
'next_due_date' => $incoming['next_due_date'],
|
||||
'next_invoice_date' => $incoming['next_invoice_date'],
|
||||
'next_payment' => $incoming['next_payment'],
|
||||
];
|
||||
}
|
||||
if ($update !== []) {
|
||||
DB::table('module_domain')->where('id', $existing->id)->update($update);
|
||||
}
|
||||
|
||||
$this->report->note(
|
||||
'domain',
|
||||
$data->name,
|
||||
$data->legacyId,
|
||||
'trung_ten_mien_da_gop',
|
||||
"Tên miền đã có ở module_domain #{$existing->id}; gộp bản ghi WHMCS #{$data->legacyId}, "
|
||||
. ($update ? 'cập nhật: ' . implode(', ', array_keys($update)) : 'không có trường nào cần cập nhật') . '.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user