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.
Result of php executing
Full code of Généré les cotations.php
- <?php
- header('Content-Type: application/json');
- $dimensions;
- $recipient;
- $quotations = [];
- $operators = [
- 'TNTE',
- 'UPSE',
- 'DHLE',
- 'FEDX',
- 'SODX'
- ]; // Liste des transporteurs dont il faut conserver les offres
- $offers;
- for($i = 0; $i < $_POST['length']; $i++) // Pour chaque expéditions
- {
- $boxtal = new \Emc\Quotation();
- $boxtal->setLogin($BoxtalID);
- $boxtal->setPassword($BoxtalPassword);
- $boxtal->setKey($BoxtalEnv == 'prod' ? $BoxtalKey : $BoxtalTestKey);
- $boxtal->setEnv($BoxtalEnv);
- $boxtal->setLocale('fr-FR');
- $forAbroad = $_POST[$i]['data']['recipient_address_country'] != 'FR';
- $dimensions = [];
- $offers = [];
- $proforma = [];
- $totalMass = array_reduce($_POST[$i]['parcels'], function($a, $e) {
- if(is_array($e))
- return $a + floatval($e['mass']);
- return $a;
- }, 0);
- for($j = 0; $j < $_POST[$i]['parcels']['length']; $j++) // Pour chaque colis
- {
- array_push($dimensions, [
- 'poids' => $_POST[$i]['parcels'][$j]['mass'],
- 'largeur' => $_POST[$i]['parcels'][$j]['width'],
- 'longueur' => $_POST[$i]['parcels'][$j]['length'],
- 'hauteur' => $_POST[$i]['parcels'][$j]['height'],
- ]);
- if($forAbroad) {
- array_push($proforma, [
- 'description_en' => 'Printed documents',
- 'description_fr' => 'Documents d\'impression',
- 'nombre' => 1,
- 'valeur' => $_POST[$i]['value'] * ($_POST[$i]['parcels'][$j]['mass'] / $totalMass),
- 'origine' => 'FR',
- 'poids' => floatval($_POST[$i]['parcels'][$j]['mass'])
- ]);
- }
- }
- $recipient = [
- 'pays' => $_POST[$i]['data']['recipient_address_country'],
- 'code_postal' => $_POST[$i]['data']['recipient_address_postal_code'],
- 'ville' => $_POST[$i]['data']['recipient_address_locality'],
- 'adresse' => $_POST[$i]['data']['recipient_address_thoroughfare'] . ($_POST[$i]['data']['recipient_address_premise'] ? ',' . $_POST[$i]['data']['recipient_address_premise'] : ''),
- 'prenom' => $_POST[$i]['data']['recipient_address_firstname'],
- 'nom' => $_POST[$i]['data']['recipient_address_name'],
- 'email' => $_POST[$i]['data']['recipient_address_email'],
- 'tel' => $_POST[$i]['data']['recipient_address_phone']
- ];
- if($_POST[$i]['data']['recipient_address_company_name'])
- {
- $recipient['type'] = 'entreprise';
- $recipient['societe'] = $_POST[$i]['data']['recipient_address_company_name'];
- }
- else
- {
- $recipient['type'] = 'particulier';
- }
- $param1 = [
- 'type' => 'colis',
- 'dimensions' => $dimensions
- ];
- $param2 = [
- 'collecte' => date("Y-m-d"),
- 'delay' => 'aucun',
- 'content_code' => 10260,
- 'valeur' => $_POST[$i]['value'],
- 'assurance' => true,
- 'description' => $_POST[$i]['data']['client']
- ];
- if($forAbroad)
- {
- $param2['reason'] = 'sale';
- $param2['colis.description'] = 'Documents d\'impression/Printed documents';
- $boxtal->setProforma($proforma);
- }
- $boxtal->getQuotation($BoxtalShipper, $recipient, $param1, $param2); // Obtenir les cotations
- $offers = array_filter(
- array_filter($boxtal->offers,
- function($e)
- {
- global $operators;
- foreach($operators as $key => $operator)
- {
- if($operator == $e['operator']['code'])
- return true;
- }
- } // Filtrer les offres par transporteur
- ),
- function($e)
- {
- if($e['collection']['type'] == 'COMPANY' && ($e['delivery']['type'] == 'HOME' || $e['delivery']['type'] == 'COMPANY'))
- return true;
- } // Filtrer les offres "Enlèvement dans nos locaux" => "Livraison à domicile ou en entreprise"
- );
- $offers['resp_error'] = $boxtal->resp_errors_list;
- $offers['curl_error'] = $boxtal->curl_error_text;
- $offers['last_request'] = $boxtal->last_request;
- $offers['xpath'] = $boxtal->xpath;
- $offers['ID'] = $_POST[$i]['id'];
- $offers['recipient'] = $recipient;
- if($forAbroad)
- $offers['proforma'] = $proforma;
- $offers['params1'] = $param1;
- $offers['params2'] = $param2;
- array_push($quotations, $offers);
- }
- // Traitrement des données {
- $responses = [];
- $response;
- foreach($quotations as $key1 => $quotation)
- {
- $response = [];
- foreach($quotation as $key2 => $offer)
- {
- if(is_numeric($key2))
- array_push($response, $offer);
- }
- $response['length'] = count($response);
- $response['ID'] = $quotation['ID'];
- $response['sender'] = $BoxtalShipper;
- $response['recipient'] = $quotation['recipient'];
- $response['proforma'] = $quotation['proforma'];
- $response['params1'] = $quotation['params1'];
- $response['params2'] = $quotation['params2'];
- array_push($responses, $response);
- }
- // }
- echo json_encode($responses);
- ?>