<?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 DetailTransaksi 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 user()
{
return $this->belongsTo(User::class);
}
public function armada()
{
return $this->belongsTo(Armada::class);
}
public function motor()
{
return $this->belongsTo(Motor::class);
}
public function transaksi()
{
return $this->belongsTo(Transaksi::class);
}
/**
* Scope a query to only include 3_segmen of a given value & 9_segmen if exist
*/
public function scopeOfSegmen($query, $segmen_3, $segmen_9 = null)
{
return $query->whereHas('motor', function($motor) use($segmen_3, $segmen_9) {
$motor->where('3_segmen', $segmen_3);
// if segmen_9 param is exist
if ($segmen_9) {
$motor->where('9_segmen', $segmen_9);
}
});
}
/**
* Scope a query to only include Complete Status where completion date based on given value
*/
public function scopeFilterComplete($query, $month, $year, $from = null, $to = null)
{
if ($from && $to) {
return $query->where('detail_transaksis.status', 'selesai')->whereMonth('detail_transaksis.tgl_selesai',$month )->whereYear('detail_transaksis.tgl_selesai', $year)->whereBetween('detail_transaksis.tgl_selesai', [$from, $to]);
}else{
return $query->where('detail_transaksis.status', 'selesai')->whereMonth('detail_transaksis.tgl_selesai',$month )->whereYear('detail_transaksis.tgl_selesai', $year);
}
// return $query->where('detail_transaksis.status', 'selesai')->whereMonth('detail_transaksis.tgl_selesai',$month )->whereYear('detail_transaksis.tgl_selesai', $year)->when($from && $to, function($when) use($from, $to){
// $when->whereBetween('detail_transaksis.tgl_selesai', [$from, $to]);
// });
}
/**
* Scope a query to filter dealer based on login user position
* if logged as dealer, just show that dealer
* if logged as Main Dealer and role as Dealer, show dealer based on perusahaan
* else show all dealer
*/
public function scopeFilterDealer($query, $dealer_id = null)
{
return $query->whereHas('transaksi', function($t) use($dealer_id) {
if(\Auth::user()->dealer){
$t->where('dealer_id', \Auth::user()->dealer_id );
}
if(\Auth::user()->posisi == 'Main Dealer' && \Auth::user()->role->nama_role == 'Dealer'){
$t->whereIn('dealer_id', \Auth::user()->perusahaan->id_dealer_of_perusahaan);
}
if ($dealer_id != NULL || $dealer_id != '') {
$t->where('dealer_id', $dealer_id);
}
});
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]