<?php
$whscode = $_SESSION['whscode'];
$whsinit = $whscode[0]['U_whscode'];
$select = '?$select=DocEntry,BPLID,DocNum,DocDate,FromWarehouse,ToWarehouse,U_TRANSPORTEUR,U_MATRICULE,U_NUMPLOMB,U_HDEBCHARGE,U_CINCHAUFFEUR,U_HDEPART,U_NOMCHAUFFEUR,U_DocNum';
$filter = '&$filter='  rawurlencode("DocumentStatus eq 'bost_Open' and ToWarehouse eq '$whsinit'");
$headers = array(.
    "Cookie: B1SESSION=" . $sessionId . "; ROUTEID=" . $routeId . ";",
    "Prefer: odata.maxpagesize=1000"
);

$transfert = curl_init();
curl_setopt($transfert, CURLOPT_HTTPHEADER, $headers);
curl_setopt($transfert, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/InventoryTransferRequests" . $select . $filter);
curl_setopt($transfert, CURLOPT_RETURNTRANSFER, true);
curl_setopt($transfert, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($transfert, CURLOPT_SSL_VERIFYHOST, false);
$resptransfert = curl_exec($transfert);
$result = json_decode($resptransfert,true);
curl_close($transfert);

$magasin = '';
$countwhscode = count($whscode);
$magasin .= '
<option value="selec">select one .....</option>
';
for($k = 0; $k < $countwhscode; $k++){
    $magasin .= '
        <option value="'. $whscode[$k]['U_whscode'] .'">'. $whscode[$k]['U_whscode'] .'</option>
    ';
}

$table = '';
if(!empty($result['value'][0])){
    $bplid = $result['value'][0]['BPLID'];
    $length = count($result['value']);
    $table ='
    <nav id="nav">Les demandes de transfert</nav>
    <div class="demande">
        <div class="tablecontainer">
        <form action="" method="POST">
        <table>
            <tr class="head" id="head">
                <td>Numéro</td>
                <td>N°Document</td>
                <td>Date de création</td>
                <td>Magasin cédant</td>
                <td>Au magasin</td>
                <td>Transporteur</td>
                <td>Matricule</td>
                <td>Numéro de plomb</td>
                <td>Heure chargement</td>
                <td>CIN Chauffeur</td>
                <td>Heure départ</td>
                <td>Nom chauffeur</td>
            </tr>';
    for($i = 0; $i < $length; $i++){
	$DocDate = $result['value'][$i]['DocDate'];
        $table .= '
            <tr class="transfer" id="transfer">
                <td>'.$result['value'][$i]['DocNum'].'</td>
                <td>'.$result['value'][$i]['U_DocNum'].'</td>
                <td>'.$result['value'][$i]['DocDate'].'</td>
                <td>'.$result['value'][$i]['FromWarehouse'].'</td>
                <td>'.$result['value'][$i]['ToWarehouse'].'</td>
                <td>'.$result['value'][$i]['U_TRANSPORTEUR'].'</td>
                <td>'.$result['value'][$i]['U_MATRICULE'].'</td>
                <td>'.$result['value'][$i]['U_NUMPLOMB'].'</td>
                <td>'.$result['value'][$i]['U_HDEBCHARGE'].'</td>
                <td>'.$result['value'][$i]['U_CINCHAUFFEUR'].'</td>
                <td>'.$result['value'][$i]['U_HDEPART'].'</td>
                <td>'.$result['value'][$i]['U_NOMCHAUFFEUR'].'</td>
            </tr>
        ';
    }
    $table .= '</div>
        </div>
        </table>
        </form>
    ';
}else{
    $table = '
    <nav id="nav">Les demandes de transfert</nav>
        <h4 class="aucun"">Aucune demande n\'est trouvée</h4>
    ';
}
// Session Timeout error
if(isset($result['error']['message']['value'])){
    echo '<script type="text/javascript"> alert("'. $result['error']['message']['value'] .'"); window.location.replace(\'index.php\');</script>';
}
?>