Skip to content
9 changes: 0 additions & 9 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,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
4 changes: 4 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): RedirectResponse
$checkout_at = $request->input('checkout_at');
}

if (is_null($asset->first_checkout_at)){
$asset->first_checkout_at = $checkout_at;
Comment thread
Godmartinz marked this conversation as resolved.
}

$expected_checkin = '';
if ($request->filled('expected_checkin')) {
$expected_checkin = $request->input('expected_checkin');
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 @@ -715,7 +715,9 @@ public function storeCheckout(AssetCheckoutRequest $request): RedirectResponse|M
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 @@ -96,6 +96,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 @@ -1302,7 +1303,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) {
Comment thread
Godmartinz marked this conversation as resolved.
$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;
Comment thread
Godmartinz marked this conversation as resolved.

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