Skip to content

A free open source IT asset/license management system

Handle side effects of accepting/declining

Till Deeke1bdf71b58455
diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php
index 5c7235a2ae..3e37871aa4 100644
--- a/app/Http/Controllers/Account/AcceptanceController.php
+++ b/app/Http/Controllers/Account/AcceptanceController.php
@@ -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));
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index cdba5f3206..76b50704c6 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -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;
@@ -27,6 +28,8 @@ class Accessory extends SnipeModel
];
use Searchable;
+
+ use Acceptable;
/**
* The attributes that should be included when searching the model.
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 0898e51902..ec38931b4f 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -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,52 +35,19 @@ class Asset extends Depreciable implements AcceptableContract
const USER = 'user';
const ACCEPTANCE_PENDING = 'pending';
+ use Acceptable;
/**
- * Accept the asset
- *
- * @param User $acceptedBy The user who accepts the asset
- * @param string $signature The filename of the signature, if provided
- */
- public function accept(User $acceptedBy, $signature = null) {
- $this->accepted = 'accepted';
- $this->save();
- }
-
- /**
- * Decline the asset
+ * Run after the checkout acceptance was declined by the user
*
- * @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) {
+ * @param User $acceptedBy
+ * @param string $signature
+ */
+ 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);
+ $this->save();
}
diff --git a/app/Models/CheckoutAcceptance.php b/app/Models/CheckoutAcceptance.php
index f3d2355e53..28442a0aeb 100644
--- a/app/Models/CheckoutAcceptance.php
+++ b/app/Models/CheckoutAcceptance.php
@@ -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);
}
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 1b3a0da156..596106c262 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -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 = [
diff --git a/app/Models/Contracts/Acceptable.php b/app/Models/Contracts/Acceptable.php
deleted file mode 100644
index de253607ad..0000000000
--- a/app/Models/Contracts/Acceptable.php
+++ /dev/null
@@ -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);
-}
\ No newline at end of file
diff --git a/app/Models/LicenseSeat.php b/app/Models/LicenseSeat.php
index 5e387930ec..ab514c0a58 100755
--- a/app/Models/LicenseSeat.php
+++ b/app/Models/LicenseSeat.php
@@ -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'];
diff --git a/app/Models/Traits/Acceptable.php b/app/Models/Traits/Acceptable.php
new file mode 100644
index 0000000000..0837b9a9ae
--- /dev/null
+++ b/app/Models/Traits/Acceptable.php
@@ -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) {}
+}