2017-05-22 21:32:22 -07:00
< ? php
2017-05-23 02:48:51 -07:00
namespace App\Models ;
2017-05-22 21:32:22 -07:00
use App\Http\Traits\UniqueUndeletedTrait ;
2018-07-16 14:13:07 -07:00
use App\Models\Traits\Searchable ;
2021-06-10 13:19:27 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory ;
2017-05-22 21:32:22 -07:00
use Watson\Validating\ValidatingTrait ;
class Department extends SnipeModel
{
2023-08-07 16:54:02 -07:00
use CompanyableTrait ;
2021-06-10 13:17:44 -07:00
use HasFactory ;
2017-05-22 21:32:22 -07:00
/**
* Whether the model should inject it ' s identifier to the unique
* validation rules before attempting validation . If this property
* is not set in the model it will default to true .
*
2021-06-10 13:15:52 -07:00
* @ var bool
2017-05-22 21:32:22 -07:00
*/
protected $injectUniqueIdentifier = true ;
use ValidatingTrait , UniqueUndeletedTrait ;
2020-04-22 06:37:40 -07:00
protected $casts = [
'manager_id' => 'integer' ,
'location_id' => 'integer' ,
'company_id' => 'integer' ,
];
2017-05-22 21:32:22 -07:00
protected $rules = [
2022-11-09 13:11:33 -08:00
'name' => 'required|max:255|is_unique_department' ,
2018-10-03 00:52:29 -07:00
'location_id' => 'numeric|nullable' ,
'company_id' => 'numeric|nullable' ,
'manager_id' => 'numeric|nullable' ,
2017-05-22 21:32:22 -07:00
];
/**
* The attributes that are mass assignable .
*
* @ var array
*/
protected $fillable = [
2024-09-17 14:16:41 -07:00
'created_by' ,
2017-05-22 21:32:22 -07:00
'name' ,
2023-07-10 11:44:21 -07:00
'phone' ,
'fax' ,
2017-05-22 21:32:22 -07:00
'location_id' ,
'company_id' ,
'manager_id' ,
'notes' ,
];
2018-07-16 14:13:07 -07:00
use Searchable ;
2021-06-10 13:15:52 -07:00
2018-07-16 14:13:07 -07:00
/**
* The attributes that should be included when searching the model .
2021-06-10 13:15:52 -07:00
*
2018-07-16 14:13:07 -07:00
* @ var array
*/
2023-07-10 11:44:21 -07:00
protected $searchableAttributes = [ 'name' , 'notes' , 'phone' , 'fax' ];
2018-07-16 14:13:07 -07:00
/**
* The relations and their attributes that should be included when searching the model .
2021-06-10 13:15:52 -07:00
*
2018-07-16 14:13:07 -07:00
* @ var array
*/
2018-08-01 00:06:41 -07:00
protected $searchableRelations = [];
2017-05-22 21:32:22 -07:00
2018-08-01 00:06:41 -07:00
/**
* Establishes the department -> company relationship
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since [ v4 . 0 ]
* @ return \Illuminate\Database\Eloquent\Relations\Relation
*/
2017-05-22 21:32:22 -07:00
public function company ()
{
2021-06-10 13:16:56 -07:00
return $this -> belongsTo ( \App\Models\Company :: class , 'company_id' );
2017-05-22 21:32:22 -07:00
}
/**
2018-08-01 00:06:41 -07:00
* Establishes the department -> users relationship
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since [ v4 . 0 ]
* @ return \Illuminate\Database\Eloquent\Relations\Relation
2017-05-22 21:32:22 -07:00
*/
public function users ()
{
2021-06-10 13:16:56 -07:00
return $this -> hasMany ( \App\Models\User :: class , 'department_id' );
2017-05-22 21:32:22 -07:00
}
2017-05-23 02:48:51 -07:00
/**
2018-08-01 00:06:41 -07:00
* Establishes the department -> manager relationship
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since [ v4 . 0 ]
* @ return \Illuminate\Database\Eloquent\Relations\Relation
*/
2017-05-23 02:48:51 -07:00
public function manager ()
{
2021-06-10 13:16:56 -07:00
return $this -> belongsTo ( \App\Models\User :: class , 'manager_id' );
2017-05-23 02:48:51 -07:00
}
2018-08-01 00:06:41 -07:00
/**
* Establishes the department -> location relationship
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since [ v4 . 0 ]
* @ return \Illuminate\Database\Eloquent\Relations\Relation
*/
2017-05-23 02:48:51 -07:00
public function location ()
{
2021-06-10 13:16:56 -07:00
return $this -> belongsTo ( \App\Models\Location :: class , 'location_id' );
2017-05-23 02:48:51 -07:00
}
2021-06-10 13:15:52 -07:00
2018-02-21 05:04:20 -08:00
/**
* Query builder scope to order on location name
*
* @ param \Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $order Order
*
* @ return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderLocation ( $query , $order )
{
return $query -> leftJoin ( 'locations as department_location' , 'departments.location_id' , '=' , 'department_location.id' ) -> orderBy ( 'department_location.name' , $order );
}
/**
* Query builder scope to order on manager name
*
* @ param \Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $order Order
*
* @ return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderManager ( $query , $order )
{
return $query -> leftJoin ( 'users as department_user' , 'departments.manager_id' , '=' , 'department_user.id' ) -> orderBy ( 'department_user.first_name' , $order ) -> orderBy ( 'department_user.last_name' , $order );
}
2017-05-22 21:32:22 -07:00
}