src/Controller/flutterapi/CommandeController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\flutterapi;
  3. use App\Entity\Commande;
  4. use App\Repository\CommandeRepository;
  5. use App\Repository\TmpVolumeRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class CommandeController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/commandes", name="agent_commandes", methods={"POST","GET"})
  14.      */
  15.     public function getCommandes(Request $request, TmpVolumeRepository $volumeRepository, CommandeRepository $commandeRepo): JsonResponse
  16.     {
  17.         return $this->processCommandes($request, $volumeRepository, $commandeRepo, true);
  18.     }
  19.     /**
  20.      * @Route("/commandTexte", name="agent_commandes_txt", methods={"POST","GET"})
  21.      */
  22.     public function getCommandet(Request $request, TmpVolumeRepository $volumeRepository, CommandeRepository $commandeRepo): JsonResponse
  23.     {
  24.         return $this->processCommandes($request, $volumeRepository, $commandeRepo, false);
  25.     }
  26.     /**
  27.      * Traitement centralisé basé sur la structure stable de getCommandet
  28.      */
  29.     private function processCommandes(
  30.         Request $request,
  31.         TmpVolumeRepository $volumeRepository,
  32.         CommandeRepository $commandeRepo,
  33.         bool $includeCommentaire
  34.     ): JsonResponse {
  35.         $data = json_decode($request->getContent(), true) ?? [];
  36.         $contact = $request->get('contact') ?? $data['contact'] ?? null;
  37.         $contact2 = $request->get('contact2') ?? $data['contact2'] ?? null;
  38.         if (!$contact) {
  39.             return $this->json(['status' => 'error', 'message' => 'Contact manquant'], 400);
  40.         }
  41.         $contact2 = ($contact2 !== null && strlen(trim($contact2)) > 10) ? trim($contact2) : null;
  42.         $queryBuilder = $commandeRepo->createQueryBuilder('c')
  43.             ->join('c.IdCtPr', 'p');
  44.         if ($contact2) {
  45.             $queryBuilder->where('c.contact = :num1 OR c.contact = :num2')
  46.                 ->andWhere('p.validation = :validation')
  47.                 ->setParameter('validation', true)
  48.                 ->setParameter('num1', $contact)
  49.                 ->setParameter('num2', $contact2);
  50.         } else {
  51.             $queryBuilder->where('c.contact = :num1')
  52.                 ->andWhere('p.validation = :validation')
  53.                 ->setParameter('validation', true)
  54.                 ->setParameter('num1', $contact);
  55.         }
  56.         /** @var Commande[] $commandes */
  57.         $commandes = $queryBuilder->orderBy('c.DateCommande', 'DESC')
  58.             ->setMaxResults(100)
  59.             ->getQuery()
  60.             ->getResult();
  61.         if (empty($commandes)) {
  62.             return $this->json(['status' => 'success', 'data' => []]);
  63.         }
  64.         $result = [];
  65.         foreach ($commandes as $cmd) {
  66.             $situation = $volumeRepository->situationFacture($cmd->getId());
  67.             $flag = '0';
  68.             $paiementSituationData = [];
  69.             if (!empty($situation)) {
  70.                 $sit = $situation[0];
  71.                 $totalPayer = floatval($sit['dejaPaie'] ?? 0) +
  72.                     floatval($sit['paieEnAvance'] ?? 0) +
  73.                     floatval($sit['compassation'] ?? 0) +
  74.                     floatval($sit['banque'] ?? 0) +
  75.                     floatval($sit['reduction'] ?? 0);
  76.                 $prixTotal = floatval($sit['pritotal'] ?? 0);
  77.                 if ($totalPayer > 0 && $totalPayer < $prixTotal) {
  78.                     $flag = '1';
  79.                 } elseif ($totalPayer >= $prixTotal && $prixTotal > 0) {
  80.                     $flag = '2';
  81.                 }
  82.                 $paiementSituationData[] = [
  83.                     'pritotal' => $prixTotal,
  84.                     'pritotalPayer' => $totalPayer,
  85.                     'dejaPaie' => $sit['dejaPaie'] ?? 0,
  86.                     'paieEnAvance' => $sit['paieEnAvance'] ?? 0,
  87.                     'banque' => $sit['banque'] ?? 0,
  88.                     'compassation' => $sit['compassation'] ?? 0,
  89.                     'reduction' => $sit['reduction'] ?? 0,
  90.                     'etat' => $flag,
  91.                 ];
  92.             }
  93.             $arrayConteneurEvolution = [];
  94.             $idCtPr = $cmd->getIdCtPr();
  95.             if ($idCtPr) {
  96.                 foreach ($idCtPr->getSuiviContenaires() as $itemSous) {
  97.                     $arraySousSuivi = [];
  98.                     foreach ($itemSous->getSousSuivis() as $sub) {
  99.                         if ($sub->getSousPosition()) {
  100.                             $subItem = [
  101.                                 'id' => $sub->getId(),
  102.                                 'libelle' => $sub->getSousPosition()->getLibelle(),
  103.                                 'date' => $sub->getCreatedAt() ? $sub->getCreatedAt()->format('d-m-Y H:i') : null,
  104.                             ];
  105.                             if ($includeCommentaire) {
  106.                                 $subItem['commentaire'] = $sub->getCommentaire();
  107.                             }
  108.                             $arraySousSuivi[] = $subItem;
  109.                         }
  110.                     }
  111.                     $arrayConteneurEvolution[] = [
  112.                         'lieuPosition' => $itemSous->getLieu(),
  113.                         'isPositionActuelle' => $itemSous->getPositionActuelleFlag(),
  114.                         'date' => $itemSous->getDateOperation() ? $itemSous->getDateOperation()->format('d-m-Y') : null,
  115.                         'sousSuivi' => $arraySousSuivi,
  116.                     ];
  117.                 }
  118.             }
  119.             $arrayConteneurRetrait = [];
  120.             foreach ($cmd->getDepots() as $itemEv) {
  121.                 $arrayConteneurRetrait[] = [
  122.                     'id' => $itemEv->getId(),
  123.                     'nom' => $itemEv->getNoms(),
  124.                     'quantite' => $itemEv->getQuantite(),
  125.                     'remarque' => $itemEv->getRemarque(),
  126.                     'dateSortie' => $itemEv->getSortieAt() ? $itemEv->getSortieAt()->format('d-m-Y') : null,
  127.                     'telephone' => $itemEv->getTelephone(),
  128.                     'user' => '',
  129.                     'plaque' => $itemEv->getPlaque(),
  130.                 ];
  131.             }
  132.             $aliasClient = $cmd->getAliasClient();
  133.             $al = (is_string($aliasClient) && strlen($aliasClient) >= 12) ? '(' . $aliasClient . ')' : '';
  134.             $nomAg = $cmd->getIdCl() ? $cmd->getIdCl()->getNomAg() : '';
  135.             $result[] = [
  136.                 'id' => $cmd->getId(),
  137.                 'alias' => trim($nomAg . ' ' . $al),
  138.                 'date' => $cmd->getDateCommande() ? $cmd->getDateCommande()->format('Y-m-d') : null,
  139.                 'total' => $cmd->getTotalAPaiyer(),
  140.                 'balance' => $cmd->getBalance(),
  141.                 'is_paid' => $flag,
  142.                 'ville' => $idCtPr ? $idCtPr->getDestination() : null,
  143.                 'remarque' => $cmd->getTotalCOLISNature(),
  144.                 'netApayer' => $cmd->getTotalAPaiyer(),
  145.                 'totalCBM' => $cmd->getTotalCBM(),
  146.                 'totalTONNE' => ($idCtPr && $idCtPr->isIsMco()) ? $cmd->getTotalKG() : $cmd->getTotalTONNE(),
  147.                 'totalCOLIS' => $cmd->getTotalCOLIS(),
  148.                 'pdf_url' => 'https://a.afisarl.com/commande/edfr327edh' . $cmd->getId() . 'iuery782efnvj/PrintFacture',
  149.                 'paiementSituation' => $paiementSituationData,
  150.                 'EvolutionPosition' => $arrayConteneurEvolution,
  151.                 'depots' => $arrayConteneurRetrait,
  152.                 'isMco' => $idCtPr ? $idCtPr->isIsMco() : false,
  153.                 'numTel' => $cmd->getContact(),
  154.                 'numeroConteneur' => ($idCtPr && $idCtPr->getIdCt()) ? $idCtPr->getIdCt()->getNomContenaire() : null,
  155.                 'dateChargement' => ($idCtPr && $idCtPr->getDateChargement()) ? $idCtPr->getDateChargement()->format('d-m-Y') : null,
  156.                 'dateDepart' => ($idCtPr && $idCtPr->getDateDepart()) ? $idCtPr->getDateDepart()->format('d-m-Y') : null,
  157.                 'dateArriver' => ($idCtPr && $idCtPr->getDateArriver()) ? $idCtPr->getDateArriver()->format('d-m-Y') : null,
  158.                 'typeConteneur' => $idCtPr ? $idCtPr->getCompletFlag() : null,
  159.                 'transporteur' => $idCtPr ? $idCtPr->getTransporteur() : null,
  160.                 'provenance' => ($idCtPr && $idCtPr->getIdVille()) ? $idCtPr->getIdVille()->getNomville() : null,
  161.                 'destination' => $idCtPr ? $idCtPr->getDestination() : null,
  162.                 'receptionNumero' => (string) $cmd->getRecuCargo(),
  163.             ];
  164.         }
  165.         return $this->json(['status' => 'success', 'data' => $result]);
  166.     }
  167. }