<?php
namespace classs;
class App_doc {
    
    public $status_code;
    public $json_code;
    public $array_code;
    public $urlpathpdf;
    public $id_pos;
    public $secret_pos;
    
    public function __construct()
    {
    
    }
    
    public function getToken(string $idSrvBaseUrl, $client_id, $client_secret){
        $url = $idSrvBaseUrl.'/connect/token';
        $data = [
            'grant_type' => "client_credentials",
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'scope' => "InvoicingAPI"
        ];
        $content = http_build_query($data);
        $options = [
            "Content-type: application/x-www-form-urlencoded"
        ];
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_ENCODING        => "",
            CURLOPT_CUSTOMREQUEST   => "POST",
            CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_POSTFIELDS      => $content,
            CURLOPT_HTTPHEADER      => $options
            // CURLOPT_SSL_VERIFYPEER  => 0,
            // CURLOPT_CONNECTTIMEOUT  => 5,
        ]);
        echo $newdata = curl_exec($ch);
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $json_array=json_decode($newdata,true);
        setcookie('timeend_token', $json_array['access_token'], time() + 3540 , "/");
        $_COOKIE['timeend_token'] = $json_array['access_token'];

        // setcookie('timeon', $json_array['access_token'], time(), "/");
        // setcookie('access_token', $json_array['access_token'], time(), "/");

        // $_SESSION['access_token'] = $json_array['access_token'];
        // $_SESSION['timeon'] = time();
        // $d=strtotime("+1800 second");
        // $_SESSION['timeend'] = $d;
    }
    
    function getToken_pos($idSrvBaseUrl, $client_id_pos, $client_secret_pos, $posserial){
        // global $client_id_pos;
        // global $client_secret_pos;
        // global $posserial;

        // $this->id_pos = $client_id_pos;
        // $this->secret_pos = $client_secret_pos;
        
        $url = $idSrvBaseUrl.'/connect/token';
        $data = [
            'grant_type' => "client_credentials",
            'client_id' => $client_id_pos,
            'client_secret' => $client_secret_pos,
            'scope' => "InvoicingAPI"
        ];
        $content = http_build_query($data);
        $options = [
            "posserial: ".$posserial,
            "pososversion: SWPOS",
            "Content-type: application/x-www-form-urlencoded"
        ];
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_ENCODING        => "",
            CURLOPT_CUSTOMREQUEST   => "POST",
            CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_POSTFIELDS      => $content,
            CURLOPT_HTTPHEADER      => $options
            // CURLOPT_SSL_VERIFYPEER  => 0,
            // CURLOPT_CONNECTTIMEOUT  => 5,
        ]);
        echo $newdata = curl_exec($ch);
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $json_array=json_decode($newdata,true);

        setcookie('timeend_token_pos', $json_array['access_token'], time() + 3540 , "/");
        $_COOKIE['timeend_token_pos'] = $json_array['access_token'];

        // $_SESSION['access_token'] = $json_array['access_token'];
        // $_SESSION['timeon'] = time();
        // $d=strtotime("+1800 second");
        // $_SESSION['timeend'] = $d;
    }
    function postData($apiBaseUrl, $path, $raw, $header){
        $url = $apiBaseUrl. $path;
        $content = $raw;
        $options = $header;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_ENCODING        => "",
            CURLOPT_MAXREDIRS       => 10,
            CURLOPT_CUSTOMREQUEST   => "POST",
            CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_POSTFIELDS      => $content,
            CURLOPT_HTTPHEADER      => $options
        ]);
        $newdata = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $json_array = json_decode($newdata,true);
        $this->status_code = $status;
        $this->array_code = $json_array;
        $this->json_code = $newdata;
    }
    public function sendReceipt($apiBaseUrl, $path, $raw, $header){
        $url = $apiBaseUrl. $path;
        $content = $raw;
        $options = $header;

        $curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => $content,
        CURLOPT_HTTPHEADER => $options ,
        ));

        $response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        $json_array = json_decode($response,true);
        $this->status_code = $status;
        $this->array_code = $json_array;
        $this->json_code = $response;
    }
    function putData($apiBaseUrl, $path, $raw, $header){
        $url = $apiBaseUrl. $path;
        $content = $raw;
        $options = $header;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_ENCODING        => "",
            CURLOPT_MAXREDIRS       => 10,
            CURLOPT_TIMEOUT         => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST   => "PUT",
            // CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
            CURLOPT_POSTFIELDS      => $content,
            CURLOPT_HTTPHEADER      => $options
            // CURLOPT_SSL_VERIFYPEER  => 0,
            // CURLOPT_CONNECTTIMEOUT  => 5,
        ]);
        $newdata = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $json_array = json_decode($newdata,true);
        $this->status_code = $status;
        $this->array_code = $json_array;
        $this->json_code = $newdata;
    }
    function getData($apiBaseUrl, $path, $header){
        $url = $apiBaseUrl. $path;
        // $data = $raw;
        // $content = json_encode($data);
        $options = $header;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_ENCODING        => "",
            CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_HTTPHEADER      => $options
            // CURLOPT_SSL_VERIFYPEER  => 0,
            // CURLOPT_CUSTOMREQUEST => "POST",
            // CURLOPT_CONNECTTIMEOUT  => 5,
            // CURLOPT_HTTPGET         => true,
            // CURLOPT_POSTFIELDS      => $content,
        ]);
        $newdata = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $json_array = json_decode($newdata,true);
        $this->status_code = $status;
        $this->array_code = $json_array;
        $this->json_code = $newdata;
    }
    function DownloadPDF($apiBaseUrl, $path, $header, $uuid) {
        $url = $apiBaseUrl. $path;
        $filename= 'document-'.$uuid.'.pdf';
        $path = 'downloade/'.$filename;
        $file_download= curl_init();
        $options = $header;
        curl_setopt($file_download, CURLOPT_URL, $url);
        curl_setopt($file_download, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($file_download, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($file_download, CURLOPT_AUTOREFERER, true);
        curl_setopt($file_download, CURLOPT_HTTPHEADER, $options);
        $result= curl_exec($file_download);
        $status = curl_getinfo($file_download, CURLINFO_HTTP_CODE);
        curl_close($file_download);
        file_put_contents($path, $result);
        $json_array = json_decode($result,true);
        $this->status_code = $status;
        $this->array_code = $json_array;
        $this->json_code = $result;
        $this->urlpathpdf = $path;
        // if ($status_code == 200) {
        //     echo 'تم الاستيراد';
        // } elseif ($status_code == 400) {
        //     // echo "Status Code: " . $status_code;
        //     // echo "<br>";
        //     echo "المستند لا يزال قيد المعالجة";
        // } elseif ($status_code == 404) {
        //     // echo "Status Code: " . $status_code;
        //     // echo "<br>";
        //     echo "المستند غير موجود";
        // } else {
        //     echo "Status Code: " . $status_code;
        //     echo "<br>";
        //     print_r($json_array);
        // }
    }
    function encodetojson($code) {
        $newcode = json_encode($code, JSON_UNESCAPED_UNICODE);
        return $newcode;
    }
    function get_array() {
        return $this->array_code;
    }
    function get_jsone() {
        return $this->json_code;
    }
    function get_status() {
        return $this->status_code;
    }
    function get_urlpathpdf() {
        return $this->urlpathpdf;
    }
}
class serializeing {

    public function hashedSerializedData($documentStructure)
    {
        $serializedData = self::serialize($documentStructure);
        return hash('sha256', $serializedData);
    }

    public function serialize($documentStructure)
    {
        if(!is_array($documentStructure))
        {
            //if(gettype($documentStructure)=='double'||gettype($documentStructure)=='integer')
            //{
                //return '"'.number_format($documentStructure,2).'"';    
            //}
            return '"'.$documentStructure.'"';
        }

        /* if (is_scalar($documentStructure)) {
            return '"' . $documentStructure . '"';
        } */
    
        $serializedString = "";
        
        foreach($documentStructure as $item => $value)
        {
            
            if(!is_array($value))
            {
                
                $serializedString .= strtoupper('"'.$item.'"');
                $serializedString .= self::serialize($value);
            }

            if(is_array($value))
            {
                $serializedString .= strtoupper('"'.$item.'"');
                foreach($value as $subItem => $subValue)
                {
                    $serializedString .= is_int($subItem)?strtoupper('"'.$item.'"'):strtoupper('"'.$subItem.'"');      
                    $serializedString .= self::serialize($subValue);             
                }
            }
        }

        return $serializedString;
    }
}
class OutputsSubmitDocuments {

    public $status_array;

    public function __construct($json)
    {
        $this->status_array = json_decode($json,true);
    }

}
