<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Perusahaan extends Model
{
use HasFactory;
use SoftDeletes;
protected $guarded = [];
protected static function boot() {
parent::boot();
static::creating(function ($model) {
if ( ! $model->getKey()) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return 'string';
}
public function dealer()
{
return $this->hasMany(Dealer::class);
}
public function armada()
{
return $this->hasMany(Armada::class);
}
/**
* Get array id dealer of perusahaan
* id_dealer_of_perusahaan
*/
public function getIdDealerOfPerusahaanAttribute()
{
$result = [];
foreach ($this->dealer as $dealer) {
$result[] = $dealer->id;
}
return $result;
}
/**
* Scope a query to only include Complete Status of DetailTransaksi where completion date & dealer destination based on given value
*/
public function scopeFilterDetailTransaksi($query, $bulan, $tahun, $dealer_id, $tanggal1, $tanggal2)
{
return $query->whereHas('armada', function($armada) use($bulan, $tahun, $dealer_id, $tanggal1, $tanggal2) {
$armada->whereHas('detailTransaksi', function($detail) use($bulan, $tahun, $dealer_id, $tanggal1, $tanggal2) {
$detail->filterDealer($dealer_id)->filterComplete($bulan, $tahun, $tanggal1, $tanggal2);
});
});
}
/**
* Scope a query to count DetailTransaksi where completion date & dealer destination based on given value of Armada
*/
public function scopeCountDetailTransaksiOfArmada($query, $bulan, $tahun, $dealer_id)
{
return $query->with(['armada' => function($armada) use($bulan, $tahun, $dealer_id) {
$armada->withCount([
'detailTransaksi as detail_transaksi_count' => function($detail) use($bulan, $tahun, $dealer_id) {
$detail->filterDealer($dealer_id)->filterComplete($bulan, $tahun);
}
]);
}]);
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]