ManagementSystem/BACKEND/app/Models/Notes.php

72 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Notes extends Model
{
use HasFactory;
protected $fillable = [
'n_user_id',
'n_day',
'n_month',
'n_year',
'n_time_type',
'n_reason',
'n_note',
'ticket_id'
];
/**
* Lấy thông tin bảng notes dựa vào tháng và năm.
*
* @param int $month
* @param int $year
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getNotesByMonthAndYear($month, $year)
{
return self::leftJoin("categories as reason", function ($join) {
$join->on('n_reason', '=', 'reason.c_code');
$join->on('reason.c_type', DB::raw("CONCAT('REASON_NOTES')"));
})
->leftJoin("categories as timeTypes", function ($join) {
$join->on('n_time_type', '=', 'timeTypes.c_code');
$join->on('timeTypes.c_type', DB::raw("CONCAT('TIME_TYPE')"));
})
->where('n_month', $month)
->where('n_year', $year)
->select(
'notes.id as n_id',
'n_user_id',
'n_day',
'n_month',
'n_year',
'n_time_type',
'n_reason',
'n_note',
'reason.c_name as reason_name',
'timeTypes.c_name as time_type_name'
)
->get();
}
public static function getNotesByMonthAndYearAndUserId($month, $year, $userId, $idNote)
{
return self::where('n_reason', 'ONLEAVE')->where('n_month', $month)->where('n_year', $year)
->where('n_user_id', $userId)
->where('id', '!=', $idNote)->get();
}
public static function getNotesByMonthAndYearAndUserIdAndReason($month, $year, $userId, $reason)
{
return self::where('n_reason', $reason)->where('n_month', $month)->where('n_year', $year)
->where('n_user_id', $userId)
->orderBy('n_day', 'asc')->orderBy('n_time_type', 'desc')->get();
}
}