<?php
set_include_path(get_include_path().':../library/');

class AgroWebService {

    private $client;
    // PARAMETROS PARA HTTPS
    // private $curl_options = array( CURLOPT_FOLLOWLOCATION => true,
    //                                CURLOPT_SSL_VERIFYPEER => true,
    //                                CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
    //                                CURLOPT_SSL_CIPHER_LIST => 'TLSv1'
    //                             );

    // PARAMETROS PARA HTTP
    private $curl_options = array( CURLOPT_FOLLOWLOCATION => true,
                                   CURLOPT_SSL_VERIFYPEER => true,
                                );


    private $config = array( 'adapter'      => 'Zend_Http_Client_Adapter_Curl',
                             'ssltransport' => 'tls',
                             'strict'       => false,
                             'persistent'   => true,
                             'curloptions'  => ''
                           );

    /*
     * Efetua login no agronet e recupera o cookie
    */
    public function __construct() {
        require_once('Zend/Loader.php');
        require_once('Zend/Http/Client.php');
        require_once('Zend/Http/Client/Adapter/Curl.php');

        $this->config['curloptions'] = $this->curl_options;
        //$uri = 'http://agronet.tonismar.dev.com/login';
        $uri = self::URL.'login';
        $this->client = new Zend_Http_Client($uri, $this->config);
        $postArray = array('usuario' => '', 'senha' => '');
        $this->client->setParameterPost($postArray);

        $this->client->setHeaders(array(
            'Host: '.self::HOST,
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
            'Accept: text/javascript, application/javascript, */*',
            'Accept-Language: en-gb,en;q=5.0',
            'Accept-Encoding: gzip, deflate',
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'X-Requested-With: XMLHttpRequest',
            'Referer: '.self::URL
        ));

        $this->client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
        $this->client->setCookie('__utmz', '174057141.1392357517.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)');
        $this->client->setCookie('__atuvc', '58');
        $this->client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');
        $this->client->setCookie('PHPSESSID',  substr(md5(date('Ymdhis')),0,27));
        $response = $this->client->request('POST');
    }

    /*
     * Retorna empresa de acordo com o id
     * URL: ?call=getEmpresas?id_empresa=valor1&cod_acesso_mobile=valor2
    */
    public function getEmpresas($params) {
        $uri = self::URL.'sistema/empresa/empresas';
        $postArray = array('id' => $params['id_empresa'], 'cod_acesso_mobile' => $params['cod_acesso_mobile']);
        $this->client->setUri($uri);
        $this->client->setParameterPost($postArray);
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Retorna usuarios de uma empresa
     * URL: ?call=getUsuarios&id_empresa=valor1
    */
    public function getUsuarios($params) {
        $uri = self::URL.'sistema/usuario/usuariosbyempresa';
        $postArray = array('id_empresa' => $params['id_empresa']);
        $this->client->setUri($uri);
        $this->client->setParameterPost($postArray);
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Retorna vistorias em analise
     * URL: ?call=getVistorias&id_empresa=valor1
    */
    public function getVistorias($params) {
        $uri = self::URL.'seguro/vistoriapreviaempresa/vistorias';
        $postArray = array('id_empresa' => $params['id_empresa']);
        $this->client->setUri($uri);
        $this->client->setParameterPost($postArray);
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Retorna vistorias em analise
     * URL: ?call=getVariedades
    */
    public function getVariedades() {
        $uri = self::URL.'produto/variedade/variedades';
        $this->client->setUri($uri);
        $this->client->setParameterPost(array());
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Retorna produtos
     * URL: ?call=getProdutos
    */
    public function getProdutos() {
        $uri = self::URL.'produto/produto/produtos';
        $this->client->setUri($uri);
        $this->client->setParameterPost(array());
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Retorna produtos variedades
     * URL: ?call=getProdutosVariedades
    */
    public function getProdutosVariedades() {
        $uri = self::URL.'produto/produto/produtosvariedades';
        $this->client->setUri($uri);
        $this->client->setParameterPost(array());
        $response = $this->client->request('POST');
        return $response->getBody();
    }

    /*
     * Transferir laudo
    */
    public function transmitirLaudo($params) {
        $uri = self::URL.'safra'.$params['id_safra'].'/lvp001/transmitir';
        $this->client->setUri($uri);
        $this->client->setParameterPost($params);
        $response = $this->client->request('POST');
        return $response->getBody();
    }
}
?>
