/** * Returns the existing location for the product in the host, * if it doesn't exist, will return the first location found in the host * * @param CGroups|CService|CBlocOperatoire|CMbObject $host Stock location's host object * @param CProduct $product Product * * @return CProductStockLocation The location */ static function getDefaultLocation(CMbObject $host, CProduct $product = null) { $stock_class = self::getStockClass($host->_class); /** @var CProductStock $stock */ $stock = new $stock_class(); $stock->setHost($host); $stock->product_id = $product->_id; $stock->loadMatchingObject(); if (!$stock->_id || !$stock->location_id) { $ds = $host->_spec->ds; $where = array("object_class" => $ds->prepare("=%", $host->_class), "object_id" => $ds->prepare("=%", $host->_id)); // pas loadMatchingObject a cause du "position" pré-rempli :( $location = new CProductStockLocation(); if (!$location->loadObject($where, "position")) { $location->name = "Lieu par défaut"; $location->group_id = $host instanceof CGroups ? $host->_id : $host->group_id; $location->setObject($host); $location->store(); } return $location; } else { return $stock->loadRefLocation(); } }