Files
portal-api/app/Models/Transaction.php
T
baodev0 c377c7e195 feat: add domain registration form and email notifications
- Create a new Blade view for domain registration (`registrant-tochuc.blade.php`) to capture organization and manager details.
- Implement email templates for new domain orders for both admin (`domain-order-new.blade.php`) and customers (`domain-order-received.blade.php`).
- Add CSV exports for client and domain data.
- Introduce tests for customer mass assignment, password reset, login throttling, and permission gating.
- Ensure service renewals use current pricing and validate ownership for virtual machines.
- Validate mass assignment behavior in service model.
2026-08-22 17:30:55 +07:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use App\Traits\HasUuid;
class Transaction extends Model
{
use HasFactory, SoftDeletes, HasUuid, LogsActivity;
protected $table = 'transactions';
protected $fillable = [
'uuid',
'public_code',
'trans',
'amount',
'customer_id',
'user_id',
'note',
'status',
'type',
'proof_image',
];
protected $appends = ['direction', 'proof_image_urls'];
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' => 'neutral',
'invoice' => str_starts_with((string) $this->note, 'Admin xác nhận')
? 'neutral'
: 'debit',
default => 'credit',
};
}
/**
* `proof_image` stores either a single relative storage path, or a
* JSON-encoded array of paths (admin-created service with multiple
* proofs). Resolve both shapes into absolute, browsable URLs so the
* raw path string never leaks into the frontend as a literal href.
*/
public function getProofImageUrlsAttribute(): array
{
if (! $this->proof_image) {
return [];
}
$paths = json_decode($this->proof_image, true);
if (! is_array($paths)) {
$paths = [$this->proof_image];
}
return array_values(array_map(
fn (string $path) => Storage::disk('public')->url($path),
array_filter($paths, fn ($path) => is_string($path) && $path !== '')
));
}
}