$stock->product_id = $stock_service->_ref_product->_id; $stock->group_id = $group_id; if ($stock->loadMatchingObject()) { $stock->updateFormFields(); $stock->_ref_stock_service = $stock_service; $stock->quantity = max(0, $stock_service->getOptimumQuantity() - $stock_service->quantity); $stocks[$stock->_id] = $stock; } } } } else { $where["product_stock_group.group_id"] = "= '{$group_id}'"; $ljoin = array('product' => 'product.product_id = product_stock_group.product_id'); $stock = new CProductStockGroup(); /** @var CProductStockGroup[] $stocks */ $stocks = $stock->seek($keywords, $where, $limit, true, $ljoin, 'product.name'); $count_stocks = $stock->_totalSeek; if ($stocks) { foreach ($stocks as $_id => $_stock) { if ($_stock->_ref_product->cancelled) { unset($stocks[$_id]); } else { $_stock->quantity = min($_stock->quantity, $_stock->getOptimumQuantity()); } } } } } // Load the already ordered dispensations foreach ($stocks as &$_stock) { $_stock->loadRefsFwd();