A while ago I wrote this relatively complex, not very efficient, page method.
Now I am using it in a pages table layout column value, but that takes a while to calculate for each page.
Other than refactoring, where can this code go to avoid recalculating on every call?
...
'isowed' => function () {
$products = $this->allTheirProducts();
$owed = 0;
if (count($products) > 0) {
foreach ($products as $product) {
$productArray = [];
$sold = 0;
foreach (page('orders')->children() as $order) {
foreach ($order->items()->toStructure() as $item) {
if ($item->id() == $product->id()) {
$sold += 1;
}
}
}
if ($sold > 0) {
$productArray['id'] = $product->id();
$productArray['sold'] = $sold;
$productPrice = $product->price()->toFloat();
$productTax = $productPrice / 100 * site()->tax()->toFloat();
$productionAndPackagingPrice = site()->productionandpackaging()->toFloat();
$productNetPrice = $productPrice - $productTax - $productionAndPackagingPrice;
$productArray['total'] = (($productNetPrice * $sold) / 100) * site()->artistpercent()->toFloat();
$owed += $productArray['total'];
}
}
}
return $owed;
},
...
Would placing it in a page model help, or that is just a way to organize methods by template?