<?php 
include "functions.php";
use SAPb1\Filters\Equal;
use SAPb1\Filters\InArray;

try 
{

    $sap = Login();
    $DocEntry = "";
    if (!empty($_POST['DocEntry'])) { $DocEntry = $_POST['DocEntry'] ; }

    if( $DocEntry == "") {

         //CONSULTATION
         $ConsultationsLines_result = $sap->getService("view.svc/B1_CONLINESB1SLQuery");
         $ConsultationsLines_result = $ConsultationsLines_result->queryBuilder()
         ->select('U_CodePatient')
         ->orderBy("DocEntry", "desc")->limit(1)->findAll();

         $CodePatient = json_decode(json_encode($ConsultationsLines_result), TRUE)["value"][0]["U_CodePatient"];

        //HISTORIQUE
        $HistoriqueLines_result = $sap->getService("view.svc/B1_GetPatientHistoryB1SLQuery")->queryBuilder()
        ->select('DocEntry, U_NomMed, U_ActivityType, U_DateConsultation, U_Poids, U_PA, U_Description, U_Notes')
        ->where(new Equal("U_CodePatient", $CodePatient ))->findAll();        

    } else {

        //CONSULTATION
        $ConsultationsLines_result = $sap->getService("view.svc/B1_CONLINESB1SLQuery")->queryBuilder()
        ->select('U_CodePatient')->where(new Equal("DocEntry", intval($DocEntry) ))->findAll();
        $CodePatient = json_decode(json_encode($ConsultationsLines_result), TRUE)["value"][0]["U_CodePatient"];

        //HISTORIQUE
        $HistoriqueLines_result = $sap->getService("view.svc/B1_GetPatientHistoryB1SLQuery")->queryBuilder()
        ->select('DocEntry, U_NomMed, U_ActivityType, U_DateConsultation, U_Poids, U_PA, U_Description, U_Notes')
        ->where(new Equal("U_CodePatient", $CodePatient ))->findAll();        
    }

    $Historique = json_decode(json_encode($HistoriqueLines_result), TRUE);
    echo json_encode($Historique['value']);

 

} catch (Exception $e) {
    echo $e->getMessage();    
}