Skip to content

Instantly share code, notes, and snippets.

@steverobbins
Last active November 14, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steverobbins/b884392f582a6282a9eec0fd06b8fe2b to your computer and use it in GitHub Desktop.
Save steverobbins/b884392f582a6282a9eec0fd06b8fe2b to your computer and use it in GitHub Desktop.
<?php
ini_set('display_errors', 1);
define('MAX_WEIGHT', 25);
$tests = [
[
'input' => [
[
'weight' => 0.1,
'qty' => 1000,
'ppb' => 144,
],
[
'weight' => 0.396,
'qty' => 1000,
'ppb' => 48,
],
],
'output' => [
'weight' => 100 + 396,
'boxes' => 6 + 20 + 2,
]
],
[
'input' => [
[
'weight' => 0.1,
'qty' => 864,
'ppb' => 144,
],
[
'weight' => 0.396,
'qty' => 1000,
'ppb' => 48,
],
],
'output' => [
'weight' => 86.4 + 396,
'boxes' => 6 + 20 + 1,
]
],
[
'input' => [
[
'weight' => 0.1,
'qty' => 1000,
'ppb' => 288,
],
[
'weight' => 0.396,
'qty' => 1000,
'ppb' => 48,
],
],
'output' => [
'weight' => 100 + 396,
'boxes' => 3 + 20 + 2,
]
],
[
'input' => [
[
'weight' => 1,
'qty' => 72,
],
],
'output' => [
'weight' => 72,
'boxes' => 3,
]
],
[
'input' => [
[
'weight' => 1,
'qty' => 72,
'ppb' => 36,
],
[
'weight' => 24,
'qty' => 2,
'ppb' => 1,
],
],
'output' => [
'weight' => 48 + 72,
'boxes' => 4,
]
],
[
'input' => [
[
'weight' => 50,
'qty' => 1,
],
[
'weight' => 1,
'qty' => 72,
]
],
'output' => [
'weight' => 122,
'boxes' => 4,
]
],
[
'input' => [
[
'weight' => 24,
'qty' => 3,
],
[
'weight' => 1,
'qty' => 72,
]
],
'output' => [
'weight' => 144,
'boxes' => 6,
]
],
];
function test($items)
{
$remaining = [];
$boxes = 0;
$weight = 0;
foreach ($items as $item) {
$weight += $item['qty'] * $item['weight'];
if (isset($item['ppb']) && $item['ppb']) {
$box = floor($item['qty'] / $item['ppb']);
$boxes += $box;
$remainder = $item['qty'] % $item['ppb'];
if ($remainder) {
$remaining[] = [
'weight' => $item['weight'],
'qty' => $remainder,
];
}
} else {
$remaining[] = [
'weight' => $item['weight'],
'qty' => $item['qty'],
];
}
}
if (count($remaining)) {
$boxes++;
$boxWeight = 0;
usort($remaining, 'sortByWeight');
foreach ($remaining as $item) {
foreach(range(1, $item['qty']) as $i) {
if ($boxWeight + $item['weight'] <= MAX_WEIGHT) {
$boxWeight += $item['weight'];
} else {
$boxes++;
$boxWeight = $item['weight'] >= MAX_WEIGHT ? 0 : $item['weight'];
}
}
}
}
return [$weight, $boxes];
}
function sortByWeight($a, $b)
{
return $a['weight'] < $b['weight'];
}
?><html>
<head>
<title>Shipping Boxes Calculation Test</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Shipping Boxes Calculation Test</h1>
<h4>Max box weight: <?php echo MAX_WEIGHT ?></h4>
</div>
<?php foreach ($tests as $i => $test): ?>
<?php
list($weight, $boxes) = test($test['input']);
$pass = $boxes == $test['output']['boxes'] && $weight == $test['output']['weight'];
?>
<div class="row">
<div class="col-sm-12">
<div class="panel panel-<?php echo $pass ? 'success' : 'danger' ?>">
<div class="panel-heading">
<h2 class="panel-title">
Test #<?php echo $i + 1 ?>
<?php if ($pass): ?>
<span class="label label-success">Pass</span>
<?php else: ?>
<span class="label label-danger">Fail</span>
<?php endif ?>
</h2>
</div>
<div class="panel-body">
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Cart</h3>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Weight</th>
<th>Qty</th>
<th>Packs per box</th>
</tr>
</thead>
<tbody>
<?php foreach ($test['input'] as $item): ?>
<tr>
<td><?php echo $item['weight'] ?></td>
<td><?php echo $item['qty'] ?></td>
<td><?php echo isset($item['ppb']) ? $item['ppb'] : 'N/A' ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Expected Result</h3>
</div>
<div class="panel-body">
<table class="table">
<tbody>
<tr>
<th>Weight</td>
<td><?php echo $test['output']['weight'] ?></td>
</tr>
<tr>
<th>Boxes</td>
<td><?php echo $test['output']['boxes'] ?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Actual Result</h3>
</div>
<div class="panel-body">
<table class="table">
<tbody>
<tr>
<th>Weight</td>
<td><?php echo $weight ?></td>
</tr>
<tr>
<th>Boxes</td>
<td><?php echo $boxes ?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endforeach ?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment