- Star (is_starred) and colored tags for services, customers, orders, invoices:
Markable trait, tags/taggables tables, PATCH administrator/marks/{type}/{key},
tag CRUD, list filters starred / tag_ids[]
- GET administrator/services accepts product_type (falls back to product type
for legacy rows without services.type)
- Mail campaigns: bulk cancel / retry failed / delete; cancelled campaigns are
skipped by SendMailCampaignRecipient
- POST administrator/settings/test-smtp sends a test email through the saved
mail transport
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
6.8 KiB
PHP
168 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Service;
|
|
|
|
use App\Helpers\OrderHelper;
|
|
use App\Http\Controllers\Api\Admin\Service\Concerns\HandlesServiceAttachments;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\VirtualMachine\Provision\ResendVmCredentialsEmailJob;
|
|
use App\Models\ActivityLog;
|
|
use App\Models\DomainPricing;
|
|
use App\Models\Invoice;
|
|
use App\Models\ModuleVmware;
|
|
use App\Models\Service;
|
|
use App\Services\Customer\CustomerServiceAssignment;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ServiceController extends Controller
|
|
{
|
|
use HandlesServiceAttachments;
|
|
|
|
public function __construct(private readonly CustomerServiceAssignment $assignment) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) ($request->per_page ?? 15), 100);
|
|
|
|
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name,product_type', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address', 'orderItem:id,order_id', 'orderItem.order:id,order_num', 'tags'])
|
|
->marked($request->all())
|
|
->where(fn($q) => $q->whereNull('order_item_id')->orWhereNull('type')->orWhere('type', '!=', 'domain'))
|
|
->when($request->filter, fn($q, $v) => $q->where('server_name', 'like', "%{$v}%"))
|
|
->when($request->status, fn($q, $v) => $q->where('status', $v))
|
|
->when($request->product_type, fn($q, $v) => $q->where(fn($typed) => $typed
|
|
->where('type', $v)
|
|
->orWhere(fn($legacy) => $legacy->whereNull('type')->whereHas('product', fn($product) => $product->where('product_type', $v)))))
|
|
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v))
|
|
->when($request->boolean('unassigned'), fn($q) => $q->whereNull('customer_id'));
|
|
|
|
$paginator = $query->latest()->paginate($perPage);
|
|
$invoices = $this->linkedInvoices($paginator->getCollection());
|
|
$paginator->getCollection()->each(fn (Service $service) => $service->setAttribute(
|
|
'linked_invoices',
|
|
$invoices->filter(fn (Invoice $invoice) => $invoice->belongsToService($service))->values(),
|
|
));
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => $paginator->items(),
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'last_page' => $paginator->lastPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function domainQuote(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'domain' => 'required|string|max:255',
|
|
'billing_cycle' => 'nullable|in:monthly,quarterly,semi_annually,annually,biennially,triennially',
|
|
]);
|
|
|
|
$pricing = DomainPricing::matchDomain($data['domain']);
|
|
if (!$pricing) {
|
|
return response()->json(['error' => false, 'data' => null]);
|
|
}
|
|
|
|
$years = DomainPricing::yearsForMonths(OrderHelper::cycleToMonths($data['billing_cycle'] ?? 'annually'));
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'tld' => $pricing->tld,
|
|
'setup_fee' => is_numeric($pricing->setup_fee) ? (int) $pricing->setup_fee : 0,
|
|
'first_year' => (int) $pricing->first_year,
|
|
'renewal' => (int) $pricing->recurring_fee,
|
|
'years' => $years,
|
|
'price' => $pricing->orderPrice($years),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$service = Service::with(['customer', 'product', 'orderItem.order:id,order_num', 'invoice', 'vmware.os.osGroup', 'addons', ...Service::HOSTING_ACCOUNT_RELATIONS])
|
|
->findOrFail($id);
|
|
|
|
ModuleVmware::resolveVirtualMachineIds([$service->vmware]);
|
|
|
|
$service->setAttribute('linked_invoices', $this->linkedInvoices(collect([$service])));
|
|
$service->setAttribute('activity_logs', ActivityLog::where('subject_type', Service::class)
|
|
->where('subject_id', $service->id)
|
|
->latest('id')
|
|
->limit(50)
|
|
->get(['id', 'event', 'description', 'causer_type', 'causer_name', 'old_values', 'new_values', 'created_at']));
|
|
|
|
return response()->json(['error' => false, 'data' => $service]);
|
|
}
|
|
|
|
/** @param Collection<int, Service> $services */
|
|
private function linkedInvoices(Collection $services): Collection
|
|
{
|
|
return Invoice::ofServices($services)
|
|
->with('order:id,order_num')
|
|
->latest('id')
|
|
->get(['id', 'order_id', 'service_id', 'invoice_number', 'invoice_type', 'status', 'total', 'created_at']);
|
|
}
|
|
|
|
public function bulkAction(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'ids' => 'required|array|min:1',
|
|
'ids.*' => 'integer|distinct|exists:services,id',
|
|
'action' => 'required|in:activate,suspend,cancel,detach-customer',
|
|
]);
|
|
|
|
if ($data['action'] === 'detach-customer') {
|
|
$updated = $this->detachCustomers($data['ids']);
|
|
} else {
|
|
$updates = match ($data['action']) {
|
|
'activate' => ['status' => 'active', 'suspended_at' => null, 'cancelled_at' => null],
|
|
'suspend' => ['status' => 'suspended', 'suspended_at' => now()],
|
|
'cancel' => ['status' => 'cancelled', 'cancelled_at' => now()],
|
|
};
|
|
|
|
$updated = Service::whereIn('id', $data['ids'])->update($updates);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => "Đã cập nhật {$updated} dịch vụ.",
|
|
'data' => ['updated' => $updated],
|
|
]);
|
|
}
|
|
|
|
public function detachCustomer(int $id): JsonResponse
|
|
{
|
|
$this->detachCustomers([Service::findOrFail($id)->id]);
|
|
|
|
return response()->json(['error' => false, 'message' => 'Đã gỡ khách hàng khỏi dịch vụ.']);
|
|
}
|
|
|
|
private function detachCustomers(array $ids): int
|
|
{
|
|
return DB::transaction(function () use ($ids) {
|
|
$services = Service::with('customer')->whereIn('id', $ids)->whereNotNull('customer_id')->get();
|
|
$services->each(fn (Service $service) => $this->assignment->detach($service));
|
|
|
|
return $services->count();
|
|
});
|
|
}
|
|
|
|
public function resendCredentials(int $id): JsonResponse
|
|
{
|
|
$service = Service::findOrFail($id);
|
|
|
|
abort_if(!$service->vm_id, 422, 'Dịch vụ chưa gắn VM.');
|
|
|
|
ResendVmCredentialsEmailJob::dispatch($service->vm_id);
|
|
|
|
return response()->json(['error' => false, 'message' => 'Đã xếp hàng gửi lại thông tin VPS cho khách hàng.']);
|
|
}
|
|
}
|