Skip to content

A free open source IT asset/license management system

fix send reminder method to handle other types

Godfrey M0ce20c1eddff
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index be00c2c0e1..6d79f80893 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -3,7 +3,11 @@
namespace App\Http\Controllers;
use App\Helpers\Helper;
+use App\Mail\CheckoutAccessoryMail;
use App\Mail\CheckoutAssetMail;
+use App\Mail\CheckoutComponentMail;
+use App\Mail\CheckoutConsumableMail;
+use App\Mail\CheckoutLicenseMail;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\Actionlog;
@@ -27,6 +31,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
+use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
use \Illuminate\Contracts\View\View;
use League\Csv\Reader;
@@ -1151,41 +1156,77 @@ class ReportsController extends Controller
public function sentAssetAcceptanceReminder(Request $request) : RedirectResponse
{
$this->authorize('reports.view');
-
- if (!$acceptance = CheckoutAcceptance::pending()->find($request->input('acceptance_id'))) {
+ $id = $request->input('acceptance_id');
+ $query = CheckoutAcceptance::query()
+ ->with([
+ 'checkoutable' => function (MorphTo $query) {
+ $query->morphWith([
+ Asset::class => ['model.category', 'assignedTo', 'company', 'checkouts'],
+ Accessory::class => ['category', 'company', 'checkouts'],
+ LicenseSeat::class => ['user', 'license', 'checkouts'],
+ Component::class => ['assignedTo', 'company', 'checkouts'],
+ Consumable::class => ['company', 'checkouts'],
+ ]);
+ },
+ 'assignedTo' => fn ($q) => $q->withTrashed(),
+ ])
+ ->pending();
+ $acceptance = $query->find($id);
+ if (!$acceptance) {
Log::debug('No pending acceptances');
// Redirect to the unaccepted assets report page with error
return redirect()->route('reports/unaccepted_assets')->with('error', trans('general.bad_data'));
}
+ $item = $acceptance->checkoutable;
+ $assignee = $acceptance->assignedTo ?? $item->assignedTo ?? null;
+ $email = $assignee?->email;
+ $locale = $assignee?->locale;
- $assetItem = $acceptance->checkoutable;
-
- Log::debug(print_r($assetItem, true));
+ Log::debug(print_r($acceptance, true));
if (is_null($acceptance->created_at)){
Log::debug('No acceptance created_at');
return redirect()->route('reports/unaccepted_assets')->with('error', trans('general.bad_data'));
} else {
- $logItem_res = $assetItem->checkouts()->where('created_at', '=', $acceptance->created_at)->get();
-
+ if($item instanceof LicenseSeat){
+ $logItem_res = $item->license->checkouts()->where('created_at', '=', $acceptance->created_at)->get();
+ }
+ else{
+ $logItem_res = $item->checkouts()->where('created_at', '=', $acceptance->created_at)->get();
+ }
if ($logItem_res->isEmpty()){
Log::debug('Acceptance date mismatch');
return redirect()->route('reports/unaccepted_assets')->with('error', trans('general.bad_data'));
}
$logItem = $logItem_res[0];
}
- $email = $assetItem->assignedTo?->email;
- $locale = $assetItem->assignedTo?->locale;
if (is_null($email) || $email === '') {
return redirect()->route('reports/unaccepted_assets')->with('error', trans('general.no_email'));
}
-
- Mail::to($email)->send((new CheckoutAssetMail($assetItem, $assetItem->assignedTo, $logItem->user, $acceptance, $logItem->note, firstTimeSending: false))->locale($locale));
+ $mailable = $this->getCheckoutMailType($acceptance, $logItem);
+ Mail::to($email)->send($mailable->locale($locale));
return redirect()->route('reports/unaccepted_assets')->with('success', trans('admin/reports/general.reminder_sent'));
}
+ private function getCheckoutMailType(CheckoutAcceptance $acceptance, $logItem) : Mailable
+ {
+ $lookup = [
+ Accessory::class => CheckoutAccessoryMail::class,
+ Asset::class => CheckoutAssetMail::class,
+ LicenseSeat::class => CheckoutLicenseMail::class,
+ Consumable::class => CheckoutConsumableMail::class,
+ Component::class => CheckoutComponentMail::class,
+ ];
+ $mailable= $lookup[get_class($acceptance->checkoutable)];
+
+ return new $mailable($acceptance->checkoutable,
+ $acceptance->checkedOutTo ?? $acceptance->assignedTo,
+ $logItem->adminuser,
+ $acceptance,
+ $acceptance->note);
+ }
/**
* sentAssetAcceptanceReminder
*
diff --git a/app/Models/Component.php b/app/Models/Component.php
index ddcb3ad575..2ce67c5ec7 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -274,7 +274,19 @@ class Component extends SnipeModel
{
return $this->category?->checkin_email;
}
-
+ /**
+ * Get the list of checkouts for this License
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v2.0]
+ * @return \Illuminate\Database\Eloquent\Relations\Relation
+ */
+ public function checkouts()
+ {
+ return $this->assetlog()->where('action_type', '=', 'checkout')
+ ->orderBy('created_at', 'desc')
+ ->withTrashed();
+ }
/**
* Check how many items within a component are remaining
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 8c6e43c490..27b7a5581f 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -313,6 +313,20 @@ class Consumable extends SnipeModel
return $remaining;
}
+ /**
+ * Get the list of checkouts for this consumable
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v2.0]
+ * @return \Illuminate\Database\Eloquent\Relations\Relation
+ */
+ public function checkouts()
+ {
+ return $this->assetlog()->where('action_type', '=', 'checkout')
+ ->orderBy('created_at', 'desc')
+ ->withTrashed();
+ }
+
/**
* -----------------------------------------------
* BEGIN MUTATORS
diff --git a/app/Models/License.php b/app/Models/License.php
index d84f42f0a1..2ee10cddba 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -391,7 +391,12 @@ class License extends Depreciable
}
return false;
}
-
+ public function checkouts()
+ {
+ return $this->assetlog()->where('action_type', '=', 'checkout')
+ ->orderBy('created_at', 'desc')
+ ->withTrashed();
+ }
/**
* Determine whether the user should be required to accept the license
*
diff --git a/app/Models/LicenseSeat.php b/app/Models/LicenseSeat.php
index 9ddd3fb431..9ad51901e7 100755
--- a/app/Models/LicenseSeat.php
+++ b/app/Models/LicenseSeat.php
@@ -118,7 +118,31 @@ class LicenseSeat extends SnipeModel implements ICompanyableChild
return false;
}
+ /**
+ * Get the list of checkouts for this License
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v2.0]
+ * @return \Illuminate\Database\Eloquent\Relations\Relation
+ */
+ public function checkouts()
+ {
+ return $this->assetlog()->where('action_type', '=', 'checkout')
+ ->orderBy('created_at', 'desc')
+ ->withTrashed();
+ }
+ /**
+ * Establishes the license -> action logs relationship
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v3.0]
+ * @return \Illuminate\Database\Eloquent\Relations\Relation
+ */
+ public function assetlog()
+ {
+ return $this->hasMany(Actionlog::class, 'item_id')->where('item_type', self::class)->orderBy('created_at', 'desc')->withTrashed();
+ }
/**
* Query builder scope to order on department
*