<?php
namespace App\Controller\Treasury;
use Knp\Snappy\Pdf;
use App\Entity\User;
use DateTimeImmutable;
use App\Service\SMSSender;
use App\Entity\LogOperation;
use App\Entity\TreasuryPayment;
use App\Entity\SettingClassroom;
use App\Entity\TreasuryCheckout;
use App\Repository\UserRepository;
use App\Entity\FoundingNotification;
use App\Entity\TreasuryCashMovement;
use App\Entity\TreasuryCashRegister;
use App\Repository\SchoolYearRepository;
use App\Form\Treasury\TreasuryPaymentType;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\TreasuryCheckoutRepository;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\RegistrationStudentRegistration;
use App\Form\Treasury\TreasuryCashRegisterType;
use App\Repository\AccountingExpenseRepository;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Repository\TreasuryCashRegisterRepository;
use App\Entity\AccountingStudentRegistrationPayment;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use App\Entity\AccountingStudentRegistrationFeeShedulPayment;
use App\Repository\RegistrationStudentRegistrationRepository;
use App\Form\Accounting\AccountingStudentRegistrationPaymentType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Repository\AccountingStudentRegistrationPaymentRepository;
use App\Repository\AccountingStudentRegistrationFeeShedulRepository;
/**
* @Route("/treasury/cash-register")
*/
class TreasuryCashRegisterController extends AbstractController
{
/**
* @Route("/index", name="treasury_cash_register_index", methods={"GET","POST"})
*/
public function index(Request $request, PaginatorInterface $paginator, TreasuryCashRegisterRepository $treasuryCashRegisterRepository, TreasuryCheckoutRepository $treasuryCheckoutRepository): Response
{
/**@var User $user */
$user = $this->getUser();
$schoolYear = $user->getSchoolYear();
$establishment = $user->getEstablishment();
if ($schoolYear == null) {
$this->addFlash('warning', "Aucune base de donnée n'est activé.");
return $this->redirectToRoute('treasury_cash_register_new', []);
}
$treasuryCashRegister = new TreasuryCashRegister();
$treasuryCashRegister->setCode($this->getUser()->getUserIdentifier().'-du-'.$treasuryCashRegister->getCreateAt()->format('d-m-Y'));
$establishment = $this->getUser()->getEstablishment();
$checkouts = $treasuryCheckoutRepository->createQueryBuilder('entity')
->andWhere('entity.created_by = :created_by')
->setParameter('created_by', $this->getUser()->getId())
->orderBy('entity.id', 'desc')
->getQuery()
->getResult();
$form = $this->createForm(TreasuryCashRegisterType::class, $treasuryCashRegister)
->add('checkout', EntityType::class, [
'class' => TreasuryCheckout::class,
'choices' => $checkouts
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$treasuryCashRegister->setSchoolYear($schoolYear);
$treasuryCashRegister->setEstablishment($treasuryCashRegister->getCheckout()->getEstablishment());
$treasuryCashRegister->setCreateAt(new DateTimeImmutable());
$entityManager->persist($treasuryCashRegister);
try {
$entityManager->flush();
$this->addFlash('success', "Un releve à été ajoutée.");
return $this->redirectToRoute('treasury_cash_register_show', ['id' => $treasuryCashRegister->getId()]);
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreure est survenue lors de l'ajout du releve.");
$this->addFlash('info', $th->getMessage());
return $this->redirectToRoute('treasury_cash_register_index', []);
}
}
if ($this->isGranted("ROLE_INDENTANT")) {
$query = $treasuryCashRegisterRepository->createQueryBuilder('entity')
->innerJoin('entity.establishment', 'establishment')
->addSelect('establishment')
->andWhere('establishment.establishmentGroup = :establishmentGroup')
->setParameter('establishmentGroup', $establishment->getEstablishmentGroup())
->orderBy('entity.id', 'DESC')
->getQuery();
}else {
$query = $treasuryCashRegisterRepository->createQueryBuilder('entity')
->innerJoin('entity.establishment', 'establishment')
->addSelect('establishment')
->andWhere('establishment.establishmentGroup = :establishmentGroup')
->setParameter('establishmentGroup', $establishment->getEstablishmentGroup())
->andWhere('entity.created_by = :created_by')
->setParameter('created_by', $user->getId())
->orderBy('entity.id', 'DESC')
->getQuery();
}
$treasuryCashRegisters = $paginator->paginate($query, $request->query->getInt('page', 1), 10);
return $this->renderForm('treasury/treasury_cash_register/index.html.twig', [
'treasury_cash_registers' => $treasuryCashRegisters,
'treasury_cash_register' => $treasuryCashRegister,
'treasuryCashRegisterRepository' => $treasuryCashRegisterRepository,
'form' => $form,
]);
}
/**
* @Route("/{id}/show", name="treasury_cash_register_show", methods={"GET", "POST"})
*/
public function show(Request $request, TreasuryCashRegister $treasuryCashRegister, RegistrationStudentRegistrationRepository $registrationStudentRegistrationRepository, AccountingStudentRegistrationPaymentRepository $accountingStudentRegistrationPaymentRepository, AccountingExpenseRepository $accountingExpenseRepository, UserRepository $userRepository, TreasuryCashRegisterRepository $treasuryCashRegisterRepository, SMSSender $smsSender): Response
{
/**@var User $user */
$user = $this->getUser();
$schoolYear = $user->getSchoolYear();
$establishment = $user->getEstablishment();
$accountingStudentRegistrationPayment = new AccountingStudentRegistrationPayment();
$registrationStudentRegistrationUnsolds = new ArrayCollection();
$studentRegistrations = $registrationStudentRegistrationRepository->createQueryBuilder('entity')
->innerJoin('entity.student', 'student')
->addSelect('student')
->andWhere('entity.establishment = :establishment')
->setParameter('establishment', $establishment)
->andWhere('entity.schoolYear = :schoolYear')
->setParameter('schoolYear', $schoolYear)
->andWhere('entity.is_abandonned = :is_abandonned')
->setParameter('is_abandonned', 0)
->orderBy('entity.id', 'DESC')
->getQuery()
->getResult();
foreach ($studentRegistrations as $key => $registrationStudentRegistration) {
if ($registrationStudentRegistration->getAmountRest() > 0) {
$registrationStudentRegistrationUnsolds->add($registrationStudentRegistration);
}
}
/* $parentStudentRegistrations = $registrationStudentRegistrationRepository->createQueryBuilder('entity')
->innerJoin('entity.student', 'student')
->addSelect('student')
->innerJoin('entity.establishment', 'establishment')
->addSelect('establishment')
->innerJoin('establishment.childEstablishments', 'childEstablishment')
->addSelect('childEstablishment')
->andWhere('childEstablishment.id = :idChild')
->setParameter('idChild', $establishment->getId())
->andWhere('entity.schoolYear = :schoolYear')
->setParameter('schoolYear', $schoolYear)
->andWhere('entity.is_abandonned = :is_abandonned')
->setParameter('is_abandonned', 0)
->orderBy('entity.id', 'DESC')
->getQuery()
->getResult();
foreach ($parentStudentRegistrations as $key => $parentStudentRegistration) {
if ($parentStudentRegistration->getAmountRest() > 0) {
if (!$registrationStudentRegistrationUnsolds->contains($parentStudentRegistration)) {
$registrationStudentRegistrationUnsolds->add($parentStudentRegistration);
}
}
} */
$form = $this->createForm(AccountingStudentRegistrationPaymentType::class, $accountingStudentRegistrationPayment)
->add('studentRegistration', EntityType::class, [
'class' => RegistrationStudentRegistration::class,
'choices' => $registrationStudentRegistrationUnsolds,
'required' => true
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
if ($schoolYear == null) {
$this->addFlash('warning', "Aucune base de donnée n'est activé.");
return $this->redirectToRoute('treasury_cash_register_show', ['id' => $treasuryCashRegister->getId()]);
}
if ($accountingStudentRegistrationPayment->getAmount() > ($accountingStudentRegistrationPayment->getStudentRegistration()->getGleAmount() - $accountingStudentRegistrationPayment->getStudentRegistration()->getAmountPaid())) {
$this->addFlash('warning', "Le montant du paiement depasse le solde. Veuillez saisir un montant inferieur ou égal au solde.");
return $this->redirectToRoute('treasury_cash_register_show', ['id' => $treasuryCashRegister->getId()]);
}
$accountingStudentRegistrationPayment->setSchoolYear($schoolYear);
$accountingStudentRegistrationPayment->setCashRegister($treasuryCashRegister);
$accountingStudentRegistrationPayment->setEstablishment($accountingStudentRegistrationPayment->getStudentRegistration()->getEstablishment());
$nextIndex = count($accountingStudentRegistrationPaymentRepository->findBy(['establishment' => $accountingStudentRegistrationPayment->getStudentRegistration()->getEstablishment()], [])) + 1;
$accountingStudentRegistrationPayment->setCode(date('Y-').$accountingStudentRegistrationPayment->getStudentRegistration()->getEstablishment()->getId().'-'.sprintf("%'05s", $nextIndex));
$entityManager->persist($accountingStudentRegistrationPayment);
try {
$entityManager->flush();
$studentRegistration = $accountingStudentRegistrationPayment->getStudentRegistration();
$studentRegistration->setLastPaymentAt(new DateTimeImmutable());
$studentRegistration->setLastAmountPaid(floatval($request->get('_shedul_payment')));
$studentRegistration->setLastPaymentId($accountingStudentRegistrationPayment->getId());
$accountingStudentRegistrationPayment->setCode(date('Y-').$accountingStudentRegistrationPayment->getStudentRegistration()->getEstablishment()->getId().'-'.sprintf("%'05s", $accountingStudentRegistrationPayment->getId()));
$logOperation = new LogOperation($request->getClientIp(), $accountingStudentRegistrationPayment->getId(), AccountingStudentRegistrationPayment::class, "Création de paiement N°: <strong>".$accountingStudentRegistrationPayment->getCode().", élève : " . $accountingStudentRegistrationPayment->getStudentRegistration()->getStudent()->getCode() . ", montant: ". number_format($accountingStudentRegistrationPayment->getAmount(), 0, ',', ' ') . " </strong>", $this->getUser()->getUserIdentifier(), "primary");
$logOperation->setEstablishment($establishment);
$entityManager->persist($logOperation);
$entityManager->flush();
$this->addFlash('success', "Un paiement à été ajouté.");
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreure est survenue lors de l'ajout du paiement.");
$this->addFlash('info', $th->getMessage());
}
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
//versements
$treasuryPayment = new TreasuryPayment();
$treasuryPayment->setCreateAt(new DateTimeImmutable());
$treasuryPaymentForm = $this->createForm(TreasuryPaymentType::class, $treasuryPayment);
$treasuryPaymentForm->handleRequest($request);
if ($treasuryPaymentForm->isSubmitted() && $treasuryPaymentForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
/*
* 20/08/2022
* si le solde de la caisse est insuffisant pour effectué un versement
* le fondateur à demande de désactiver cette option pour permeetre
* un versement même si le solde est insuffisant
*
if ($treasuryCashRegister->toForward($treasuryCashRegisterRepository) < $treasuryPayment->getAmount()) {
$this->addFlash('warning', "Le solde de la caisse est insuffisant.");
return $this->redirectToRoute('treasury_cash_register_show', ['id' => $treasuryCashRegister->getId()]);
}
*/
$treasuryPayment->setCashRegister($treasuryCashRegister);
$treasuryPayment->setEstablishment($treasuryCashRegister->getEstablishment());
$treasuryPayment->setSchoolYear($treasuryCashRegister->getSchoolYear());
$entityManager->persist($treasuryPayment);
//notification fondateur
$foundingNotification = new FoundingNotification();
$foundingNotification->setCreateDate(new DateTimeImmutable());
$foundingNotification->setAction("Versement");
$foundingNotification->setElement("Versement: #".$treasuryCashRegister->getCode());
$foundingNotification->setDescription("Versement: #".$treasuryCashRegister->getCode().' Mt: '.number_format($treasuryPayment->getAmount(), 0, ',', ' ').' Date: '.$foundingNotification->getCreateDate()->format('d/m/Y'));
$foundingNotification->setEstablishment($treasuryPayment->getEstablishment());
$foundingNotification->setIpAddress($request->getClientIp());
$foundingNotification->setSchoolYear($treasuryPayment->getSchoolYear());
$foundingNotification->setUsername($this->getUser()->getUserIdentifier());
$entityManager->persist($foundingNotification);
/*---------------------------------------------*/
try {
$entityManager->flush();
$this->addFlash('success', "Un versement à été ajouté.");
$message = "La caissiere ".$treasuryCashRegister->getCheckout()->getLabel()." vient d'effectuer un versement a la banque. Vous etes prie de bien vouloir valider, Montant : ".number_format($treasuryPayment->getAmount(), 0, ',', ' ').' Date: '.$foundingNotification->getCreateDate()->format('d/m/Y');
$contacts = [];
//$contacts[] = '225'.str_replace(' ', '',trim('0748808080'));
//$contacts[] = '225'.str_replace(' ', '',trim('0749565147'));
//$contacts[] = '225'.str_replace(' ', '',trim('0757357534'));
$contacts[] = '225'.str_replace(' ', '',trim($establishment->getSmsBankPayment()));
$response = $smsSender->sendSmsByEstablishment($establishment, $message, $contacts, $smsSender::UNICODE_CHARSET);
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreure est survenue lors de l'ajout du versement.");
$this->addFlash('info', $th->getMessage());
}
return $this->redirectToRoute('treasury_cash_register_show', ['id' => $treasuryCashRegister->getId()]);
}
// depenses
$expenses = $accountingExpenseRepository->createQueryBuilder('entity')
->innerJoin('entity.establishment', 'establishment')
->addSelect('establishment')
->andWhere('establishment.establishmentGroup = :establishmentGroup')
->setParameter('establishmentGroup', $establishment->getEstablishmentGroup())
->andWhere('entity.is_aproved = :is_aproved')
->setParameter('is_aproved', 1)
->andWhere('entity.is_submited = :is_submited')
->setParameter('is_submited', 1)
/* ->andWhere('entity.is_paid = :is_paid')
->setParameter('is_paid', 0) */
->andWhere('entity.is_canceled = :is_canceled')
->setParameter('is_canceled', 0)
->orderBy('entity.id', 'DESC')
->getQuery()
->getResult();
return $this->renderForm('treasury/treasury_cash_register/show.html.twig', [
'treasury_cash_register' => $treasuryCashRegister,
'registrationStudentRegistrationUnsolds' => $registrationStudentRegistrationUnsolds,
'form' => $form,
'treasuryPaymentForm' => $treasuryPaymentForm,
'treasuryPayment' => $treasuryPayment,
'expenses' => $expenses,
'userRepository' => $userRepository,
'treasuryCashRegisterRepository' => $treasuryCashRegisterRepository,
]);
}
/**
* @Route("/shedul-payment/{id}", name="treasury_cash_register_shedul_payments", methods={"GET", "POST"})
*/
public function shedulPayment(Request $request, AccountingStudentRegistrationPayment $accountingStudentRegistrationPayment, AccountingStudentRegistrationFeeShedulRepository $accountingStudentRegistrationFeeShedulRepository): Response
{
if ($accountingStudentRegistrationPayment->isPrintable()) {
return $this->redirectToRoute('treasury_cash_register_shedul_payment_print', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
if ($request->get('_submitting') == $this->getUser()->getUserIdentifier()) {
if (($accountingStudentRegistrationPayment->getAmount() - $accountingStudentRegistrationPayment->getShedulAmount()) < floatval($request->get('_shedul_payment'))) {
$this->addFlash('warning', "Le montant doit être inférieur ou égale au reste à imputer.");
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
$accountingStudentRegistrationFeeShedul = $accountingStudentRegistrationFeeShedulRepository->find(intval($request->get('_feeId')));
if (null == $accountingStudentRegistrationFeeShedul) {
$this->addFlash('warning', "L'échéancié que vous souhaitez payer n'existe pas.");
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
$accountingStudentRegistrationFee = $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee();
$studentRegistration = $accountingStudentRegistrationPayment->getStudentRegistration();
if (floatval($request->get('_shedul_payment')) > 0) {
if(floatval($request->get('_shedul_payment')) <= $accountingStudentRegistrationFeeShedul->getAmountRest()){
$accountingStudentRegistrationFeeShedulPayment = new AccountingStudentRegistrationFeeShedulPayment();
$accountingStudentRegistrationFeeShedulPayment->setCode('PE-'.$accountingStudentRegistrationPayment->getId().'-'.time());
$accountingStudentRegistrationFeeShedulPayment->setLabel('PE-'.$accountingStudentRegistrationPayment->getId().'-'.time());
$accountingStudentRegistrationFeeShedulPayment->setAmount(floatval($request->get('_shedul_payment')));
$accountingStudentRegistrationFeeShedulPayment->setCashRegister($accountingStudentRegistrationPayment->getCashRegister());
$accountingStudentRegistrationFeeShedulPayment->setEstablishment($accountingStudentRegistrationPayment->getEstablishment());
$accountingStudentRegistrationFeeShedulPayment->setStudentRegistrationFeeShedul($accountingStudentRegistrationFeeShedul);
$accountingStudentRegistrationFeeShedulPayment->setStudentRegistrationFee($accountingStudentRegistrationFee);
$accountingStudentRegistrationFeeShedulPayment->setStudentRegistrationPayment($accountingStudentRegistrationPayment);
$accountingStudentRegistrationFeeShedulPayment->setAccountingAccount($accountingStudentRegistrationFee->getFee()->getAccountingAccount());
$accountingStudentRegistrationFeeShedul->setAmountPaid($accountingStudentRegistrationFeeShedul->getAmountPaid() + floatval($request->get('_shedul_payment')));
$accountingStudentRegistrationFeeShedul->setLastPaymentAt(new DateTimeImmutable());
$accountingStudentRegistrationFeeShedul->setLastAmountPaid(floatval($request->get('_shedul_payment')));
$accountingStudentRegistrationFeeShedul->setLastPaymentId($accountingStudentRegistrationPayment->getId());
$accountingStudentRegistrationFee->setLastPaymentAt(new DateTimeImmutable());
$accountingStudentRegistrationFee->setLastAmountPaid(floatval($request->get('_shedul_payment')));
$accountingStudentRegistrationFee->setLastPaymentId($accountingStudentRegistrationPayment->getId());
$studentRegistration->setLastPaymentAt(new DateTimeImmutable());
$studentRegistration->setLastAmountPaid(floatval($request->get('_shedul_payment')));
$studentRegistration->setLastPaymentId($accountingStudentRegistrationPayment->getId());
$cashMovement = new TreasuryCashMovement();
$cashMovement->setAmount(floatval($request->get('_shedul_payment')));
$cashMovement->setAccountingStudentRegistrationFeeShedulPayment($accountingStudentRegistrationFeeShedulPayment);
$cashMovement->setCashRegister($accountingStudentRegistrationPayment->getCashRegister());
$cashMovement->setCode($accountingStudentRegistrationPayment->getId().'-'.time());
$cashMovement->setEstablishment($accountingStudentRegistrationPayment->getEstablishment());
$cashMovement->setType('in');
$cashMovement->setLabel('paiement écheancié'.$accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getFee()->getLabel());
$cashMovement->setAccountingStudentRegistrationFeeShedulPayment($accountingStudentRegistrationFeeShedulPayment);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($accountingStudentRegistrationFeeShedulPayment);
$entityManager->persist($cashMovement);
try {
$entityManager->flush();
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreur s'est produite.");
$this->addFlash('info', $th->getMessage());
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
}else {
$this->addFlash('warning', "Le montant doit être inférieur ou égale au reste de l'échéancié.");
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
}else{
$this->addFlash('warning', "Le montant doit être supérieur à zéro(0).");
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
}
return $this->render('treasury/treasury_cash_register/shedul_payments.html.twig', [
'accounting_student_registration_payment' => $accountingStudentRegistrationPayment,
'treasury_cash_register' => $accountingStudentRegistrationPayment->getCashRegister(),
'student_registration' => $accountingStudentRegistrationPayment->getStudentRegistration(),
'student_registration_fees' => $accountingStudentRegistrationPayment->getStudentRegistration()->getAccountingStudentRegistrationFees(),
]);
}
/**
* @Route("/shedul-payment/{id}/print", name="treasury_cash_register_shedul_payment_print", methods={"GET"})
*/
public function print(Pdf $knpSnappyPdf, AccountingStudentRegistrationPayment $accountingStudentRegistrationPayment, AccountingStudentRegistrationFeeShedulRepository $accountingStudentRegistrationFeeShedulRepository, UserRepository $userRepository)
{
if (!$accountingStudentRegistrationPayment->isPrintable()) {
$this->addFlash('warning', "Veuillez terminer les imputations avant d'imprimer.");
return $this->redirectToRoute('treasury_cash_register_shedul_payments', ['id' => $accountingStudentRegistrationPayment->getId()]);
}
$setting = $accountingStudentRegistrationPayment->getEstablishment();
$html = $this->renderView('treasury/treasury_cash_register/print/payment.html.twig', [
'ref' => sprintf("%'04s", $accountingStudentRegistrationPayment->getId()),
'accounting_student_registration_payment' => $accountingStudentRegistrationPayment,
'student_registration' => $accountingStudentRegistrationPayment->getStudentRegistration(),
'student_registration_fees' => $accountingStudentRegistrationPayment->getStudentRegistration()->getAccountingStudentRegistrationFees(),
'school_year' => $accountingStudentRegistrationPayment->getSchoolYear(),
'userRepository' => $userRepository,
'setting' => $setting,
]);
$file_name = 'RECU_N_'.$accountingStudentRegistrationPayment->getStudentRegistration()->getStudent()->getCode().'_'.$accountingStudentRegistrationPayment->getCode().".pdf";
$footer = $this->renderView('print/footer.html.twig', ['setting' => $setting]);
$header = $this->renderView('print/header.html.twig', ['setting' => $setting]);
$options = [
'orientation' => 'Portrait',
/*'header-html' => $header
'footer-html' => $footer*/
];
try {
$knpSnappyPdf->generateFromHtml($html, $this->getParameter('app.app_directory').'/downloads/cashRegister/' . $file_name, $options, true);
} catch (\Throwable $th) {
$this->addFlash('info', "Ce reçu à déjà été imprimé.");
}
$id = $accountingStudentRegistrationPayment->getCashRegister()->getId();
return $this->redirectToRoute('preview_payment', [
'id' => $id,
'file' => $file_name,
'dir' => 'cashRegister'
]);
}
/**
* @Route("/{id}/edit", name="treasury_cash_register_edit", methods={"GET","POST"})
*/
public function edit(Request $request, TreasuryCashRegister $treasuryCashRegister): Response
{
$form = $this->createForm(TreasuryCashRegisterType::class, $treasuryCashRegister);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', "le releve à été édité.");
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreure est survenue lors de l'édition du releve.");
$this->addFlash('info', $th->getMessage());
}
return $this->redirectToRoute('treasury_cash_register_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('treasury/treasury_cash_register/edit.html.twig', [
'treasury_cash_register' => $treasuryCashRegister,
'form' => $form,
]);
}
/**
* @Route("/delete-selection", name="treasury_cash_registers_selected_delete", methods={"GET"})
*/
public function deleteSelected(Request $request, TreasuryCashRegisterRepository $entityRepository): Response
{
$list = $request->get('entities');
$entityManager = $this->getDoctrine()->getManager();
$errors = 0;
foreach ($list as $key => $id) {
$entity = $entityRepository->findOneBy(['id' => intval($id)], []);
if ($entity != null) {
if (count($entity->getAccountingStudentRegistrationPayments()) <= 0) {
$entityManager->remove($entity);
}
}
}
try {
$entityManager->flush();
$this->addFlash('success', "Traitement effectué");
return $this->json(['code' => 200, 'message' => "Traitement effectué :)"], 200);
} catch (\Throwable $th) {
//$th->getMessage()
$this->addFlash('warning', $th->getMessage());
}
$this->addFlash('warning', "Traitement non effectué");
return $this->json(['code' => 500, 'message' => "Traitement non effectué"], 200);
}
/**
* @Route("/close/{id}", name="treasury_cash_register_close", methods={"GET"})
*/
public function close(TreasuryCashRegister $entity): Response
{
foreach ($entity->getAccountingStudentRegistrationPayments() as $key => $studentRegistrationPayment) {
if ($studentRegistrationPayment->isCanBeShow()) {
return $this->json(['code' => 500, 'message' => "Tous les paiement n'ont pas été traités correctement."], 200);
}
}
$entityManager = $this->getDoctrine()->getManager();
$entity->setIsClosed(true);
try {
$entityManager->flush();
//$this->addFlash('success', "Paiement supprimé");
return $this->json(['code' => 200, 'message' => "Caisse clôturé :)"], 200);
} catch (\Throwable $th) {
$this->addFlash('warning', $th->getMessage());
return $this->json(['code' => 500, 'message' => "Une erreur s'est produite !)"], 200);
}
return $this->json(['code' => 500, 'message' => "Caisse non clôturé"], 200);
}
/**
* @Route("/reOpen/{id}", name="treasury_cash_register_reopen", methods={"GET"})
*/
public function reOpen(TreasuryCashRegister $entity): Response
{
foreach ($entity->getAccountingStudentRegistrationPayments() as $key => $studentRegistrationPayment) {
if ($studentRegistrationPayment->isCanBeShow()) {
return $this->json(['code' => 500, 'message' => "Tous les paiement n'ont pas été traités correctement."], 200);
}
}
$entityManager = $this->getDoctrine()->getManager();
$entity->setIsClosed(false);
try {
$entityManager->flush();
//$this->addFlash('success', "Paiement supprimé");
return $this->json(['code' => 200, 'message' => "Caisse ré-ouverte :)"], 200);
} catch (\Throwable $th) {
$this->addFlash('warning', $th->getMessage());
return $this->json(['code' => 500, 'message' => "Une erreur s'est produite !)"], 200);
}
return $this->json(['code' => 500, 'message' => "Caisse non clôturé"], 200);
}
/**
* @Route("/re-print-all/{id}", name="treasury_cash_register_re_print", methods={"GET"})
*/
public function rePrint(Pdf $knpSnappyPdf, SettingClassroom $settingClassroom, AccountingStudentRegistrationPaymentRepository $accountingStudentRegistrationPaymentRepository, UserRepository $userRepository)
{
$accountingStudentRegistrations = $accountingStudentRegistrationPaymentRepository->createQueryBuilder('entity')
->innerJoin('entity.studentRegistration', 'studentRegistration')
->addSelect('studentRegistration')
->andWhere('studentRegistration.classroom = :classroom')
->setParameter('classroom', $settingClassroom)
->getQuery()
->getResult();
$setting = $this->getUser()->getEstablishment();
$html = $this->renderView('treasury/treasury_cash_register/print/payments.html.twig', [
'accounting_student_registration_payments' => $accountingStudentRegistrations,
'userRepository' => $userRepository,
'setting' => $this->getUser()->getEstablishment(),
]);
$file_name = "RECUS_".date('dmY_Hi_').".pdf";
$footer = $this->renderView('print/footer.html.twig', ['setting' => $setting]);
$header = $this->renderView('print/header.html.twig', ['setting' => $setting]);
$options = [
'orientation' => 'Portrait',
'header-html' => $header
//'footer-html' => $footer
];
return new PdfResponse(
$knpSnappyPdf->getOutputFromHtml($html, $options),
$file_name,
'application/pdf',
'attachment'
);
}
}