<?php
namespace App\Controller\flutterapi;
use App\Entity\Commande;
use App\Repository\CommandeRepository;
use App\Repository\TmpVolumeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CommandeController extends AbstractController
{
/**
* @Route("/commandes", name="agent_commandes", methods={"POST","GET"})
*/
public function getCommandes(Request $request, TmpVolumeRepository $volumeRepository, CommandeRepository $commandeRepo): JsonResponse
{
return $this->processCommandes($request, $volumeRepository, $commandeRepo, true);
}
/**
* @Route("/commandTexte", name="agent_commandes_txt", methods={"POST","GET"})
*/
public function getCommandet(Request $request, TmpVolumeRepository $volumeRepository, CommandeRepository $commandeRepo): JsonResponse
{
return $this->processCommandes($request, $volumeRepository, $commandeRepo, false);
}
/**
* Traitement centralisé basé sur la structure stable de getCommandet
*/
private function processCommandes(
Request $request,
TmpVolumeRepository $volumeRepository,
CommandeRepository $commandeRepo,
bool $includeCommentaire
): JsonResponse {
$data = json_decode($request->getContent(), true) ?? [];
$contact = $request->get('contact') ?? $data['contact'] ?? null;
$contact2 = $request->get('contact2') ?? $data['contact2'] ?? null;
if (!$contact) {
return $this->json(['status' => 'error', 'message' => 'Contact manquant'], 400);
}
$contact2 = ($contact2 !== null && strlen(trim($contact2)) > 10) ? trim($contact2) : null;
$queryBuilder = $commandeRepo->createQueryBuilder('c')
->join('c.IdCtPr', 'p');
if ($contact2) {
$queryBuilder->where('c.contact = :num1 OR c.contact = :num2')
->andWhere('p.validation = :validation')
->setParameter('validation', true)
->setParameter('num1', $contact)
->setParameter('num2', $contact2);
} else {
$queryBuilder->where('c.contact = :num1')
->andWhere('p.validation = :validation')
->setParameter('validation', true)
->setParameter('num1', $contact);
}
/** @var Commande[] $commandes */
$commandes = $queryBuilder->orderBy('c.DateCommande', 'DESC')
->setMaxResults(100)
->getQuery()
->getResult();
if (empty($commandes)) {
return $this->json(['status' => 'success', 'data' => []]);
}
$result = [];
foreach ($commandes as $cmd) {
$situation = $volumeRepository->situationFacture($cmd->getId());
$flag = '0';
$paiementSituationData = [];
if (!empty($situation)) {
$sit = $situation[0];
$totalPayer = floatval($sit['dejaPaie'] ?? 0) +
floatval($sit['paieEnAvance'] ?? 0) +
floatval($sit['compassation'] ?? 0) +
floatval($sit['banque'] ?? 0) +
floatval($sit['reduction'] ?? 0);
$prixTotal = floatval($sit['pritotal'] ?? 0);
if ($totalPayer > 0 && $totalPayer < $prixTotal) {
$flag = '1';
} elseif ($totalPayer >= $prixTotal && $prixTotal > 0) {
$flag = '2';
}
$paiementSituationData[] = [
'pritotal' => $prixTotal,
'pritotalPayer' => $totalPayer,
'dejaPaie' => $sit['dejaPaie'] ?? 0,
'paieEnAvance' => $sit['paieEnAvance'] ?? 0,
'banque' => $sit['banque'] ?? 0,
'compassation' => $sit['compassation'] ?? 0,
'reduction' => $sit['reduction'] ?? 0,
'etat' => $flag,
];
}
$arrayConteneurEvolution = [];
$idCtPr = $cmd->getIdCtPr();
if ($idCtPr) {
foreach ($idCtPr->getSuiviContenaires() as $itemSous) {
$arraySousSuivi = [];
foreach ($itemSous->getSousSuivis() as $sub) {
if ($sub->getSousPosition()) {
$subItem = [
'id' => $sub->getId(),
'libelle' => $sub->getSousPosition()->getLibelle(),
'date' => $sub->getCreatedAt() ? $sub->getCreatedAt()->format('d-m-Y H:i') : null,
];
if ($includeCommentaire) {
$subItem['commentaire'] = $sub->getCommentaire();
}
$arraySousSuivi[] = $subItem;
}
}
$arrayConteneurEvolution[] = [
'lieuPosition' => $itemSous->getLieu(),
'isPositionActuelle' => $itemSous->getPositionActuelleFlag(),
'date' => $itemSous->getDateOperation() ? $itemSous->getDateOperation()->format('d-m-Y') : null,
'sousSuivi' => $arraySousSuivi,
];
}
}
$arrayConteneurRetrait = [];
foreach ($cmd->getDepots() as $itemEv) {
$arrayConteneurRetrait[] = [
'id' => $itemEv->getId(),
'nom' => $itemEv->getNoms(),
'quantite' => $itemEv->getQuantite(),
'remarque' => $itemEv->getRemarque(),
'dateSortie' => $itemEv->getSortieAt() ? $itemEv->getSortieAt()->format('d-m-Y') : null,
'telephone' => $itemEv->getTelephone(),
'user' => '',
'plaque' => $itemEv->getPlaque(),
];
}
$aliasClient = $cmd->getAliasClient();
$al = (is_string($aliasClient) && strlen($aliasClient) >= 12) ? '(' . $aliasClient . ')' : '';
$nomAg = $cmd->getIdCl() ? $cmd->getIdCl()->getNomAg() : '';
$result[] = [
'id' => $cmd->getId(),
'alias' => trim($nomAg . ' ' . $al),
'date' => $cmd->getDateCommande() ? $cmd->getDateCommande()->format('Y-m-d') : null,
'total' => $cmd->getTotalAPaiyer(),
'balance' => $cmd->getBalance(),
'is_paid' => $flag,
'ville' => $idCtPr ? $idCtPr->getDestination() : null,
'remarque' => $cmd->getTotalCOLISNature(),
'netApayer' => $cmd->getTotalAPaiyer(),
'totalCBM' => $cmd->getTotalCBM(),
'totalTONNE' => ($idCtPr && $idCtPr->isIsMco()) ? $cmd->getTotalKG() : $cmd->getTotalTONNE(),
'totalCOLIS' => $cmd->getTotalCOLIS(),
'pdf_url' => 'https://a.afisarl.com/commande/edfr327edh' . $cmd->getId() . 'iuery782efnvj/PrintFacture',
'paiementSituation' => $paiementSituationData,
'EvolutionPosition' => $arrayConteneurEvolution,
'depots' => $arrayConteneurRetrait,
'isMco' => $idCtPr ? $idCtPr->isIsMco() : false,
'numTel' => $cmd->getContact(),
'numeroConteneur' => ($idCtPr && $idCtPr->getIdCt()) ? $idCtPr->getIdCt()->getNomContenaire() : null,
'dateChargement' => ($idCtPr && $idCtPr->getDateChargement()) ? $idCtPr->getDateChargement()->format('d-m-Y') : null,
'dateDepart' => ($idCtPr && $idCtPr->getDateDepart()) ? $idCtPr->getDateDepart()->format('d-m-Y') : null,
'dateArriver' => ($idCtPr && $idCtPr->getDateArriver()) ? $idCtPr->getDateArriver()->format('d-m-Y') : null,
'typeConteneur' => $idCtPr ? $idCtPr->getCompletFlag() : null,
'transporteur' => $idCtPr ? $idCtPr->getTransporteur() : null,
'provenance' => ($idCtPr && $idCtPr->getIdVille()) ? $idCtPr->getIdVille()->getNomville() : null,
'destination' => $idCtPr ? $idCtPr->getDestination() : null,
'receptionNumero' => (string) $cmd->getRecuCargo(),
];
}
return $this->json(['status' => 'success', 'data' => $result]);
}
}