<?php
session_start();
include "functions.php";

require_once '../Mpdf/pdf/autoload.php';
use SAPb1\Filters\Equal;
use SAPb1\Filters\InArray;

    $sap = Login();
    if(isset($_POST['creer'])){
     //variables + Array
     $client=''; $project=''; $dateDocument='';$requestTransfer;$jsonFormat;$remarque='';//var header
     //var ligne
     $listArticle=[];
     $listLot=[];
     $PM_DocumentsCollection=[];
     //get disable input   
     if(!empty($_POST['codeClient'])){
      $code=$_POST['codeClient'];
      $indice=strpos($code,'-');
      $client=substr($code,0,$indice);
     }
     if(!empty($_POST['codeClient'])){
        $code=$_POST['codeClient'];
        $indice=strpos($code,'-');
        $clientName=substr($code,$indice,strlen($code));
       }


     if(!empty($_POST['typeProject'])){
      $project=$_POST['typeProject'];
     }
     if(!empty($_POST['dateDocument'])){
      $dateF=date_create($_POST['dateDocument']);
      //$dateDocument=date_format($dateF,"d/m/Y");
      $dateDocument=$_POST['dateDocument'];
     }
     if(!empty($_POST['remarque'])){
      $remarque=$_POST['remarque'];
     }
     if (!empty($_POST['listArticle'])){
          // this page CreateMarchandise.php
      $count = count($_POST['listArticle']['idArticle']);
      for($i=1; $i <= $count; $i++ ) {
        $listArticleLine = [];
        $listArticleLine['ItemCode'] = $_POST['listArticle']['idArticle'][$i-1];
        $listArticleLine['Quantity'] = $_POST['listArticle']['quantite'][$i-1];
        $listArticleLine['WarehouseCode'] =$_POST['listArticle']['selectMagasin'][$i-1];
        $listArticleLine['U_NbrPallette'] = $_POST['listArticle']['NbrPallette'][$i-1];
            $countLot = count($_POST['listArticle']['idArticle']);
            // for($y=1; $y <= $countLot; $y++ ) {
                $listLot  = [];
                $listLotLine = [];
                $listLotLine['BatchNumber'] = $_POST['listArticle']['lot_p'][$i-1];
                $listLotLine['Quantity'] = $_POST['listArticle']['quantite'][$i-1];
                $listLotLine['U_CardCode']=$client;
                $listLotLine['U_CodeProject']=$project;
                $listLotLine['U_NbrPallette'] = $_POST['listArticle']['NbrPallette'][$i-1];
                array_push($listLot, $listLotLine);
                $listArticleLine['BatchNumbers'] = $listLot;
            // }       
        array_push($listArticle, $listArticleLine);
    }
  }
  try{
    $requestTransfer = $sap->getService("InventoryGenExits");
    $result = $requestTransfer->create(
        [
            "DocDate"=> $dateDocument,
            "U_CardCode" =>$client,
            "U_CodeProject" =>$project,
            "Comments"=> $remarque,
            "DocumentLines"=> $listArticle
        ]
    );
    //  $result = (
    //         [
    //                     "DocDate"=> $dateDocument,
    //                     "DocDueDate"=> $dateDocument,
    //                     "TaxDate"=> $dateDocument,
    //                     "U_CardCode" =>$client,
    //                     "U_CodeProject" =>$project,
    //                     "Comments"=> $remarque,
    //                     "DocumentLines"=> $listArticle
    //         ]);
    $datajson= json_encode($result);
    //echo $datajson;
    //echo json_encode($result);
     if( $datajson!==null){
       //  echo 'Transfer Succes </br> JSON DATA=>'.$datajson;
         $dataDocEntry=json_decode($datajson);
         //$docEntry=$dataDocEntry->DocEntry;
         //$_SESSION['DocEntry']=$docEntry;
         $_SESSION['MessageSortie']='Sortie de marchandise Succes';

         $title="<H1 class='static' style='text-align: center;'> Sortie des marchandises </H1>";
        
       
        $name="<div class='firstD'><p class='static' style='text-align: right;'>Client: ".str_replace('-','',$clientName)."</p>
        <p class='static' style='text-align: right;'>Date document:  ".$dateDocument."</p>
        </div>";
        $table = '<table style="border-collapse: collapse; width: 100%;">
        <tr>
            <th style="border: 1px solid black; padding:3px;">Article</th>
            <th style="border: 1px solid black; padding: 3px;">n°lot</th>
            <th style="border: 1px solid black; padding: 3px;">Quantité</th>
            <th style="border: 1px solid black; padding: 3px;">Nombre de palette</th>
        </tr>';
            $countqte=0;
            foreach ($listArticle as $item) {
                $itemCode = $item['ItemCode'];
                $quantity = $item['Quantity'];
                $warehouseCode = $item['WarehouseCode'];
                $nbrPallette = $item['U_NbrPallette'];
                $countqte +=$quantity;
                if (isset($item['BatchNumbers'])) {
                    foreach ($item['BatchNumbers'] as $batch) {
                        $batchNumber = $batch['BatchNumber'];
                        $batchQuantity = $batch['Quantity'];
                        $cardCode = $batch['U_CardCode'];
                        $codeProject = $batch['U_CodeProject'];
                        $nbrPalletteBatch = $batch['U_NbrPallette'];

                        $table .= '<tr style="border: 1px solid black; padding:3px;">
                                        <td style="border: 1px solid black; padding:3px; ">'.$itemCode.'</td>
                                        <td style="border: 1px solid black; padding:3px;">'.$batchNumber.'</td>
                                        <td style="border: 1px solid black; padding:3px; text-align: right;">'.$quantity.'</td>
                                        <td style="border: 1px solid black; padding:3px; text-align: right;">'.$nbrPalletteBatch.'</td>
                                    </tr>';
                    }
                }
            }

            $table .= '</table>';
            // $total="<p class='static' style='text-align: right;'> Total : ".$countqte." </p>";
            $mpdf = new \Mpdf\Mpdf();
            $stylesheet = file_get_contents('../Mpdf/style.css');
            $mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);
            $mpdf->WriteHTML($title, \Mpdf\HTMLParserMode::HTML_BODY);
            $mpdf->WriteHTML($name, \Mpdf\HTMLParserMode::HTML_BODY);
            $mpdf->WriteHTML($table, \Mpdf\HTMLParserMode::HTML_BODY);
            // $mpdf->WriteHTML($total, \Mpdf\HTMLParserMode::HTML_BODY);
            $mpdf->Output(); 
        // echo '<script>location.replace("../SortieStock.php")</script>';
    }
    }catch(SAPb1\SAPException $e){
       // echo 'Transfer Succes </br> JSON DATA=>'.$datajson;
       $_SESSION['MessageSortiedanger']=$e->getMessage();
      //  echo '<script>alert("'.$e->getMessage().'")</script>';
        echo '<script>location.replace("../SortieStock.php")</script>';
    }
        //print_r($listArticle);
      
}

?>