ldc-crm/app/Models/Claim.php
2025-07-28 09:08:01 +02:00

143 lines
4.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Claims\ClaimStatus;
use App\Enums\Claims\ClaimType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id Claim id.
* @property string $reference Claim reference.
* @property string $first_name Claimant\'s first name.
* @property string $last_name Claimant\'s last name.
* @property string $email Claimant\'s email.
* @property string|null $address Claimant\'s address.
* @property string|null $city Claimant\'s city.
* @property string|null $postcode Claimant\'s postcode.
* @property string|null $file Claimant\'s file.
* @property string $message Claimant\'s message.
* @property \App\Enums\Claims\ClaimType $type Claim type (ClaimType enum).
* @property \App\Enums\Claims\ClaimStatus $status Claim status (ClaimStatus enum).
* @property \App\Models\Brand $brand_id Claim associated Brand.
* @property \App\Models\ClaimSubject $subject_id Claim associated Subject.
* @property \App\Models\Customer|null $customer_id Claim associated Customer.
* @property-read \Illuminate\Support\Carbon $created_at Claim created date.
* @property-read \Illuminate\Support\Carbon $updated_at Claim updated date.
*/
class Claim extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'reference',
'first_name',
'last_name',
'email',
'address',
'city',
'postcode',
'file',
'message',
'type',
'status',
'brand_id',
'subject_id',
'customer_id',
];
/**
* The attributes that should be cast.
*
* @var array<string, mixed>
*/
protected $casts = [
'type' => ClaimType::class,
'status' => ClaimStatus::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::created(function (self $model) {
$model->reference = self::encodeIntegerToBase36($model->getKey());
$model->saveOrFail();
});
}
// * RELATIONS
/**
* Brand belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Brand::class);
}
/**
* ClaimSubject subject belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function claimSubject(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(ClaimSubject::class);
}
/**
* Customer subject belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* ClaimMessage has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function claimMessages(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ClaimMessage::class);
}
// * METHODS
/**
* Convert the integer to base 36 and make uppercase.
*
* @param integer $int
* @return string
*/
public static function encodeIntegerToBase36(int $int): string
{
return strtoupper(base_convert($int, 10, 36));
}
/**
* Convert back to base 10 from base 36, case insensitive.
*
* @param string $str
* @return string
*/
public static function decodeBase36ToInteger(string $str): string
{
return base_convert(strtolower($str), 36, 10);
}
}