- Customer tab "delete" no longer deletes the service: it detaches the
customer, discards the order/invoices and logs a `detached` activity
with the previous customer and order.
- New POST customers/{uuid}/services/attach assigns an unassigned
service (GET services?unassigned=1) to a customer, creating a pending
order + unpaid invoice when missing, and logs `attached`.
- Detach/attach also sync module_vmware/module_hosting_accounts
customer_id: ResolvesVm authorises by module_vmware.customer_id, so a
detached customer could still control the VM.
- /services detach-customer (single + bulk) go through the same path.
- GET services/{id} returns the service activity logs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Customer;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\Invoice;
|
|
use App\Models\ModuleHostingAccount;
|
|
use App\Models\ModuleVmware;
|
|
use App\Models\Service;
|
|
use App\Services\ActivityLogger;
|
|
use App\Services\Order\ServiceOrderRecorder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CustomerServiceAssignment
|
|
{
|
|
public function __construct(
|
|
private readonly ServiceOrderRecorder $orderRecorder,
|
|
private readonly ActivityLogger $activityLogger,
|
|
) {}
|
|
|
|
public function attach(Service $service, Customer $customer): Service
|
|
{
|
|
abort_if($service->customer_id !== null, 422, 'Dịch vụ đang thuộc khách hàng khác, hãy gỡ trước khi gán.');
|
|
|
|
DB::transaction(function () use ($service, $customer) {
|
|
$service->fill(['customer_id' => $customer->id])->saveQuietly();
|
|
$this->syncModuleOwner($service, $customer->id);
|
|
$this->orderRecorder->ensureForService($service->refresh());
|
|
});
|
|
|
|
$this->activityLogger->log(
|
|
event: 'attached',
|
|
logName: 'Service',
|
|
description: "{$this->activityLogger->getCauserName()} gán dịch vụ #{$service->id} cho khách hàng {$customer->fullname} ({$customer->email})",
|
|
subject: $service,
|
|
newValues: [...$this->customerValues($customer), ...$this->orderValues($service)],
|
|
);
|
|
|
|
return $service;
|
|
}
|
|
|
|
public function detach(Service $service): void
|
|
{
|
|
$customer = $service->customer;
|
|
if (! $customer) {
|
|
return;
|
|
}
|
|
|
|
$orderValues = $this->orderValues($service);
|
|
$orderItemId = $service->order_item_id;
|
|
|
|
$service->fill(['customer_id' => null, 'order_item_id' => null, 'invoice_id' => null])->saveQuietly();
|
|
$this->syncModuleOwner($service, null);
|
|
Invoice::where('service_id', $service->id)->delete();
|
|
$this->orderRecorder->discardOrder($orderItemId);
|
|
|
|
$this->activityLogger->log(
|
|
event: 'detached',
|
|
logName: 'Service',
|
|
description: "{$this->activityLogger->getCauserName()} gỡ dịch vụ #{$service->id} khỏi khách hàng {$customer->fullname} ({$customer->email}), xoá đơn hàng/hoá đơn",
|
|
subject: $service,
|
|
oldValues: [...$this->customerValues($customer), ...$orderValues],
|
|
);
|
|
}
|
|
|
|
private function syncModuleOwner(Service $service, ?int $customerId): void
|
|
{
|
|
if ($service->vm_id) {
|
|
ModuleVmware::whereKey($service->vm_id)->update(['customer_id' => $customerId]);
|
|
}
|
|
|
|
ModuleHostingAccount::where('service_id', $service->id)->update(['customer_id' => $customerId]);
|
|
}
|
|
|
|
private function customerValues(Customer $customer): array
|
|
{
|
|
return array_filter([
|
|
'customer_id' => $customer->id,
|
|
'customer_uuid' => $customer->uuid,
|
|
'customer_name' => $customer->fullname,
|
|
'customer_email' => $customer->email,
|
|
], fn ($value) => $value !== null);
|
|
}
|
|
|
|
private function orderValues(Service $service): array
|
|
{
|
|
$order = $service->orderItem?->order;
|
|
|
|
return array_filter([
|
|
'order_id' => $order?->id,
|
|
'order_num' => $order?->order_num,
|
|
], fn ($value) => $value !== null);
|
|
}
|
|
}
|