<?php
//session_start();
include('db.php');
require_once(__DIR__ . '/vendor/autoload.php');

//function dump($V){
    //echo '<pre>';
    //var_dump($V);
    //echo '</pre>';
//}

//if(!empty($_SESSION['commande']) && !empty($_SESSION['commande']['items'])) {
    //$articles = $_SESSION['commande']['items'];

    // Configure API key authorization: Bearer
    //test $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjEyMDEyMDEzMzk5MDh8VFMwMTAxNDEyMCIsInJvbGUiOiJUYXhwYXllciIsIm5iZiI6MTc0MDU4NzMzOCwiZXhwIjoxOTA4ODMxNjAwLCJpYXQiOjE3NDA1ODczMzgsImlzcyI6ImltcG90cy5iaiIsImF1ZCI6ImltcG90cy5iaiJ9.1vNnQopocz7Lrk_c6kRBnVtWAfa161dF9Olh-WTFm6o');
    $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjEyMDEyMDEzMzk5MDh8VFMwMTAxNDEyMCIsInJvbGUiOiJUYXhwYXllciIsIm5iZiI6MTc0MDU4OTc1NiwiZXhwIjoxNzUzNDg0NDAwLCJpYXQiOjE3NDA1ODk3NTYsImlzcyI6ImltcG90cy5iaiIsImF1ZCI6ImltcG90cy5iaiJ9.E8PU677zgoAra4Kn1uxwbgXItdjs2B_SDfFF4ETIQBw');
    // print_r($config);
    //dump($config);
    // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
    // $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Authorization', 'Bearer');

    $apiInvoiceInstance = new Swagger\Client\Api\SfeInvoiceApi(
        // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
        // This is optional, `GuzzleHttp\Client` will be used as default.
        new GuzzleHttp\Client(array('verify'=> false)),
        $config
    );

    //dump($apiInvoiceInstance);

    $apiInfoInstance = new Swagger\Client\Api\SfeInfoApi(
        // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
        // This is optional, `GuzzleHttp\Client` will be used as default.
        new GuzzleHttp\Client(array('verify'=> false)),
        $config
    );

    //INFO
    try {
        $infoResponseDto = $apiInfoInstance->apiInfoStatusGet();
        // print_r($infoResponseDto);
        //dump($infoResponseDto);
    } catch (Exception $e) {
        echo 'Exception when calling SfeInvoiceApi->apiInvoiceGet: ', $e->getMessage(), PHP_EOL;
    }

    try {
        $invoiceTypesDto = $apiInfoInstance->apiInfoInvoiceTypesGet();
        // print_r($invoiceTypesDto);
       // dump($invoiceTypesDto);
    } catch (Exception $e) {
        echo 'Exception when calling SfeInvoiceApi->apiInvoiceGet: ', $e->getMessage(), PHP_EOL;
    }

    try {
        $taxGroupsDto = $apiInfoInstance->apiInfoTaxGroupsGet();
        // print_r($taxGroupsDto);
       // dump($taxGroupsDto);
    } catch (Exception $e) {
        echo 'Exception when calling SfeInvoiceApi->apiInvoiceGet: ', $e->getMessage(), PHP_EOL;
    }

    try {
        $paymentTypesDto = $apiInfoInstance->apiInfoPaymentTypesGet();
        // print_r($paymentTypesDto);
       // dump($paymentTypesDto);
    } catch (Exception $e) {
        echo 'Exception when calling SfeInvoiceApi->apiInvoiceGet: ', $e->getMessage(), PHP_EOL;
    }

    //INVOICE
    try {
        $statusReponseDto = $apiInvoiceInstance->apiInvoiceGet();
        // print_r($statusReponseDto);
        //dump($statusReponseDto);
    } catch (Exception $e) {
        echo 'Exception when calling SfeInvoiceApi->apiInvoiceGet: ', $e->getMessage(), PHP_EOL;
    }


    $body = new \Swagger\Client\Model\InvoiceRequestDataDto(); // \Swagger\Client\Model\InvoiceRequestDataDto | 
    $body->setIfu('1201201339908');//YOUR IFU HERE

    $operatorDto = new \Swagger\Client\Model\OperatorDto();
    $operatorDto->setName("Client");
    $body->setOperator($operatorDto);

    $body->setType(Swagger\Client\Model\InvoiceTypeEnum::FV);

    $items = array();
    
    $article=$db->prepare('SELECT * FROM table_invoice_details WHERE num_cmd=?');
    $article->execute(array($numCmd));
    //while($articles=$article->fetchAll) {
    foreach ($article as $key => $articles) {
        $item = new \Swagger\Client\Model\ItemDto();
        $item->setName($articles['designation']);
        $item->setPrice($articles['price']);
        $item->setQuantity($articles['qte']);
        $item->setTaxGroup(Swagger\Client\Model\TaxGroupTypeEnum::B);
        array_push($items, $item);
    }

    /*foreach ($articles as $key => $article) {
        # code...
        $item = new \Swagger\Client\Model\ItemDto();
        $item->setName($article['nom']);
        $item->setPrice($article['pu']);
        $item->setQuantity($article['qte']);
        $item->setTaxGroup(Swagger\Client\Model\TaxGroupTypeEnum::B);
        array_push($items, $item);
    }*/

    // $item1 = new \Swagger\Client\Model\ItemDto();
    // $item1->setName("Jus d'orange");
    // $item1->setPrice(1200);
    // $item1->setQuantity(3);
    // $item1->setTaxGroup(Swagger\Client\Model\TaxGroupTypeEnum::B);
    // array_push($items, $item1);
    //$item2 = new \Swagger\Client\Model\ItemDto();
    // $item2->setName("Article exonere");
    // $item2->setPrice(800);
    // $item2->setQuantity(2.5);
    // $item2->setTaxGroup(Swagger\Client\Model\TaxGroupTypeEnum::A);
    // array_push($items, $item2);

    $body->setItems($items);

    try {
        $invoiceResponseDto = $apiInvoiceInstance->apiInvoicePost($body);
        // print_r($invoiceResponseDto);
        //dump($invoiceResponseDto);
    } catch (Exception $e) {
        echo '<br><br>Exception when calling SfeInvoiceApi->apiInvoicePost: ', $e->getMessage(), PHP_EOL;
    }


    $uid = $invoiceResponseDto['uid']; // string | 
    if (!is_null($uid)){
        try {
            $invoiceDetailsDto = $apiInvoiceInstance->apiInvoiceUidGet($uid);
            //   print_r($invoiceDetailsDto);
           // dump($invoiceDetailsDto);
            
            try {
                $securityElementsDto = $apiInvoiceInstance->apiInvoiceUidConfirmPut($uid);
                // print_r($securityElementsDto);
               // dump($securityElementsDto);
               $date_time=json_encode($securityElementsDto['date_time']);
               $qr_code=json_encode($securityElementsDto['qr_code']);
               $code_me_ce_fdgi=json_encode($securityElementsDto['code_me_ce_fdgi']);
               $counters=json_encode($securityElementsDto['counters']);
               $nim=json_encode($securityElementsDto['nim']);
               //echo $date_time;
               //echo $qr_code;
            } catch (Exception $e) {
                echo 'Exception when calling SfeInvoiceApi->apiInvoiceUidConfirmPut: ', $e->getMessage(), PHP_EOL;
            }
        } catch (Exception $e) {
            echo 'Exception when calling SfeInvoiceApi->apiInvoiceUidConfirmPut: ', $e->getMessage(), PHP_EOL;
        }
    } 
//}

?>