2018-09-09 03:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2018-10-21 15:36:02 +00:00
|
|
|
use Auth;
|
|
|
|
use Exception;
|
2021-05-20 23:37:35 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-09-09 03:26:57 +00:00
|
|
|
|
|
|
|
class Item extends Model
|
|
|
|
{
|
2018-09-11 23:48:09 +00:00
|
|
|
protected $fillable = ['product_id', 'name'];
|
|
|
|
|
|
|
|
public function product()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Product::class);
|
|
|
|
}
|
|
|
|
|
2018-09-12 05:31:03 +00:00
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class);
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:51:41 +00:00
|
|
|
public static function deleteAndDetach($item)
|
2018-09-09 03:26:57 +00:00
|
|
|
{
|
2018-09-17 03:51:41 +00:00
|
|
|
//Detach users from this item
|
|
|
|
foreach ($item->users as $user) {
|
|
|
|
User::findOrFail($user->id)->items()->detach($item->id);
|
|
|
|
}
|
2018-09-09 03:26:57 +00:00
|
|
|
|
2018-09-17 03:51:41 +00:00
|
|
|
//Delete item
|
|
|
|
$item->delete();
|
|
|
|
}
|
2018-09-19 17:58:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the items from logged in user
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-09-19 17:58:10 +00:00
|
|
|
* @return \App\Item
|
|
|
|
*/
|
|
|
|
public static function fromAuthUser()
|
|
|
|
{
|
2018-10-21 15:36:02 +00:00
|
|
|
return (new static)->where('user_id', Auth::id());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a specified item
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-21 15:36:02 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function takeItem()
|
|
|
|
{
|
|
|
|
if (isset($this->used_by)) {
|
|
|
|
throw new Exception("Trying to take an Item that is in use", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->used_by = Auth::id();
|
|
|
|
$this->waiting_user_id = null;
|
|
|
|
$this->save();
|
2018-09-19 17:58:10 +00:00
|
|
|
}
|
2018-10-01 00:32:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a specified item
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-01 00:32:33 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function returnItem()
|
|
|
|
{
|
2018-10-21 15:36:02 +00:00
|
|
|
if ($this->used_by != Auth::id()) {
|
|
|
|
throw new Exception("Trying to return an empty Item or from other user", 1);
|
|
|
|
}
|
|
|
|
|
2018-10-01 00:32:33 +00:00
|
|
|
$this->used_by = null;
|
2018-10-21 15:36:02 +00:00
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a waiting user to the item
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-21 15:36:02 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function storeAlert()
|
|
|
|
{
|
|
|
|
$this->waiting_user_id = Auth::id();
|
|
|
|
$this->timestamps = false;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a waiting user to the item
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-21 15:36:02 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function removeAlert()
|
|
|
|
{
|
2018-10-01 00:32:33 +00:00
|
|
|
$this->waiting_user_id = null;
|
2018-10-21 15:36:02 +00:00
|
|
|
$this->timestamps = false;
|
2018-10-01 00:32:33 +00:00
|
|
|
$this->save();
|
|
|
|
}
|
2018-09-09 03:26:57 +00:00
|
|
|
}
|