Files
portal-api/app/Services/Customer/CustomerWalletService.php
baodev0andClaude Opus 5.5 94614d9cb3 feat: khởi tạo/huỷ domain Plesk qua job, gửi mail hoá đơn qua queue, đính kèm triển khai
- ProvisionDnsDomainJob/SuspendDnsDomainJob: tạo domain admin lên Plesk, huỷ = suspend (is_created=3)
- Khách xem zone DNS được gán ở /dich-vu/domain, quản lý bản ghi A/AAAA/CNAME/TXT/MX/SRV
- send-mail hoá đơn đẩy SendInvoiceMailJob, preview + sửa nội dung, ghi mail.order_id
- invoice_attachments.type (payment_proof/deployment)
- CacheService xoá theo SCAN pattern, Redis không persistent mặc định
- Backfill transactions.public_code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 09:56:18 +07:00

97 lines
3.1 KiB
PHP

<?php
namespace App\Services\Customer;
use App\Enums\TransactionStatus;
use App\Models\Customer;
use App\Models\Log;
use App\Models\Transaction;
use App\Models\User;
use App\Repositories\Customer\CustomerRepositoryInterface;
use App\Services\Customer\Concerns\ResolvesCustomer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class CustomerWalletService
{
use ResolvesCustomer;
public function __construct(
private readonly CustomerRepositoryInterface $customerRepository,
) {}
public function adjustCredit(string $uuid, User $admin, int $amount, bool $isAdd): Customer
{
$customer = $this->resolveCustomer($uuid);
return DB::transaction(function () use ($customer, $admin, $amount, $isAdd) {
$this->customerRepository->update($customer->id, [
'credit' => (int) $customer->credit + ($isAdd ? $amount : -$amount),
'total' => (int) $customer->total + ($isAdd ? $amount : -$amount),
]);
$this->recordTransaction(
$customer,
$admin,
$amount,
$isAdd ? 'admin_add_credit' : 'admin_deduct_credit',
sprintf(
'%s %s tiền trực tiếp cho khách hàng: %s VNĐ',
$admin->username ?? $admin->email ?? 'Admin',
$isAdd ? 'cộng' : 'trừ',
number_format($amount),
),
);
return $customer->fresh();
});
}
public function addTotal(string $uuid, User $admin, int $amount, string $note, string $clientIp): Transaction
{
$customer = $this->resolveCustomer($uuid);
return DB::transaction(function () use ($customer, $admin, $amount, $note, $clientIp) {
$transaction = $this->recordTransaction(
$customer,
$admin,
$amount,
'admin_add_total',
$note,
);
$customer->update(['total' => (int) $customer->total + $amount]);
Log::create([
'agent' => 'admin',
'client_ip' => $clientIp,
'description' => sprintf(
'%s cộng tiền trực tiếp cho customer %s: %s VNĐ - %s',
$admin->username ?? $admin->email,
$customer->email,
number_format($amount),
$note,
),
'customer_id' => $customer->id,
]);
return $transaction;
});
}
private function recordTransaction(Customer $customer, User $admin, int $amount, string $type, string $note): Transaction
{
return Transaction::create([
'uuid' => (string) Str::uuid(),
'public_code' => Transaction::newPublicCode('ADM'),
'trans' => 'ADMIN' . now()->format('YmdHis'),
'amount' => $amount,
'customer_id' => $customer->id,
'user_id' => $admin->id,
'status' => TransactionStatus::COMPLETED->value,
'type' => $type,
'note' => $note,
]);
}
}