limiting number of items per each box
Guys, I have a situation where I need to implement a constrained item restriction per item per specific box and I am not being able to implement it via the constrained item function.
Can anyone shed a light?
Imagine that I have 1 item that could fit in 2 different boxes, box 1 fits up to 2 and box 2 fits up to 5 items.
So I need to put a restriction that box 2 can only have up to 5 items (regardless of dimensions) and the same applies to box 1 (upt to 2 items max).
public function canBePackedInBox(ItemList $alreadyPackedItems, Box $box) {
$alreadyPackedType = array_filter(
$alreadyPackedItems->asArray(),
function (Item $item) {
return $item->getDescription() === $this->getDescription();
}
);
return count($alreadyPackedType) + 1 <= static::$limit;
}
tks in advance
hey @dvdoug , any insight here is much appreciated.
tks
Hi
Sorry, I don't think I understand your problem - the parameters to the callback (items packed in the box so far, and the box type itself) should be enough to let you handle this I'd have thought.
Can you give some more details on what's stopping you from implementing what you need?
Nice, I got it with your guidance, it was pretty simple with the constrained function:
public function canBePackedInBox(ItemList $alreadyPackedItems, Box $box) {
$alreadyPackedType = array_filter(
$alreadyPackedItems->asArray(),
function (GadolItem $item) {
return $item->getDescription() === $this->getDescription();
}
);
// checking if custom restriction per box
$maxes = $box->getMaxInBox();
// checkig if limitation for this prod exists, if so, use that as the limit
$item_id = $this->getId();
if (array_key_exists($item_id, $maxes)) {
$limit = $maxes[$item_id];
} else {
$limit = static::$limit;
}
return count($alreadyPackedType) + 1 <= $limit;
}