Généré les cotations - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Généré les cotations.php

  1. <?php
  2. header('Content-Type: application/json');
  3. $dimensions;
  4. $recipient;
  5. $quotations = [];
  6. $operators = [
  7.     'TNTE',
  8.     'UPSE',
  9.     'DHLE',
  10.     'FEDX',
  11.     'SODX'
  12. ]; // Liste des transporteurs dont il faut conserver les offres
  13. $offers;
  14. for($i = 0; $i < $_POST['length']; $i++) // Pour chaque expéditions
  15. {
  16.     $boxtal = new \Emc\Quotation();
  17.     $boxtal->setLogin($BoxtalID);
  18.     $boxtal->setPassword($BoxtalPassword);
  19.     $boxtal->setKey($BoxtalEnv == 'prod' ? $BoxtalKey : $BoxtalTestKey);
  20.     $boxtal->setEnv($BoxtalEnv);
  21.     $boxtal->setLocale('fr-FR');
  22.     $forAbroad = $_POST[$i]['data']['recipient_address_country'] != 'FR';
  23.         $dimensions = [];
  24.     $offers = [];
  25.     $proforma = [];
  26.     $totalMass = array_reduce($_POST[$i]['parcels'], function($a, $e) {
  27.         if(is_array($e))
  28.             return $a + floatval($e['mass']);
  29.         return $a;
  30.     }, 0);
  31.         for($j = 0; $j < $_POST[$i]['parcels']['length']; $j++) // Pour chaque colis
  32.         {
  33.                 array_push($dimensions, [
  34.             'poids' => $_POST[$i]['parcels'][$j]['mass'],
  35.             'largeur' => $_POST[$i]['parcels'][$j]['width'],
  36.             'longueur' => $_POST[$i]['parcels'][$j]['length'],
  37.             'hauteur' => $_POST[$i]['parcels'][$j]['height'],
  38.             
  39.                 ]);
  40.         if($forAbroad) {
  41.             array_push($proforma, [
  42.                 'description_en' => 'Printed documents',
  43.                 'description_fr' => 'Documents d\'impression',
  44.                 'nombre' => 1,
  45.                 'valeur' => $_POST[$i]['value'] * ($_POST[$i]['parcels'][$j]['mass'] / $totalMass),
  46.                 'origine' => 'FR',
  47.                 'poids' => floatval($_POST[$i]['parcels'][$j]['mass'])
  48.             ]);
  49.         }
  50.         }
  51.         $recipient = [
  52.                 'pays' => $_POST[$i]['data']['recipient_address_country'],
  53.                 'code_postal' => $_POST[$i]['data']['recipient_address_postal_code'],
  54.                 'ville' => $_POST[$i]['data']['recipient_address_locality'],
  55.                 'adresse' => $_POST[$i]['data']['recipient_address_thoroughfare'] . ($_POST[$i]['data']['recipient_address_premise'] ? ',' . $_POST[$i]['data']['recipient_address_premise'] : ''),
  56.         'prenom' => $_POST[$i]['data']['recipient_address_firstname'],
  57.         'nom' => $_POST[$i]['data']['recipient_address_name'],
  58.         'email' => $_POST[$i]['data']['recipient_address_email'],
  59.         'tel' => $_POST[$i]['data']['recipient_address_phone']
  60.         
  61.         ];
  62.     if($_POST[$i]['data']['recipient_address_company_name'])
  63.     {
  64.         $recipient['type'] = 'entreprise';
  65.         $recipient['societe'] = $_POST[$i]['data']['recipient_address_company_name'];
  66.     }
  67.     else
  68.     {
  69.         $recipient['type'] = 'particulier';
  70.     }
  71.     $param1 = [
  72.         'type' => 'colis',
  73.         'dimensions' => $dimensions
  74.     ];
  75.     $param2 = [
  76.         'collecte' => date("Y-m-d"),
  77.         'delay' => 'aucun',
  78.         'content_code' => 10260,
  79.         'valeur' => $_POST[$i]['value'],
  80.         'assurance' => true,
  81.         'description' => $_POST[$i]['data']['client']
  82.     ];
  83.     if($forAbroad)
  84.     {
  85.         $param2['reason'] = 'sale';
  86.         $param2['colis.description'] = 'Documents d\'impression/Printed documents';
  87.         $boxtal->setProforma($proforma);
  88.     }
  89.     $boxtal->getQuotation($BoxtalShipper, $recipient, $param1, $param2); // Obtenir les cotations
  90.     $offers = array_filter(
  91.         array_filter($boxtal->offers,
  92.                      function($e)
  93.                      {
  94.                          global $operators;
  95.                          foreach($operators as $key => $operator)
  96.                          {
  97.                              if($operator == $e['operator']['code'])
  98.                                  return true;
  99.                          }
  100.                      } // Filtrer les offres par transporteur
  101.                     ),
  102.         function($e)
  103.         {
  104.             if($e['collection']['type'] == 'COMPANY' && ($e['delivery']['type'] == 'HOME' || $e['delivery']['type'] == 'COMPANY'))
  105.                 return true;
  106.         } // Filtrer les offres "Enlèvement dans nos locaux" => "Livraison à domicile ou en entreprise"
  107.     );
  108.     $offers['resp_error'] = $boxtal->resp_errors_list;
  109.     $offers['curl_error'] = $boxtal->curl_error_text;
  110.     $offers['last_request'] = $boxtal->last_request;
  111.     $offers['xpath'] = $boxtal->xpath;
  112.     $offers['ID'] = $_POST[$i]['id'];
  113.     $offers['recipient'] = $recipient;
  114.     if($forAbroad)
  115.         $offers['proforma'] = $proforma;
  116.     $offers['params1'] = $param1;
  117.     $offers['params2'] = $param2;
  118.         array_push($quotations, $offers);
  119. }
  120. // Traitrement des données {
  121. $responses = [];
  122. $response;
  123. foreach($quotations as $key1 => $quotation)
  124. {
  125.         $response = [];
  126.         foreach($quotation as $key2 => $offer) 
  127.         {
  128.         if(is_numeric($key2))
  129.             array_push($response, $offer);
  130.         }
  131.     $response['length'] = count($response);
  132.     $response['ID'] = $quotation['ID'];
  133.     $response['sender'] = $BoxtalShipper;
  134.     $response['recipient'] = $quotation['recipient'];
  135.     $response['proforma'] = $quotation['proforma'];
  136.     $response['params1'] = $quotation['params1'];
  137.     $response['params2'] = $quotation['params2'];
  138.         array_push($responses, $response);
  139. }
  140. // }
  141. echo json_encode($responses);
  142. ?>
File Description
  • Généré les cotations
  • PHP Code
  • 23 Oct-2019
  • 5.14 Kb
You can Share it: