Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use App;
  6. use App\Salary;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Carbon\Carbon;
  9. use App\Date;
  10. use Sofa\Revisionable\Laravel\Revisionable;
  11.  
  12. class Employee extends Model
  13. {
  14. use Revisionable;
  15.  
  16. protected $fillable = [
  17. 'person_id',
  18. 'contractor_id',
  19. 'location_id',
  20. 'admission',
  21. 'contractdate',
  22. 'decline',
  23. 'contract_id',
  24. 'category_id',
  25. 'unhealty'
  26. ];
  27.  
  28.  
  29. protected $appends = [
  30. 'antiquity',
  31. 'totalSalary'
  32. ];
  33.  
  34. protected $revisionable = [
  35. 'password',
  36. 'location_id',
  37. 'admission',
  38. 'contractdate',
  39. 'decline',
  40. 'unhealty',
  41. ];
  42.  
  43. protected $hidden = ['password'];
  44.  
  45.  
  46. public function person()
  47. {
  48. return $this->belongsTo('App\Person');
  49. }
  50.  
  51. public function phones()
  52. {
  53. return $this->person->phones();
  54. }
  55.  
  56. public function getFullnameAttribute()
  57. {
  58. return $this->person->fullname;
  59. }
  60.  
  61. public function getAgeAttribute()
  62. {
  63. return $this->person->age;
  64. }
  65.  
  66. public function getNationalityAttribute()
  67. {
  68. return $this->person->nationality;
  69. }
  70.  
  71. public function getContractorNameAttribute()
  72. {
  73. return $this->contractor->name;
  74. }
  75.  
  76. public function getContractorPrettyNameAttribute()
  77. {
  78. return $this->contractor->prettyname;
  79. }
  80.  
  81. public function getPositionAttribute()
  82. {
  83. return $this->category->name;
  84. }
  85.  
  86. public function getAllocatedAttribute()
  87. {
  88. if (count($this->location->roles) >= 1) {
  89. if ($this->location->roles[0]->slug = 'business.hotel') {
  90. return trans('business.hotel') . ' ' . $this->location->name;
  91. }
  92.  
  93. if ($this->location->roles[0]->slug = 'business.restaurant') {
  94. return trans('business.restaurant') . ' ' . $this->location->name;
  95. }
  96. }
  97. return trans('business.restaurant') . ' ' . $this->location->name;
  98. }
  99.  
  100. public function getPeriodAttribute()
  101. {
  102. return $this->periods->first()->from . " - " . $this->periods->first()->to;
  103. }
  104.  
  105. public function getClearanceAttribute()
  106. {
  107. return ucfirst($this->clearances->first()->dayname);
  108. }
  109.  
  110. public function ammounts()
  111. {
  112. return $this->belongsToMany('App\Ammount')->orderBy('from', 'DESC');
  113. }
  114.  
  115. public function getAmmount($ammount_id)
  116. {
  117. return $this->ammounts()->where('ammount_id', $ammount_id)->first();
  118. }
  119.  
  120. public function scopeCurrentammount($query, $date = false)
  121. {
  122. if (!$date) {
  123. $date = date('Y-m-d');
  124. } else {
  125. $date = date('Y-m-d', strtotime($date));
  126. }
  127.  
  128. if ($ammount = $this->ammounts()->where('from', '<=', $date)->orderBy('from', 'DESC')->first()) {
  129. return (int) $ammount->value;
  130. }
  131. }
  132.  
  133. public function scopeCurrentClearance($query, $date = false)
  134. {
  135. if (!$date) {
  136. $date = date('Y-m-d');
  137. } else {
  138. $date = date('Y-m-d', strtotime($date));
  139. }
  140.  
  141. return $this->clearances()->where('date', '<=', $date)->orderBy('date', 'DESC')->first();
  142. }
  143.  
  144. public function scopeCurrentTransport($query, $date = false)
  145. {
  146. if (!$date) {
  147. $date = date('Y-m-d');
  148. } else {
  149. $date = date('Y-m-d', strtotime($date));
  150. }
  151.  
  152. if ($transport = $this->transports()->where('from', '<=', $date)->orderBy('from', 'DESC')->first()) {
  153. return $transport;
  154. }
  155. }
  156.  
  157. public function scopeCurrentPeriod($query, $date = false)
  158. {
  159. if (!$date) {
  160. $date = date('Y-m-d');
  161. } else {
  162. $date = date('Y-m-d', strtotime($date));
  163. }
  164.  
  165. if ($period = $this->periods()->where('start', '<=', $date)->orderBy('start', 'DESC')->first()) {
  166. return $period;
  167. }
  168. }
  169.  
  170. public function category()
  171. {
  172. return $this->belongsTo('App\Category');
  173. }
  174.  
  175. public function periods()
  176. {
  177. return $this->belongsToMany('App\Period')->orderBy('start', 'DESC');
  178. }
  179.  
  180.  
  181. public function getPeriod($period_id)
  182. {
  183. return $this->periods()->where('period_id', $period_id)->first();
  184. }
  185.  
  186. public function transports()
  187. {
  188. return $this->belongsToMany('App\Transport')->orderBy('from', 'DESC');
  189. }
  190.  
  191. public function getTransport($transport_id)
  192. {
  193. return $this->transports()->where('transport_id', $transport_id)->first();
  194. }
  195.  
  196. public function clearances()
  197. {
  198. return $this->hasMany('App\Clearance')->orderBy('date', 'DESC');
  199. }
  200.  
  201. public function points($date)
  202. {
  203. if (!$date)
  204. $date = Carbon::now();
  205.  
  206. if ($date instanceOf Carbon)
  207. $date = $date->format('Y-m-d');
  208.  
  209. $period = $this->currentPeriod();
  210. $from = $period->from($date)->subHours(2);
  211. $to = $period->to($date)->addHours(6);
  212.  
  213. return $this->hasMany('App\Point')
  214. ->whereBetween('timestamp', [$from, $to])
  215. ->orderBy('timestamp', 'ASC');
  216. }
  217.  
  218. public function getClearance($clearance_id)
  219. {
  220. return $this->clearances()->where('id', $clearance_id)->first();
  221. }
  222.  
  223. public function contractor()
  224. {
  225. return $this->belongsTo('App\Business');
  226. }
  227.  
  228. public function location()
  229. {
  230. return $this->belongsTo('App\Business');
  231. }
  232.  
  233. public function getAdmissionAttribute()
  234. {
  235. return Date::php($this->attributes['admission']);
  236. }
  237.  
  238. public function getContractdateAttribute()
  239. {
  240. return Date::php($this->attributes['contractdate']);
  241. }
  242.  
  243. public function getdeclineAttribute()
  244. {
  245. return Date::php($this->attributes['decline']);
  246. }
  247.  
  248. public function getAntiquityAttribute()
  249. {
  250. Carbon::setLocale(App::getlocale());
  251. $admission = $this->attributes['admission'];
  252. return Carbon::createFromFormat('Y-m-d', $admission)->diffForHumans(null, true);
  253. }
  254.  
  255. //Scopes
  256. public function scopeActive($query)
  257. {
  258. return $query->whereDate('decline', '>=', date('Y-m-01'))
  259. ->WhereDate('decline', '=', '0000-00-00', 'or');
  260. }
  261.  
  262. public function scopeInactive($query)
  263. {
  264. return $query->whereDate('decline', '<', date('Y-m-d'))
  265. ->WhereDate('decline', '!=', '0000-00-00', 'and');
  266. }
  267.  
  268. public function scopeWithDetails($query)
  269. {
  270. return $query->with('person')
  271. ->with('category')
  272. ->with('location')
  273. ->with('periods')
  274. ->with('clearances');
  275. }
  276.  
  277. public function salary($month = false, $year = false)
  278. {
  279. $this->salary = new Salary($this, $month, $year);
  280. return $this->salary;
  281. }
  282.  
  283. public function getTotalSalaryAttribute($month = false, $year = false)
  284. {
  285. return $this->salary($month, $year)->totalurl;
  286. }
  287.  
  288. public function getBasicinfo()
  289. {
  290. $info = [
  291. 'employee' => $this->person->fullname,
  292. 'password' => isset($this->password),
  293. ];
  294.  
  295. return $info;
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement