/** * For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity * * @param int $product_id * @param int $order_shop_id * @return bool */ public static function synchronize($product_id, $order_shop_id = null) { if (!JeproshopTools::isUnsignedInt($product_id)) { return false; } $db = JFactory::getDBO(); // gets warehouse ids grouped by shops $warehouse_ids = JeproshopWarehouseModelWarehouse::getWarehousesGroupedByShops(); if ($order_shop_id !== null) { $order_warehouses = array(); $warehouses = JeproshopWarehouseModelWarehouse::getWarehouses(false, (int) $order_shop_id); foreach ($warehouses as $warehouse) { $order_warehouses[] = $warehouse->warehouse_id; } } // gets all product attributes ids $product_attribute_ids = array(); foreach (JeproshopProductModelProduct::getProductAttributesIds($product_id) as $product_attribute_id) { $product_attribute_ids[] = $product_attribute_id->product_attribute_id; } // Allow to order the product when out of stock? $out_of_stock = JeproshopStockAvailableModelStockAvailable::outOfStock($product_id); $manager = JeproshopStockManagerFactory::getManager(); // loops on $ids_warehouse to synchronize quantities foreach ($warehouse_ids as $shop_id => $warehouses) { // first, checks if the product depends on stock for the given shop $id_shop if (JeproshopStockAvailableModelStockAvailable::dependsOnStock($product_id, $shop_id)) { // init quantity $product_quantity = 0; // if it's a simple product if (empty($product_attribute_ids)) { $allowed_warehouse_for_product = JeproshopWarehouseModelWarehouse::getProductWarehouseList((int) $product_id, 0, (int) $shop_id); $allowed_warehouse_for_product_clean = array(); foreach ($allowed_warehouse_for_product as $warehouse) { $allowed_warehouse_for_product_clean[] = (int) $warehouse->warehouse_id; } $allowed_warehouse_for_product_clean = array_intersect($allowed_warehouse_for_product_clean, $warehouses); if ($order_shop_id != null && !count(array_intersect($allowed_warehouse_for_product_clean, $order_warehouses))) { continue; } $product_quantity = $manager->getProductRealQuantities($product_id, null, $allowed_warehouse_for_product_clean, true); /*Hook::exec('actionUpdateQuantity', array( 'id_product' => $id_product, 'id_product_attribute' => 0, 'quantity' => $product_quantity ) );*/ } else { // else this product has attributes, hence loops on $ids_product_attribute foreach ($product_attribute_ids as $product_attribute_id) { $allowed_warehouse_for_combination = JeproshopWarehouseModelWarehouse::getProductWarehouseList((int) $product_id, (int) $product_attribute_id, (int) $shop_id); $allowed_warehouse_for_combination_clean = array(); foreach ($allowed_warehouse_for_combination as $warehouse) { $allowed_warehouse_for_combination_clean[] = (int) $warehouse->warehouse_id; } $allowed_warehouse_for_combination_clean = array_intersect($allowed_warehouse_for_combination_clean, $warehouses); if ($order_shop_id != null && !count(array_intersect($allowed_warehouse_for_combination_clean, $order_warehouses))) { continue; } $quantity = $manager->getProductRealQuantities($product_id, $product_attribute_id, $allowed_warehouse_for_combination_clean, true); $query = new DbQuery(); $query->select('COUNT(*)'); $query->from('stock_available'); $query->where('id_product = ' . (int) $product_id . ' AND id_product_attribute = ' . (int) $product_attribute_id . StockAvailable::addSqlShopRestriction(null, $shop_id)); if ((int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query)) { $query = array('table' => 'stock_available', 'data' => array('quantity' => $quantity), 'where' => 'id_product = ' . (int) $product_id . ' AND id_product_attribute = ' . (int) $product_attribute_id . StockAvailable::addSqlShopRestriction(null, $shop_id)); Db::getInstance()->update($query['table'], $query['data'], $query['where']); } else { $query = array('table' => 'stock_available', 'data' => array('quantity' => $quantity, 'depends_on_stock' => 1, 'out_of_stock' => $out_of_stock, 'id_product' => (int) $id_product, 'id_product_attribute' => (int) $id_product_attribute)); StockAvailable::addSqlShopParams($query['data']); Db::getInstance()->insert($query['table'], $query['data']); } $product_quantity += $quantity; Hook::exec('actionUpdateQuantity', array('id_product' => $id_product, 'id_product_attribute' => $id_product_attribute, 'quantity' => $quantity)); } } // updates // if $id_product has attributes, it also updates the sum for all attributes $query = array('table' => 'stock_available', 'data' => array('quantity' => $product_quantity), 'where' => 'id_product = ' . (int) $id_product . ' AND id_product_attribute = 0' . StockAvailable::addSqlShopRestriction(null, $shop_id)); Db::getInstance()->update($query['table'], $query['data'], $query['where']); } } // In case there are no warehouses, removes product from StockAvailable if (count($warehouse_ids) == 0 && JeproshopStockAvailableModelStockAvailable::dependsOnStock((int) $product_id)) { $query = "UPDATE " . $db->quoteName('#__jeproshop_stock_available') . " SET " . $db->quoteName('quantity') . " = 0 "; $query .= " WHERE " . $db->quoteName('product_id') . " = " . (int) $product_id; $db->setQuery($query); $db->query(); } JeproshopCache::clean('jeproshop_stock_available_get_quantity_available_by_product_' . (int) $product_id . '_*'); }
/** * Post treatment for warehouses */ public function processWarehouses() { $app = JFactory::getApplication(); $product = new JeproshopProductModelProduct((int) $app->input->get('product_id')); if ((int) $app->input->get('warehouse_loaded') === 1 && JeproshopTools::isLoadedObject($product, 'product_id')) { // Get all id_product_attribute $warehouse_attributes = $product->getAttributesResume($this->context->language->lang_id); if (empty($warehouse_attributes)) { $attribute = new JObject(); $attribute->set('product_attribute_id', 0); $attribute->set('attribute_designation', ''); $warehouse_attributes[] = $attribute; } // Get all available warehouses $warehouses = JeproshopWarehouseModelWarehouse::getWarehouses(true); // Get already associated warehouses $associated_warehouses_collection = JeproshopWarehouseProductLocationModelWarehouseProductLocation::getCollection($product->product_id); $elements_to_manage = array(); // get form information foreach ($warehouse_attributes as $attribute) { foreach ($warehouses as $warehouse) { $key = $warehouse->warehouse_id . '_' . $product->product_id . '_' . $attribute->product_attribute_id; // get elements to manage if ($app->input->get('check_warehouse_' . $key)) { $location = $app->input->get('location_warehouse_' . $key, ''); $elements_to_manage[$key] = $location; } } } // Delete entry if necessary foreach ($associated_warehouses_collection as $awc) { if (!array_key_exists($awc->warehouse_id . '_' . $awc->product_id . '_' . $awc->product_attribute_id, $elements_to_manage)) { $awc->delete(); } } // Manage locations foreach ($elements_to_manage as $key => $location) { $params = explode('_', $key); $wpl_id = (int) JeproshopWarehouseProductLocationModelWarehouseProductLocation::getIdByProductAndWarehouse((int) $params[1], (int) $params[2], (int) $params[0]); if (empty($wpl_id)) { //create new record $warehouse_location_entity = new JeproshopWarehouseProductLocationModelWarehouseProductLocation(); $warehouse_location_entity->product_id = (int) $params[1]; $warehouse_location_entity->product_attribute_id = (int) $params[2]; $warehouse_location_entity->warehouse_id = (int) $params[0]; $warehouse_location_entity->location = JFactory::getDBO()->query($location); $warehouse_location_entity->save(); } else { $warehouse_location_entity = new WarehouseProductLocation((int) $wpl_id); $location = $location; if ($location != $warehouse_location_entity->location) { $warehouse_location_entity->location = $location; $warehouse_location_entity->update(); } } } JeproshopStockAvailableModelStockAvailable::synchronize((int) $product->product_id); } }