# php – Calculation of packages

## Question:

I have the following expression:

`````` \$pacotes = intval(\$_peso / 30) + 1;
``````

The idea is to know how many equal packages will give.

For example: if I have a `\$_peso` of `29kg` , divided by `30kgs` (maximum allowed by the post office), I will get a result of `0,96666666666666666666666666666667` . Therefore, less than 1. In this case, I'm taking the integer (0) and adding 1. After all, there is no `0 pacote` .

But when the \$_peso is exactly 30, 60, 90, etc…… I have problems because

``````  30/30 = 1.
1+1=2
``````

And `2 pacotes` are not needed.

How to solve this logic?

Will I have to get the `topo` value? In other words, if you give `1,2 pacotes` will be `2 pacotes` and if you give `0,8 pacote` will be `1 Pacote` ?

If yes, how to get the `topo` (inverse of the base) of a value in php?

I developed a class that helps me.

Let's say that the package, which can be folded of course, weighs, for example, `55kg` .

The `correios` only takes `30kg` . So I can `dividir em 2 pacotes` (less than 30kg).

In this case, taking `57` and making `ceil(57/30)` I will have the rounding (always upwards) of the number of packages needed, of a maximum of `30kg` each.

In this case, `57/30 = 1.9` , rounding up with `ceil()` we get 2 packages.

So, we make `57 kg / 2 pacotes` = `2 pacotes de 28,5kg cada` .

We calculate the shipping cost for `1 pacote` and multiply it by the number of packages and present it 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";

\$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!
``````
Scroll to Top
AllEscort