-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiprock.php
141 lines (114 loc) · 4.24 KB
/
shiprock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
class Shiprock {
public $boxesList = array();
public $productsList = array();
public $shipProducts = array();
public function createBox( $array ) {
if ( !isset( $array["volume"] ) ) {
$array["volume"] = ( $array["measures"]["width"] * $array["measures"]["length"] * $array["measures"]["height"] );
}
array_push( $this->boxesList, $array );
}
public function createProductType( $array ) {
if ( !isset( $array["volume"] ) ) {
$array["volume"] = ( $array["measures"]["width"] * $array["measures"]["length"] * $array["measures"]["height"] );
}
array_push( $this->productsList, $array );
}
public function addProduct( $array ) {
array_push( $this->shipProducts, $array );
}
private function calculateFill() {
foreach ( $this->boxesList as $box ) {
foreach ( $this->productsList as $key => $product ) {
$exclude_box = 0;
if ( !isset( $this->productsList[$key]["boxes"] ) ) {
$this->productsList[$key]["boxes"] = array();
}
if ( !isset( $this->productsList[$key]["exclude_boxes"] ) ) {
$this->productsList[$key]["exclude_boxes"] = array();
}
foreach ( $this->productsList[$key]["measures"] as $product_measure ) {
$oversize = 0;
foreach ( $box["measures"] as $box_measure ) {
if ( $product_measure > $box_measure && $product["flexible"] == false ) {
$oversize++;
}
}
if ( $oversize == 3 ) {
$exclude_box = 1;
}
}
$fill = number_format( ( ( $this->productsList[$key]["volume"] * 100 ) / $box["volume"] ), 3 );
array_push( $this->productsList[$key]["boxes"], array( "name" => $box["name"], "fill" => $fill, "exclude" => $exclude_box ) );
}
}
}
public function findWhere( $array, $matching ) {
foreach ( $array as $item ) {
$is_match = true;
foreach ( $matching as $key => $value ) {
if ( is_object( $item ) ) {
if ( ! isset( $item->$key ) ) {
$is_match = false;
break;
}
} else {
if ( ! isset( $item[$key] ) ) {
$is_match = false;
break;
}
}
if ( is_object( $item ) ) {
if ( $item->$key != $value ) {
$is_match = false;
break;
}
} else {
if ( $item[$key] != $value ) {
$is_match = false;
break;
}
}
}
if ( $is_match ) {
return $item;
}
}
return false;
}
public function pack($filter) {
$this->calculateFill();
$available_boxes = array();
$min_size = 0;
foreach ( $this->shipProducts as $shipProduct ) {
$product_type = $this->findWhere( $this->productsList, array( "type" => $shipProduct["type"] ) ); //Get the product type specs
$min_size += ( min( $product_type["measures"] ) * $shipProduct["quantity"] ); //Add the smallest side to min_size
foreach ( $product_type["boxes"] as $box ) {
$original_box = $this->findWhere( $this->boxesList, array( "name" => $box["name"] ) ); //Get the original box data
if ( $box["exclude"] == 0 ) {
$available_boxes[$box["name"]]["name"] = $box["name"];
$available_boxes[$box["name"]]["fill"] += $box["fill"] * $shipProduct["quantity"];
$available_boxes[$box["name"]]["exclude"] = 0;
//Calculate the smallest side of the packed items and check if can fit into the box
if ( $min_size > max( $original_box["measures"] ) ) {
$available_boxes[$box["name"]]["exclude"] = 1;
}
//Exclude the box if the fill rate is greater than 100%
if ( $available_boxes[$box["name"]]["fill"] > 100 )
$available_boxes[$box["name"]]["exclude"] = 1;
}
}
}
//Sort boxes from the most to the less filled
foreach ( $available_boxes as $key => $row ) {
$fill[$key] = $row["fill"];
}
array_multisort( $fill, SORT_DESC, $available_boxes );
//Filter only packages that can fit the items
if($filter)
$available_boxes = array_filter( $available_boxes, function( $el ) { return $el["exclude"] != 1; } );
$this->boxesList = $available_boxes;
}
}
?>