Skip to content

Commit

Permalink
Merge pull request #14346 from snipe/feature/sc-18728
Browse files Browse the repository at this point in the history
Added `min_amt` functionality to licenses
  • Loading branch information
snipe authored Feb 28, 2024
2 parents 1e66985 + 92eff65 commit ee75df0
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 17 deletions.
32 changes: 27 additions & 5 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\Depreciation;
use App\Models\Setting;
use App\Models\Statuslabel;
use App\Models\License;
use Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Image;
Expand Down Expand Up @@ -715,18 +716,19 @@ public static function generateRandomString($length = 10)
*/
public static function checkLowInventory()
{
$alert_threshold = \App\Models\Setting::getSettings()->alert_threshold;
$consumables = Consumable::withCount('consumableAssignments as consumable_assignments_count')->whereNotNull('min_amt')->get();
$accessories = Accessory::withCount('users as users_count')->whereNotNull('min_amt')->get();
$components = Component::whereNotNull('min_amt')->get();
$asset_models = AssetModel::where('min_amt', '>', 0)->get();
$licenses = License::where('min_amt', '>', 0)->get();

$avail_consumables = 0;
$items_array = [];
$all_count = 0;

foreach ($consumables as $consumable) {
$avail = $consumable->numRemaining();
if ($avail < ($consumable->min_amt) + \App\Models\Setting::getSettings()->alert_threshold) {
if ($avail < ($consumable->min_amt) + $alert_threshold) {
if ($consumable->qty > 0) {
$percent = number_format((($avail / $consumable->qty) * 100), 0);
} else {
Expand All @@ -745,7 +747,7 @@ public static function checkLowInventory()

foreach ($accessories as $accessory) {
$avail = $accessory->qty - $accessory->users_count;
if ($avail < ($accessory->min_amt) + \App\Models\Setting::getSettings()->alert_threshold) {
if ($avail < ($accessory->min_amt) + $alert_threshold) {
if ($accessory->qty > 0) {
$percent = number_format((($avail / $accessory->qty) * 100), 0);
} else {
Expand All @@ -764,7 +766,7 @@ public static function checkLowInventory()

foreach ($components as $component) {
$avail = $component->numRemaining();
if ($avail < ($component->min_amt) + \App\Models\Setting::getSettings()->alert_threshold) {
if ($avail < ($component->min_amt) + $alert_threshold) {
if ($component->qty > 0) {
$percent = number_format((($avail / $component->qty) * 100), 0);
} else {
Expand All @@ -787,7 +789,7 @@ public static function checkLowInventory()
$total_owned = $asset->where('model_id', '=', $asset_model->id)->count();
$avail = $asset->where('model_id', '=', $asset_model->id)->whereNull('assigned_to')->count();

if ($avail < ($asset_model->min_amt)+ \App\Models\Setting::getSettings()->alert_threshold) {
if ($avail < ($asset_model->min_amt) + $alert_threshold) {
if ($avail > 0) {
$percent = number_format((($avail / $total_owned) * 100), 0);
} else {
Expand All @@ -803,6 +805,26 @@ public static function checkLowInventory()
}
}

foreach ($licenses as $license){
$avail = $license->remaincount();
if ($avail < ($license->min_amt) + $alert_threshold) {
if ($avail > 0) {
$percent = number_format((($avail / $license->min_amt) * 100), 0);
} else {
$percent = 100;
}

$items_array[$all_count]['id'] = $license->id;
$items_array[$all_count]['name'] = $license->name;
$items_array[$all_count]['type'] = 'licenses';
$items_array[$all_count]['percent'] = $percent;
$items_array[$all_count]['remaining'] = $avail;
$items_array[$all_count]['min_amt'] = $license->min_amt;
$all_count++;
}

}

return $items_array;
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function index(Request $request)
'seats',
'termination_date',
'depreciation_id',
'min_amt',
];
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
$licenses = $licenses->orderBy($sort, $order);
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/Licenses/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function store(Request $request)
$license->category_id = $request->input('category_id');
$license->termination_date = $request->input('termination_date');
$license->user_id = Auth::id();
$license->min_amt = $request->input('min_amt');

if ($license->save()) {
return redirect()->route('licenses.index')->with('success', trans('admin/licenses/message.create.success'));
Expand Down Expand Up @@ -176,6 +177,7 @@ public function update(Request $request, $licenseId = null)
$license->manufacturer_id = $request->input('manufacturer_id');
$license->supplier_id = $request->input('supplier_id');
$license->category_id = $request->input('category_id');
$license->min_amt = $request->input('min_amt');

if ($license->save()) {
return redirect()->route('licenses.show', ['license' => $licenseId])->with('success', trans('admin/licenses/message.update.success'));
Expand Down Expand Up @@ -245,12 +247,6 @@ public function show($licenseId = null)
$available_seats_count = $license->availCount()->count();
$checkedout_seats_count = ($total_seats_count - $available_seats_count);

\Log::debug('Total: '.$total_seats_count);
\Log::debug('Users: '.$users_count);
\Log::debug('Available: '.$available_seats_count);
\Log::debug('Checkedout: '.$checkedout_seats_count);


$this->authorize('view', $license);
return view('licenses.view', compact('license'))
->with('users_count', $users_count)
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Transformers/LicensesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function transformLicense(License $license)
'company' => ($license->company) ? ['id' => (int) $license->company->id, 'name'=> e($license->company->name)] : null,
'manufacturer' => ($license->manufacturer) ? ['id' => (int) $license->manufacturer->id, 'name'=> e($license->manufacturer->name)] : null,
'product_key' => (Gate::allows('viewKeys', License::class)) ? e($license->serial) : '------------',
'order_number' => e($license->order_number),
'purchase_order' => e($license->purchase_order),
'order_number' => ($license->order_number) ? e($license->order_number) : null,
'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : null,
'purchase_date' => Helper::getFormattedDateObject($license->purchase_date, 'date'),
'termination_date' => Helper::getFormattedDateObject($license->termination_date, 'date'),
'depreciation' => ($license->depreciation) ? ['id' => (int) $license->depreciation->id,'name'=> e($license->depreciation->name)] : null,
Expand All @@ -38,8 +38,9 @@ public function transformLicense(License $license)
'expiration_date' => Helper::getFormattedDateObject($license->expiration_date, 'date'),
'seats' => (int) $license->seats,
'free_seats_count' => (int) $license->free_seats_count,
'license_name' => e($license->license_name),
'license_email' => e($license->license_email),
'min_amt' => ($license->min_amt) ? (int) ($license->min_amt) : null,
'license_name' => ($license->license_name) ? e($license->license_name) : null,
'license_email' => ($license->license_email) ? e($license->license_email) : null,
'reassignable' => ($license->reassignable == 1) ? true : false,
'maintained' => ($license->maintained == 1) ? true : false,
'supplier' => ($license->supplier) ? ['id' => (int) $license->supplier->id, 'name'=> e($license->supplier->name)] : null,
Expand Down
2 changes: 2 additions & 0 deletions app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class License extends Depreciable
'purchase_date' => 'date_format:Y-m-d|nullable|max:10',
'expiration_date' => 'date_format:Y-m-d|nullable|max:10',
'termination_date' => 'date_format:Y-m-d|nullable|max:10',
'min_amt' => 'numeric|nullable|gte:0',
];

/**
Expand Down Expand Up @@ -81,6 +82,7 @@ class License extends Depreciable
'supplier_id',
'termination_date',
'user_id',
'min_amt',
];

use Searchable;
Expand Down
9 changes: 8 additions & 1 deletion app/Presenters/LicensePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ public static function dataTableLayout()
'searchable' => false,
'sortable' => true,
'title' => trans('admin/accessories/general.remaining'),
], [
],
[
'field' => 'min_amt',
'searchable' => false,
'sortable' => true,
'title' => trans('mail.min_QTY'),
'formatter' => 'minAmtFormatter',
],[
'field' => 'purchase_date',
'searchable' => true,
'sortable' => true,
Expand Down
34 changes: 34 additions & 0 deletions database/migrations/2024_02_28_093807_add_min_qty_to_licenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddMinQtyToLicenses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('licenses', function (Blueprint $table) {
$table->integer('min_amt')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('licenses', function (Blueprint $table) {
if (Schema::hasColumn('licenses', 'min_amt')) {
$table->dropColumn('min_amt');
}
});
}
}
3 changes: 3 additions & 0 deletions resources/lang/en-US/admin/licenses/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@

],
],

'below_threshold' => 'There are only :remaining_count seats left for this license with a minimum quantity of :min_amt. You may want to consider purchasing more seats.',
'below_threshold_short' => 'This item is below the minimum required quantity.',
);
2 changes: 2 additions & 0 deletions resources/views/licenses/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</div>
@endcan


<!-- Seats -->
<div class="form-group {{ $errors->has('seats') ? ' has-error' : '' }}">
<label for="seats" class="col-md-3 control-label">{{ trans('admin/licenses/form.seats') }}</label>
Expand All @@ -32,6 +33,7 @@
</div>
{!! $errors->first('seats', '<div class="col-md-8 col-md-offset-3"><span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span></div>') !!}
</div>
@include ('partials.forms.edit.minimum_quantity')

@include ('partials.forms.edit.company-select', ['translated_name' => trans('general.company'), 'fieldname' => 'company_id'])
@include ('partials.forms.edit.manufacturer-select', ['translated_name' => trans('general.manufacturer'), 'fieldname' => 'manufacturer_id',])
Expand Down
12 changes: 11 additions & 1 deletion resources/views/licenses/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,23 @@
</strong>
</div>
<div class="col-md-9">

@if ($license->remaincount() <= ($license->min_amt - \App\Models\Setting::getSettings()->alert_threshold))
<span data-tooltip="true" title="{{ trans('admin/licenses/general.below_threshold', ['remaining_count' => $license->remaincount(), 'min_amt' => $license->min_amt]) }}"><i class="fas fa-exclamation-triangle text-danger" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.warning') }}</span>
</span>
@endif

{{ $license->seats }}
@if ($license->remaincount() <= ($license->min_amt - \App\Models\Setting::getSettings()->alert_threshold))

@endif

</div>
</div>
@endif



<div class="row">
<div class="col-md-3">
<strong>
Expand Down
12 changes: 12 additions & 0 deletions resources/views/partials/bootstrap-table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,18 @@ function prettyLog(str) {
return frags.join(' ');
}
// Show the warning if below min qty
function minAmtFormatter(row, value) {
if ((row) && (row!=undefined)) {
if (value.free_seats_count <= value.min_amt) {
return '<span class="text-danger text-bold" data-tooltip="true" title="{{ trans('admin/licenses/general.below_threshold_short') }}">' + value.min_amt + '</span>';
}
return value.min_amt
}
}
// Create a linked phone number in the table list
function phoneFormatter(value) {
Expand Down

0 comments on commit ee75df0

Please sign in to comment.