Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ public function index(FilterRequest $request, $action = null, $upcoming_status =
}

$assets = Asset::select('assets.*')
// ->addSelect([
// 'first_checkout_at' => Actionlog::query()
// ->select('created_at')
// ->whereColumn('item_id', 'assets.id')
// ->where('item_type', Asset::class)
// ->where('action_type', 'checkout')
// ->orderBy('created_at')
// ->limit(1),
// ])
->with(
'model',
'location',
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/Assets/AssetCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function store(AssetCheckoutRequest $request, $assetId) : RedirectRespons
$checkout_at = $request->input('checkout_at');
}

if (is_null($asset->first_checkout_at)){
$asset->first_checkout_at = $checkout_at;
}

$expected_checkin = '';
if ($request->filled('expected_checkin')) {
$expected_checkin = $request->input('expected_checkin');
Expand All @@ -102,6 +106,7 @@ public function store(AssetCheckoutRequest $request, $assetId) : RedirectRespons
}



if(!empty($asset->licenseseats->all())){
if(request('checkout_to_type') == 'user') {
foreach ($asset->licenseseats as $seat){
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Assets/BulkAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse
if ($request->filled('status_id')) {
$asset->status_id = $request->input('status_id');
}

if (is_null($asset->first_checkout_at)){
$asset->first_checkout_at = $checkout_at;
}
$checkout_success = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->input('note')), $asset->name, null);

//TODO - I think this logic is duplicated in the checkOut method?
Expand Down
9 changes: 8 additions & 1 deletion app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function declinedCheckout(User $declinedBy, $signature)
'last_checkout' => 'datetime',
'last_checkin' => 'datetime',
'expected_checkin' => 'datetime:m-d-Y',
'first_checkout_at' => 'datetime:m-d-Y',
'last_audit_date' => 'datetime',
'next_audit_date' => 'datetime:m-d-Y',
'model_id' => 'integer',
Expand Down Expand Up @@ -1212,7 +1213,13 @@ protected function lastCheckin(): Attribute
set: fn ($value) => $value ? Carbon::parse($value)->format('Y-m-d H:i:s') : null,
);
}

protected function firstCheckoutAt(): Attribute
{
return Attribute::make(
get: fn ($value) => $value ? Carbon::parse($value) : null,
set: fn ($value) => $value ? Carbon::parse($value) : null,
);
}
protected function assetEolDate(): Attribute
{
return Attribute::make(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('action_logs', function (Blueprint $table) {
$table->index(['item_type','item_id','action_type', 'created_at'],
'action_logs_item_type_item_id_action_type_created_at_index');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('action_logs', function (Blueprint $table) {
$table->dropIndex('action_logs_item_type_item_id_action_type_created_at_index');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table("assets", function (Blueprint $table) {
$table->timestamp('first_checkout_at')->after('next_audit_date')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table("assets", function (Blueprint $table) {
$table->dropColumn('first_checkout_at');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use App\Models\Actionlog;
use App\Models\Asset;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$batchSize = 500;
$minId = (int)DB::table('assets')->min('id');
$maxId = (int)DB::table('assets')->max('id');

if (!$minId || !$maxId) {
return;
}

for ($start = $minId; $start <= $maxId; $start += $batchSize) {
$end = $start + $batchSize - 1;

DB::update("
UPDATE assets asset
SET asset.first_checkout_at = (
SELECT MIN(log.created_at)
FROM action_logs log
WHERE log.item_type = 'App\\\\Models\\\\Asset'
AND log.action_type = 'checkout'
AND log.item_id = asset.id
)
WHERE asset.id BETWEEN {$start} AND {$end}
AND asset.first_checkout_at IS NULL
");
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
DB::table('assets')->update(['first_checkout_at' => null]);
}
};
Loading