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.
75 lines
2.0 KiB
75 lines
2.0 KiB
<?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' |
|
]; |
|
|
|
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) { |
|
$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; |
|
} |
|
|
|
public static function monthlyWithUserInfo($yyyymm) |
|
{ |
|
|
|
$rawWhere = DB::raw("to_char(start_at+INTERVAL '1 day', 'YYYYMM')"); |
|
$userIds = UserMonthlyShift::where($rawWhere, $yyyymm)->select('user_id'); |
|
|
|
$users = User::whereIn('id', $userIds) |
|
->orderBy('id') |
|
->select('id', 'name', 'ktp_number') |
|
->get(); |
|
|
|
$schedules = UserMonthlyShift::where($rawWhere, $yyyymm)->get(); |
|
if ($schedules) { |
|
foreach($schedules as $schedule) |
|
{ |
|
$schedule->assignUserInfo($users); |
|
} |
|
} |
|
|
|
return $schedules; |
|
} |
|
}
|
|
|