123 lines
4.7 KiB
PHP
123 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Branch;
|
|
use App\Models\BranchDocument;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class BranchController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return response()->json(Branch::with('documents')->get());
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string',
|
|
'location' => 'required|string',
|
|
'manager_name' => 'required|string',
|
|
'operational_start_date' => 'required|date',
|
|
'payroll_from_day' => 'required|integer|min:1|max:31',
|
|
'payroll_to_day' => 'required|integer|min:1|max:31',
|
|
'salary_generation_day' => 'required|integer|min:1|max:31',
|
|
'docs' => 'required|array|min:1',
|
|
'docs.*.file' => 'required|file',
|
|
'docs.*.name' => 'required|string',
|
|
'docs.*.document_number' => 'nullable|string',
|
|
'docs.*.expiry_date' => 'required|date',
|
|
'docs.*.reminder_days' => 'nullable|integer|min:0',
|
|
]);
|
|
|
|
$branch = Branch::create([
|
|
'name' => $validated['name'],
|
|
'location' => $validated['location'],
|
|
'manager_name' => $validated['manager_name'],
|
|
'operational_start_date' => $validated['operational_start_date'],
|
|
'payroll_from_day' => $validated['payroll_from_day'],
|
|
'payroll_to_day' => $validated['payroll_to_day'],
|
|
'salary_generation_day' => $validated['salary_generation_day'],
|
|
'status' => 'Active'
|
|
]);
|
|
|
|
foreach ($validated['docs'] as $doc) {
|
|
$path = $doc['file']->store('branch_documents', 'public');
|
|
BranchDocument::create([
|
|
'branch_id' => $branch->id,
|
|
'name' => $doc['name'],
|
|
'document_number' => $doc['document_number'] ?? null,
|
|
'path' => $path,
|
|
'expiry_date' => $doc['expiry_date'],
|
|
'reminder_days' => $doc['reminder_days'] ?? 30
|
|
]);
|
|
}
|
|
|
|
return response()->json(['message' => 'Branch created successfully', 'branch' => $branch]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$branch = Branch::findOrFail($id);
|
|
$validated = $request->validate([
|
|
'name' => 'required|string',
|
|
'location' => 'required|string',
|
|
'manager_name' => 'required|string',
|
|
'operational_start_date' => 'required|date',
|
|
'payroll_from_day' => 'required|integer|min:1|max:31',
|
|
'payroll_to_day' => 'required|integer|min:1|max:31',
|
|
'salary_generation_day' => 'required|integer|min:1|max:31',
|
|
'status' => 'required|string',
|
|
'new_docs' => 'nullable|array',
|
|
'new_docs.*.file' => 'required|file',
|
|
'new_docs.*.name' => 'required|string',
|
|
'new_docs.*.document_number' => 'nullable|string',
|
|
'new_docs.*.expiry_date' => 'required|date',
|
|
'new_docs.*.reminder_days' => 'nullable|integer|min:0',
|
|
]);
|
|
|
|
$branch->update([
|
|
'name' => $validated['name'],
|
|
'location' => $validated['location'],
|
|
'manager_name' => $validated['manager_name'],
|
|
'operational_start_date' => $validated['operational_start_date'],
|
|
'payroll_from_day' => $validated['payroll_from_day'],
|
|
'payroll_to_day' => $validated['payroll_to_day'],
|
|
'salary_generation_day' => $validated['salary_generation_day'],
|
|
'status' => $validated['status'],
|
|
]);
|
|
|
|
if (isset($validated['new_docs'])) {
|
|
foreach ($validated['new_docs'] as $doc) {
|
|
$path = $doc['file']->store('branch_documents', 'public');
|
|
BranchDocument::create([
|
|
'branch_id' => $branch->id,
|
|
'name' => $doc['name'],
|
|
'document_number' => $doc['document_number'] ?? null,
|
|
'path' => $path,
|
|
'expiry_date' => $doc['expiry_date'],
|
|
'reminder_days' => $doc['reminder_days'] ?? 30
|
|
]);
|
|
}
|
|
}
|
|
|
|
return response()->json(['message' => 'Branch updated successfully', 'branch' => $branch->load('documents')]);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
return response()->json(Branch::with('documents')->findOrFail($id));
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$branch = Branch::findOrFail($id);
|
|
// Documents will be auto-deleted via cascade constraint in migration
|
|
$branch->delete();
|
|
|
|
return response()->json(['message' => 'Branch deleted successfully']);
|
|
}
|
|
}
|