<?php
session_start();
include "functions.php";
require_once '../Mpdf/pdf/autoload.php';
//require_once '../Mpdf/Product_pdf.php';
use SAPb1\Filters\Equal;
use SAPb1\Filters\InArray;
    $sap = Login();
    //check click button
    if(isset($_POST['creer'])){
        //variables + Array
       $client=''; $project=''; $dateDocument='';$requestTransfer;$jsonFormat;$remarque='';$magasin='';//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['typeProject'])){
        $project=$_POST['typeProject'];
       }
       if(!empty($_POST['dateDocument'])){
        $dateF=date_create($_POST['dateDocument']);
        // $dateDocument=date_format($dateF,"Y/m/d");
        $dateDocument=$_POST['dateDocument'];
       }
       if(!empty($_POST['magasin'])){
        $magasin=$_POST['magasin'];
       }
       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['ExpiryDate'] = date("Y-m-d", strtotime($_POST['listArticle']['dateDOT'][$i-1]));
                    $listLotLine['AddmisionDate'] =  date("Y-m-d", strtotime($_POST['listArticle']['dateDEX'][$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);
        }
    }
    // send data jso to isp 
    try{
        $requestTransfer = $sap->getService("InventoryGenEntries");
        $result = $requestTransfer->create(
            [
                "DocDate"=> $dateDocument,
                "DocDueDate"=> $dateDocument,
                "TaxDate"=> $dateDocument,
                "U_CardCode" =>$client,
                "U_CodeProject" =>$project,
                "Comments"=> $remarque,
                "DocumentLines"=> $listArticle
            ]
        );
        // $datajson= json_encode($result);
        //echo $datajson;
//--2023-06-13
        // $result = (
        //     [
        //         "DocDate"=> $dateDocument,
        //                 "DocDueDate"=> $dateDocument,
        //                 "TaxDate"=> $dateDocument,
        //                 "U_CardCode" =>$client,
        //                 "U_CodeProject" =>$project,
        //                 "Comments"=> $remarque,
        //                 "WarehouseCode"=> $magasin,
        //                 "DocumentLines"=> $listArticle
        //     ]
        // );
        $datajson= json_encode($result);
       // echo $datajson;
        if($datajson!==null){
            //echo 'Transfer Succes </br> JSON DATA=>'.$datajson;
            $dataDocEntry=json_decode($datajson);
            $_SESSION['Message']='Entree de marchandise Succes';
            
            $mpdf = new \Mpdf\Mpdf();
                
            //$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A2']);
            $stylesheet = file_get_contents('../Mpdf/style.css');
            $mpdf->WriteHTML($stylesheet,\Mpdf\HTMLParserMode::HEADER_CSS);
            foreach ($listArticle as $item) {
                $mpdf->AddPage();
                $itemCode = $item['ItemCode'];
                $quantity = $item['Quantity'];
                $warehouseCode = $item['WarehouseCode'];
                $nbrPallette = $item['U_NbrPallette'];

                  //-create PDF 
               
                // $name="<div class='firstD'><p class='static'>Client: ".$cardCode."</p>
                //     <p class='static'>Date document:"'.$dateDocument.'"</p>
                // </div>";
                
                $article="<p class='static'>Article: ".$itemCode."</p>";
                // $lot="<p class='static'>n°lot: ".$batchNumber."</p>";
                $quantite="<p class='static'>Quantité: ".$quantity."</p>";
                // $dateDex="<p class='static'>Date D'expiration	:".$expiryDate."</p>";
                // $prix="<p class='static'>Nombre de palette: ". $nbrPalletteBatch.".00</p>";
               
               
                // echo "Item Code: " . $itemCode . "<br>";
                // echo "Quantity: " . $quantity . "<br>";
                // echo "Warehouse Code: " . $warehouseCode . "<br>";
                // echo "Number of Pallets: " . $nbrPallette . "<br>";
            
                if (isset($item['BatchNumbers'])) {
                    foreach ($item['BatchNumbers'] as $batch) {
                        $batchNumber = $batch['BatchNumber'];
                        $batchQuantity = $batch['Quantity'];
                        $expiryDate = $batch['ExpiryDate'];
                        $admissionDate = $batch['AddmisionDate'];
                        $cardCode = $batch['U_CardCode'];
                        $codeProject = $batch['U_CodeProject'];
                        $nbrPalletteBatch = $batch['U_NbrPallette'];

                        $name="<div class='firstD'><p class='static' style='text-align: right;'>Client: ".$cardCode."</p>
                        <p class='static' style='text-align: right;'>Date document:".$dateDocument."</p>
                        </div>";
                        $lot="<p class='static'>n°lot: ".$batchNumber."</p>";
                        $dateDex="<p class='static'>Date D'expiration	:".$expiryDate."</p>";
                        $prix="<p class='static'>Nombre de palette: ". $nbrPalletteBatch.".00</p>";
                    }
                }
                $mpdf->WriteHTML($name,\Mpdf\HTMLParserMode::HTML_BODY);
                $mpdf->WriteHTML($article,\Mpdf\HTMLParserMode::HTML_BODY);
                $mpdf->WriteHTML($lot,\Mpdf\HTMLParserMode::HTML_BODY);
                $mpdf->WriteHTML($quantite,\Mpdf\HTMLParserMode::HTML_BODY);
                $mpdf->WriteHTML($dateDex,\Mpdf\HTMLParserMode::HTML_BODY);
                $mpdf->WriteHTML($prix,\Mpdf\HTMLParserMode::HTML_BODY);
              
               
               
            } $mpdf->Output(); 
            //-create PDF 
                // $mpdf = new \Mpdf\Mpdf();
                // //$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A2']);
                // $stylesheet = file_get_contents('../Mpdf/style.css');
                // $mpdf->WriteHTML($stylesheet,\Mpdf\HTMLParserMode::HEADER_CSS);
                // $name="<div class='firstD'><p class='static'>Client: ".$cardCode."</p>
                //     <p class='static'>Date document:"'.$dateDocument.'"</p>
                // </div>";
                // $article="<p class='static'>Article: ".$itemCode."</p>";
                // $lot="<p class='static'>n°lot: ".'$batchNumber'."</p>";
                // $quantite="<p class='static'>Quantité: ".$quantity."</p>";
                // $dateDex="<p class='static'>Date D'expiration	:".$expiryDate."</p>";
                // $prix="<p class='static'>Nombre de palette: ". $nbrPalletteBatch.".00</p>";
                // $mpdf->WriteHTML($name,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->WriteHTML($article,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->WriteHTML($lot,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->WriteHTML($quantite,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->WriteHTML($dateDex,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->WriteHTML($prix,\Mpdf\HTMLParserMode::HTML_BODY);
                // $mpdf->Output(); 
            // end PDF
           //echo '<script>location.replace("../EntreeStock.php")</script>';
        }
    }catch(SAPb1\SAPException $e){
        echo '<script>alert("'.$e->getMessage().'");</script>';
        echo '<script>location.replace("../EntreeStock.php")</script>';
    }
}
//InventoryGenExits
/*
{"DocDate":"2023-06-22","DocDueDate":"2023-06-22","TaxDate":"2023-06-22","U_CardCode":"C001023","U_CodeProject":"TEST2","Comments":"","WarehouseCode":"","DocumentLines":[{"ItemCode":"A000001","Quantity":"10000","WarehouseCode":"01","U_NbrPallette":"","BatchNumbers":[{"BatchNumber":"C001023-TEST2-220623-012","Quantity":"10000","ExpiryDate":"2023-06-22","AddmisionDate":"2023-06-22","U_CardCode":"C001023","U_CodeProject":"TEST2","U_NbrPallette":""}]},{"ItemCode":"A000002","Quantity":"10000","WarehouseCode":"test","U_NbrPallette":"","BatchNumbers":[{"BatchNumber":"C001023-TEST2-220623-013","Quantity":"10000","ExpiryDate":"2023-06-22","AddmisionDate":"2023-06-22","U_CardCode":"C001023","U_CodeProject":"TEST2","U_NbrPallette":""}]}]}
*/
?>
