Skip to content

Commit

Permalink
Merge pull request #9 from memiljamel/feature/EijTrwoX-inactive-assets
Browse files Browse the repository at this point in the history
feat: add inactive-assets feature
  • Loading branch information
memiljamel authored Oct 4, 2024
2 parents d205365 + c0a8c49 commit a18ac13
Show file tree
Hide file tree
Showing 18 changed files with 1,661 additions and 8,251 deletions.
51 changes: 51 additions & 0 deletions app/Enums/ReasonEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Enums;

enum ReasonEnum: string
{
/**
* define the reason as sold.
*/
case SOLD = 'sold';

/**
* define the reason as donated.
*/
case DONATED = 'donated';

/**
* define the reason as discarded.
*/
case DISCARDED = 'discarded';

/**
* define the reason as lost.
*/
case LOST = 'lost';

/**
* define the reason as completely demaged.
*/
case COMPLETELY_DAMAGED = 'completely_damaged';

/**
* define the reason as others.
*/
case OTHERS = 'others';

/**
* Get the label for the reason enum.
*/
public function label(): string
{
return match ($this) {
ReasonEnum::SOLD => __('Sold'),
ReasonEnum::DONATED => __('Donated'),
ReasonEnum::DISCARDED => __('Discarded'),
ReasonEnum::LOST => __('Lost'),
ReasonEnum::COMPLETELY_DAMAGED => __('Completely Damaged'),
ReasonEnum::OTHERS => __('Others'),
};
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/ActiveAssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function index(Request $request): View
$page = $request->query('page');
$search = $request->query('search');

$assets = Asset::with(['category', 'brand', 'latestHistory'])
$assets = Asset::active(true)->with(['category', 'brand', 'latestHistory'])
->when($search, function (Builder $query, ?string $search) {
$query->where('name', 'LIKE', "%{$search}%")
->orWhere('code', 'LIKE', "%{$search}%")
Expand Down
58 changes: 58 additions & 0 deletions app/Http/Controllers/AssetArchiveController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreAssetArchiveRequest;
use App\Models\Asset;
use App\Models\AssetArchive;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class AssetArchiveController extends Controller
{
/**
* Show the form for creating a new resource.
*/
public function create(Asset $asset): View
{
return view('asset-archives.create', compact('asset'));
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreAssetArchiveRequest $request, Asset $asset): RedirectResponse
{
DB::transaction(function () use ($request, $asset) {
$asset->active = false;
$asset->save();

$archive = new AssetArchive;
$archive->inactive_date = $request->input('inactive_date');
$archive->reason = $request->input('reason');
$archive->notes = $request->input('notes');
$archive->asset_id = $asset->getAttribute('id');
$archive->save();
});

return redirect()->route('inactive-assets.index')
->with('message', 'The asset has been archived.');
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Asset $asset): RedirectResponse
{
DB::transaction(function () use ($asset) {
$asset->active = true;
$asset->save();

$asset->assetArchive()->delete();
});

return redirect()->route('active-assets.index')
->with('message', 'The asset has been unarchived.');
}
}
50 changes: 50 additions & 0 deletions app/Http/Controllers/InactiveAssetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Controllers;

use App\Models\Asset;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\View\View;

class InactiveAssetController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): View
{
$page = $request->query('page');
$search = $request->query('search');

$assets = Asset::active(false)->with(['category', 'assetArchive'])
->when($search, function (Builder $query, ?string $search) {
$query->where('name', 'LIKE', "%{$search}%")
->orWhere('code', 'LIKE', "%{$search}%")
->orWhereHas('category', function (Builder $query) use ($search) {
$query->where('name', 'LIKE', "%{$search}%");
})
->orWhereHas('assetArchive', function (Builder $query) use ($search) {
$query->where('inactive_date', 'LIKE', "%{$search}%")
->orWhere('reason', 'LIKE', "%{$search}%");
});
})
->latest()
->paginate()
->withQueryString();

if ($page > $assets->lastPage() && $page > 1) {
abort(404);
}

return view('inactive-assets.index', compact('assets', 'search'));
}

/**
* Display the specified resource.
*/
public function show(Asset $asset): View
{
return view('inactive-assets.show', compact('asset'));
}
}
46 changes: 46 additions & 0 deletions app/Http/Requests/StoreAssetArchiveRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Requests;

use App\Enums\ReasonEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreAssetArchiveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'inactive_date' => [
'required',
'date',
'date_format:Y-m-d',
'before_or_equal:today',
],
'reason' => [
'required',
'string',
Rule::Enum(ReasonEnum::class),
],
'notes' => [
'nullable',
'string',
'min:3',
'max:255',
],
];
}
}
17 changes: 17 additions & 0 deletions app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -76,6 +77,14 @@ class Asset extends Model
'notes',
];

/**
* Scope a query to only include active asset.
*/
public function scopeActive(Builder $query, bool $active): void
{
$query->where('active', $active);
}

/**
* Interact with the asset's qrcode url.
*/
Expand Down Expand Up @@ -159,4 +168,12 @@ public function assetFinances(): HasMany
{
return $this->hasMany(AssetFinance::class, 'asset_id', 'id');
}

/**
* Get the asset archive associated with the asset.
*/
public function assetArchive(): HasOne
{
return $this->hasOne(AssetArchive::class);
}
}
77 changes: 77 additions & 0 deletions app/Models/AssetArchive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Models;

use App\Enums\ReasonEnum;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AssetArchive extends Model
{
use HasFactory, HasUuids;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'asset_archives';

/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* Indicates if the model's ID is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The data type of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'inactive_date',
'reason',
'notes',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'reason' => ReasonEnum::class,
];

/**
* Get the asset that owns the asset status.
*/
public function asset(): BelongsTo
{
return $this->belongsTo(Asset::class);
}
}
Loading

0 comments on commit a18ac13

Please sign in to comment.