Location: PHPKode > scripts > Gnarus Frete > gnarus-frete/class.GnarusFrete.php
<?php

/**
* Gnarus Frete
* .
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @package Gnarus Frete
* @author José Carlos Nascimento hide@address.com
* @version 1.0.0 40-10-2004
* @copyright lisence GPL
*/

class GnarusFrete
{
  /**
  * @access public
  */
  var $arq_frete = 'frete.csv';

  /**
  * @access public
  */
  var $arq_cep = 'cep.csv';

  /**
  * @access private
  */
  var $gnarus_frete_dir = '';

  /**
  * GnarusFrete Constructor
  *
  * @access public
  */
  function GnarusFrete () {
    $this->gnarus_frete_dir = dirname(__FILE__);
  }

  /**
  * frete
  * retorna o valor do frete pegando do arquivo csv
  *
  * @access public
  */
  function frete ($cep_origem, $cep_destino, $peso, $servico = '40010') {
    // servico,  40010 = SEDEX,  40215 = SEDEX 10, 40045 = SEDEX a Cobrar
    // no arquivo csv  existe apenas o  40010
    $cep_origem = substr($cep_origem, 0, 5);
    $cep_destino = substr($cep_destino, 0, 5);
    $ret = 0;

    list($uf_origem, $regiao_origem) = $this->uf_regiao($cep_origem);
    list($uf_destino, $regiao_destino) = $this->uf_regiao($cep_destino);
    if ($regiao_origem == "I" OR $regiao_destino == "I") $regiao = "I";
    else $regiao = "C";
    //SP;C;3;4;40010;10,00
    // ele vai no arq todo e se nao axar nda, ele pega o valor padrao q esta c/ UF no final do arq
    $handle = fopen ($this->gnarus_frete_dir ."/". $this->arq_frete, "r");
    while ($data = fgetcsv($handle, 1000, ";")) {
      if ($data[4] == $servico AND $data[1] == $regiao AND ereg($uf_destino, $data[0]) AND ($peso >= $data[2] AND $peso <= $data[3])) {
        $ret = $data[5];
        break;
      }
      elseif ($data[4] == $servico AND $data[1] == $regiao AND $data[0] == "" AND ($peso >= $data[2] AND $peso <= $data[3])) {
        $ret = $data[5];
        break;
      }
    }
    fclose ($handle);

    return $ret;
  }

  /**
  * regiao
  * retorna array c/  I (interior) ou C (capital)   e  UF
  *
  * @access public
  */
  function uf_regiao ($cep) {
    $cep = substr($cep, 0, 5);
    $ret = array();
    // no arquiv csv  o F  = faixa e deve vir antes dos C = capital
    //   AC;F;69900000;69999999
    $handle = fopen ($this->gnarus_frete_dir ."/". $this->arq_cep, "r");
    while ($data = fgetcsv($handle, 1000, ";")) {
      if ($data[1] == "F" AND $cep >= substr($data[2], 0, 5) AND $cep <= substr($data[3], 0, 5))
        $ret[0] = $data[0];
      elseif ($data[1] == "C" AND $cep >= substr($data[2], 0, 5) AND $cep <= substr($data[3], 0, 5))
        $ret[1] = $data[1];
    }
    fclose ($handle);
    if (!empty($ret[0]) AND empty($ret[1])) $ret[1] = "I";
    
    return $ret;
  }

  /**
  * frete_correios
  * retorna o valor do frete pegando do site dos correios
  *
  * @access public
  */
  function frete_correios ($cep_origem, $cep_destino, $peso, $servico = '40010') {
    $ret = 0;
    $url = "http://www.correios.com.br/encomendas/precos/calculo.cfm?servico=".$servico."&CepOrigem=".$cep_origem."&CepDestino=".$cep_destino."&Peso=".$peso;

    $res = join("", file($url));
    ereg ("Tarifa=([0-9]+\.[0-9]+)", $res, $reg);
    if ($reg[1] > 0) $ret = $reg[1];

    return $ret;
  }

}

?>
Return current item: Gnarus Frete