Files
portal-api/app/Services/Customer/CustomerWalletService.php
T
baodev0 c917e3bf5e feat: Add IT Support catalog and related migrations, seeders, and email templates
- Created migration to add 'group_label' to 'product_type_fields' table.
- Implemented ItSupportCatalogSeeder to populate IT Support features and tiers.
- Added ProductCategorySeeder for initial product categories.
- Developed ProductMockupSeeder for mockup products in various categories.
- Introduced ProductTypeFieldSeeder to define fields for different product types.
- Created email templates for VPS service information and service renewal reminders.
2026-08-26 16:19:38 +07:00

96 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(),
'trans' => 'ADMIN' . now()->format('YmdHis'),
'amount' => $amount,
'customer_id' => $customer->id,
'user_id' => $admin->id,
'status' => TransactionStatus::COMPLETED->value,
'type' => $type,
'note' => $note,
]);
}
}