<?php

//essa função atualiza a doca na vtex utilizando a coluna total

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


function totalVtex(){

//env.php
$token = getenv('token');
$key = getenv('appkey');

global $entityManager;

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

$quantArray = 2;




    for ($i=0; $i < $quantArray; $i++) { 
        
        $skuVtex[$i] = $stock[$i]->getSkuVtex();
        $stockTotal[$i] = $stock[$i]->getTotal();

        if($stockTotal[$i] == null){
            $stockTotal[$i] = 0;
        }
        

        $url = "https://logistics.myvtex.com/api/logistics/pvt/inventory/skus/$skuVtex[$i]/warehouses/outletCvto?an=arsdesign";

        

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => false,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "PUT",
            CURLOPT_POSTFIELDS =>"{\n    \"unlimitedQuantity\": false,\n    \"dateUtcOnBalanceSystem\": null,\n    \"quantity\": {$stockTotal[$i]},\n    \"timeToRefill\": 0\n}",
            CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json; charset=utf-8",
            "Accept: application/json",
            "X-VTEX-API-AppToken: {$token}",
            "X-VTEX-API-AppKey: {$key}"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err . " - " . $skuVtex[$i] . "<br>";
            
        } else {
            echo "Ok " . $skuVtex[$i] . " - " . $response . "<br>";
            
        }
        
        

    }

}

totalVtex();