134 lines
3.4 KiB
PHP
134 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Teknisi extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'teknisis';
|
|
|
|
/**
|
|
* The primary key associated with the table.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'id_teknisi';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'nama',
|
|
'tanggal_lahir',
|
|
'alamat',
|
|
'email',
|
|
'no_telephone',
|
|
'tanggal_masuk',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'tanggal_lahir' => 'date',
|
|
'tanggal_masuk' => 'date',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [];
|
|
|
|
/**
|
|
* Append computed attributes for API responses.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $appends = ['has_unpaid_kasbon', 'unpaid_kasbon_amount'];
|
|
|
|
// ─── Relationships ────────────────────────────────────────────
|
|
|
|
/**
|
|
* Relationship to Kasbon entries.
|
|
*/
|
|
public function kasbons()
|
|
{
|
|
return $this->hasMany(\App\Models\Kasbon::class, 'id_teknisi', 'id_teknisi');
|
|
}
|
|
|
|
// ─── Scopes ───────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Scope a query to only include active teknisi.
|
|
*/
|
|
public function scopeAktif($query)
|
|
{
|
|
return $query->where('status', 'aktif');
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include inactive teknisi.
|
|
*/
|
|
public function scopeTidakAktif($query)
|
|
{
|
|
return $query->where('status', 'tidak_aktif');
|
|
}
|
|
|
|
// ─── Helper Methods ───────────────────────────────────────────
|
|
|
|
/**
|
|
* Check if the teknisi has any unpaid (belum_lunas) kasbon.
|
|
*/
|
|
public function hasUnpaidKasbon(): bool
|
|
{
|
|
return \App\Models\Kasbon::where('id_teknisi', $this->id_teknisi)
|
|
->where('status', 'belum_lunas')
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* Get total amount of unpaid kasbon.
|
|
*/
|
|
public function unpaidKasbonAmount(): float
|
|
{
|
|
return (float) \App\Models\Kasbon::where('id_teknisi', $this->id_teknisi)
|
|
->where('status', 'belum_lunas')
|
|
->sum('jumlah_kasbon');
|
|
}
|
|
|
|
// ─── Accessors ────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Accessor for has_unpaid_kasbon attribute.
|
|
*/
|
|
public function getHasUnpaidKasbonAttribute(): bool
|
|
{
|
|
return $this->hasUnpaidKasbon();
|
|
}
|
|
|
|
/**
|
|
* Accessor for unpaid_kasbon_amount attribute.
|
|
*/
|
|
public function getUnpaidKasbonAmountAttribute(): float
|
|
{
|
|
return $this->unpaidKasbonAmount();
|
|
}
|
|
} |