%PDF- %PDF-
Direktori : /var/www/html/management/app/Models/ |
Current File : /var/www/html/management/app/Models/User.php |
<?php namespace App\Models; use Carbon\Carbon; use HttpOz\Roles\Contracts\HasRole as HasRoleContract; use HttpOz\Roles\Traits\HasRole; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Tymon\JWTAuth\Contracts\JWTSubject; class User extends Authenticatable implements HasRoleContract, JWTSubject { use Notifiable, HasRole; const ADMIN = 'ADMIN'; const CLIENT = 'CLIENT'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'full_name', 'email', 'password', 'avatar', 'short_desc', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } public function usages() { return $this->hasMany(Usage::class); } public function gifts() { return $this->hasMany(Gift::class); } public function unusedGifts() { return $this->gifts()->where('is_used', false); } /** * @param $value */ public function setPasswordAttribute($value) { $this->attributes['password'] = bcrypt($value); } /** * @return int */ public function hoursSpentThisMonth() { $total = 0; $usages = $this->usages()->whereNotNull('end_at')->where('start_at', '>', Carbon::now()->startOfMonth())->get(); foreach ($usages as $usage) { /** @var Usage $usage */ $total += Carbon::parse($usage->end_at)->diffInMinutes(Carbon::parse($usage->start_at)); } return round($total / 60, 2); } public function hoursSpentInLastThreeMonths() { $total1 = 0; $total2 = 0; $total3 = 0; $start1 = Carbon::now()->subMonth()->startOfMonth(); $end1 = Carbon::now()->subMonth()->endOfMonth(); $start2 = Carbon::now()->subMonths(2)->endOfMonth(); $end2 = Carbon::now()->subMonths(2)->endOfMonth(); $start3 = Carbon::now()->subMonths(3)->endOfMonth(); $end3 = Carbon::now()->subMonths(3)->endOfMonth(); $usages1 = $this->usages()->whereNotNull('end_at')->where('start_at', '>', $start1)->where('end_at', '<', $end1)->get(); $usages2 = $this->usages()->whereNotNull('end_at')->where('start_at', '>', $start2)->where('end_at', '<', $end2)->get(); $usages3 = $this->usages()->whereNotNull('end_at')->where('start_at', '>', $start3)->where('end_at', '<', $end3)->get(); foreach ($usages1 as $usage) { /** @var Usage $usage */ $total1 += Carbon::parse($usage->end_at)->diffInMinutes(Carbon::parse($usage->start_at)); } foreach ($usages2 as $usage) { /** @var Usage $usage */ $total2 += Carbon::parse($usage->end_at)->diffInMinutes(Carbon::parse($usage->start_at)); } foreach ($usages3 as $usage) { /** @var Usage $usage */ $total3 += Carbon::parse($usage->end_at)->diffInMinutes(Carbon::parse($usage->start_at)); } return [ 'a_month_ago' => [ 'month' => Carbon::now()->subMonth()->format('F'), 'hours' => round($total1 / 60, 2) ], 'two_months_ago' => [ 'month' => Carbon::now()->subMonths(2)->format('F'), 'hours' => round($total2 / 60, 2) ], 'three_months_ago' => [ 'month' => Carbon::now()->subMonths(3)->format('F'), 'hours' => round($total3 / 60, 2) ] ]; } public function getGiftHours() { return $this->gifts()->where('is_used', false)->whereHas('product', function ($q) { $q->where('sku', Product::HOURLY_RATE); })->count(); } public function gift($quantity = 1) { $product = Product::query()->where('sku', Product::HOURLY_RATE)->first(); for ($i = 0; $i < $quantity; $i++) { $this->gifts()->create([ 'product_id' => $product->id, 'is_used' => false ]); } } }