Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

3 years ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class UserMonthlyShift extends Model
{
protected $table = 't_users_monthly_shift';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
'company_id', 'project_id', 'start_at', 'end_at', 'user_id', 'remarks', 'schedules',
'created_at', 'created_by', 'updated_at', 'updated_by'
];
// Custom properties
protected $appends = [
'user_name', 'user_ktp_number'
];
private $user_name = "";
private $user_ktp_number = "";
public function getUserNameAttribute() {
return $this->user_name;
}
public function getUserKtpNumberAttribute() {
return $this->user_ktp_number;
}
private function assignUserInfo($users)
{
foreach ($users as $user) {
if ($user->id == $this->user_id) {
//Log::debug("User ID matched {$user->id}");
$this->setUserInfo($user);
break;
}
}
}
private function setUserInfo($user)
{
$this->user_name = $user->name;
$this->user_ktp_number = $user->ktp_number;
}
public static function deleteYYYYMM($yyyymm)
{
$rawWhere = DB::raw("to_char(start_at + (end_at - start_at) / 2, 'YYYYMM')");
$res = UserMonthlyShift::where($rawWhere, $yyyymm)->delete();
return $res;
}
// get monthly schedules
public static function monthlyWithUserInfo($yyyymm)
{
// fetch schedule with user name and KTP no
// get user_id that match the condition
$rawWhere = DB::raw("to_char(start_at+INTERVAL '1 day', 'YYYYMM')");
$userIds = UserMonthlyShift::where($rawWhere, $yyyymm)->select('user_id');
// Fetch users from database
$users = User::whereIn('id', $userIds)
->orderBy('id')
->select('id', 'name', 'ktp_number')
->get();
//var_dump($users);
//Log::info($users);
// Get all schedules
$schedules = UserMonthlyShift::where($rawWhere, $yyyymm)->get();
if ($schedules) {
foreach($schedules as $schedule)
{
$schedule->assignUserInfo($users);
//Log::debug($schedule);
}
}
//Log::debug($schedules);
return $schedules;
}
}