<?php
namespace App\Controller;
use App\Repository\ChildsRepository;
use App\Repository\UserRepository;
use App\Repository\SessionsRepository;
use Symfony\Component\Asset\Package;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractController
{
private $childsRepository;
private $userRepository;
private $sessionsRepository;
private $security;
public function __construct(ChildsRepository $childsRepository, UserRepository $userRepository, SessionsRepository $sessionsRepository, Security $security)
{
$this->childsRepository = $childsRepository;
$this->userRepository = $userRepository;
$this->sessionsRepository = $sessionsRepository;
$this->security = $security;
}
#[Route('/dashboard')]
public function index(): Response
{
$current_user = $this->security->getUser();
// Show only current stakeholder childs if user is stakeholder
$assigned_childs = array();
foreach ($current_user->getChilds() as $value) {
$assigned_childs[] = $value;
}
//if( !empty($assigned_childs) ){
if( isset($current_user->getRoles()[0]) && $current_user->getRoles()[0] == 'ROLE_STAKEHOLDER' ){
$childs = $this->childsRepository->findByUser($current_user->getId());
$sessions = $this->sessionsRepository->findByUser($current_user->getId());
}else{
$childs = $this->childsRepository->findAll();
$sessions = $this->sessionsRepository->findAll();
}
$users = $this->userRepository->findByRoles('ROLE_STAKEHOLDER');
$sessions_accounting = array();
$sessions_accounting[] = $this->sessionsRepository->findByYear(date('Y'));
$sessions_accounting[] = $this->sessionsRepository->findByYear(date('Y',strtotime("-1 year")));
$sessions_accounting[] = $this->sessionsRepository->findByYear(date('Y',strtotime("-2 year")));
return $this->render('dashboard.html.twig', ['childs' => $childs, 'users' => $users, 'sessions' => $sessions, 'sessions_accounting' => $sessions_accounting]);
}
#[Route('/accounting')]
public function accounting(): Response
{
$childs = $this->childsRepository->findAll();
$users = $this->userRepository->findByRoles('ROLE_STAKEHOLDER');
$sessions = $this->sessionsRepository->findAll();
$sessions_accounting_current = $this->sessionsRepository->findByYear(date('Y'));
return $this->render('accounting.html.twig', ['childs' => $childs, 'users' => $users, 'sessions' => $sessions, 'sessions_accounting_current' => $sessions_accounting_current]);
}
#[Route('/accounting/childreport', name: 'child_report_all')]
public function childReportAll(Request $request)
{
$rows = array();
$childs = $this->childsRepository->findAll();
$filename = 'childs-report-' . date('d-m-Y') . '.csv';
if( !empty($childs) ){
$rows[] = implode(',', array('Session Date', 'Domaine', 'Intervenant', 'Cout', 'Contribution', 'Enfant'));
foreach( $childs as $child ){
$childs_sessions = $child->getSessions();
foreach ($childs_sessions as $session) {
$total = 0;
$totalcontribution = 0;
$userdomains = $session->getUser()->getUserdomains(); //Fetch all the userdomains of this session's user
$contribution = $session->getContribution(); //Fetch the contribution of this session
if( !empty($userdomains) ){
foreach($userdomains as $userdomain){
// Check if the userdomains user and domain matches the sessions'
if( $userdomain->getDomains()->getId() == $session->getDomain()->getId() && $userdomain->getUser()->getId() == $session->getUser()->getId() ){
// Add the domain fee in total
$total = $total + $userdomain->getFee();
}
}
}
if( !empty($contribution) ){
// Add the session contribution in total contribution
$totalcontribution = $totalcontribution + $contribution->getParentcontribution();
}
$data = array($session->getDate()->format('d-m-Y'), $session->getDomain()->getName(), $session->getUser()->getFirstname().' '.$session->getUser()->getLastname(), $total, $totalcontribution, $session->getChild()->getFirstname().' '.$session->getChild()->getLastname());
$rows[] = implode(',', $data);
}
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/accounting/childreport/{id}', name: 'child_report')]
public function childReport($id, Request $request)
{
$rows = array();
if( !empty($id) ){
$child = $this->childsRepository->find($id);
$filename = $child->getFirstname() . ' ' . $child->getLastname() . '-' . date('d-m-Y') . '.csv';
$childs_sessions = $child->getSessions(); // Fetch all sessions of this child
$rows[] = implode(',', array('Session Date', 'Domaine', 'Intervenant', 'Cout', 'Contribution'));
foreach ($childs_sessions as $session) {
$total = 0;
$totalcontribution = 0;
$userdomains = $session->getUser()->getUserdomains(); //Fetch all the userdomains of this session's user
$contribution = $session->getContribution(); //Fetch the contribution of this session
if( !empty($userdomains) ){
foreach($userdomains as $userdomain){
// Check if the userdomains user and domain matches the sessions'
if( $userdomain->getDomains()->getId() == $session->getDomain()->getId() && $userdomain->getUser()->getId() == $session->getUser()->getId() ){
// Add the domain fee in total
$total = $total + $userdomain->getFee();
}
}
}
if( !empty($contribution) ){
// Add the session contribution in total contribution
$totalcontribution = $totalcontribution + $contribution->getParentcontribution();
}
$data = array($session->getDate()->format('d-m-Y'), $session->getDomain()->getName(), $session->getUser()->getFirstname().' '.$session->getUser()->getLastname(), $total, $totalcontribution);
$rows[] = implode(',', $data);
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/accounting/stakeholderreport/{id}', name: 'stakeholder_report')]
public function stakeholderReport($id, Request $request)
{
$rows = array();
if( !empty($id) ){
$user = $this->userRepository->find($id);
$filename = $user->getFirstname() . ' ' . $user->getLastname() . '-' . date('d-m-Y') . '.csv';
$user_sessions = $user->getSessions(); // Fetch all sessions of this child
$rows[] = implode(',', array('Seances Date', 'Domaine', 'Enfant', 'Cout'));
foreach ($user_sessions as $session) {
$total = 0;
$totalcontribution = 0;
$userdomains = $session->getUser()->getUserdomains(); //Fetch all the userdomains of this session's user
if( !empty($userdomains) ){
foreach($userdomains as $userdomain){
// Check if the userdomains user and domain matches the sessions'
if( $userdomain->getDomains()->getId() == $session->getDomain()->getId() && $userdomain->getUser()->getId() == $session->getUser()->getId() ){
// Add the domain fee in total
$total = $total + $userdomain->getFee();
}
}
}
$data = array($session->getDate()->format('d-m-Y'), $session->getDomain()->getName(), $session->getChild()->getFirstname().' '.$session->getChild()->getLastname(), $total);
$rows[] = implode(',', $data);
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/accounting/stakeholderreport', name: 'stakeholder_report_all')]
public function stakeholderReportAll(Request $request)
{
$rows = array();
$users = $this->userRepository->findAll();
$filename = 'stakeholders-report-' . date('d-m-Y') . '.csv';
$rows[] = implode(',', array('Seances Date', 'Domaine', 'Enfant', 'Cout', 'Intervenant'));
if( !empty($users) ){
foreach( $users as $user ){
$user_sessions = $user->getSessions(); // Fetch all sessions of this user
foreach ($user_sessions as $session) {
$total = 0;
$totalcontribution = 0;
$userdomains = $session->getUser()->getUserdomains(); //Fetch all the userdomains of this session's user
if( !empty($userdomains) ){
foreach($userdomains as $userdomain){
// Check if the userdomains user and domain matches the sessions'
if( $userdomain->getDomains()->getId() == $session->getDomain()->getId() && $userdomain->getUser()->getId() == $session->getUser()->getId() ){
// Add the domain fee in total
$total = $total + $userdomain->getFee();
}
}
}
$data = array($session->getDate()->format('d-m-Y'), $session->getDomain()->getName(), $session->getChild()->getFirstname().' '.$session->getChild()->getLastname(), $total, $session->getUser()->getFirstname().' '.$session->getUser()->getLastname());
$rows[] = implode(',', $data);
}
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/accounting/sessionreport/', name: 'session_report')]
public function sessionReport(Request $request)
{
$rows = array();
$sessions = $this->sessionsRepository->findAll();
$filename = 'session-report-' . date('d-m-Y') . '.csv';
//$user_sessions = $session->getUser()->getSessions(); // Fetch all sessions of this child
$rows[] = implode(',', array('Seances Date', 'Enfant', 'Stakeholder', 'Price'));
foreach ($sessions as $session) {
$total = 0;
$userdomains = $session->getUser()->getUserdomains(); //Fetch all the userdomains of this session's user
if( !empty($userdomains) ){
foreach($userdomains as $userdomain){
// Check if the userdomains user and domain matches the sessions'
if( $userdomain->getDomains()->getId() == $session->getDomain()->getId() && $userdomain->getUser()->getId() == $session->getUser()->getId() ){
// Add the domain fee in total
$total = $total + $userdomain->getFee();
}
}
}
$data = array($session->getDate()->format('d-m-Y'), $session->getChild()->getFirstname().' '.$session->getChild()->getLastname(), $session->getUser()->getFirstname().' '.$session->getUser()->getLastname(), $total);
$rows[] = implode(',', $data);
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/accounting/childexport', name: 'child_export')]
public function childExport(Request $request)
{
$rows = array();
$childs = $this->childsRepository->findAll();
$filename = 'childs-export-' . date('d-m-Y') . '.csv';
if( !empty($childs) ){
$array = array_map("utf8_decode", array('Date de naissance', 'Nom du père', 'Téléphone du père', 'Email du père', 'Nom de la mère', 'Téléphone du mère', 'Nom du médecin', 'Adresse du médecin', 'Téléphone du médecin', 'Email', 'Nom', 'Téléphone ', 'Sexe', 'Commentaire', 'Ville Née', 'Email du mère', 'Mères Adresse', 'Code postal', 'Ville', 'Status'));
$rows[] = implode(',', $array);
foreach( $childs as $child ){
$data = array($child->getDob()->format('d-m-Y'), $child->getFathername(), $child->getFatherphone(), $child->getFatheremail(), $child->getMothername(), $child->getMotherphone(), $child->getDoctorname(), $child->getDoctorAddress(), $child->getDoctorPhone(), $child->getEmail(), $child->getFirstname(). ' ' .$child->getLastname(), $child->getPhone(), $child->getGender(), $child->getCommentaire(), $child->getCityborn(), $child->getMotheremail(), $child->getChildaddress(), $child->getChildzipcode(), $child->getChildcity(), $child->getStatus());
$rows[] = implode(',', $data);
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
return $response;
}
#[Route('/calendar', name: 'app_calendar')]
public function calendar(): Response
{
return $this->render('calendar.html.twig');
}
}