Package calculation

Asked

Viewed 83 times

3

I have the following expression:

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

The idea is to know how many matching packages.

For example: if I have one $_peso of 29kg, divided by 30kgs (maximum allowed by the post office), I will have a result of 0,96666666666666666666666666666667. Therefore, less than 1. In this case, I am taking the whole value (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 they’re not necessary 2 pacotes.

How to solve this logic?

Will I have to take the value of topo? I mean, if you can 1,2 pacotes evening 2 pacotes and if they do 0,8 pacotes will be 1 Pacote?

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

  • In case you wanted to take the result for $packages (before adding the +1) and just round it up ? Ever tried to do so?

  • 2

    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!

  • But what about the cases of 30, 60 and 90? This function also meets?

  • 1

    definitely answer

  • @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/

  • I agree! This done!

Show 1 more comment

1 answer

1


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!
  • I agree and already changed. See if it’s good!

  • Much better. Somehow I had already voted positive before, but the improvement can help attract more votes, and has more chance to help other people with the same problem.

  • valeu: $objective->helpPeople(); instantiated the class. rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.