florida_gym/app/Http/Controllers/BranchController.php
2026-03-14 17:13:13 +05:30

188 lines
7.6 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(Request $request)
{
$query = Branch::with('documents');
if ($request->has('status')) {
$query->where('status', $request->status);
}
$branches = $query->get();
// Attach is_deletable flag and total revenue to each branch
$branches->each(function ($branch) {
$inUse = \App\Models\Staff::where('branch_id', $branch->id)->exists()
|| \App\Models\Product::where('branch_id', $branch->id)->exists()
|| \App\Models\Expense::where('branch_id', $branch->id)->exists()
|| \App\Models\Collection::where('branch_id', $branch->id)->exists()
|| \App\Models\Account::where('branch_id', $branch->id)->exists()
|| \App\Models\ProductSale::where('branch_id', $branch->id)->exists()
|| \App\Models\Receptionist::where('branch_id', $branch->id)->exists();
$branch->is_deletable = !$inUse;
// Calculate total revenue (total credit in accounts)
$branch->total_revenue = \App\Models\Account::where('branch_id', $branch->id)->sum('credit');
});
return response()->json($branches);
}
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 ($validated['status'] === 'Inactive') {
$staffAction = $request->input('staff_action');
if ($staffAction === 'move') {
$targetBranchId = $request->input('move_to_branch_id');
if ($targetBranchId) {
\App\Models\Staff::where('branch_id', $id)
->where('status', 'Active')
->update(['branch_id' => $targetBranchId]);
}
} elseif ($staffAction === 'inactivate') {
\App\Models\Staff::where('branch_id', $id)
->where('status', 'Active')
->update(['status' => 'Inactive']);
}
}
// Process new documents if provided
if (!empty($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);
// Check for dependencies
$dependencies = [
'Staff' => \App\Models\Staff::where('branch_id', $id)->exists(),
'Products' => \App\Models\Product::where('branch_id', $id)->exists(),
'Expenses' => \App\Models\Expense::where('branch_id', $id)->exists(),
'Collections' => \App\Models\Collection::where('branch_id', $id)->exists(),
'Accounts' => \App\Models\Account::where('branch_id', $id)->exists(),
'Sales' => \App\Models\ProductSale::where('branch_id', $id)->exists(),
'Receptionists' => \App\Models\Receptionist::where('branch_id', $id)->exists(),
];
$usedIn = array_keys(array_filter($dependencies));
if (!empty($usedIn)) {
return response()->json([
'message' => 'Branch cannot be deleted because it is being used in: ' . implode(', ', $usedIn)
], 422);
}
$branch->delete();
return response()->json(['message' => 'Branch deleted successfully']);
}
public function activeStaff($id)
{
$staff = \App\Models\Staff::where('branch_id', $id)
->where('status', 'Active')
->get(['id', 'full_name', 'role']);
return response()->json($staff);
}
}