<?php
namespace App\Controller;
use App\Entity\Dto\EmployeePictureDto;
use App\Entity\Employee;
use App\Entity\EmployeeHistory;
use App\Enum\EmployeeHistory\SalaryType;
use App\Form\EmployeeType;
use App\Helper\All;
use App\Repository\EmployeeDepartmentRepository;
use App\Repository\EmployeeRepository;
use App\Service\EmployeeAdvanceService;
use App\Service\EmployeeService;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
/**
* @Route("/employee")
*/
class EmployeeController extends AbstractController
{
/** @var EmployeeRepository $employeeRepository */
private $employeeRepository;
/** @var ManagerRegistry $entityManager */
private $entityManager;
/** @var EmployeeService $employeeService */
private $employeeService;
public function __construct(
EmployeeRepository $employeeRepository,
ManagerRegistry $entityManager,
EmployeeService $employeeService
)
{
$this->employeeRepository = $employeeRepository;
$this->entityManager = $entityManager->getManager();
$this->employeeService = $employeeService;
}
/**
* @Route("/production_plan", name="employee_production_plan", methods={"GET"})
*/
public function production_plan()
{
return $this->render(
'employee/production_plan.html.twig',
[
'title' => "Plan de production",
'menu' => 'employee_production_plan',
'employees' => $this->employeeRepository->getOnlyMachinary(),
]
);
}
/**
* @Route("/grid_view", name="employee_index_grid", methods={"GET"})
* @IsGranted("EMPLOYEE_INDEX_GRID")
*/
public function employee_grid(): Response
{
$employees = $this->employeeRepository->activeAndHourlyEmployees();
//group by position
$listByDepartment = [];
foreach ($employees as $employee) {
/** @var Employee $employee */
$department = $employee->getLastHistory()->getDepartment();
if (empty($listByDepartment[$department->getId()])) {
$listByDepartment[$department->getId()] = [];
$listByDepartment[$department->getId()]['department'] = $department;
}
$listByDepartment[$department->getId()]['list'][$employee->getId()] = $employee;
}
asort($listByDepartment);
return $this->render(
'employee_log_hour/index_grid.html.twig',
[
'title' => "Liste des Employé(e)s",
'menu' => 'employee_index_grid',
'listByDepartment' => $listByDepartment,
'count' => count($listByDepartment),
]
);
}
/**
* @Route("/", name="employee_index", methods={"GET"})
* @IsGranted("EMPLOYEE_INDEX")
*/
public function employee(EmployeeDepartmentRepository $employeeDepartmentRepository): Response
{
return $this->render(
'employee/index.html.twig',
[
'title' => "Liste des Employé(e)s",
'menu' => 'employee_index',
'departments' => $employeeDepartmentRepository->findAll()
]
);
}
/**
* @Route("/list", name="employee_list", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_INDEX")
* @param Request $request
* @param UploaderHelper $uploaderHelper
* @param Packages $packages
* @return JsonResponse
*/
public function listTable(
Request $request,
UploaderHelper $uploaderHelper,
Packages $packages
): JsonResponse
{
$query = $request->get('search')['value'];
$tableColumns = [
"",
"e.id",
"e.registration_number",
"e.firstName",
"e.lastName",
"e.cin",
"e.phoneNumber",
"eh.startDate",
"eh.endDate",
"ehd.name",
"ehp.name",
"eh.price",
"e.cnss",
"e.dateCnss",
"",
""
];
$orderByColumn = !empty($tableColumns[$request->get('order')[0]['column']]) ? $tableColumns[$request->get(
'order'
)[0]['column']] : 'e.id';
$orderDirection = $request->get('order')[0]['dir'];
$limit = $request->get('length') != '-1' ? $request->get('length') : '';
$offset = $request->get('start');
$draw = intval($request->get('draw'));
$filters = !empty($request->get('filters')) && is_array($request->get('filters')) ? $request->get('filters') : '';
$employees = $this->employeeRepository->getListTable(
$query,
$filters,
$orderByColumn,
$orderDirection,
$limit,
$offset
);
$data = [];
foreach ($employees['data'] as $employee) {
/** @var Employee $employee */
$picture = $uploaderHelper->asset($employee, 'pictureFile') . '?v4';
if (empty($employee->getPicture())) {
$picture = $packages->getUrl('assets/theme/media/employees/blank.png');
}
$picture = '
<div class="d-flex align-items-center">
<div class="symbol symbol-50 flex-shrink-0">
<img src="' . $picture . '" alt="' . $employee->getFullName() . '" class="zoom-image" data-image-zoom="' . $picture . '">
</div>
</div>
';
$buttons = '';
$show_link = $this->generateUrl('employee_show', ['id' => $employee->getId()]);
$buttons .= '<a href="' . $show_link . '"><i class="flaticon-search-1"> Afficher</i> </a>';
$editPositionUrl = '';
$startDate = $endDate = '-';
$departement = '';
$price = '******';
if ($employee->getLastHistory() instanceof EmployeeHistory) {
$employeeHistory = $employee->getLastHistoryOrNull();
/** @var EmployeeHistory $employeeHistory */
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles()) || SalaryType::HOURLY === $employeeHistory->getSalaryType()) {
$price = $employee->getLastHistory()->getPrice();
}
$startDate = $employee->getLastHistoryOrNull()->getStartDate()->format('d/m/Y');
if (!empty($employee->getLastHistoryOrNull()->getEndDate())) {
$endDate = $employee->getLastHistoryOrNull()->getEndDate()->format('d/m/Y');
}
$departement = $employeeHistory->getDepartment()->getName();
$position = $employeeHistory->getPosition()->getName();
$editPositionUrl = sprintf('<a href="%s">%s</a>', $this->generateUrl('employee_history_edit', ['id' => $employee->getLastHistoryOrNull()->getId()]), $position);
}
$dateCnss = $employee->getDateCnss();
if (!empty($dateCnss)) {
$dateCnss = $dateCnss->format('d/m/Y');
}
$data[] = [
$picture,
$employee->getId(),
$employee->getRegistrationNumber(),
$employee->getFirstName(),
$employee->getLastName(),
$employee->getCin(),
$employee->getPhoneNumber(),
$startDate,
$endDate,
$departement,
$editPositionUrl,
$price,
$employee->getCnss(),
$dateCnss,
All::statusBadge($employee->isActiveEmployee()),
$buttons,
];
}
$data = [
"draw" => $draw,
"recordsTotal" => intval($employees['recordsTotal']),
"recordsFiltered" => intval($employees['recordsFiltered']),
"data" => $data,
];
return new JsonResponse($data);
}
/**
* @Route("/new", name="employee_new", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_NEW", subject="", message="Permission requise")
*/
public function new(
Request $request
): Response
{
$employee = new Employee();
$form = $this->createForm(EmployeeType::class, $employee);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictureFile = $request->files->get('pictureFile');
if (!empty($pictureFile)) {
$pictureDto = new EmployeePictureDto($pictureFile, $employee);
$this->employeeService->updatePicture($pictureDto);
$this->entityManager->flush();
$this->addFlash('success', 'Photo ajouté avec succès');
} elseif (!empty($request->get('no_picture'))) {
//if no picture remove the actual
$employee->setPicture(null);
}
$this->entityManager->persist($employee);
$this->entityManager->flush();
$this->addFlash(
'success',
"L'employé a bien été ajouté !"
);
return $this->redirectToRoute('employee_index');
}
return $this->render(
'employee/new_edit.html.twig',
[
'title' => 'Ajouter',
'menu' => 'employee_new',
'employee' => $employee,
'form' => $form->createView(),
]
);
}
/**
* @Route("/{id}/edit", name="employee_edit", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_EDIT", subject="", message="Permission requise")
*/
public function edit(
Request $request,
Employee $employee
): Response
{
$form = $this->createForm(EmployeeType::class, $employee);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pictureFile = $request->files->get('pictureFile');
if (!empty($pictureFile)) {
$pictureDto = new EmployeePictureDto($pictureFile, $employee);
$this->employeeService->updatePicture($pictureDto);
$this->entityManager->flush();
$this->addFlash('success', 'Photo mis à jour avec succès');
} elseif (!empty($request->get('no_picture'))) {
//if no picture remove the actual
$employee->setPicture(null);
}
$this->entityManager->persist($employee);
$this->entityManager->flush();
$this->addFlash(
'success',
"L'employé a bien été edité !"
);
return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
}
return $this->render(
'employee/new_edit.html.twig',
[
'title' => sprintf('%s: %s', 'Fiche', $employee->getFullName()),
'menu' => 'employee_new',
'employee' => $employee,
'form' => $form->createView(),
]
);
}
/**
* @Route("/{id}", name="employee_show", methods={"GET"}, requirements={"id":"\d+"})
* @IsGranted("EMPLOYEE_SHOW", subject="", message="Permission requise")
*/
public function show(Employee $employee, EmployeeAdvanceService $employeeAdvanceService)
{
return $this->render(
'employee/show.html.twig',
[
'title' => sprintf('%s: %s', 'Fiche', $employee->getFullName()),
'menu' => 'employee_new',
'employee' => $employee,
'form' => $this->createForm(EmployeeType::class, $employee, ['disabled' => true])->createView(),
'readonly' => true,
//'employee_log_hour' => $this->employeeService->htmlHourLogForEmployee($employee)
]
);
}
}