-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from memiljamel/feature/EijTrwoX-inactive-assets
feat: add inactive-assets feature
- Loading branch information
Showing
18 changed files
with
1,661 additions
and
8,251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.