public function calcularFreteCorreios() { if ($this->getId() != '' && $this->getEndereco()->getCep() != '') { $valor = 0; $prazo = 0; $itens = new ListaPedidoItens(); $a[1] = array('campo' => ListaPedidoItens::IDSESSAO, 'valor' => $this->id); $itens->condicoes($a); $quantidade = 0; $total = 0; $freteGratis = false; $semFrete = false; try { while ($pI = $itens->listar()) { if ($pI->frete == Produto::FRETE_GRATIS) { $freteGratis = true; } elseif ($pI->frete == Produto::SEM_FRETE) { $semFrete = true; } else { for ($i = 1; $i <= $pI->quantidade; $i++) { $this->getEndereco()->addItemCorreios($pI->peso->num, $pI->comprimento->num, $pI->largura->num, $pI->altura->num); } } } $pacotes = $this->getEndereco()->getPacotesCorreios(); if (count($pacotes) > 0) { foreach ($pacotes as $k) { $e = PedidoEnderecoEntrega::__CalcularValorCorreios($this->getEndereco()->getCep(), $this->getEndereco()->tipo, $k['p'], $k['c'] < PedidoEnderecoEntrega::COMPRIMENTO_MIN_CORREIOS ? PedidoEnderecoEntrega::COMPRIMENTO_MIN_CORREIOS : $k['c'], $k['l'] < PedidoEnderecoEntrega::LARGURA_MIN_CORREIOS ? PedidoEnderecoEntrega::LARGURA_MIN_CORREIOS : $k['l'], $k['a'] < PedidoEnderecoEntrega::ALTURA_MIN_CORREIOS ? PedidoEnderecoEntrega::ALTURA_MIN_CORREIOS : $k['a']); $valor += $e['valor']; $prazo = $e['prazo'] > $prazo ? $e['prazo'] : $prazo; } } if ($valor == 0 && !$freteGratis && $semFrete) { $this->existeFrete = false; } if ($valor == 0 && $freteGratis) { $this->freteGratis = true; } $this->getEndereco()->setValor($valor); $this->getEndereco()->prazo = $prazo; } catch (Exception $e) { throw new Exception($e->getMessage()); $this->existeFrete = false; $this->getEndereco()->setValor(0); $this->getEndereco()->prazo = 0; } } else { $this->getEndereco()->setValor(0); $this->getEndereco()->prazo = 0; } }