<?php
include_once '../config.php';
include_once '../../env.php';

class Stock
{

    private $token;
    private $key;


    function __construct()
    {
        $this->chave();
        $this->stockApiVtex();
    }

    public function chave()
    {
        $this->token = getenv('token');
        $this->key = getenv('appkey');
    }

    public function stockApiVtex()
    {

        global $entityManager;

        $stockRepository = $entityManager->getRepository('App\Models\Entity\StockDb');

        $stock = $stockRepository->findAll();


        foreach ($stock as $p) {
            $urlStockVtex = 'https://arsdesign.myvtex.com/api/logistics/pvt/inventory/skus/'.$p->getSkuVtex();

            $curl = curl_init();
            curl_setopt_array($curl, array(
                    CURLOPT_URL => $urlStockVtex,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 1800,
                    CURLOPT_FOLLOWLOCATION => false,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                    "Accept: application/json",
                    "Content-Type: application/json",
                    "X-VTEX-API-AppToken: {$this->token}",
                    "X-VTEX-API-AppKey: {$this->key}"
      ),
    ));
            $respostaCurlInput = curl_exec($curl);
            $respostaCurlOutput = json_decode($respostaCurlInput, true);
            //echo json_encode($respostaCurlOutput["balance"][0]["warehouseId"]);
            foreach ($respostaCurlOutput["balance"] as $value) {
                switch ($value["warehouseId"]) {
                    case 'outletCvto':
                        echo json_encode($value["warehouseId"])." -- ".$p->getId();
                        break;
                    case 'acreSergio':
                        echo json_encode($value["warehouseId"])." -- ".$p->getId();
                    break;
                    default:
                        # code...
                        break;
                }
                

            }
            exit;
            //echo json_encode($respostaCurlOutput["balance"][0]);
            exit;
        }
    }
}

$teste = new Stock();
