<?php
namespace App\Controller\GestionComerciale;
use App\Entity\Clients\Client;
use App\Entity\GestionComerciale\Acompte;
use App\Entity\GestionComerciale\Commande;
use App\Entity\GestionComerciale\MouvementCaisse;
use App\Entity\GestionComerciale\TypeReglement;
use App\Form\GestionComerciale\AcompteType;
use App\Form\GestionComerciale\SupprimerAcompteType;
use App\Library\Datatable\Util\Datatable;
use App\Model\GestionCommerciale\TypeDocumentCommercial;
use App\Security\Voter\EntityVoter;
use App\Service\GestionComerciale\AcompteService;
use App\Service\GestionComerciale\CommandeService;
use App\Service\GestionComerciale\FactureService;
use App\Service\GestionComerciale\MouvementCaisseService;
use App\Service\GestionComerciale\NumerotationDocumentService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class AcompteController extends AbstractController
{
/**
* @Route("/vente/acompte/nouveau/{id}/{type}/{redirection}", name="dtc_acompte_ajouter")
*/
public function ajouterAction(Request $request, $id, $type = "", $redirection = "", EntityManagerInterface $em, NumerotationDocumentService $numerotationService,
AcompteService $acompteService, TranslatorInterface $translator, ValidatorInterface $validator, CommandeService $commandeService
) {
$titre_modal = $translator->trans("Nouvel acompte");
$acompte = new Acompte();
$repo_acompte = $em->getRepository(Acompte::class);
$type_acompte = $em->getRepository(TypeReglement::class)->find(1);
$acompte->setTypeReglement($type_acompte);
$user = $this->getUser();
$acompte->setUtilisateur($user);
$repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
$acompte->setCommande($objet);
//echo "<div>".$objet->getReference()."</div>";
if (is_object($objet->getCommande())) {
$acompte->setParent($objet->getCommande());
}
$client = $objet->getClient();
if ( ! is_object($client)) {
$titre_modal = $translator->trans('Informations');
$rendu = '<div class="modal-body modal_rouge"> <div class="alert alert-danger alert-dismissable text-center">Merci de sélectionner un client</div> </div><div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button></div>';
return new Response(json_encode(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]), 200, ['Content-Type' => 'application/json']);
}
$delai = $client->getDelaiPaiement();
$acompte->setDelaiPaiement($delai);
$acompte->setClient($client);
$moderegle = $client->getmodeReglement();
//echo "HELLO ".$acompte->getModeReglement()->getId();
//$acompte->setModeReglement($moderegle);
$acompte->setModeReglement(null);
$form = $this->createForm(AcompteType::class, $acompte);
$errors = "";
$form->handleRequest($request);
$avoirs = $repo_acompte->getAcomptesNonUtilisésTotalement($client);
if ($form->isSubmitted()) {
$objet_redirection = $repo_objet->find($redirection);
if ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::COMMANDE) {
$url = $this->generateUrl('dtc_commande_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::ORDREREPARATION) {
$url = $this->generateUrl('dtc_ordre_reparation_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::BP) {
$url = $this->generateUrl('dtc_bon_preparation_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::BL) {
$url = $this->generateUrl('dtc_bon_livraison_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::FACTURE) {
$url = $this->generateUrl('dtc_facture_afficher', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
}
$avoirsAutiliser = $request->request->get('selectsAvoir');
if (is_array($avoirsAutiliser) && count($avoirsAutiliser) > 0) {
foreach ($avoirsAutiliser as $avoirAutiliserId) {
$avoirAutiliser = $repo_acompte->find($avoirAutiliserId);
if (is_object($avoirAutiliser)) {
$montantAutiliser = $request->request->get('montantAvoir_'.$avoirAutiliserId);
if ($montantAutiliser == $avoirAutiliser->getMontant()) {
//$avoirAutiliser->setCommentaire('Utilisation totale : '.$montantAutiliser);
$avoirAutiliser->setCommande($objet);
$avoirAutiliser->setTypeReglement($type_acompte);
$avoirAutiliser->setCommentaire("Acompte de ".$montantAutiliser."€ versé pour la Commande : ".$objet->getReference()."<br/>".$avoirAutiliser->getCommentaire());
} else {
//$avoirAutiliser->setCommentaire('Utilisation partielle : '.$montantAutiliser);
$avoirAutiliser->setEstParent(true);
for ($a = 0; $a < 2; $a++) {
$acompteTmp = new Acompte();
$acompteTmp->setUtilisateur($user);
if ($a == 0) {
$montant_acompte = $montantAutiliser;
//$montant_acompte = 10;
$acompteTmp->setCommande($objet);
$acompteTmp->setCommentaire("Acompte de ".$montant_acompte."€ versé pour la Commande : ".$objet->getReference()."<br/>".$acompteTmp->getCommentaire());
} else {
$montant_acompte = $avoirAutiliser->getMontant() - $montantAutiliser;
$acompteTmp->setCommande(null);
$acompteTmp->setCommentaire("Reste dû de l'avoir ".$avoirAutiliser->getNumTraite());
}
$acompteTmp->setMontant($montant_acompte);
$acompteTmp->setAcompteParent($avoirAutiliser);
$acompteTmp->setTypeReglement($type_acompte);
$delai = $avoirAutiliser->getDelaiPaiement();
$acompteTmp->setDelaiPaiement($delai);
$moderegle = $avoirAutiliser->getmodeReglement();
$acompteTmp->setModeReglement($moderegle);
$nom = $objet->getClient()->getPrenom().' '.$objet->getClient()->getNom();
$acompteTmp->setNomClient($nom);
$acompteTmp->setClient($objet->getClient());
$acompteTmp->setNumTraite($numerotationService->generer($acompteTmp));
$em->persist($acompteTmp);
$em->flush();
}
}
$em->persist($avoirAutiliser);
$this->addFlash(
'notice',
$avoirAutiliser->getNumTraite().' a été déduit de cette commande !'
);
}
}
$em->flush();
}
if ($acompte->getMontant() != 0 && $acompte->getMontant() != '') {
if ($form->isValid()) {
$nom = $client->getPrenom().' '.$client->getNom();
$acompte->setCommentaire("Acompte de ".$acompte->getMontant()."€ versé pour la Commande : ".$objet->getReference()."<br/>".$acompte->getCommentaire());
$acompte->setNomClient($nom);
$acompte->setClient($client);
$acompte->setNumTraite($numerotationService->generer($acompte));
$em->persist($acompte);
$em->flush();
$client = $acompte->getClient();
if (is_object($client)) {
$soldeClient = $acompteService->getSoldeClient($client);
$acompte->setSolde($soldeClient);
$em->persist($acompte);
$em->flush();
}
if (is_object($acompte->getCommande())) {
//recalculer statut de paiement
$commandeService->changerStatutPaiement($acompte->getCommande());
}
$this->addFlash(
'notice',
$translator->trans('Acompte ajouté avec succès !')
);
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url]);
} else {
$errors = $validator->validate($acompte);
$rendu = $this->renderView(
'GestionComerciale/Acompte/ajouter.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'id' => $id, 'type' => $type, 'objet' => $objet, 'avoirs' => $avoirs]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
} else {
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url]);
}
}
$rendu = $this->renderView(
'GestionComerciale/Acompte/ajouter.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'id' => $id, 'type' => $type, 'objet' => $objet, 'avoirs' => $avoirs]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
//return $this->render('GestionComerciale/Acompte/ajouter.html.twig', array('form' => $form->createView(),'errors'=>$errors,'id'=>$id,'type'=>$type));
}
/**
* @Route("/vente/acompte/modifier/{objet}/{id}/{type}/{redirection}", name="dtc_acompte_modifier")
*/
public function modifierAction(Request $request, Acompte $objet, $id, $type, EntityManagerInterface $em, AcompteService $acompteService,
TranslatorInterface $translator, ValidatorInterface $validator
) {
$acompte = $objet;
$titre_modal = $translator->trans("Modifier acompte");
$user = $this->getUser();
$acompte->setUtilisateur($user);
$form = $this->createForm(AcompteType::class, $acompte);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
$droit = $this->isGranted(EntityVoter::UPDATE, Acompte::class);
if ( ! $droit) {
$rendu = $this->renderView(
'GestionComerciale/Acompte/ajouter.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'id' => $id, 'type' => $type, 'objet' => $objet]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
if ($form->isValid()) {
$repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
$acompte->setCommande($objet);
$url = $this->generateUrl('dtc_commande_modifier', ['id' => $objet->getId(), 'tab' => 'acomptes']);
$em->persist($acompte);
$em->flush();
$client = $acompte->getClient();
if (is_object($client)) {
$soldeClient = $acompteService->getSoldeClient($client);
$acompte->setSolde($soldeClient);
$em->persist($acompte);
$em->flush();
}
$this->addFlash(
'notice',
$translator->trans('Acompte modifié avec succès !')
);
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url]);
} else {
$errors = $validator->validate($acompte);
$rendu = $this->renderView(
'GestionComerciale/Acompte/ajouter.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'id' => $id, 'type' => $type, 'objet' => $objet]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Acompte/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'id' => $id, 'type' => $type, 'objet' => $objet]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("/vente/acompte/supprimer/{objet}/{id}/{type}/{redirection}", name="dtc_acompte_supprimer")
*/
public function supprimerAction(Request $request, Acompte $objet, $id, $type, $redirection, EntityManagerInterface $em, FactureService $serviceFacture,
TranslatorInterface $translator, ValidatorInterface $validator, MouvementCaisseService $mouvementCaisseService
) {
$acompte = $objet;
$titre_modal = $translator->trans("Demande de confirmation");
$user = $this->getUser();
$form = $this->createForm(SupprimerAcompteType::class, $acompte);
$errors = "";
$nonSupprimable = false;
$supprimable = true;
if ((is_object($objet->getMouvementCaisse()) && is_object($objet->getMouvementCaisse()->getDateTransfertCompta())) || (is_object($objet->getBordereau()))) {
$nonSupprimable = true;
$supprimable = false;
$this->addFlash(
'warning',
$translator->trans('Acompte non supprimable !')
);
}
$form->handleRequest($request);
$factures = [];
$reglementsEnfant = $acompte->getReglementsEnfant();
$nbEnfants = count($reglementsEnfant);
if ($nbEnfants > 0) {
foreach ($reglementsEnfant as $reglementEnfant) {
if (is_object($reglementEnfant) && is_object($reglementEnfant->getCommande())) {
$factures[] = $reglementEnfant->getCommande();
}
}
}
if (is_object($acompte->getCommande())) {
$factures[] = $acompte->getCommande();
}
if ($form->isSubmitted()) {
if ($form->isValid()) {
$reglementsEnfant = $acompte->getReglementsEnfant();
$nbEnfants = count($reglementsEnfant);
//$supprimable = true;
if ($nbEnfants > 0) {
foreach ($reglementsEnfant as $reglementEnfant) {
/*
if($reglementEnfant->getMontant() == $acompte->getMontant()){
$commande = $reglementEnfant->getCommande();
$em->remove($reglementEnfant);
$em->flush();
} else {
$supprimable = false;
}
*
*/
$commande = $reglementEnfant->getCommande();
$em->remove($reglementEnfant);
$em->flush();
if (is_object($commande) && is_int($commande->getTypeDocumentCommercial()) && in_array($commande->getTypeDocumentCommercial(), [TypeDocumentCommercial::FACTURE, TypeDocumentCommercial::AVOIR]
)) {//facture
$em->refresh($commande);
$serviceFacture->changerStatutPaiementFacture($commande);
}
}
}
$commande = $acompte->getCommande();
if ($supprimable) {
$repo_acompte = $em->getRepository(Acompte::class);
$enfants = $repo_acompte->findBy(["remboursement" => $acompte->getId()]);
if (count($enfants) > 0) {
foreach ($enfants as $e) {
$e->setRemboursement(null);
$em->persist($e);
}
}
/*
if(is_object($acompte->getCommande()) and is_object($acompte->getCommande()->getTypeDocumentCommercial()) and $acompte->getCommande()->getTypeDocumentCommercial() == TypeDocumentCommercial::FACTURE) {
$statutPaiement = $em->getRepository('DTCGestionComercialeBundle:StatutPaiement')->find(1);
$acompte->getCommande()->setStatutPaiement($statutPaiement);
$em->persist($acompte->getCommande());
}
*
*/
$em->remove($acompte);
$em->flush();
$retour = 'test';
if (is_object($commande) && is_int($commande->getTypeDocumentCommercial()) && in_array($commande->getTypeDocumentCommercial(), [TypeDocumentCommercial::FACTURE, TypeDocumentCommercial::AVOIR])) {//facture
$em->refresh($commande);
$serviceFacture->changerStatutPaiementFacture($commande);
}
//Si reglement en especes il faut supprimer le mouvement de caisse
if (is_object($acompte->getModeReglement()) && $acompte->getModeReglement()->getId() == 1) {
$repo_mvt_caisse = $em->getRepository(MouvementCaisse::class);
$mouvementCaisse = $repo_mvt_caisse->findOneByReglement($acompte);
//Il existe un mouvement de caisse
if (is_object($mouvementCaisse)) {
$mouvementCaisseService->supprimerMouvementCaisse($mouvementCaisse);
}
}
$flashMessage = 'Acompte supprimé avec succès !';
$flashType = 'notice';
/*
$this->addFlash(
'notice',
$translator->trans('Acompte supprimé avec succès !')
);
*
*/
} else {
/*
$this->addFlash(
'warning',
$translator->trans('Acompte non supprimable !')
);
*
*/
$flashMessage = 'Acompte non supprimable !';
$flashType = 'warning';
}
$repo_objet = $em->getRepository(Commande::class);
if ($type == 'client') {
$repo_objet = $em->getRepository(Client::class);
}
$objet_redirection = $repo_objet->find($redirection);
//echo "ddddd";
$callBackType = '';
$rendu = '';
$this->addFlash($flashType, $flashMessage);
if (is_object($objet_redirection)) {
//echo "eeeee";
if ($type == 'client') {
$url = $this->generateUrl('dtc_client_modifier', ['id' => $objet_redirection->getId()]);
} elseif (is_object($objet_redirection->getTypeDocumentCommercial()) and $objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::COMMANDE) {
$url = $this->generateUrl('dtc_commande_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif (is_object($objet_redirection->getTypeDocumentCommercial()) and $objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::ORDREREPARATION) {
$url = $this->generateUrl('dtc_ordre_reparation_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif (is_object($objet_redirection->getTypeDocumentCommercial()) and $objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::BP) {
$url = $this->generateUrl('dtc_bon_preparation_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif ($objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::BL) {
$url = $this->generateUrl('dtc_bon_livraison_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif (is_object($objet_redirection->getTypeDocumentCommercial()) and $objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::FACTURE) {
$url = $this->generateUrl('dtc_facture_afficher', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
} elseif (is_object($objet_redirection->getTypeDocumentCommercial()) and $objet_redirection->getTypeDocumentCommercial() == TypeDocumentCommercial::AVOIR) {
$url = $this->generateUrl('dtc_avoir_modifier', ['id' => $objet_redirection->getId(), 'tab' => 'acomptes']);
}
} else {
$url = $this->generateUrl('dtc_reglement_remise_banque_liste', ['type' => '']);
$callBackType = 'supprimer-reglement-datatable';
$rendu = $this->renderView(
'FO/Supprimer/supprimer.html.twig',
[
'form' => $form->createView(),
'errors' => $errors,
'id' => $id,
'type' => $type,
'objet' => $objet,
'factures' => $factures,
"nonSupprimable" => $nonSupprimable,
]
);
}
//echo "TEST".$url;
//exit;
return new JsonResponse(['rendu' => $rendu, 'valide' => '1', 'url' => $url, 'type' => $callBackType, 'id' => $acompte->getId()]);
} else {
$errors = $validator->validate($acompte);
$rendu = $this->renderView(
'FO/Supprimer/supprimer.html.twig',
[
'form' => $form->createView(),
'errors' => $errors,
'id' => $id,
'type' => $type,
'objet' => $objet,
'factures' => $factures,
"nonSupprimable" => $nonSupprimable,
]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView(
'FO/Supprimer/supprimer.html.twig',
[
'form' => $form->createView(),
'errors' => $errors,
'id' => $id,
'type' => $type,
'objet' => $objet,
'factures' => $factures,
"nonSupprimable" => $nonSupprimable,
]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("/vente/acompte/{id}/{type}/{redirection}", name="dtc_acompte_liste")
*/
public function listerAction(Request $request, $id, $type = "", $redirection, EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
$repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
$tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center", "targets" => [0], "visible" => true, "orderable" => false];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id text-right", "targets" => [1], "visible" => true, "orderable" => false];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [2], "visible" => true, "orderable" => false];
$tableau_class_cellule[] = ["orderable" => false, "className" => "colonne_id", "targets" => [3], "visible" => true];
$this->datatable($objet, $type, $redirection, $datatable, $translator);
return $this->render('GestionComerciale/Acompte/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $id, "type" => $type, "redirection" => $redirection]
);
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatable($objet, $type = "", $redirection = "", Datatable $datatable, TranslatorInterface $translator)
{
//$type_jointure = 'x.'.$type;
$datatable->setDatatableId('dta-acomptes')
->setEntity(Acompte::class, "x")
->setFields(
[
//$translator->trans("ID") => 'x.id',
$translator->trans("Date") => 'x.dateReglement',
$translator->trans("Montant") => 'x.montant',
$translator->trans("Mode de Règlement") => 'r.libelle',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.modeReglement', 'r', Join::LEFT_JOIN);
if ($type == 'multiBl') {
//$bls = $em->getRepository(Commande::class)->getBonsLivraison($objet->getCommande()->getId());
$bls = $objet->getBls();
$commandes = [];
foreach ($bls as $bl) {
$commandes[] = $bl->getCommande()->getId();
}
$datatable->addJoin('x.commande', 'c', Join::LEFT_JOIN);
$where = 'c.id IN (:commandes) and x.typeReglement = :typeReglement';
$parameters = ['commandes' => $commandes, 'typeReglement' => 1];
} else {
$where = 'x.'.$type.' = :objet and x.typeReglement = :typeReglement';
$parameters = ['objet' => $objet, 'typeReglement' => 1];
}
$datatable->setWhere(
$where,
$parameters
)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/heure.html.twig',
'params' => [],
],
1 => [
'view' => 'FO/DataTable/prix.html.twig',
'params' => [],
],
3 => [
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => [
//'edit_route' => 'dtc_acompte_modifier',
'supprimer_route' => 'dtc_acompte_supprimer',
'id' => $objet->getId(),
'type' => $type,
'redirection' => $redirection,
'entite' => 'adresse',
'objet' => Acompte::class,
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([1, 2]);
return $datatable;
}
/**
* @Route("/vente/acompte/grid/{id}/{type}/{redirection}", name="dtc_acompte_liste_grid")
*/
public function gridAction(Request $request, $id = "", $type = "", $redirection = "", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
$repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
return $this->datatable($objet, $type, $redirection, $datatable, $translator)->execute();
}
}