<?php
namespace App\Controller\IRMS;
use App\Controller\BaseController;
use App\Controller\SecurePageInterface;
use Empire\Core\Branch;
use Empire\Core\BranchServiceHistory;
use Empire\Core\Core;
use Empire\Core\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Class BranchServiceHistoryController
* @package App\Controller
* @Route("/irms/branch-service-history")
*/
class BranchServiceHistoryController extends BaseController implements SecurePageInterface
{
/**
* @return Response
* @Route("", name="view_my_branch_service_history", methods={"GET"})
*/
public function viewMyHistory(): Response
{
$member = Core::user();
$history = $member->getBranchServiceHistory();
$branches = Branch::loadAll();
$summaries = BranchServiceHistory::getServiceSummaries($member->getID());
return $this->render('irms/branch_service_history.twig', [
'gejs' => ['ajax'],
'title' => 'My Branch Service History',
'member' => $member,
'history' => $history,
'branches' => $branches,
'summaries' => $summaries
]);
}
/**
* @param int $id
* @return Response
* @Route("~{id}", name="view_branch_service_history", methods={"GET"})
*/
public function view(int $id): Response
{
$member = User::load([$id]);
if (!$member) {
throw $this->createNotFoundException('Member not found');
}
if (!Core::user()->hasAccess([['branch_service_hist', 1]])) {
$this->addFlash('error', 'Incorrect permissions!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
if (!Core::user()->canView($member)) {
$this->addFlash('error', 'No access to members history!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
$history = $member->getBranchServiceHistory();
$branches = Branch::loadAll();
$summaries = BranchServiceHistory::getServiceSummaries($member->getID());
return $this->render('irms/branch_service_history.twig', [
'gejs' => ['ajax'],
'title' => 'Branch Service History',
'member' => $member,
'history' => $history,
'branches' => $branches,
'summaries' => $summaries
]);
}
/**
* @param Request $request
* @param int $id
* @return Response
* @Route("/add~{id}", name="add_branch_service_history", methods={"POST"})
*/
public function add(Request $request, int $id): Response
{
if (!Core::user()->hasAccess([['branch_service_hist', 2]])) {
$this->addFlash('error', 'Incorrect permissions!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
$member = User::load([$id]);
if (!$member) {
throw $this->createNotFoundException('Member not found');
}
$branchID = $request->request->get('branch');
$rankID = $request->request->get('rank');
$startDateStr = $request->request->get('start_date');
$endDateStr = $request->request->get('end_date');
$reason = $request->request->get('reason');
if (!$branchID || !$rankID || !$startDateStr) {
$this->addFlash('error', 'Missing required fields');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Validate branch exists
$branch = Branch::load([$branchID]);
if (!$branch) {
$this->addFlash('error', 'Selected branch does not exist');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Validate reason length if provided
if ($reason && strlen($reason) > 200) {
$this->addFlash('error', 'Reason cannot be longer than 200 characters');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Validate start date
$startDate = strtotime($startDateStr);
if (!$startDate || !checkdate(date('m', $startDate), date('d', $startDate), date('Y', $startDate))) {
$this->addFlash('error', 'Invalid start date format');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Check if start date is within valid range
$minDate = strtotime('2000-01-01');
$maxDate = time();
if ($startDate < $minDate || $startDate > $maxDate) {
$this->addFlash('error', 'Start date must be between January 1, 2000 and today');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Validate end date if provided
$endDate = null;
if ($endDateStr) {
$endDate = strtotime($endDateStr);
if (!$endDate || !checkdate(date('m', $endDate), date('d', $endDate), date('Y', $endDate))) {
$this->addFlash('error', 'Invalid end date format');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Check if end date is within valid range
if ($endDate < $minDate || $endDate > $maxDate) {
$this->addFlash('error', 'End date must be between January 1, 2000 and today');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
if ($endDate < $startDate) {
$this->addFlash('error', 'End date cannot be earlier than start date');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
}
$member->addBranchServiceHistory(
$branchID,
$rankID,
$startDate,
$endDate,
$reason,
$this->getUser()->getID()
);
$this->addFlash('success', 'Service history entry added successfully');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
/**
* @param Request $request
* @param int $id
* @return Response
* @Route("/end~{id}", name="end_branch_service_history", methods={"POST"})
*/
public function end(Request $request, int $id): Response
{
if (!Core::user()->hasAccess([['branch_service_hist', 2]])) {
$this->addFlash('error', 'Incorrect permissions!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
$member = User::load([$id]);
if (!$member) {
throw $this->createNotFoundException('Member not found');
}
$branchID = $request->request->get('branch');
$endDateStr = $request->request->get('end_date');
$reason = $request->request->get('reason');
if (!$branchID || !$endDateStr) {
$this->addFlash('error', 'Missing required fields');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Validate end date
$endDate = strtotime($endDateStr);
if (!$endDate || !checkdate(date('m', $endDate), date('d', $endDate), date('Y', $endDate))) {
$this->addFlash('error', 'Invalid end date format');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Check if end date is within valid range
$minDate = strtotime('2000-01-01');
$maxDate = time();
if ($endDate < $minDate || $endDate > $maxDate) {
$this->addFlash('error', 'End date must be between January 1, 2000 and today');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
// Get the start date of the current service period
$history = $member->getBranchServiceHistory();
$currentService = null;
foreach ($history->statuses as $status) {
if ($status->branch_id == $branchID && !$status->end_date) {
$currentService = $status;
break;
}
}
if (!$currentService) {
$this->addFlash('error', 'No active service found for this branch');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
if ($endDate < $currentService->start_date) {
$this->addFlash('error', 'End date cannot be earlier than start date');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
$member->endBranchService($branchID, $endDate, $reason, $this->getUser()->getID());
$this->addFlash('success', 'Service history entry updated successfully');
return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
}
/**
* @param int $branchId
* @return Response
* @Route("/api/branches/{branchId}/ranks", name="get_branch_ranks", methods={"GET"})
*/
public function getBranchRanks(int $branchId): Response
{
if (!Core::user()->hasAccess(['branch_service_hist' => 1])) {
$this->addFlash('error', 'Unauthorised access!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
$branch = Branch::load([$branchId]);
if (!$branch) {
throw $this->createNotFoundException('Branch not found');
}
$ranks = $branch->getRanks();
$rankData = [];
foreach ($ranks as $rank) {
$rankData[] = [
'id' => $rank->getID(),
'name' => $rank->getName()
];
}
return $this->json($rankData);
}
}