Created
February 28, 2020 19:31
-
-
Save pravodev/13d01ccd5a84bd153ef8a810dadaf769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
use DB; | |
class ProductivityProblem extends Model | |
{ | |
protected $fillable = [ | |
'hour_awal', | |
'hour_akhir', | |
'problem_id', | |
'note', | |
'shift', | |
'location', | |
'kilometer', | |
'hourmeter' | |
]; | |
protected $appends = ['problem_name']; | |
public function houlers() | |
{ | |
return $this->belongsToMany(UnitHouler::class, 'productivity_problem_houlers', 'problem_id', 'houler_id'); | |
} | |
public function problem() | |
{ | |
return $this->belongsTo(Problem::class); | |
} | |
public function setupunits() | |
{ | |
return $this->belongsTo(SetupUnit::class, 'setup_unit_id'); | |
} | |
public function getProblemNameAttribute() | |
{ | |
return $this->problem->name; | |
} | |
public function scopeFilters($query, $request) | |
{ | |
$filters = collect($request->filters)->filter(function($d){return !empty($d);})->toArray(); | |
if(isset($filters['shift'])){ | |
$query = $query->where('shift', $filters['shift']); | |
} | |
if(isset($filters['date'])){ | |
$query = $query->whereDate('created_at', \Carbon\Carbon::parse($filters['date'])); | |
} | |
return $query; | |
} | |
public function scopeInRangeHour($query, $hour) | |
{ | |
// return $query->whereTime('hour_awal', '>=', $hour); | |
// if($hour->format('H') == 12){ | |
// return $query->whereTime('hour_awal', '>=', $hour)->whereTime('hour_akhir', '<=', $hour); | |
// } | |
return $query->where(function($model)use($hour){ | |
$model->whereRaw('HOUR(hour_awal) = ?', [$hour->format('H')])->whereTime('hour_akhir', '>', $hour); | |
}) | |
->orWhere(function($model)use($hour){ | |
$model->whereTime('hour_akhir', '>', $hour)->whereTime('hour_awal', '<', $hour); | |
}) | |
; | |
} | |
public function unitOperator() | |
{ | |
return $this->belongsTo(Person::class, 'unit_operator_id'); | |
} | |
public function addProblem($request, $setup = null, $problemID = null) | |
{ | |
$hour_akhir = $request->hour_akhir; | |
if(isset($request->hour_awal) && empty($request->hour_akhir)){ | |
$hour_akhir = get_shift()->end; | |
} | |
$problem = new self; | |
if($problemID){ | |
$problem = $problem->find($problemID); | |
} | |
if($setup == null){ | |
$setup = Setup::find($request->setup_id ?: $problem->setup_id); | |
} | |
$problem->hour_awal = $request->hour_awal; | |
$problem->hour_akhir = $hour_akhir; | |
$problem->problem_id = $request->problem_id; | |
$problem->note = $request->note; | |
$problem->setup_id = $setup->id; | |
$problem->hourmeter = $request->hourmeter; | |
$problem->kilometer = $request->kilometer; | |
$problem->location = $request->location; | |
$problem->is_all_unit = $request->unit_id == 'all'; | |
if(!$problem->is_all_unit){ | |
if($request->unit_category == 'houler'){ | |
$setupunit = SetupUnit::find($request->setup_unit_id); | |
$problem->unit_operator_id = $setupunit->driver_id; | |
}else if($request->unit_category == 'loader'){ | |
$setupunit = SetupUnit::where('setup_id', $setup->id)->where('loader_id', $request->unit_id)->first(); | |
$problem->unit_operator_id = $setupunit->operator_id; | |
} | |
$problem->setup_unit_id = $request->setup_unit_id; | |
$problem->unit_category = $request->unit_category; | |
$problem->unit_id = $request->unit_id; | |
} | |
$problem->save(); | |
if(isset($request->move_unit_id)){ | |
$problem->move_unit_id = $request->move_unit_id; | |
$problem->save(); | |
if($request->unit_category == 'loader'){ | |
SetupUnit::changeLoader($setup, $request->unit_id, $request->move_unit_id); | |
}elseif($request->unit_category == 'houler'){ | |
$setupunit = SetupUnit::find($request->setup_unit_id); | |
$setupunit->transferDriverToOtherHouler($request->move_unit_id); | |
} | |
} | |
} | |
public function updateProblem($request, $setup = null) | |
{ | |
$this->addProblem($request, $setup, $this->id); | |
} | |
public static function getQueryDatatable() | |
{ | |
$request = request(); | |
$selectedColumns = " | |
app_productivity_problems.id, | |
CASE | |
WHEN is_all_unit = 0 THEN unit_id | |
WHEN is_all_unit = 1 THEN \"Semua Unit\" | |
END as unit_id, | |
app_problems.name as problem_name, | |
problem_id, | |
hour_awal, | |
hour_akhir, | |
hourmeter, | |
kilometer, | |
location, | |
shift, | |
note, | |
date, | |
app_persons.name as driver | |
"; | |
$query = DB::table('productivity_problems') | |
->leftJoin('problems', 'problems.id', 'productivity_problems.problem_id') | |
->leftJoin('persons', 'persons.id', 'productivity_problems.unit_operator_id') | |
->orderBy('productivity_problems.created_at', 'DESC') | |
->selectRaw($selectedColumns); | |
$filters = collect($request->filters)->filter(function($d){return !empty($d);})->toArray(); | |
if(isset($filters['shift'])){ | |
$query = $query->where('shift', $filters['shift']); | |
} | |
if(isset($filters['date'])){ | |
$query = $query->whereDate('productivity_problems.created_at', \Carbon\Carbon::parse($filters['date'])); | |
} | |
return $query; | |
} | |
public static function boot() | |
{ | |
parent::boot(); | |
static::creating(function($model){ | |
$model->shift = get_shift()->name; | |
if(empty($model->date)) { | |
$model->date = now(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment