feat: nạp tiền thủ công mã 30 phút, gia hạn trừ số dư, đồng bộ domain từ Plesk
- Nạp tiền: mã thanh toán sống 30 phút (ManualDepositDraft), khôi phục/huỷ mã, lệnh dọn mã hết hạn; total_pending chỉ tính giao dịch đã nộp chứng từ, admin duyệt/từ chối/xoá giao dịch - Gia hạn dịch vụ: phát hành invoice gia hạn trên order gốc và trừ số dư ngay - Đồng bộ domain từ Plesk theo server, ServiceOrderRecorder tạo order/invoice cho dịch vụ admin dựng - Danh sách dịch vụ trả field theo loại (service_plan, dns_records_count), đếm dịch vụ theo loại Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
10af41c5cf
commit
c5115b52dd
@@ -4,7 +4,11 @@ namespace App\Http\Controllers\Api\Admin\Customer\Concerns;
|
||||
|
||||
use App\Models\DnsDomain;
|
||||
use App\Models\DnsRecord;
|
||||
use App\Models\Service;
|
||||
use App\Services\Domain\CustomerDomainServiceSync;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
trait ManagesCustomerDomains
|
||||
@@ -21,6 +25,7 @@ trait ManagesCustomerDomains
|
||||
public function getDnsDomains(string $uuid): JsonResponse
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
$domainServices = $this->customerDomainServices($customer->id);
|
||||
|
||||
$domains = DnsDomain::query()
|
||||
->with(['server:id,name,ip_address', 'registrar:id,name,short_name,website'])
|
||||
@@ -30,6 +35,7 @@ trait ManagesCustomerDomains
|
||||
->get()
|
||||
->map(fn($d) => [
|
||||
'id' => $d->id,
|
||||
'service_id' => $domainServices->get($this->domainKey($d->domain_name))?->id,
|
||||
'domain_name' => $d->domain_name,
|
||||
'is_created' => (int) $d->is_created,
|
||||
'plesk_id' => $d->plesk_id,
|
||||
@@ -46,6 +52,7 @@ trait ManagesCustomerDomains
|
||||
'short_name' => $d->registrar->short_name,
|
||||
'website' => $d->registrar->website,
|
||||
] : null,
|
||||
'expires_at' => $domainServices->get($this->domainKey($d->domain_name))?->expires_at,
|
||||
'created_at' => $d->created_at,
|
||||
'updated_at' => $d->updated_at,
|
||||
]);
|
||||
@@ -53,6 +60,84 @@ trait ManagesCustomerDomains
|
||||
return response()->json(['error' => false, 'data' => $domains]);
|
||||
}
|
||||
|
||||
public function updateDnsDomain(string $uuid, int $domainId, Request $request): JsonResponse
|
||||
{
|
||||
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
|
||||
|
||||
$data = $request->validate([
|
||||
'is_created' => 'required|integer|in:0,1,2,3',
|
||||
'created_at' => 'required|date',
|
||||
'expires_at' => 'nullable|date',
|
||||
]);
|
||||
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
DB::transaction(function () use ($domain, $customer, $data) {
|
||||
$domain->forceFill(['is_created' => $data['is_created'], 'created_at' => $data['created_at']])->save();
|
||||
app(CustomerDomainServiceSync::class)->updateCustomerDomain($domain, $customer->id, $data['expires_at']);
|
||||
});
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Cập nhật domain thành công']);
|
||||
}
|
||||
|
||||
public function detachDnsDomain(string $uuid, int $domainId): JsonResponse
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
|
||||
|
||||
$remainingCustomerIds = $domain->customer()->pluck('customers.id')->reject(
|
||||
fn ($customerId) => (int) $customerId === (int) $customer->id,
|
||||
)->all();
|
||||
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $remainingCustomerIds);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã gỡ domain khỏi khách hàng']);
|
||||
}
|
||||
|
||||
public function assignDnsDomain(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
$data = $request->validate([
|
||||
'domain_id' => ['required', 'integer', 'exists:dns_manager_domain,id'],
|
||||
'service_id' => ['required', 'integer', 'exists:services,id'],
|
||||
]);
|
||||
|
||||
abort_unless(
|
||||
Service::query()->whereKey($data['service_id'])->where('customer_id', $customer->id)->exists(),
|
||||
422,
|
||||
'Dịch vụ không thuộc khách hàng này',
|
||||
);
|
||||
|
||||
$domain = DnsDomain::query()->findOrFail($data['domain_id']);
|
||||
$customerIds = $domain->customer()->pluck('customers.id')->push($customer->id)->all();
|
||||
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $customerIds);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Gán domain thành công']);
|
||||
}
|
||||
|
||||
private function customerDomainServices(int $customerId): Collection
|
||||
{
|
||||
return Service::query()
|
||||
->where('customer_id', $customerId)
|
||||
->where('type', 'domain')
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->keyBy(fn(Service $service) => $this->domainKey($service->server_name));
|
||||
}
|
||||
|
||||
private function domainKey(?string $domainName): string
|
||||
{
|
||||
return mb_strtolower(trim((string) $domainName));
|
||||
}
|
||||
|
||||
private function resolveCustomerDnsDomain(string $uuid, int $domainId): DnsDomain
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
return DnsDomain::query()
|
||||
->whereHas('customer', fn($q) => $q->where('customers.id', $customer->id))
|
||||
->findOrFail($domainId);
|
||||
}
|
||||
|
||||
public function getDnsDomainRecords(string $uuid, int $domainId): JsonResponse
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Jobs\DnsManager\SyncDnsFromPlesk;
|
||||
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
|
||||
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
|
||||
use App\Services\ServerService;
|
||||
use App\Services\Domain\CustomerDomainServiceSync;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -32,6 +33,7 @@ class DomainController extends Controller
|
||||
private readonly DnsDomainRepositoryInterface $domainRepository,
|
||||
private readonly DnsRecordRepositoryInterface $recordRepository,
|
||||
private readonly ServerService $serverService,
|
||||
private readonly CustomerDomainServiceSync $customerDomainServiceSync,
|
||||
) {}
|
||||
|
||||
// ─── Domains ─────────────────────────────────────────────────────────────
|
||||
@@ -114,7 +116,7 @@ class DomainController extends Controller
|
||||
]);
|
||||
|
||||
if (!empty($request->customer_ids)) {
|
||||
$domain->customer()->sync($request->customer_ids);
|
||||
$this->customerDomainServiceSync->syncCustomers($domain, $request->customer_ids);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
@@ -153,6 +155,7 @@ class DomainController extends Controller
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$previousDomainName = $domain->domain_name;
|
||||
$this->domainRepository->update($id, $request->only([
|
||||
'domain_name',
|
||||
'dns_manager_server_id',
|
||||
@@ -161,8 +164,19 @@ class DomainController extends Controller
|
||||
]));
|
||||
|
||||
// Sync customers (empty array = detach all)
|
||||
$freshDomain = $domain->fresh();
|
||||
if ($request->has('customer_ids')) {
|
||||
$domain->customer()->sync($request->customer_ids ?? []);
|
||||
$this->customerDomainServiceSync->syncCustomers(
|
||||
$freshDomain,
|
||||
$request->customer_ids ?? [],
|
||||
$previousDomainName,
|
||||
);
|
||||
} elseif ($previousDomainName !== $freshDomain->domain_name) {
|
||||
$this->customerDomainServiceSync->syncCustomers(
|
||||
$freshDomain,
|
||||
$freshDomain->customer()->pluck('customers.id')->all(),
|
||||
$previousDomainName,
|
||||
);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
@@ -270,9 +284,13 @@ class DomainController extends Controller
|
||||
|
||||
// ─── Sync from Plesk ─────────────────────────────────────────────────────
|
||||
|
||||
public function syncDns(): JsonResponse
|
||||
public function syncDns(Request $request): JsonResponse
|
||||
{
|
||||
dispatch(new SyncDnsFromPlesk());
|
||||
$validated = $request->validate([
|
||||
'dns_manager_server_id' => ['nullable', 'integer', 'exists:dns_manager_servers,id'],
|
||||
]);
|
||||
|
||||
dispatch(new SyncDnsFromPlesk($validated['dns_manager_server_id'] ?? null));
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
|
||||
@@ -373,6 +373,7 @@ class AdminDomainOrderController extends Controller
|
||||
'refunded_at' => $service->refunded_at?->toIso8601String(),
|
||||
'cancelled_at' => $service->cancelled_at?->toIso8601String(),
|
||||
'created_at' => $service->created_at?->toIso8601String(),
|
||||
'updated_at' => $service->updated_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,8 +84,12 @@ class ProductCategoryController extends Controller
|
||||
'name' => 'required|string|max:255',
|
||||
'parent_id' => 'nullable|integer|exists:product_categories,id',
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'nullable|integer',
|
||||
'sort_order' => 'required|integer|min:0',
|
||||
'type' => 'nullable|string',
|
||||
], [
|
||||
'sort_order.required' => 'Vui lòng nhập thứ tự hiển thị (sort_order).',
|
||||
'sort_order.integer' => 'Thứ tự hiển thị phải là số nguyên.',
|
||||
'sort_order.min' => 'Thứ tự hiển thị không được âm.',
|
||||
]);
|
||||
|
||||
$validated['slug'] = Str::slug($validated['name']);
|
||||
@@ -119,8 +123,12 @@ class ProductCategoryController extends Controller
|
||||
'name' => 'sometimes|required|string|max:255',
|
||||
'parent_id' => 'nullable|integer|exists:product_categories,id',
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'nullable|integer',
|
||||
'sort_order' => 'sometimes|required|integer|min:0',
|
||||
'type' => 'nullable|string',
|
||||
], [
|
||||
'sort_order.required' => 'Vui lòng nhập thứ tự hiển thị (sort_order).',
|
||||
'sort_order.integer' => 'Thứ tự hiển thị phải là số nguyên.',
|
||||
'sort_order.min' => 'Thứ tự hiển thị không được âm.',
|
||||
]);
|
||||
|
||||
if (isset($validated['name']) && $validated['name'] !== $category->name) {
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Service;
|
||||
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\VirtualMachine\Provision\ResendVmCredentialsEmailJob;
|
||||
use App\Models\DomainPricing;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -16,6 +18,7 @@ class ServiceController extends Controller
|
||||
$perPage = min((int) ($request->per_page ?? 15), 100);
|
||||
|
||||
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address'])
|
||||
->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->customer_id, fn($q, $v) => $q->where('customer_id', $v));
|
||||
@@ -34,6 +37,33 @@ class ServiceController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
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', 'invoice', 'vmware.os.osGroup', 'addons', ...Service::HOSTING_ACCOUNT_RELATIONS])
|
||||
|
||||
@@ -137,8 +137,7 @@ class AdminTransactionController extends Controller
|
||||
$creditEarned = Customer::creditFor($received);
|
||||
$customer->total += $received;
|
||||
$customer->credit += $creditEarned;
|
||||
// Trừ pending theo số khách khai — đó chính là số đã được giữ chỗ lúc tạo yêu cầu.
|
||||
$customer->total_pending = max(0, $customer->total_pending - $declared);
|
||||
$this->releasePending($customer, $transaction, $declared);
|
||||
$customer->save();
|
||||
|
||||
Log::create([
|
||||
@@ -158,7 +157,7 @@ class AdminTransactionController extends Controller
|
||||
'type' => 'deposit_success',
|
||||
'title' => 'Nạp tiền thành công',
|
||||
'body' => 'Giao dịch ' . $transaction->public_code . ' đã được duyệt.',
|
||||
'link' => '/lich-su-giao-dich/' . $transaction->id,
|
||||
'link' => '/lich-su-giao-dich/' . $transaction->uuid,
|
||||
'data' => [
|
||||
'transaction_uuid' => $transaction->uuid,
|
||||
'amount' => $received,
|
||||
@@ -231,7 +230,7 @@ class AdminTransactionController extends Controller
|
||||
$transaction->update(['status' => TransactionStatus::FAILED->value]);
|
||||
|
||||
$customer = Customer::where('id', $transaction->customer_id)->lockForUpdate()->first();
|
||||
$customer->total_pending = max(0, $customer->total_pending - $transaction->amount);
|
||||
$this->releasePending($customer, $transaction, (int) $transaction->amount);
|
||||
$customer->save();
|
||||
|
||||
Log::create([
|
||||
@@ -288,6 +287,57 @@ class AdminTransactionController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(string $uuid): JsonResponse
|
||||
{
|
||||
$transaction = DB::transaction(function () use ($uuid) {
|
||||
$transaction = Transaction::where('uuid', $uuid)
|
||||
->where('type', 'manual')
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if (! $transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((int) $transaction->status === TransactionStatus::PENDING->value) {
|
||||
$customer = Customer::where('id', $transaction->customer_id)->lockForUpdate()->first();
|
||||
if ($customer) {
|
||||
$this->releasePending($customer, $transaction, (int) $transaction->amount);
|
||||
$customer->save();
|
||||
}
|
||||
}
|
||||
|
||||
$transaction->delete();
|
||||
|
||||
Log::create([
|
||||
'agent' => 'admin',
|
||||
'client_ip' => request()->ip(),
|
||||
'description' => sprintf(
|
||||
'Admin xoá giao dịch nạp tiền thủ công %s: %s VNĐ (%s)',
|
||||
$transaction->public_code,
|
||||
number_format($transaction->amount),
|
||||
TransactionStatus::from((int) $transaction->status)->label(),
|
||||
),
|
||||
'customer_id' => $transaction->customer_id,
|
||||
]);
|
||||
|
||||
return $transaction;
|
||||
});
|
||||
|
||||
if (! $transaction) {
|
||||
return response()->json(['error' => true, 'message' => 'Giao dịch không tồn tại'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã xoá giao dịch']);
|
||||
}
|
||||
|
||||
private function releasePending(Customer $customer, Transaction $transaction, int $amount): void
|
||||
{
|
||||
if ($transaction->proof_image) {
|
||||
$customer->total_pending = max(0, $customer->total_pending - $amount);
|
||||
}
|
||||
}
|
||||
|
||||
public function proofImage(Transaction $transaction, int $index = 0)
|
||||
{
|
||||
abort_unless($transaction->proof_image, 404);
|
||||
|
||||
Reference in New Issue
Block a user