Example #1
0
 /**
  *
  * This method allow to add stock information on a product detail
  *
  * If advanced stock management is active, get physical stock of this product in the warehouse associated to the product for the current order
  * Else get the available quantity of the product in function of the shop associated to the order
  *
  * @param array &$product
  */
 protected function setProductCurrentStock(&$product)
 {
     if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && (int) $product->advanced_stock_management == 1 && (int) $product->warehouse_id > 0) {
         $product->current_stock = JeproshopStockManagerFactory::getManager()->getProductPhysicalQuantities($product->product_id, $product->product_attribute_id, (int) $product->warehouse_id, true);
     } else {
         $product->current_stock = JeproshopStockAvailableModelStockAvailable::getQuantityAvailableByProduct($product->product_id, $product->product_attribute_id, (int) $this->shop_id);
     }
 }
Example #2
0
 public function delete()
 {
     $app = JFactory::getApplication();
     $product_id = $app->input->get('product_id');
     $product = new JeproshopProductModelProduct((int) $product_id);
     if (JeproshopTools::isLoadedObject($product, 'product_id') && isset($this->fieldImageSettings)) {
         // check if request at least one object with noZeroObject
         if (isset($product->noZeroObject) && count($taxes = call_user_func(array('JeproshopProductModelProduct', $product->noZeroObject))) <= 1) {
             $this->has_errors = true;
             JError::raiseError(500, JText::_('COM_JEPROSHOP_YOU_NEED_AT_LEAST_ONE_LABEL') . ' <b> ' . JText::_('COM_JEPROSHOP_PRODUCT_LABEL') . ' </b><br />' . JText::_('COM_JEPROSHOP_YOU_CANNOT_DELETE_ALL_OF_THE_ITEMS_LABEL'));
         } else {
             /*
              * @since 1.5.0
              * It is NOT possible to delete a product if there are currently:
              * - physical stock for this product
              * - supply order(s) for this product
              */
             if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && $product->advanced_stock_management) {
                 $stock_manager = JeproshopStockManagerFactory::getManager();
                 $physical_quantity = $stock_manager->getProductPhysicalQuantities($product->product_id, 0);
                 $real_quantity = $stock_manager->getProductRealQuantities($product->product_id, 0);
                 if ($physical_quantity > 0 || $real_quantity > $physical_quantity) {
                     $this->has_errors = true;
                     JError::raiseError('COM_JEPROSHOP_YOU_CANNOT_DELETE_THIS_BECAUSE_THERE_IS_PHYSICAL_STOCK_LEFT_LABEL');
                 }
             }
             if (!$this->has_errors) {
                 if ($product->delete()) {
                     $category_id = (int) $app->input->get('category_id');
                     $category_url = empty($category_id) ? '' : '&category_id=' . (int) $category_id;
                     $app->enqueueMessage(JText::_('COM_JEPROSHOP_PRODUCT_LABEL') . ' ' . $product->product_id . ' ' . JText::_('COM_JEPROSHOP_WAS_SUCCESSFULLY_DELETED_BY_MESSAGE') . ' ' . JeproshopContext::getContext()->employee->firstname . ' ' . JeproshopContext::getContext()->employee->lastname);
                     $redirect_after = 'index.php?option=com_jeproshop&view=product&conf=1&' . JeproshopTools::getProductToken() . '=1' . $category_url;
                     $app->redirect($redirect_after);
                 } else {
                     $this->has_errors = true;
                     Tools::displayError('An error occurred during deletion.');
                 }
             }
         }
     } else {
         $this->has_errors = true;
         JError::raiseError(500, JText::_('COM_JEPROSHOP_AN_ERROR_OCCURRED_WHILE_DELETING_THE_PRODUCT_MESSAGE') . ' <b> ' . JText::_('COM_JEPROSHOP_PRODUCT_LABEL') . '</b> ' . JText::_('COM_JEPROSHOP_CANNOT_BE_LOADED_MESSAGE'));
     }
 }
Example #3
0
 /**
  * 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 . '_*');
 }
Example #4
0
 /**
  * It is NOT possible to delete a product if there are currently:
  * - physical stock for this product
  * - supply order(s) for this product
  **/
 public function delete()
 {
     $db = JFactory::getDBO();
     if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && $this->advanced_stock_management) {
         $stockManager = JeproshopStockManagerFactory::getManager();
         $physicalQuantity = $stockManager->getProductPhysicalQuantities($this->product_id, 0);
         $realQuantity = $stockManager->getProductRealQuantities($this->product_id, 0);
         if ($physicalQuantity > 0) {
             return false;
         }
         if ($realQuantity > $physicalQuantity) {
             return false;
         }
     }
     $this->clearCache();
     $result = true;
     if (JeproshopShopModelShop::isTableAssociated('product')) {
         $shopListIds = JeproshopShopModelShop::getContextShopGroupID();
         if (count($this->shop_list_id) > 0) {
             $shopListIds = $this->shop_list_id;
         }
         if (!is_array($shopListIds)) {
             $shopListIds = array($shopListIds);
         }
         $query = "DELETE FROM " . $db->quoteName('#__jeproshop_product_shop') . " WHERE " . $db->quoteName('product_id') . " = " . (int) $this->product_id . " AND " . $db->quoteName('shop_id') . " IN(" . implode($shopListIds) . ")";
         $db->setQuery($query);
         $result &= $db->query();
     }
     $hasMultiShopEntries = $this->hasMultishopEntries();
     if ($result && !$hasMultiShopEntries) {
         $query = "DELETE FROM " . $db->quoteName('#__jeproshop_product') . " WHERE " . $db->quoteName('product_id') . " = " . $this->product_id;
         $db->setQuery($query);
         $result &= $db->query();
     }
     if (!$result) {
         return false;
     }
     $query = "DELETE FROM " . $db->quoteName('#__jeproshop_product_lang') . " WHERE " . $db->quoteName('product_id') . " = " . $this->product_id;
     $db->setQuery($query);
     $result &= $db->query();
     JeproshopStockAvailableModelStockAvailable::removeProductFromStockAvailable($this->product_id);
     $result &= $this->deleteProductAttributes() && $this->deleteImages() && $this->deleteSceneProducts();
     if ($this->hasMultiShopEntries()) {
         return true;
     }
     if (!$result || !JeproshopGroupReductionModelGroupReduction::deleteProductReduction($this->product_id) || !$this->deleteCategories(true) || !$this->deleteProductFeatures() || !$this->deleteTags() || !$this->deleteCartProducts() || !$this->deleteAttributesImpacts() || !$this->deleteAttachments(false) || !$this->deleteCustomization() || !JeproshopSpecificPriceModelSpecificPrice::deleteByProductId((int) $this->product_id) || !$this->deletePack() || !$this->deleteProductSale() || !$this->deleteSearchIndexes() || !$this->deleteAccessories() || !$this->deleteFromAccessories() || !$this->deleteFromSupplier() || !$this->deleteDownload() || !$this->deleteFromCartRules()) {
         return false;
     }
     return true;
 }