Files
portal-api/app/Models/Customer.php
T

231 lines
4.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
class Customer extends Authenticatable
{
use HasFactory, HasApiTokens, HasUuids;
protected $table = 'customers';
/**
* Chỉ định cột sẽ nhận UUID.
* Mặc định HasUuids dùng cột 'id', mình đổi sang 'uuid'.
*/
public function uniqueIds()
{
return ['uuid'];
}
protected $fillable = [
'fullname',
'avatar',
'address',
'state',
'city',
'postcode',
'country',
'company',
'phone',
'email',
'email_verify',
'email_hash',
'email_marketing',
'receive_deposit',
'identity_number',
'front_identity_number',
'back_identity_number',
'password',
'credit',
'total',
'remember_token',
'status',
'gender',
'province_code',
'ward_code',
];
/**
* Appends for API
*/
protected $appends = [
'point',
'gender_label',
];
protected $casts = [
'gender' => \App\Enums\Client\ClientGender::class,
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Quan hệ với ModuleHosting (Hosting).
*/
public function hosting()
{
return $this->hasMany(ModuleHosting::class);
}
/**
* Quan hệ với ModuleDomain (Domain).
*/
public function domain()
{
return $this->hasMany(ModuleDomain::class);
}
/**
* Quan hệ với ModuleVmware (VPS).
*/
public function vps()
{
return $this->hasMany(ModuleVmware::class);
}
/**
* Quan hệ với Invoice.
*/
public function invoice()
{
return $this->hasMany(Invoice::class);
}
/**
* Quan hệ với Ticket.
*/
public function ticket()
{
return $this->hasMany(Ticket::class);
}
/**
* Quan hệ với Mail.
*/
public function mail()
{
return $this->hasMany(Mail::class);
}
/**
* Quan hệ với File.
*/
public function file()
{
return $this->hasMany(File::class, 'customer_id');
}
/**
* Quan hệ với Order.
*/
public function order()
{
return $this->hasMany(Order::class);
}
/**
* Quan hệ với Transaction.
*/
public function transaction()
{
return $this->hasMany(Transaction::class);
}
/**
* Quan hệ với InvoiceItem.
*/
public function invoiceItem()
{
return $this->hasMany(InvoiceItem::class);
}
/**
* Quan hệ với Log.
*/
public function log()
{
return $this->hasMany(Log::class);
}
/**
* Quan hệ với ModuleSnapshotLimit (Snapshot).
*/
public function snapShot()
{
return $this->hasOne(ModuleSnapshotLimit::class);
}
public function getStatus()
{
return match ($this->attributes['status']) {
1 => '<span class="badge bg-success">Actived</span>',
2 => '<span class="badge bg-secondary">De-actived</span>',
default => '<span class="badge bg-danger">Banned</span>'
};
}
public function getEmailMarketingBoolAttribute()
{
return (bool) $this->email_marketing;
}
public function getReceiveDepositBoolAttribute()
{
return (bool) $this->receive_deposit;
}
public function getPointAttribute()
{
return (string) floor($this->total / 100);
}
public function getCmndBeforeAttribute()
{
return $this->front_identity_number ?? '';
}
public function getCmndAfterAttribute()
{
return $this->back_identity_number ?? '';
}
public function getCmndAttribute()
{
return $this->identity_number;
}
public function getOffMailMarketingAttribute()
{
return $this->email_marketing;
}
public function province()
{
return $this->belongsTo(Province::class, 'province_code', 'code');
}
public function ward()
{
return $this->belongsTo(Ward::class, 'ward_code', 'code');
}
public function getGenderLabelAttribute()
{
return $this->gender?->label() ?? 'Khác';
}
}