- 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>
122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Services\Cache\CacheServiceInterface;
|
|
use App\Traits\LogsActivity;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Str;
|
|
use App\Traits\HasUuid;
|
|
|
|
class Transaction extends Model
|
|
{
|
|
use HasFactory, SoftDeletes, HasUuid, LogsActivity;
|
|
protected $table = 'transactions';
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'public_code',
|
|
'trans',
|
|
'amount',
|
|
'received_amount',
|
|
'customer_id',
|
|
'user_id',
|
|
'note',
|
|
'status',
|
|
'type',
|
|
'proof_image',
|
|
];
|
|
|
|
protected $appends = ['direction', 'proof_image_urls'];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Transaction $transaction) {
|
|
$transaction->public_code ??= self::newPublicCode('TXN');
|
|
});
|
|
|
|
$flushListCache = fn () => DB::afterCommit(
|
|
fn () => app(CacheServiceInterface::class)->clearByModule('transaction')
|
|
);
|
|
static::saved($flushListCache);
|
|
static::deleted($flushListCache);
|
|
}
|
|
|
|
public static function newPublicCode(string $prefix): string
|
|
{
|
|
return $prefix . '-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6));
|
|
}
|
|
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class, 'customer_id');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* Whether this transaction added ('credit'), removed ('debit') or left
|
|
* ('neutral') the customer's balance untouched. `amount` is always stored
|
|
* positive, so the direction has to be derived from `type` (and, for
|
|
* 'invoice', from the note) to mirror the actual balance mutation logic
|
|
* in InvoicePaymentService / CustomerController / AdminTransactionController.
|
|
*/
|
|
public function getDirectionAttribute(): string
|
|
{
|
|
return match ($this->type) {
|
|
'admin_add_total', 'admin_add_credit', 'manual', 'refund' => 'credit',
|
|
'admin_deduct_credit' => 'debit',
|
|
'balance' => 'debit',
|
|
'transfer', 'admin_record' => 'neutral',
|
|
'invoice' => str_starts_with((string) $this->note, 'Admin xác nhận')
|
|
? 'neutral'
|
|
: 'debit',
|
|
default => 'credit',
|
|
};
|
|
}
|
|
|
|
public function typeLabel(): string
|
|
{
|
|
return match ($this->type) {
|
|
'manual' => 'Nạp tiền chuyển khoản',
|
|
'vnpay' => 'Nạp tiền qua VNPay',
|
|
'invoice' => 'Thanh toán hoá đơn',
|
|
'refund' => 'Hoàn tiền',
|
|
'admin_add_total', 'admin_add_credit' => 'Cộng số dư',
|
|
'admin_deduct_credit' => 'Trừ số dư',
|
|
'admin_record' => 'Ghi nhận giao dịch',
|
|
default => 'Giao dịch',
|
|
};
|
|
}
|
|
|
|
public function getProofImageUrlsAttribute(): array
|
|
{
|
|
if (! $this->proof_image) {
|
|
return [];
|
|
}
|
|
|
|
$paths = json_decode($this->proof_image, true);
|
|
if (! is_array($paths)) {
|
|
$paths = [$this->proof_image];
|
|
}
|
|
|
|
$paths = array_values(array_filter($paths, fn ($path) => is_string($path) && $path !== ''));
|
|
|
|
return array_map(
|
|
fn (int $index) => URL::temporarySignedRoute(
|
|
'admin.transactions.proof-image',
|
|
now()->addDay(),
|
|
['transaction' => $this->uuid, 'index' => $index],
|
|
),
|
|
array_keys($paths),
|
|
);
|
|
}
|
|
}
|