mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
Handle side effects of accepting/declining
When declining an asset, it gets checked in.
This commit is contained in:
parent
8648d53d25
commit
1bdf71b584
|
@ -85,11 +85,7 @@ class AcceptanceController extends Controller {
|
|||
|
||||
if ($request->input('asset_acceptance') == 'accepted') {
|
||||
|
||||
$acceptance->accepted_at = now();
|
||||
$acceptance->signature_filename = $sig_filename;
|
||||
$acceptance->save();
|
||||
|
||||
// TODO: Update state for the checkoutable
|
||||
$acceptance->accept($sig_filename);
|
||||
|
||||
event(new CheckoutAccepted($acceptance));
|
||||
|
||||
|
@ -97,11 +93,7 @@ class AcceptanceController extends Controller {
|
|||
|
||||
} else {
|
||||
|
||||
$acceptance->declined_at = now();
|
||||
$acceptance->signature_filename = $sig_filename;
|
||||
$acceptance->save();
|
||||
|
||||
// TODO: Update state for the checkoutable
|
||||
$acceptance->decline($sig_filename);
|
||||
|
||||
event(new CheckoutDeclined($acceptance));
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\Acceptable;
|
||||
use App\Models\Traits\Searchable;
|
||||
use App\Presenters\Presentable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
@ -28,6 +29,8 @@ class Accessory extends SnipeModel
|
|||
|
||||
use Searchable;
|
||||
|
||||
use Acceptable;
|
||||
|
||||
/**
|
||||
* The attributes that should be included when searching the model.
|
||||
*
|
||||
|
|
|
@ -5,7 +5,7 @@ use App\Events\AssetCheckedOut;
|
|||
use App\Exceptions\CheckoutNotAllowed;
|
||||
use App\Http\Traits\UniqueSerialTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
use App\Models\Contracts\Acceptable as AcceptableContract;
|
||||
use App\Models\Traits\Acceptable;
|
||||
use App\Models\Traits\Searchable;
|
||||
use App\Models\User;
|
||||
use App\Presenters\Presentable;
|
||||
|
@ -20,13 +20,12 @@ use Watson\Validating\ValidatingTrait;
|
|||
use DB;
|
||||
use App\Notifications\CheckinAssetNotification;
|
||||
use App\Notifications\CheckoutAssetNotification;
|
||||
|
||||
/**
|
||||
* Model for Assets.
|
||||
*
|
||||
* @version v1.0
|
||||
*/
|
||||
class Asset extends Depreciable implements AcceptableContract
|
||||
class Asset extends Depreciable
|
||||
{
|
||||
protected $presenter = 'App\Presenters\AssetPresenter';
|
||||
use Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait, UniqueSerialTrait;
|
||||
|
@ -36,54 +35,21 @@ class Asset extends Depreciable implements AcceptableContract
|
|||
const USER = 'user';
|
||||
|
||||
const ACCEPTANCE_PENDING = 'pending';
|
||||
use Acceptable;
|
||||
|
||||
/**
|
||||
* Accept the asset
|
||||
* Run after the checkout acceptance was declined by the user
|
||||
*
|
||||
* @param User $acceptedBy The user who accepts the asset
|
||||
* @param string $signature The filename of the signature, if provided
|
||||
* @param User $acceptedBy
|
||||
* @param string $signature
|
||||
*/
|
||||
public function accept(User $acceptedBy, $signature = null) {
|
||||
$this->accepted = 'accepted';
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decline the asset
|
||||
*
|
||||
* @param User $declinedBy The user who declines the asset
|
||||
* @param string $signature The filename of the signature, if provided
|
||||
*/
|
||||
public function decline(User $declinedBy, $signature = null) {
|
||||
public function declinedCheckout(User $declinedBy, $signature) {
|
||||
$this->assigned_to = null;
|
||||
$this->assigned_type = null;
|
||||
$this->accepted = null;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the asset already accepted?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAccepted() : bool {
|
||||
return $this->accepted != 'pending';
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the asset checked out to this user?
|
||||
*
|
||||
* @param User $user
|
||||
* @return boolean
|
||||
*/
|
||||
public function isCheckedOutTo(User $user) {
|
||||
if (is_null($this->assignedTo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->assignedTo->is($user);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The database table used by the model.
|
||||
|
|
|
@ -40,6 +40,28 @@ class CheckoutAcceptance extends Model
|
|||
return $this->assignedTo->is($user);
|
||||
}
|
||||
|
||||
public function accept($signature_filename) {
|
||||
$this->accepted_at = now();
|
||||
$this->signature_filename = $signature_filename;
|
||||
$this->save();
|
||||
|
||||
/**
|
||||
* Update state for the checked out item
|
||||
*/
|
||||
$this->checkoutable->acceptedCheckout($this->assignedTo, $signature_filename);
|
||||
}
|
||||
|
||||
public function decline($signature_filename) {
|
||||
$this->declined_at = now();
|
||||
$this->signature_filename = $signature_filename;
|
||||
$this->save();
|
||||
|
||||
/**
|
||||
* Update state for the checked out item
|
||||
*/
|
||||
$this->checkoutable->declinedCheckout($this->assignedTo, $signature_filename);
|
||||
}
|
||||
|
||||
public function scopeForUser(Builder $query, User $user) {
|
||||
return $query->where('assigned_to_id', $user->id);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\Acceptable;
|
||||
use App\Models\Traits\Searchable;
|
||||
use App\Presenters\Presentable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
@ -14,6 +15,8 @@ class Consumable extends SnipeModel
|
|||
use Loggable, Presentable;
|
||||
use SoftDeletes;
|
||||
|
||||
use Acceptable;
|
||||
|
||||
protected $dates = ['deleted_at', 'purchase_date'];
|
||||
protected $table = 'consumables';
|
||||
protected $casts = [
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
namespace App\Models\Contracts;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
interface Acceptable {
|
||||
public function accept(User $acceptedBy, $signature);
|
||||
public function decline(User $declinedBy, $signature);
|
||||
public function isAccepted();
|
||||
public function isCheckedOutTo(User $user);
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
namespace App\Models;
|
||||
|
||||
use App\Models\Loggable;
|
||||
use App\Models\Traits\Acceptable;
|
||||
use App\Notifications\CheckinLicenseNotification;
|
||||
use App\Notifications\CheckoutLicenseNotification;
|
||||
use App\Presenters\Presentable;
|
||||
|
@ -21,6 +22,8 @@ class LicenseSeat extends SnipeModel implements ICompanyableChild
|
|||
protected $guarded = 'id';
|
||||
protected $table = 'license_seats';
|
||||
|
||||
use Acceptable;
|
||||
|
||||
public function getCompanyableParents()
|
||||
{
|
||||
return ['asset', 'license'];
|
||||
|
|
27
app/Models/Traits/Acceptable.php
Normal file
27
app/Models/Traits/Acceptable.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\CustomField;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
|
||||
trait Acceptable {
|
||||
/**
|
||||
* Run after the checkout acceptance was accepted by the user
|
||||
*
|
||||
* @param User $acceptedBy
|
||||
* @param string $signature
|
||||
*/
|
||||
public function acceptedCheckout(User $acceptedBy, $signature) {}
|
||||
|
||||
/**
|
||||
* Run after the checkout acceptance was declined by the user
|
||||
*
|
||||
* @param User $acceptedBy
|
||||
* @param string $signature
|
||||
*/
|
||||
public function declinedCheckout(User $declinedBy, $signature) {}
|
||||
}
|
Loading…
Reference in a new issue