I’ve developed a class that helps me.
Let’s say the order, which can be folded of course, weighs for example 55kg
.
The correios
only lead 30kg
. So I can dividir em 2 pacotes
(less than 30 kg).
In case, taking 57
and making ceil(57/30)
I will have the rounding (always up) of the number of required packages of at most 30kg
each.
In the case, 57/30 = 1.9
, rounding up with ceil()
we will have 2 packages.
So we do 57 kg / 2 pacotes
= 2 pacotes de 28,5kg cada
.
We do the freight calculation for 1 pacote
and multiply by the number of packages and present to the customer.
<?php
class CalculaFrete {
//Sedex: 40010
//Pac: 41106
private $url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?";
private $precoPacote = 5;
private $pesoPacote = 0.2;
private $cepOrigem;
private $cepDestino;
private $peso;
private $valor;
private $tipoFrete;
private $altura = 6;
private $largura = 20;
private $comprimento = 20;
private $quantosPacotes;
private $erro;
public function __construct (
$_cepDestino,
$_peso,
$_valor,
$_tipoFrete
) {
$pacotes = ceil($_peso / 30);
$this->cepDestino = $_cepDestino;
$this->peso = $_peso / $pacotes;
$this->valor = $_valor;
$this->tipoFrete = $_tipoFrete;
$this->quantosPacotes = $pacotes;
$this->cepOrigem = $constantes->getCepSite();
}
public function getPrecoPacote () {
return $this->precoPacote;
}
public function getPesoPacote () {
return $this->pesoPacote;
}
public function getPacotes () {
return $this->quantosPacotes;
}
public function getPeso () {
return $this->peso;
}
public function getErro () {
return $this->erro;
}
public function calcularFrete() {
$url = $this->url;
$url .= "nCdEmpresa=";
$url .= "&sDsSenha=";
$url .= "&sCepOrigem=" . $this->cepOrigem;
$url .= "&sCepDestino=" . $this->cepDestino;
$url .= "&nVlPeso=" . $this->peso;
$url .= "&nVlLargura=" . $this->largura;
$url .= "&nVlAltura=" . $this->altura;
$url .= "&nCdFormato=1";
$url .= "&nVlComprimento=" . $this->comprimento;
$url .= "&sCdMaoProria=n";
$url .= "&nVlValorDeclarado=" . $this->valor;
$url .= "&sCdAvisoRecebimento=n";
$url .= "&nCdServico=" . $this->tipoFrete;
$url .= "&nVlDiametro=0";
$url .= "&StrRetorno=xml";
$xml = simplexml_load_file($url);
$this->erro = $xml->cServico->Erro;
return $xml->cServico;
}
}
?>
The implementation:
<?php
$pesoTotal = 0;
$precoTotal = 0;
foreach($_SESSION["carrinho"] as $key => $produto) {
$pesoTotal += $produto["peso"] * $produto["quantidade"];
$precoTotal += $produto["precoUnitario"] * $produto["quantidade"];
}
$frete = new CalculaFrete($phpUtil->limpaCaracters($_POST["cep"]), $pesoTotal, $precoTotal, 41106);
$freteValor = $frete->calcularFrete();
$precoPacote = $frete->getPrecoPacote();
$pacotes = $frete->getPacotes();
$peso = $frete->getPeso();
$valorFrete = $pacotes * (str_replace(",", ".", $freteValor->Valor) + $precoPacote);
$valorFinal = $precoTotal + $valorFrete;
?>
É isso!
In case you wanted to take the result for $packages (before adding the +1) and just round it up ? Ever tried to do so?
– Leonardo Coelho
I already figured it out. Actually after I typed in the question I ended up remembering the result settings I need the Ceil() function. Thanks!
– Carlos Rocha
But what about the cases of 30, 60 and 90? This function also meets?
– Leonardo Coelho
definitely answer
– Carlos Rocha
@Carlosrocha, it would be interesting for you to add the answer you found yourself and mark as correct. There is no problem answering your own questions: https://blog.stackoverflow.com/2009/01/accept-your-own-answers/
– dap.tci
I agree! This done!
– Carlos Rocha