%PDF- %PDF-
Direktori : /var/www/html/management/app/Models/ |
Current File : /var/www/html/management/app/Models/Computer.php |
<?php namespace App\Models; use App\Exceptions\ComputerHasNoActiveOrders; use App\Exceptions\ComputerIsBeingUsedException; use App\Exceptions\NotEnoughGiftsException; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; class Computer extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'code', 'is_enabled', ]; protected $dates = [ 'start_at', 'end_at', ]; public function usages() { return $this->hasMany(Usage::class); } public function orders() { return $this->hasMany(Order::class); } public function createOrder($userId = null) { $order = $this->getCurrentOrder(); if (!$order) { $order = Order::query()->create([ 'status' => Order::PENDING, 'user_id' => $userId, 'computer_id' => $this->id, ]); } $product = Product::query()->where('sku', Product::HOURLY_RATE)->first(); $item = Item::query()->create([ 'product_id' => $product->id, 'order_id' => $order->id, 'quantity' => 1, 'price' => $product->price ]); Usage::query()->create([ 'start_at' => Carbon::now(), 'item_id' => $item->id, 'computer_id' => $this->id, 'user_id' => $userId ]); return $order; } public function endUsage($price) { $usages = $this->usages()->whereNull('end_at')->get(); $usages->each(function ($usage) use ($price) { /** @var Usage $usage */ $usage->update([ 'end_at' => Carbon::now() ]); if (isset($price)) { $usage->item()->update([ 'price' => $price ]); } }); } public function endOrder($total = 0) { $this->orders()->where('status', Order::PENDING)->get()->each(function ($order) use ($total) { /** @var Order $order */ $order->update([ 'status' => Order::COMPLETED ]); $order->invoices()->create([ 'total' => $total ]); }); } public function isBeingUsed() { return $this->usages()->whereNull('end_at')->exists() || $this->usages()->where('end_at', '>', Carbon::now())->exists(); } public function hasActiveOrder() { return $this->orders()->where('status', Order::PENDING)->exists(); } public function getCurrentUsage() { return $this->usages()->whereNull('end_at')->first(); } public function getCurrentOrder() { return $this->orders()->where('status', Order::PENDING)->first(); } public function transferOrder(Computer $newComputer) { if ($newComputer->hasActiveOrder()) { throw new ComputerIsBeingUsedException; } if (!$this->hasActiveOrder()) { throw new ComputerHasNoActiveOrders; } $this->usages()->whereNull('end_at')->update([ 'computer_id' => $newComputer->id ]); } public function applyGiftDiscount($quantity) { /** @var Usage $usage */ $usage = $this->getCurrentUsage(); $user = $usage->user; if ($quantity > $user->unusedGifts()->count()) { throw new NotEnoughGiftsException; } $gifts = $user->unusedGifts()->take($quantity)->get(); foreach ($gifts as $gift) { /** @var Gift $gift */ $gift->order_id = $usage->item->order->id; $gift->is_used = true; $gift->save(); } } public function applyMoneyDiscount($money) { /** @var Usage $usage */ $usage = $this->getCurrentUsage(); $discountProduct = Product::query()->where('sku', Product::DISCOUNT)->first(); $usage->item->order->items()->create([ 'product_id' => $discountProduct->id, 'quantity' => 1, 'price' => -$money ]); } public function addProduct(Product $product) { /** @var Usage $usage */ $usage = $this->getCurrentUsage(); $usage->item->order->items()->create([ 'product_id' => $product->id, 'quantity' => 1, 'price' => $product->price ]); } public function fixedTimer($money, $userId = null) { $order = $this->getCurrentOrder(); $product = Product::query()->where('sku', Product::HOURLY_RATE)->first(); if (!$order) { $order = Order::query()->create([ 'status' => Order::PENDING, 'user_id' => $userId, 'computer_id' => $this->id, ]); } //TODO: implement time extending $item = Item::query()->create([ 'product_id' => $product->id, 'order_id' => $order->id, 'quantity' => 1, 'price' => $money ]); $minutes = ($money / $product->price) * 60; Usage::query()->create([ 'start_at' => Carbon::now(), 'end_at' => Carbon::now()->addMinutes($minutes), 'item_id' => $item->id, 'computer_id' => $this->id, 'user_id' => $userId ]); return $order; } }