*/ protected $fillable = [ 'nama', 'tanggal_lahir', 'alamat', 'email', 'no_telephone', 'tanggal_masuk', 'status', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'tanggal_lahir' => 'date', 'tanggal_masuk' => 'date', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = []; /** * Append computed attributes for API responses. * * @var array */ 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(); } }