Organização de itens em caixas para cálculo de frete nos Correios
A quanto tempo não posto aqui. As novidades do fórum estão bem bacanas... já volto com uma bomba rs
Estou adaptando um classe para arranjar os produtos do carrinho de um e-comerce em caixas para calcular o frete juntos aos correios.
Gostaria de sugestões dos colegas para implementar, sempre que um caixa atingir o limite, gerar uma nova caixa... a parte da comunicação com a API do correios está bem tranquilo.
O que tá pegando mesmo é essa parte da organização dos itens em várias caixas.
Vou deixar aqui o código completo que estou testando...
/
config.php
<?php
define("MIN_LARGURA", 11);
define("MAX_LARGURA", 105);
define("MIN_ALTURA", 2);
define("MAX_ALTURA", 105);
define("MIN_COMPRIMENTO", 16);
define("MAX_COMPRIMENTO", 105);
define("MIN_SOMA_CLA", 29);
define("MAX_SOMA_CLA", 200);
function dd($data) {
echo '<pre>';
var_dump($data);
echo '</pre>';
}
index.php
<?php
require_once 'config.php';
require_once 'Boxes.php';
$cart = [
[
'title' => 'Book - The art of war',
'A' => 25,
'L' => 70,
'C' => 90,
],
[
'title' => 'Book - The art of war',
'A' => 25,
'L' => 70,
'C' => 90,
],
[
'title' => 'Book - The art of war',
'A' => 25,
'L' => 70,
'C' => 90,
],
];
$boxes = new Boxes($cart);
dd($boxes->createBoxes());
Boxes.php
<?php
class Boxes {
private $cart = [];
private $box = [];
private $box_properties = [];
/**
* __construct
*
* @param array $cart
*/
public function __construct($cart = [])
{
$this->cart = $cart;
$this->box_properties = [
'altura' => 0,
'largura' => 0,
'comprimento' => 0,
'qtd_itens' => 0,
'message' => null,
'volume' => 0,
'volume_itens' => 0,
'volume_vazio' => 0,
'comprimento_remanescente' => 0,
'largura_remanescente' => 0,
'altura_remanescente' => 0
];
}
/**
* Order items inside the box
*
* @return array
*/
public function orderBox()
{
foreach ($this->cart as $k => $item):
$new_height = min($item['A'], $item['L'], $item['C']);
$new_length = max($item['A'], $item['L'], $item['C']);
$width = [$item['A'], $item['L'], $item['C']];
sort($width) ;
array_shift($width);
array_pop($width);
$item['L'] = isset($width[0]) ? $width[0] : $new_height;
$item['A'] = $new_height ;
$item['C'] = $new_length ;
$item['LC'] = $item['L'] * $item['C'] ;
$this->cart[$k] = $item;
endforeach;
usort($this->cart, function($a, $b){
return $a['LC'] < $b['LC'];
});
return $this->cart ;
}
/**
* Create and put items inside the box
*
* @return array
*/
public function createBoxes()
{
$this->cart = $this->orderBox();
$this->box = json_decode(json_encode($this->box_properties,FALSE));
$this->organizeItems();
$this->box->volume = ($this->box->altura * $this->box->largura * $this->box->comprimento);
$this->box->volume_vazio = $this->box->volume - $this->box->volume_itens;
$this->minSpecifications();
$this->errors();
return $this->box;
}
private function organizeItems()
{
foreach ($this->cart as $item):
$this->box->qtd_itens++;
$this->box->volume_itens += ($item['A'] * $item['L'] * $item['C']);
$this->allocateItems($item);
$this->box->altura += $item['A'];
if($item['L'] > $this->box->largura):
$this->box->largura = $item['L'];
endif;
if ($item['C'] > $this->box->comprimento):
$this->box->comprimento = $item['C'];
endif;
$this->box->comprimento_remanescente = $this->box->comprimento;
$this->box->largura_remanescente = $this->box->largura - $item['L'];
$this->box->altura_remanescente = $item['A'];
endforeach;
}
private function allocateItems($item)
{
if($this->box->comprimento_remanescente >= $item['C'] && $this->box->largura_remanescente >= $item['L']):
if($item['A'] > $this->box->altura_remanescente):
$this->box->altura += $item['A'] - $this->box->altura_remanescente ;
endif;
if($item['C'] > $this->box->comprimento):
$this->box->comprimento = $item['C'];
endif;
$this->box->comprimento_remanescente = $this->box->comprimento - $item['C'];
$this->box->largura_remanescente = $this->box->largura_remanescente - $item['L'] ;
$this->box->altura_remanescente = $item['A'] > $this->box->altura_remanescente ? $item['A'] : $this->box->altura_remanescente ;
endif;
}
private function minSpecifications()
{
if($this->box->altura > 0 && $this->box->altura < MIN_ALTURA):
$this->box->altura = MIN_ALTURA ;
endif;
if($this->box->largura > 0 && $this->box->largura < MIN_LARGURA):
$this->box->largura = MIN_LARGURA ;
endif;
if($this->box->comprimento > 0 && $this->box->comprimento < MIN_COMPRIMENTO):
$this->box->comprimento = MIN_COMPRIMENTO ;
endif;
}
private function errors()
{
if($this->box->altura > MAX_ALTURA):
$this->box->message = "Erro: Altura maior que o permitido.";
endif;
if($this->box->largura > MAX_LARGURA ):
$this->box->message = "Erro: Largura maior que o permitido.";
endif;
if($this->box->comprimento > MAX_COMPRIMENTO ):
$this->box->message = "Erro: Comprimento maior que o permitido.";
endif;
if(($this->box->comprimento + $this->box->comprimento + $this->box->comprimento) < MIN_SOMA_CLA):
$this->box->message = "Erro: Soma dos valores C+L+A menor que o permitido.";
endif;
if(($this->box->comprimento + $this->box->comprimento + $this->box->comprimento) > MAX_SOMA_CLA):
$this->box->message = "Erro: Soma dos valores C+L+A maior que o permitido.";
endif;
}
}Discussão (7)
Carregando comentários...