src/Controller/ApiController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CarrierZone;
  4. use App\Repository\CarrierRepository;
  5. use App\Repository\CarrierZoneRepository;
  6. use App\Repository\PriceGridRepository;
  7. use App\Repository\RangeWeightRepository;
  8. use App\Repository\InvoiceDetailRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ApiController extends AbstractController
  15. {
  16.     #[Route('/api/get_range_price'name'api_get_range_price',methods "POST")]
  17.     public function index(
  18.         Request $request,
  19.         RangeWeightRepository $rangeWeightRepo,
  20.         CarrierZoneRepository $carrierZoneRepo,
  21.         PriceGridRepository $priceGridRepo,
  22.         CarrierRepository $carrierRepository,
  23.     ): Response
  24.     {
  25.         $data json_decode(
  26.             $request->getContent()
  27.         );
  28.         $carrier $carrierRepository->find($data->id_carrier);
  29.         $carrierType $carrier->getCarrierType();
  30.         if(isset($data->cr_inter_moyenne) && $data->cr_inter_moyenne){
  31.             $rangeWeight $rangeWeightRepo->findAverageByCarrierWeightZipDate($data->id_carrier$data->weight$data->date);
  32.             if(($data->id_carrier == || $carrierType->getId() == 3) && $data->weight 30)
  33.                 $rangeWeight $rangeWeightRepo->findAverageByCarrierWeightZipDate($data->id_carrier, ($data->weight 30 30 $data->weight), $data->date);
  34.         }
  35.         else{
  36.             $rangeWeight $rangeWeightRepo->findOneByCarrierWeightZipDate($data->id_carrier$data->weight$data->zipcode$data->date);
  37.             if(($data->id_carrier == || $carrierType->getId() == 3) && $data->weight 30)
  38.                 $rangeWeight $rangeWeightRepo->findOneByCarrierWeightZipDate($data->id_carrier, ($data->weight 30 30 $data->weight), $data->zipcode$data->date);
  39.         }
  40.         if($rangeWeight){
  41.             $priceGrid $priceGridRepo->find($rangeWeight['price_grid_id']);
  42.             if($data->id_carrier == 1){
  43.                 if($data->weight 30)
  44.                     $rangeWeight['price'] = round($rangeWeight['price'] + ((int)($data->weight 29.001) * $priceGrid->getOutOfRange()), 2);
  45.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100); 
  46.                 if(substr($data->zipcode,,2) == '20' && strlen($data->zipcode) == 5){
  47.                     $rangeWeight['price'] += $priceGrid->getLivraisonCorse();
  48.                 }
  49.                 foreach($priceGrid->getCarrierZoneEloignee()->getZipZones() as $zipZone){
  50.                     if($data->zipcode == $zipZone->getZipcode()){
  51.                         $rangeWeight['price'] += $priceGrid->getZoneEloignee();
  52.                     }
  53.                 }
  54.             }
  55.             elseif(in_array($data->id_carrier, [3])){
  56.                 $zone_difficile  $carrierZoneRepo->find(28);
  57.                 $zone_saisonniere =  $carrierZoneRepo->find(35);
  58.                 $zone_parisienne =  $carrierZoneRepo->find(32);
  59.                 if($data->weight 99)
  60.                     $rangeWeight['price'] = $rangeWeight['price'] * (ceil($data->weight 10) / 10);
  61.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  62.                 $rangeWeight['price'] += $priceGrid->getSurete();
  63.                 if($this->checkZoneDifficile($zone_difficile$data->zipcode))
  64.                     $rangeWeight['price'] += (float)$priceGrid->getIlesMontagnes();
  65.                 if($this->checkGeodisParis($zone_parisiennesubstr($data->zipcode02)))
  66.                     $rangeWeight['price'] += (float)$priceGrid->getGeodisParis();
  67.                 if($this->checkSaison($zone_saisonniere$data->zipcode, (int)(str_replace('-'''substr($data->date5)))))
  68.                     $rangeWeight['price'] += $rangeWeight['price']  * (float)$priceGrid->getLivraisonSaison() / 100;
  69.                 if(substr($data->zipcode02) == 20)
  70.                     $rangeWeight['price'] += (float)$priceGrid->getLivraisonCorse();
  71.                 if(isset($data->particulier) && $data->particulier)
  72.                     $rangeWeight['price'] += (float)$priceGrid->getOnDemand();
  73.             }
  74.             elseif($data->id_carrier == 6){//Schenker
  75.                 $zipCodes array_column($carrierZoneRepo->findZipCodeByIdZone(43), 'zipcode');
  76.                 $zipCodesUrbaine array_column($carrierZoneRepo->findZipCodeByIdZone(28), 'zipcode');
  77.                 $date_begin date_create(date('Y') . '-06-01');
  78.                 $date_end date_create(date('Y') . '-08-31');
  79.                 $date_begin_jo date_create('2024-07-01');
  80.                 $date_end_jo date_create('2024-09-15');
  81.                 $date date_create($data->date);
  82.                 if(in_array($data->zipcode$zipCodes) && in_array($data->inseecode$zipCodes))
  83.                     $rangeWeight $rangeWeightRepo->findOneByCarrierWeightZipDateZESchenker($data->id_carrier$data->weight,  $data->zipcode$data->date);
  84.                 if($data->weight 100 && $data->weight 1000){
  85.                     $arrondi_weight ceil($data->weight 10);
  86.                     $rangeWeight['price'] = $rangeWeight['price'] * $arrondi_weight 10;
  87.                 }
  88.                 elseif($data->weight >= 1000){
  89.                     $arrondi_weight ceil($data->weight 10);
  90.                     $rangeWeight['price'] = $rangeWeight['price'] * 50 $arrondi_weight 100;
  91.                 }
  92.                 if($data->particulier)
  93.                     $rangeWeight['price'] += $priceGrid->getLivraisonParticulier();
  94.                 if(in_array($data->zipcode$zipCodesUrbaine))
  95.                     $rangeWeight['price'] += $priceGrid->getZonesUrbaines();
  96.                 if(in_array(substr($data->zipcode02), [7577789192939495]))
  97.                     $rangeWeight['price'] += $priceGrid->getLivraisonParis();
  98.                 if(in_array($data->zipcode$zipCodes) && in_array($data->inseecode$zipCodes))
  99.                     $rangeWeight['price']  += $priceGrid->getZoneEloignee();
  100.                 if($date >= $date_begin && $date <= $date_end)
  101.                     $rangeWeight['price']  = round($rangeWeight['price'] * (+ ( $priceGrid->getLivraisonSaisonniere() / 100)), 2);
  102.                 if($data->express)
  103.                     $rangeWeight['price'] = round($rangeWeight['price'] * (+ ( $priceGrid->getSchenkerExpress() / 100)), 2);
  104.                 $rangeWeight['price'] = round($rangeWeight['price'] + $priceGrid->getSchenkerSurete(), 2);
  105.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  106.                 if($date >= $date_begin_jo && $date <= $date_end_jo && in_array(substr($data->zipcode02), [759293])){
  107.                     $rangeWeight['price'] += 19.81;
  108.                 }
  109.             }
  110.             elseif(in_array($data->id_carrier, [789])){//DPD
  111.                 $rangeWeight['price'] = round($rangeWeight['price'] + $priceGrid->getDpdSurete() + $priceGrid->getDpdContibutionLogistique(), 2);
  112.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  113.             }
  114.             elseif(in_array($data->id_carrier, [10114445])){//DPD INTER
  115.                 $rangeWeight['price'] = round($rangeWeight['price'] + $priceGrid->getDpdSurete() + $priceGrid->getDpdContibutionLogistique(), 2);
  116.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  117.             }
  118.             elseif($carrierType->getId() == 3){//FEDEX INTER
  119.                 if((float)$data->weight 70.99)
  120.                     $rangeWeight['price'] = round($rangeWeight['price'] + ((int)($data->weight 70.99) * $priceGrid->getOutOfRange()), 2);
  121.                 if(isset($data->width) && isset($data->height) && isset($data->depth)){
  122.                     if((float)$data->width 121 && ((float)$data->depth 76 || (float)$data->height 76) ||
  123.                         ((float)$data->width + ((float)$data->height 2) + ((float)$data->depth 2)) > 266){
  124.                             $rangeWeight['price'] += $priceGrid->getFedexLivraisonNonConvoyable();
  125.                     }
  126.                     if((float)$data->width 243 ||
  127.                         ((float)$data->width + ((float)$data->height 2) + ((float)$data->depth 2)) > 330){
  128.                             $rangeWeight['price'] += $priceGrid->getFedexHorsNorme();
  129.                     }
  130.                 }
  131.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  132.             }
  133.             elseif($carrierType->getId() == 4){//SCHENKER INTER
  134.                 if($data->weight 100 && $data->weight 1000){
  135.                     $arrondi_weight ceil($data->weight 10);
  136.                     $rangeWeight['price'] = $rangeWeight['price'] * $arrondi_weight 10;
  137.                 }
  138.                 elseif($data->weight >= 1000){
  139.                     $arrondi_weight ceil($data->weight 10);
  140.                     $rangeWeight['price'] = $rangeWeight['price'] * 50 $arrondi_weight 100;
  141.                 }
  142.                 if($data->id_carrier == 17)
  143.                     $rangeWeight['price'] = $rangeWeight['price'] * ($priceGrid->getSchenkerInterMarpol() / 100);
  144.                 $rangeWeight['price'] += $priceGrid->getSchenkerInterSurete();
  145.                 $rangeWeight['price'] = $rangeWeight['price'] * ($data->surcharge 100);
  146.                 $rangeWeight['price'] += $priceGrid->getSchenkerInterContibutionEnergetique();
  147.             }
  148.             return new JsonResponse(['rangeweight' => [
  149.                 'price' => round($rangeWeight['price'], 2), 'date' => substr($data->date5)
  150.             ]]);
  151.         }
  152.         else{
  153.             return new JsonResponse(['rangeweight' => false]);
  154.         }
  155.     }
  156.     #[Route('/api/get_price_by_tracking'name'api_get_price_by_tracking'methods: ['POST'])]
  157.     public function indexPriceByTracking(
  158.         Request $request,
  159.         InvoiceDetailRepository $invoice_detail_repository
  160.     ): JsonResponse
  161.     {
  162.         $data json_decode($request->getContent(), true);
  163.         if (!isset($data['tracking'])) {
  164.             return new JsonResponse(['error' => 'Tracking value is required'], 400);
  165.         }
  166.         $invoice_details $invoice_detail_repository->findBy(['tracking' => $data['tracking']]);
  167.         if (!empty($invoice_details)) {
  168.             $invoice_detail $invoice_details[0];
  169.             return new JsonResponse(['price' => round($invoice_detail->getOptionsPrice() + $invoice_detail->getPrice() , 2)]);
  170.         }
  171.     
  172.         return new JsonResponse(['price' => false]);
  173.     }
  174.     private function checkZoneDifficile(CarrierZone $zone_difficile$zipcode){
  175.         $response false;
  176.         $zips $zone_difficile->getZipZones();
  177.         foreach($zips as $zip){
  178.             if($zip == $zipcode)
  179.                 $response true;
  180.         }
  181.         return $response;
  182.     }
  183.     private function checkGeodisParis(CarrierZone $zone_difficile$zipcode){
  184.         $response false;
  185.         $zips $zone_difficile->getZipZones();
  186.         foreach($zips as $zip){
  187.             if($zip == $zipcode)
  188.                 $response true;
  189.         }
  190.         return $response;
  191.     }
  192.     private function checkSaison(CarrierZone $zone_difficile$zipcode$date){
  193.         $response false;
  194.         if($date >= 501 && $date <= 831){
  195.             $zips $zone_difficile->getZipZones();
  196.             foreach($zips as $zip){
  197.                 if($zip == $zipcode)
  198.                     $response true;
  199.             }
  200.         }
  201.         return $response;
  202.     }
  203. }