88 lines
3.3 KiB
PHP
88 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Writers;
|
|
|
|
use App\Integrations\Whmcs\Support\SyncReport;
|
|
use App\Support\PhoneNumber;
|
|
use Modules\Lead\App\Models\Lead;
|
|
|
|
class LeadWriter
|
|
{
|
|
private const FLAGS = [
|
|
'receives_invoice_emails' => 'invoiceemails',
|
|
'receives_domain_emails' => 'domainemails',
|
|
'receives_support_emails' => 'supportemails',
|
|
'receives_product_emails' => 'productemails',
|
|
'receives_general_emails' => 'generalemails',
|
|
];
|
|
|
|
public function __construct(private SyncReport $report)
|
|
{
|
|
}
|
|
|
|
public function write(object $row, ?int $customerId): void
|
|
{
|
|
$email = strtolower(trim((string) $row->email));
|
|
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$this->report->note('contact', $email, $row->id, 'email_khong_hop_le', 'Email liên hệ không hợp lệ, bỏ qua.');
|
|
|
|
return;
|
|
}
|
|
if (preg_match('/\.kgm$/i', $email)) {
|
|
$this->report->note('contact', $email, $row->id, 'email_nghi_sai', 'Email có đuôi .kgm, giữ nguyên như WHMCS.');
|
|
}
|
|
|
|
$attributes = $this->attributes($row, $customerId);
|
|
$lead = Lead::query()->where('email', $email)->first();
|
|
|
|
if ($lead === null) {
|
|
Lead::create([...$attributes, 'email' => $email, 'legacy_id' => $row->id, 'source' => 'whmcs_contact', 'status' => 'new']);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->merge($lead, $attributes, $row);
|
|
}
|
|
|
|
private function attributes(object $row, ?int $customerId): array
|
|
{
|
|
$fullname = trim("{$row->firstname} {$row->lastname}") ?: ($row->companyname ?: null);
|
|
$address = implode(', ', array_filter([$row->address1, $row->address2, $row->city, $row->state, $row->country]));
|
|
$flags = array_map(fn (string $column) => (bool) $row->{$column}, self::FLAGS);
|
|
|
|
return [
|
|
'customer_id' => $customerId,
|
|
'whmcs_client_id' => $row->userid,
|
|
'fullname' => $fullname,
|
|
'company' => $row->companyname ?: null,
|
|
'phone' => PhoneNumber::normalize($row->phonenumber),
|
|
'address' => $address ?: null,
|
|
'note' => $customerId === null ? "Liên hệ của khách WHMCS #{$row->userid} (khách đã bị xoá)." : null,
|
|
...$flags,
|
|
];
|
|
}
|
|
|
|
private function merge(Lead $lead, array $incoming, object $row): void
|
|
{
|
|
$update = [];
|
|
foreach (['customer_id', 'whmcs_client_id', 'fullname', 'company', 'phone', 'address', 'note'] as $column) {
|
|
if (blank($lead->{$column}) && filled($incoming[$column])) {
|
|
$update[$column] = $incoming[$column];
|
|
}
|
|
}
|
|
foreach (array_keys(self::FLAGS) as $flag) {
|
|
if (! $lead->{$flag} && $incoming[$flag]) {
|
|
$update[$flag] = true;
|
|
}
|
|
}
|
|
if ($lead->legacy_id === null && ! Lead::where('legacy_id', $row->id)->exists()) {
|
|
$update['legacy_id'] = $row->id;
|
|
}
|
|
if ($update !== []) {
|
|
$lead->update($update);
|
|
}
|
|
|
|
$this->report->note('contact', $lead->email, $row->id, 'email_trung_da_gop', "Email đã có ở khách tiềm năng {$lead->uuid}; gộp liên hệ WHMCS #{$row->id}" . ($update ? ', bổ sung: ' . implode(', ', array_keys($update)) : '') . '.');
|
|
}
|
|
}
|