src/Controller/flutterapi/ReceptionController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\flutterapi;
  3. use App\Repository\ReceptionRepository;
  4. use App\Repository\AgentRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Repository\PaiementReceptionRepository; // À ajouter en haut de votre fichier
  11. class ReceptionController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/receptions", name="agent_receptions", methods={"POST","GET"})
  15.      */
  16.     public function getReceptions(
  17.         Request $request,
  18.         RequestStack $requestStack,
  19.         ReceptionRepository $receptionRepo,
  20.         AgentRepository $agentRepo,
  21.         PaiementReceptionRepository $paiementRepo
  22.     ): JsonResponse {
  23.         $data = json_decode($request->getContent(), true);
  24.         $codeComplet = $data['code'] ?? null;
  25.         if (!$codeComplet) {
  26.             return $this->json(['status' => 'error', 'message' => 'Code agent manquant'], 400);
  27.         }
  28.         // Extraction du préfixe (lettres) et du code (chiffres)
  29.         preg_match('/([a-zA-Z]+)(\d+)/', $codeComplet, $matches);
  30.         if (count($matches) < 3) {
  31.             return $this->json(['status' => 'error', 'message' => 'Format de code invalide'], 400);
  32.         }
  33.         $prefixe = $matches[1];
  34.         $codeNumerique = $matches[2];
  35.         $agent = $agentRepo->findOneBy([
  36.             'prefixcode' => $prefixe,
  37.             'code' => $codeNumerique
  38.         ]);
  39.         if (!$agent) {
  40.             return $this->json(['status' => 'error', 'message' => 'Agent introuvable'], 404);
  41.         }
  42.         $receptions = $receptionRepo->findBy(['client' => $agent], ['dateOperation' => 'DESC'], 50);
  43.         $result = [];
  44.         foreach ($receptions as $rec) {
  45.             $totalPaye = 0;
  46.             $montantTotalAPayer = 0;
  47.             foreach ($rec->getTmpReceptions() as $tmpRec) {
  48.                 // 1. Récupération du total payé pour cette ligne d'élément
  49.                 $totalPaye += $paiementRepo->getSommePayeeParTmpReception($tmpRec->getId());
  50.                 // 2. Alignement strict du Calcul de Prix Unitaire
  51.                 // Priorité : prixFixe -> categoriePrix.prix -> 0 (Fallback à fret si non défini)
  52.                 $prixUnitaire = 0;
  53.                 if ($tmpRec->getPrixFixe() !== null && (float) $tmpRec->getPrixFixe() > 0) {
  54.                     $prixUnitaire = (float) $tmpRec->getPrixFixe();
  55.                 } elseif ($tmpRec->getCategoriePrix()) {
  56.                     $prixUnitaire = (float) $tmpRec->getCategoriePrix()->getPrix();
  57.                 } 
  58.                 // 3. Quantité / Mesure retenue (Alignée sur Twig : TONNE = tonne, Sinon = volume)
  59.                 $quantiteMesure = ($tmpRec->getUnite() === 'TONNE')
  60.                     ? (float) $tmpRec->getTonne()
  61.                     : (float) $tmpRec->getVolume();
  62.                 // Total dû par article
  63.                 $montantTotalAPayer += ($quantiteMesure * $prixUnitaire);
  64.             }
  65.             // 4. Calcul du solde restant
  66.             $soldeReste = max(0, $montantTotalAPayer - $totalPaye);
  67.             // 5. Détermination du statut de paiement
  68.             if ($totalPaye >= $montantTotalAPayer && $montantTotalAPayer > 0) {
  69.                 $statutPaiement = 'PAID';    // Payé entièrement
  70.                 $isPaye = '1';
  71.             } elseif ($totalPaye > 0) {
  72.                 $statutPaiement = 'PARTIAL'; // Payé partiellement
  73.                 $isPaye = '2';
  74.             } else {
  75.                 $statutPaiement = 'UNPAID';  // Non payé
  76.                 $isPaye = '0';
  77.             }
  78.             $result[] = [
  79.                 'id' => $rec->getId(),
  80.                 'numero_recu' => $rec->getRecunumero(),
  81.                 'date' => $rec->getDateOperation()->format('Y-m-d H:i'),
  82.                 'type' => $rec->getType(),
  83.                 'fret' => $rec->getFret(),
  84.                 'destination' => $rec->getDestville() ? $rec->getDestville()->getNomville() : null,
  85.                 'is_charge' => $rec->getCharge(),
  86.                 'colis_count' => $rec->getTotalColis(),
  87.                 'poids' => $rec->getTotalKG(),
  88.                 'pdf_url' => 'https://a.afisarl.com/reception/Print_receipt_reception/dwb45'.$rec->getId().'45%23%23',
  89.                 'code' => (string) $rec->getClient()->getCode(),
  90.                 'recu' => $rec->getRecunumero(),
  91.                 'cbm' => (string) $rec->getWerehouse() == 'AIR' ? 'KG' :  'CBM',
  92.                 'tonne' => 'TONE',
  93.                 'volume' => (string) $rec->getTotalCBM(),
  94.                 'tonnage' => (string) $rec->getTotalTONNE(),
  95.                 'number' => $rec->getNumero(),
  96.                 'trucking' => $rec->getTruckingNumber(),
  97.                 'werehouse' => $rec->getWerehouse(),
  98.                 'alias' => $rec->getAlias(),
  99.                 'quantite' => (string) $rec->getTotalColis(),
  100.                 'numerodepot' => '########',
  101.                 'emballage' => $rec->getConteneurtype(),
  102.                 'typeColis' => $rec->getType(),
  103.                 'destId' => $rec->getDestville() ? $rec->getDestville()->getId() : null,
  104.                 'ProvId' => $rec->getVille() ? (string) $rec->getVille()->getId() : null,
  105.                 'naturecolis' => $rec->getTotalNatureColis(),
  106.                 'image1' => $rec->getImageBon() != 'image' ? 'https://a.afisarl.com/file/agents/reception/'.$rec->getDateOperation()->format('d-m-Y').'-'.$rec->getRecunumero().'/'.$rec->getImageBon() : "image",
  107.                 'image2' => $rec->getImageColis() != 'image' ? 'https://a.afisarl.com/file/agents/reception/'.$rec->getDateOperation()->format('d-m-Y').'-'.$rec->getRecunumero().'/'.$rec->getImageColis() : "image",
  108.                 'client' => (string) $rec->getClient()->getId(),
  109.                 'provenance' => $rec->getVille() ? (string) $rec->getVille()->getNomville() : null,
  110.                 'supplier' => (string) $rec->getSupplier(),
  111.                 'nomClient' => $rec->getClient()->getNomAg(),
  112.                 'charge' => (string) $rec->getCharge(),
  113.                 'numeroRecuStore' => (string) $rec->getNumeroRecuStore(),
  114.                 'montantValeurs' => (string) $rec->getMontantValeur(),
  115.                 'ui' => (string) $rec->getFlutterid(),
  116.                 'isSave' => '1',
  117.                 // Données de paiement harmonisées
  118.                 'montantTotal'   => (string) number_format($montantTotalAPayer, 2, '.', ''),
  119.                 'montantPaye'    => (string) number_format($totalPaye, 2, '.', ''),
  120.                 'resteAPayer'    => (string) number_format($soldeReste, 2, '.', ''),
  121.                 'statutPaiement' => $statutPaiement,
  122.                 'isPaye'         => $isPaye,
  123.             ];
  124.         }
  125.         return $this->json(['status' => 'success', 'data' => $result]);
  126.     }
  127. }