Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- use App;
- use App\Salary;
- use Illuminate\Database\Eloquent\Model;
- use Carbon\Carbon;
- use App\Date;
- use Sofa\Revisionable\Laravel\Revisionable;
- class Employee extends Model
- {
- use Revisionable;
- protected $fillable = [
- 'person_id',
- 'contractor_id',
- 'location_id',
- 'admission',
- 'contractdate',
- 'decline',
- 'contract_id',
- 'category_id',
- 'unhealty'
- ];
- protected $appends = [
- 'antiquity',
- 'totalSalary'
- ];
- protected $revisionable = [
- 'password',
- 'location_id',
- 'admission',
- 'contractdate',
- 'decline',
- 'unhealty',
- ];
- protected $hidden = ['password'];
- public function person()
- {
- return $this->belongsTo('App\Person');
- }
- public function phones()
- {
- return $this->person->phones();
- }
- public function getFullnameAttribute()
- {
- return $this->person->fullname;
- }
- public function getAgeAttribute()
- {
- return $this->person->age;
- }
- public function getNationalityAttribute()
- {
- return $this->person->nationality;
- }
- public function getContractorNameAttribute()
- {
- return $this->contractor->name;
- }
- public function getContractorPrettyNameAttribute()
- {
- return $this->contractor->prettyname;
- }
- public function getPositionAttribute()
- {
- return $this->category->name;
- }
- public function getAllocatedAttribute()
- {
- if (count($this->location->roles) >= 1) {
- if ($this->location->roles[0]->slug = 'business.hotel') {
- return trans('business.hotel') . ' ' . $this->location->name;
- }
- if ($this->location->roles[0]->slug = 'business.restaurant') {
- return trans('business.restaurant') . ' ' . $this->location->name;
- }
- }
- return trans('business.restaurant') . ' ' . $this->location->name;
- }
- public function getPeriodAttribute()
- {
- return $this->periods->first()->from . " - " . $this->periods->first()->to;
- }
- public function getClearanceAttribute()
- {
- return ucfirst($this->clearances->first()->dayname);
- }
- public function ammounts()
- {
- return $this->belongsToMany('App\Ammount')->orderBy('from', 'DESC');
- }
- public function getAmmount($ammount_id)
- {
- return $this->ammounts()->where('ammount_id', $ammount_id)->first();
- }
- public function scopeCurrentammount($query, $date = false)
- {
- if (!$date) {
- $date = date('Y-m-d');
- } else {
- $date = date('Y-m-d', strtotime($date));
- }
- if ($ammount = $this->ammounts()->where('from', '<=', $date)->orderBy('from', 'DESC')->first()) {
- return (int) $ammount->value;
- }
- }
- public function scopeCurrentClearance($query, $date = false)
- {
- if (!$date) {
- $date = date('Y-m-d');
- } else {
- $date = date('Y-m-d', strtotime($date));
- }
- return $this->clearances()->where('date', '<=', $date)->orderBy('date', 'DESC')->first();
- }
- public function scopeCurrentTransport($query, $date = false)
- {
- if (!$date) {
- $date = date('Y-m-d');
- } else {
- $date = date('Y-m-d', strtotime($date));
- }
- if ($transport = $this->transports()->where('from', '<=', $date)->orderBy('from', 'DESC')->first()) {
- return $transport;
- }
- }
- public function scopeCurrentPeriod($query, $date = false)
- {
- if (!$date) {
- $date = date('Y-m-d');
- } else {
- $date = date('Y-m-d', strtotime($date));
- }
- if ($period = $this->periods()->where('start', '<=', $date)->orderBy('start', 'DESC')->first()) {
- return $period;
- }
- }
- public function category()
- {
- return $this->belongsTo('App\Category');
- }
- public function periods()
- {
- return $this->belongsToMany('App\Period')->orderBy('start', 'DESC');
- }
- public function getPeriod($period_id)
- {
- return $this->periods()->where('period_id', $period_id)->first();
- }
- public function transports()
- {
- return $this->belongsToMany('App\Transport')->orderBy('from', 'DESC');
- }
- public function getTransport($transport_id)
- {
- return $this->transports()->where('transport_id', $transport_id)->first();
- }
- public function clearances()
- {
- return $this->hasMany('App\Clearance')->orderBy('date', 'DESC');
- }
- public function points($date)
- {
- if (!$date)
- $date = Carbon::now();
- if ($date instanceOf Carbon)
- $date = $date->format('Y-m-d');
- $period = $this->currentPeriod();
- $from = $period->from($date)->subHours(2);
- $to = $period->to($date)->addHours(6);
- return $this->hasMany('App\Point')
- ->whereBetween('timestamp', [$from, $to])
- ->orderBy('timestamp', 'ASC');
- }
- public function getClearance($clearance_id)
- {
- return $this->clearances()->where('id', $clearance_id)->first();
- }
- public function contractor()
- {
- return $this->belongsTo('App\Business');
- }
- public function location()
- {
- return $this->belongsTo('App\Business');
- }
- public function getAdmissionAttribute()
- {
- return Date::php($this->attributes['admission']);
- }
- public function getContractdateAttribute()
- {
- return Date::php($this->attributes['contractdate']);
- }
- public function getdeclineAttribute()
- {
- return Date::php($this->attributes['decline']);
- }
- public function getAntiquityAttribute()
- {
- Carbon::setLocale(App::getlocale());
- $admission = $this->attributes['admission'];
- return Carbon::createFromFormat('Y-m-d', $admission)->diffForHumans(null, true);
- }
- //Scopes
- public function scopeActive($query)
- {
- return $query->whereDate('decline', '>=', date('Y-m-01'))
- ->WhereDate('decline', '=', '0000-00-00', 'or');
- }
- public function scopeInactive($query)
- {
- return $query->whereDate('decline', '<', date('Y-m-d'))
- ->WhereDate('decline', '!=', '0000-00-00', 'and');
- }
- public function scopeWithDetails($query)
- {
- return $query->with('person')
- ->with('category')
- ->with('location')
- ->with('periods')
- ->with('clearances');
- }
- public function salary($month = false, $year = false)
- {
- $this->salary = new Salary($this, $month, $year);
- return $this->salary;
- }
- public function getTotalSalaryAttribute($month = false, $year = false)
- {
- return $this->salary($month, $year)->totalurl;
- }
- public function getBasicinfo()
- {
- $info = [
- 'employee' => $this->person->fullname,
- 'password' => isset($this->password),
- ];
- return $info;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement