Пример #1
0
 private function initQuantitiesForm()
 {
     if (!$this->context->controller->default_form_language) {
         $this->languages = $this->context->controller->getLanguages();
     }
     if ($this->product->product_id) {
         if ($this->product_exists_in_shop) {
             //Get all product_attribute_id
             $attributes = $this->product->getAttributesResume($this->context->language->lang_id);
             if (empty($attributes)) {
                 $attributes[] = new JObject();
                 $attributes[0]->set('product_attribute_id', 0);
                 $attributes[0]->set('attribute_designation', '');
             }
             /** get available quantities **/
             $available_quantity = array();
             $product_designation = array();
             foreach ($attributes as $attribute) {
                 $product_attribute_id = is_object($attribute) ? $attribute->product_attribute_id : $attribute['product_attribute_id'];
                 $attribute_designation = is_object($attribute) ? $attribute->attribute_designation : $attribute['attribute_designation'];
                 // Get available quantity for the current product attribute in the current shop
                 $available_quantity[$product_attribute_id] = JeproshopStockAvailableModelStockAvailable::getQuantityAvailableByProduct((int) $this->product->product_id, $product_attribute_id);
                 // Get all product designation
                 $product_designation[$product_attribute_id] = rtrim($this->product->name[$this->context->language->lang_id] . ' - ' . $attribute_designation, ' - ');
             }
             $show_quantities = true;
             $shop_context = JeproshopShopModelShop::getShopContext();
             $shop_group = new JeproshopShopGroupModelShopGroup((int) JeproshopShopModelShop::getContextShopGroupID());
             // if we are in all shops context, it's not possible to manage quantities at this level
             if (JeproshopShopModelShop::isFeaturePublished() && $shop_context == JeproshopShopModelShop::CONTEXT_ALL) {
                 $show_quantities = false;
                 // if we are in group shop context
             } elseif (JeproshopShopModelShop::isFeaturePublished() && $shop_context == JeproshopShopModelShop::CONTEXT_GROUP) {
                 // if quantities are not shared between shops of the group, it's not possible to manage them at group level
                 if (!$shop_group->share_stock) {
                     $show_quantities = false;
                 }
             } else {
                 // if we are in shop context
                 // if quantities are shared between shops of the group, it's not possible to manage them for a given shop
                 if ($shop_group->share_stock) {
                     $show_quantities = false;
                 }
             }
             $stock_management = JeproshopSettingModelSetting::getValue('stock_management');
             $this->assignRef('stock_management', $stock_management);
             $has_attribute = $this->product->hasAttributes();
             $this->assignRef('has_attribute', $has_attribute);
             // Check if product has combination, to display the available date only for the product or for each combination
             $db = JFactory::getDBO();
             if (JeproshopCombinationModelCombination::isFeaturePublished()) {
                 $query = "SELECT COUNT(product_id) FROM " . $db->quoteName('#__jeproshop_product_attribute') . " WHERE ";
                 $query .= " product_id = " . (int) $this->product->product_id;
                 $db->setQuery($query);
                 $countAttributes = (int) $db->loadResult();
             } else {
                 $countAttributes = false;
             }
             $this->assignRef('count_attributes', $countAttributes);
             // if advanced stock management is active, checks associations
             $advanced_stock_management_warning = false;
             if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && $this->product->advanced_stock_management) {
                 $product_attributes = JeproshopProductModelProduct::getProductAttributesIds($this->product->product_id);
                 $warehouses = array();
                 if (!$product_attributes) {
                     $warehouses[] = JeproshopWarehouseModelWarehouse::getProductWarehouseList($this->product->product_id, 0);
                 }
                 foreach ($product_attributes as $product_attribute) {
                     $ws = JeproshopWarehouseModelWarehouse::getProductWarehouseList($this->product->product_id, $product_attribute->product_attribute_id);
                     if ($ws) {
                         $warehouses[] = $ws;
                     }
                 }
                 $warehouses = JeproshopTools::arrayUnique($warehouses);
                 if (empty($warehouses)) {
                     $advanced_stock_management_warning = true;
                 }
             }
             if ($advanced_stock_management_warning) {
                 JError::raiseWarning(500, JText::_('If you wish to use the advanced stock management, you must:'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your products with warehouses.'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your warehouses with carriers.'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your warehouses with the appropriate shops.'));
             }
             $pack_quantity = null;
             // if product is a pack
             if (JeproshopProductPack::isPack($this->product->product_id)) {
                 $items = JeproshopProductPack::getItems((int) $this->product->product_id, JeproshopSettingModelSetting::getValue('default_lang'));
                 // gets an array of quantities (quantity for the product / quantity in pack)
                 $pack_quantities = array();
                 foreach ($items as $item) {
                     if (!$item->isAvailableWhenOutOfStock((int) $item->out_of_stock)) {
                         $pack_id_product_attribute = JeproshopProductModelProduct::getDefaultAttribute($item->product_id, 1);
                         $pack_quantities[] = JeproshopProductModelProduct::getQuantity($item->id, $pack_id_product_attribute) / ($item->pack_quantity !== 0 ? $item->pack_quantity : 1);
                     }
                 }
                 // gets the minimum
                 if (count($pack_quantities)) {
                     $pack_quantity = $pack_quantities[0];
                     foreach ($pack_quantities as $value) {
                         if ($pack_quantity > $value) {
                             $pack_quantity = $value;
                         }
                     }
                 }
                 if (!JeproshopWarehouseModelWarehouse::getPackWarehouses((int) $this->product->product_id)) {
                     $this->displayWarning($this->l('You must have a common warehouse between this pack and its product.'));
                 }
             }
             $this->assignRef('attributes', $attributes);
             $this->assignRef('available_quantity', $available_quantity);
             $this->assignRef('pack_quantity', $pack_quantity);
             $stock_management_active = JeproshopSettingModelSetting::getValue('advanced_stock_management');
             $this->assignRef('stock_management_active', $stock_management_active);
             $this->assignRef('product_designation', $product_designation);
             $this->assignRef('show_quantities', $show_quantities);
             $order_out_of_stock = JeproshopSettingModelSetting::getValue('allow_out_of_stock_ordering');
             $this->assignRef('order_out_of_stock', $order_out_of_stock);
             /*'token_preferences' => Tools::getAdminTokenLite('AdminPPreferences'),
                       'token' => $this->token,
                       'languages' => $this->_languages,
                       'id_lang' => $this->context->language->id
               ));*/
         } else {
             JError::raiseWarning(500, JText::_('You must save the product in this shop before managing quantities.'));
         }
     } else {
         JError::raiseWarning(500, JText::_('You must save this product before managing quantities.'));
     }
 }
Пример #2
0
 /**
  * For a given product id, sets if product is available out of stocks
  *
  * @param int $product_id
  * @param bool $out_of_stock Optional false by default
  * @param int $shop_id Optional gets context by default
  * @param int $product_attribute_id
  * @return bool
  */
 public static function setProductOutOfStock($product_id, $out_of_stock = false, $shop_id = null, $product_attribute_id = 0)
 {
     if (!JeproshopTools::isUnsignedInt($product_id)) {
         return false;
     }
     $db = JFactory::getDBO();
     $existing_id = JeproshopStockAvailableModelStockAvailable::getStockAvailableIdByProductId((int) $product_id, (int) $product_attribute_id, $shop_id);
     if ($existing_id > 0) {
         $query = "UPDATE " . $db->quoteName('#__jeproshop_stock_available') . " SET " . $db->quoteName('out_of_stock') . " = " . (int) $out_of_stock;
         $query .= " WHERE " . $db->quoteName('product_id') . " = " . (int) $product_id . ($product_attribute_id ? " AND " . $db->quoteName('product_attribute_id') . " = " . (int) $product_attribute_id : " ");
         $query .= JeproshopStockAvailableModelStockAvailable::addShopRestriction($shop_id);
         $db->setQuery($query);
         $db->query();
     } else {
         $context = JeproshopContext::getContext();
         $groupOk = false;
         // get shop group too
         if ($shop_id === null) {
             if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_GROUP) {
                 $shop_group = JeproshopShopModelShop::getContextShopGroupID();
             } else {
                 $shop_group = $context->shop->getShopGroup();
                 $shop_id = $context->shop->shop_id;
             }
         } else {
             $shop = new JeproshopShopModelShop($shop_id);
             $shop_group = $shop->getShopGroup();
         }
         // if quantities are shared between shops of the group
         if ($shop_group->share_stock) {
             $fields = ", " . $db->quoteName('shop_group_id') . ", " . $db->quoteName('shop_d');
             $values = ", " . (int) $shop_group->shop_group_id . ", 0";
             $groupOk = true;
         } else {
             $fields = ", " . $db->quoteName('shop_group_id');
             $values = ", 0";
         }
         // if no group specific restriction, set simple shop restriction
         if (!$groupOk) {
             $fields = ", " . $db->quoteName('shop_id');
             $values = ", " . (int) $shop_id;
         }
         $query = "INSERT INTO " . $db->quoteName('#__jeproshop_stock_available') . "(" . $db->quoteName('out_of_stock') . ", " . $db->quoteName('product_id');
         $query .= ", " . $db->quoteName('product_attribute_id') . $fields . ") VALUES (" . (int) $out_of_stock . ", " . (int) $product_id . ", " . $product_attribute_id . $values . ")";
         $db->setQuery($query);
         $db->query();
     }
 }
Пример #3
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;
 }