Premier commit
This commit is contained in:
148
app/Http/Controllers/ContractController.php
Normal file
148
app/Http/Controllers/ContractController.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Contract;
|
||||
use App\Models\ContractMeta;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class ContractController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$contracts = Contract::with('municipality')->latest()->paginate(10);
|
||||
return view('contracts.index', compact('contracts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
if (!auth()->user()->isManager()) {
|
||||
abort(403);
|
||||
}
|
||||
$municipalities = \App\Models\Municipality::active()->orderBy('name')->get();
|
||||
$licenseLevels = \App\Models\LicenseLevel::active()->orderBy('name')->get();
|
||||
return view('contracts.create', compact('municipalities', 'licenseLevels'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
if (!auth()->user()->isManager()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'reference' => 'nullable|string|unique:contracts',
|
||||
'provider' => 'required|string',
|
||||
'municipality_id' => 'nullable|exists:municipalities,id',
|
||||
'start_date' => 'required|date',
|
||||
'end_date' => 'nullable|date|after_or_equal:start_date',
|
||||
'amount' => 'nullable|numeric',
|
||||
'type' => 'required|string',
|
||||
'meta' => 'nullable|array', // key-value pairs
|
||||
]);
|
||||
|
||||
$contract = Contract::create($validated);
|
||||
|
||||
if ($request->has('meta')) {
|
||||
foreach ($request->meta as $key => $value) {
|
||||
if ($value) {
|
||||
$contract->meta()->create(['key' => $key, 'value' => $value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('contracts.index')
|
||||
->with('success', 'Contract created successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Contract $contract)
|
||||
{
|
||||
$contract->load(['meta', 'documents', 'municipality']);
|
||||
return view('contracts.show', compact('contract'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Contract $contract)
|
||||
{
|
||||
if (!auth()->user()->isManager()) {
|
||||
abort(403);
|
||||
}
|
||||
$contract->load('meta');
|
||||
$municipalities = \App\Models\Municipality::active()->orderBy('name')->get();
|
||||
$licenseLevels = \App\Models\LicenseLevel::active()->orderBy('name')->get();
|
||||
return view('contracts.edit', compact('contract', 'municipalities', 'licenseLevels'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Contract $contract)
|
||||
{
|
||||
if (!auth()->user()->isManager()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'reference' => 'nullable|string|unique:contracts,reference,' . $contract->id,
|
||||
'provider' => 'required|string',
|
||||
'municipality_id' => 'nullable|exists:municipalities,id',
|
||||
'start_date' => 'required|date',
|
||||
'end_date' => 'nullable|date|after_or_equal:start_date',
|
||||
'amount' => 'nullable|numeric',
|
||||
'status' => 'required|string',
|
||||
]);
|
||||
|
||||
$contract->update($validated);
|
||||
|
||||
// Handle Meta Data Update
|
||||
if ($request->has('meta')) {
|
||||
foreach ($request->meta as $key => $value) {
|
||||
if ($value) {
|
||||
$contract->meta()->updateOrCreate(
|
||||
['key' => $key],
|
||||
['value' => $value]
|
||||
);
|
||||
} else {
|
||||
// If value is empty, maybe delete? Or just leave null.
|
||||
// Let's delete if empty to keep clean
|
||||
$contract->meta()->where('key', $key)->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('contracts.index')
|
||||
->with('success', 'Contract updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Contract $contract)
|
||||
{
|
||||
if (!auth()->user()->isAdmin()) { // Only admin can delete? Or manager?
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$contract->delete();
|
||||
|
||||
return redirect()->route('contracts.index')
|
||||
->with('success', 'Contract deleted.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user