<?php
$countqte = 0;
$requestentries = '';
$detailline = '';
$requesttransfer = '';
$detailtransfer = '';
$lengthlines = $_POST['lengthlines'];
$codeemp = $_SESSION['codeemp'];
if($_SESSION['DocStatus'] == 'O'){
    for($k = 0; $k < $lengthlines; $k++){
        $docentry = $_POST['docentry'][$k];
        $qte = $_POST['poidnet'][$k];
        if(!empty($qte)){
            if($countqte == 0){
                 $requestentries = '{"Series" : '. $_SESSION['Series']  .' ,"BPL_IDAssignedToInvoice": ' .   $_SESSION['BPLID'] . ',"DocDate" : "' . $_SESSION['DocDate']  
                    . '", "DocDueDate" : "' .$_SESSION['DocDate']  
                    . '" ,"U_TYPEENTREE": "RECOLTE" , "DocumentReferences" : [{ "RefDocEntr" : ' . $docentry . ' , "RefObjType" : "rot_InventoryTransferRequest"}]
                    "DocumentLines": [{';
                $requesttransfer = '{"BPLID": ' .   $_SESSION['BPLID'] . ',"DocDate" : "' .$_SESSION['DocDate']  . '", "DueDate" : "'
                    .$_SESSION['DocDate']  . '","SalesPersonCode":' . $codeemp . ',"StockTransferLines": [{';
            }
            $countqte ++;
            // ItemCode,BatchNumber,LineNum,DocEntry...
            $itemcode = $_POST['itemcode'][$k];
            $lot = $_POST['lot'][$k];
            $linenum = $_POST['linenum'][$k];
            $qtebrut = $_POST['qtee'][$k];
            $tare = $_POST['tare'][$k];
            $tarecolis = $_POST['tarecolis'][$k];
            $docentry = $_POST['docentry'][$k];
            $fromwarehouse = $_POST['fromwarehouse'][$k];
            $daterecote = $_POST['daterecote'][$k];
            $equipe = $_POST['equipe'][$k];
            $codecontenant = $_POST['codecontenant'][$k];
            $nbcolis = $_POST['nbrcolis'][$k];
            $typecontenant = $_POST['typecontenant'][$k];
            $qteestimee = $_POST['qteestimee'][$k];
            $typelot = $_POST['typelot'][$k];
            $codetracabilite = $_POST['codetracabilite'][$k];
            $codecmixte = $_POST['codemixte'][$k];
            $domain = $_POST['domain'][$k];
            $desccontenent = $_POST['desccontenent'][$k];
            $poidmoyenqte = $_POST['poidmoyenqte'][$k];
            $U_TYPECOLIS = $_POST['U_TYPECOLIS'][$k];
            if($countqte > 1){
                $requestentries .= ',{';
                $requesttransfer .= ',{';
            }
            $detailline = '"ItemCode": "' . $itemcode.'","Quantity": ' . $qte . ',"WarehouseCode": "' . $fromwarehouse .'","U_TARECONTENANT": ' . $tare 
            . ' , "U_TARECOLIS" : ' . $tarecolis . ' ,"U_POIDSBRUT": ' .
             $qtebrut . ' ,"U_DATERECOLTE": "'. $daterecote .'","U_EQUIPE": "' . $equipe . '","U_CODECONTENANT": "' . $lot . '","U_NBCOLIS": ' . $nbcolis . 
             ',"U_TYPECONTENANT": "' . $typecontenant . '","U_QTEESTIMEE": ' . $qteestimee . ',"U_TYPELOT": "' . $typelot . '","U_LOT": "' . $codetracabilite .
              '","U_BATCHNUM": "' . $lot . '" ,"U_CODECMIXTE": "' . $codecmixte . '"
            ,"U_DOMAINE": "' . $domain . '","U_DESCRIPTIONCONTENANT": "' . $desccontenent . '" , "U_TYPECOLIS" : "' .$U_TYPECOLIS . '"';
   
            $detailtransfer = '"Quantity": ' . $qte . ',"BaseType": 1250000001,"BaseLine":' . $linenum . ',"BaseEntry":' . $docentry . ',"U_TARECONTENANT": ' . $tare 
            . ' , "U_TARECOLIS" : ' . $tarecolis .' ,"U_POIDSBRUT": ' . $qtebrut ;
    
            if(!empty($lot)){
    
              
                $detailline .= ',"BatchNumbers": [{"BatchNumber": "' . $lot . '","Quantity": ' . $qte . '  ,"U_POIDSBRUT": ' . $qtebrut . ' , "U_TARECOLIS" : '. $tarecolis . ', "U_TARECONTENANT" :' . $tare
                     . ' ,"U_POIDSNETCOLIS" : ' . $poidmoyenqte . ' ,"U_DATERECOLTE": "'. $daterecote .'","U_EQUIPE": "' . $equipe . '","U_CODECONTENANT": "' .
                      $lot . '","U_NBCOLIS": ' . $nbcolis . ',"U_TYPECONTENANT": "' . $typecontenant . '","U_TYPELOT": "' . $typelot . 
                      '","U_LOT": "' . $codetracabilite . '" ,"U_CODECMIXTE": "' . $codecmixte . '","U_DOMAINE": "' . $domain . 
                      '","U_DESCRIPTIONCONTENANT": "' . $desccontenent .'", "U_TYPECOLIS" : "' .$U_TYPECOLIS . '"}]';
                $detailtransfer .= ',"BatchNumbers": [{"BatchNumber": "' . $lot . '","Quantity": ' . $qte .'}]';
            }
            $detailline .= '}';
            $detailtransfer .= '}';
            $requestentries .= $detailline;
            $requesttransfer .= $detailtransfer;
        }
    }
    echo  $lengthlines . "   " . $countqte;
    
    if($countqte > 0 && $lengthlines == $countqte){
        if($_SESSION['cnt'] == 0){
            $requestentries .= ']}';
            $requesttransfer .= ']}';
            $addqte = curl_init();
            curl_setopt($addqte, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($addqte, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/InventoryGenEntries");
            curl_setopt($addqte, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($addqte, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($addqte, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($addqte, CURLOPT_VERBOSE, 1);
            curl_setopt($addqte, CURLOPT_POSTFIELDS, $requestentries);
            $response = curl_exec($addqte);
            $result = json_decode($response, true);
            curl_close($addqte);
            if(isset($result['error']['message']['value'])){
                echo '
                <div class="vide" style="display:flex">
                    <div>
                        <span>'.$result['error']['message']['value'].'</span>
                        <button>OK</button>
                    </div>
                </div>';
            }else{
                $createtransfert = curl_init();
                curl_setopt($createtransfert, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($createtransfert, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/StockTransfers");
                curl_setopt($createtransfert, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($createtransfert, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($createtransfert, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($createtransfert, CURLOPT_VERBOSE, 1);
                curl_setopt($createtransfert, CURLOPT_POSTFIELDS, $requesttransfer);
                $response = curl_exec($createtransfert);
                $result = json_decode($response, true);
                curl_close($createtransfert);
                if(isset($result['error']['message']['value'])){
                    echo '
                    <div class="vide" style="display:flex">
                        <div>
                            <span>'.$result['error']['message']['value'].'</span>
                            <button>OK</button>
                        </div>
                    </div>';
                }else{
                    echo '
                <form method="POST">    
                    <div class="vide" style="display:flex">
                        <div>
                            <span>La demande créée avec succès</span>
                            <button >OK</button>
                        </div>
                    </div>
                </form>';
                }
            }
        }
        else{
            // StockTransfers
            $requesttransfer .= ']}';
            $createtransfert = curl_init();
            curl_setopt($createtransfert, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($createtransfert, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/StockTransfers");
            curl_setopt($createtransfert, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($createtransfert, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($createtransfert, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($createtransfert, CURLOPT_VERBOSE, 1);
            curl_setopt($createtransfert, CURLOPT_POSTFIELDS, $requesttransfer);
            $response = curl_exec($createtransfert);
            $result = json_decode($response, true);
            curl_close($createtransfert);
            if(isset($result['error']['message']['value'])){
                echo '
                <div class="vide" style="display:flex">
                    <div>
                        <span>'.$result['error']['message']['value'].'</span>
                        <button>OK</button>
                    </div>
                </div>';
            }else{
                echo '
            <form method="POST">    
                <div class="vide" style="display:flex">
                    <div>
                        <span>La demande créée avec succès</span>
                        <button >OK</button> 
                    </div>
                </div>
            </form>';
            }
        }

        
    }
    else if($lengthlines <> $countqte){
        echo '
        <div class="vide" style="display:flex">
            <div>
                <span>Merci de sélectionner tous les articles</span>
                <button>OK</button>
            </div>
        </div>
        ';
    }
    else if($countqte==0){
        echo '
        <div class="vide" style="display:flex">
            <div>
                <span>Aucun détail n\'est sélectionné</span>
                <button>OK</button>
            </div>
        </div>
        ';
    }
}

else{
    echo '
    <form method="POST">    
        <div class="vide" style="display:flex">
            <div>
                <span>Déjà clôrturer</span>
                <button >OK</button>
            </div>
        </div>
    </form>';
}

?>