Files
asset-management-system/app/Http/Controllers/SetupWizardController.php
2025-12-02 17:15:08 +07:00

120 lines
5.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Asset;
use App\Models\AssetCategory;
use App\Models\AssetClass;
use App\Models\AssetLocation;
use App\Models\AssetStatus;
use App\Models\Department;
use App\Models\PersonInCharge;
use App\Models\Setting;
use App\Models\Unit;
use App\Models\User;
use App\Models\Warranty;
use App\Services\Asset\AssetService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;
use Spatie\Permission\Models\Role;
class SetupWizardController extends Controller
{
public function index(Request $request): View|RedirectResponse
{
$step = (int) $request->get('step', 1);
return view('setup.wizard', [
'step' => $step,
'hasUser' => User::exists(),
'hasSettings' => Setting::exists(),
'hasMaster' => AssetStatus::exists() && AssetClass::exists() && AssetCategory::exists(),
'hasAsset' => Asset::exists(),
]);
}
public function store(Request $request): RedirectResponse
{
$step = (int) $request->input('step', 1);
if ($step === 1) {
$data = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', 'unique:users,email'],
'password' => ['required', 'confirmed', 'min:8'],
]);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$role = Role::firstOrCreate(['name' => 'super-admin', 'guard_name' => 'web']);
$user->assignRole($role);
auth()->login($user);
return redirect()->route('setup.index', ['step' => 2])->with('success', 'User admin dibuat.');
}
if ($step === 2) {
$data = $request->validate([
'company_name' => ['required', 'string', 'max:255'],
'timezone' => ['required', 'string', 'max:100'],
]);
Setting::updateOrCreate(['key' => 'system.company_name'], [
'value' => $data['company_name'],
'type' => 'string',
'group' => 'system',
]);
Setting::updateOrCreate(['key' => 'system.timezone'], [
'value' => $data['timezone'],
'type' => 'string',
'group' => 'system',
]);
return redirect()->route('setup.index', ['step' => 3])->with('success', 'Setting dasar disimpan.');
}
if ($step === 3) {
$status = AssetStatus::firstOrCreate(['code' => 'ACTIVE'], ['name' => 'Active']);
AssetStatus::firstOrCreate(['code' => 'DISPOSED'], ['name' => 'Disposed']);
$class = AssetClass::firstOrCreate(['code' => 'IT'], ['name' => 'Perangkat IT']);
$category = AssetCategory::firstOrCreate(['code' => 'LAPTOP'], ['name' => 'Laptop']);
$unit = Unit::firstOrCreate(['symbol' => 'pcs'], ['name' => 'Unit']);
$dept = Department::firstOrCreate(['code' => 'GEN'], ['name' => 'General']);
$pic = PersonInCharge::firstOrCreate(['email' => 'pic@example.com'], ['name' => 'Default PIC']);
$loc = AssetLocation::firstOrCreate(['code' => 'WH'], ['name' => 'Warehouse']);
$warranty = Warranty::firstOrCreate(['name' => '1 Tahun'], ['duration_months' => 12]);
session()->flash('master_refs', compact('status','class','category','unit','dept','pic','loc','warranty'));
return redirect()->route('setup.index', ['step' => 4])->with('success', 'Master data dasar dibuat.');
}
if ($step === 4) {
$service = app(AssetService::class);
$refs = session('master_refs', []);
$asset = $service->create([
'name' => $request->input('asset_name', 'Sample Asset'),
'asset_status_id' => $refs['status']->id ?? AssetStatus::first()->id ?? null,
'asset_class_id' => $refs['class']->id ?? AssetClass::first()->id ?? null,
'asset_category_id' => $refs['category']->id ?? AssetCategory::first()->id ?? null,
'unit_id' => $refs['unit']->id ?? Unit::first()->id ?? null,
'department_id' => $refs['dept']->id ?? Department::first()->id ?? null,
'person_in_charge_id' => $refs['pic']->id ?? PersonInCharge::first()->id ?? null,
'asset_user_id' => null,
'asset_location_id' => $refs['loc']->id ?? AssetLocation::first()->id ?? null,
'warranty_id' => $refs['warranty']->id ?? null,
'purchase_date' => now()->subMonth(),
'cost' => 15000000,
'depreciation_method' => 'straight_line',
'useful_life_months' => 36,
'residual_value' => 2000000,
]);
return redirect()->route('index')->with('success', "Setup selesai. Aset sample {$asset->code} dibuat.");
}
return back()->with('error', 'Langkah tidak dikenal.');
}
}