Пример #1
0
 public static function setProductReduction($product_id, $group_id = null, $category_id, $reduction = null)
 {
     $res = true;
     JeproshopGroupReductionModelGroupReduction::deleteProductReduction((int) $product_id);
     $reductions = JeproshopGroupReductionModelGroupReduction::getGroupsByCategoryId((int) $category_id);
     if ($reductions) {
         $db = JFactory::getDBO();
         foreach ($reductions as $reduction) {
             $query = "INSERT INTO " . $db->quoteName('#__jeproshop_product_group_reduction_cache') . " (" . $db->quoteName('product_id');
             $query .= ", " . $db->quoteName('group_id') . ", " . $db->quoteName('reduction') . ") VALUES (" . (int) $product_id . ", ";
             $query .= (int) $reduction->group_id . ", " . (double) $reduction->reduction . ")";
             $db->setQuery($query);
             $res &= $db->query();
         }
     }
     return $res;
 }
Пример #2
0
 /**
  * Price calculation / Get product price
  *
  * @param integer $shop_id Shop id
  * @param integer $product_id Product id
  * @param integer $product_attribute_id Product attribute id
  * @param integer $country_id Country id
  * @param integer $state_id State id
  * @param $zipcode
  * @param integer $currency_id Currency id
  * @param integer $group_id Group id
  * @param integer $quantity Quantity Required for Specific prices : quantity discount application
  * @param boolean $use_tax with (1) or without (0) tax
  * @param integer $decimals Number of decimals returned
  * @param boolean $only_reduction Returns only the reduction amount
  * @param boolean $use_reduction Set if the returned amount will include reduction
  * @param boolean $with_ecotax insert ecotax in price output.
  * @param $specific_price
  * @param $use_group_reduction
  * @param int $customer_id
  * @param bool $use_customer_price
  * @param int $cart_id
  * @param int $real_quantity
  * @internal param \variable_reference $specific_price_output If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object*    If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object
  * @return float Product price
  */
 public static function priceCalculation($shop_id, $product_id, $product_attribute_id, $country_id, $state_id, $zipcode, $currency_id, $group_id, $quantity, $use_tax, $decimals, $only_reduction, $use_reduction, $with_ecotax, &$specific_price, $use_group_reduction, $customer_id = 0, $use_customer_price = true, $cart_id = 0, $real_quantity = 0)
 {
     static $address = null;
     static $context = null;
     if ($address === null) {
         $address = new JeproshopAddressModelAddress();
     }
     if ($context == null) {
         $context = JeproshopContext::getContext()->cloneContext();
     }
     if ($shop_id !== null && $context->shop->shop_id != (int) $shop_id) {
         $context->shop = new JeproshopShopModelShop((int) $shop_id);
     }
     if (!$use_customer_price) {
         $customer_id = 0;
     }
     if ($product_attribute_id === null) {
         $product_attribute_id = JeproshopProductModelProduct::getDefaultAttribute($product_id);
     }
     $cache_id = $product_id . '_' . $shop_id . '_' . $currency_id . '_' . $country_id . '_' . $state_id . '_' . $zipcode . '_' . $group_id . '_' . $quantity . '_' . $product_attribute_id . '_' . ($use_tax ? '1' : '0') . '_' . $decimals . '_' . ($only_reduction ? '1' : '0') . '_' . ($use_reduction ? '1' : '0') . '_' . $with_ecotax . '_' . $customer_id . '_' . (int) $use_group_reduction . '_' . (int) $cart_id . '-' . (int) $real_quantity;
     // reference parameter is filled before any returns
     $specific_price = JeproshopSpecificPriceModelSpecificPrice::getSpecificPrice((int) $product_id, $shop_id, $currency_id, $country_id, $group_id, $quantity, $product_attribute_id, $customer_id, $cart_id, $real_quantity);
     if (isset(self::$_prices[$cache_id])) {
         return self::$_prices[$cache_id];
     }
     $db = JFactory::getDBO();
     // fetch price & attribute price
     $cache_id_2 = $product_id . '-' . $shop_id;
     if (!isset(self::$_pricesLevel2[$cache_id_2])) {
         $select = "SELECT product_shop." . $db->quoteName('price') . ", product_shop." . $db->quoteName('ecotax');
         $from = $db->quoteName('#__jeproshop_product') . " AS product INNER JOIN " . $db->quoteName('#__jeproshop_product_shop');
         $from .= " AS product_shop ON (product_shop.product_id =product.product_id AND product_shop.shop_id = " . (int) $shop_id . ")";
         if (JeproshopCombinationModelCombination::isFeaturePublished()) {
             $select .= ", product_attribute_shop.product_attribute_id, product_attribute_shop." . $db->quoteName('price') . " AS attribute_price, product_attribute_shop.default_on";
             $leftJoin = " LEFT JOIN " . $db->quoteName('#__jeproshop_product_attribute') . " AS product_attribute ON product_attribute.";
             $leftJoin .= $db->quoteName('product_id') . " = product." . $db->quoteName('product_id') . " LEFT JOIN " . $db->quoteName('#__jeproshop_product_attribute_shop');
             $leftJoin .= " AS product_attribute_shop ON (product_attribute_shop.product_attribute_id = product_attribute.product_attribute_id AND product_attribute_shop.shop_id = " . (int) $shop_id . ")";
         } else {
             $select .= ", 0 as product_attribute_id";
             $leftJoin = "";
         }
         $query = $select . " FROM " . $from . $leftJoin . " WHERE product." . $db->quoteName('product_id') . " = " . (int) $product_id;
         $db->setQuery($query);
         $results = $db->loadObjectList();
         foreach ($results as $row) {
             $array_tmp = array('price' => $row->price, 'ecotax' => $row->ecotax, 'attribute_price' => isset($row->attribute_price) ? $row->attribute_price : null);
             self::$_pricesLevel2[$cache_id_2][(int) $row->product_attribute_id] = $array_tmp;
             if (isset($row->default_on) && $row->default_on == 1) {
                 self::$_pricesLevel2[$cache_id_2][0] = $array_tmp;
             }
         }
     }
     if (!isset(self::$_pricesLevel2[$cache_id_2][(int) $product_attribute_id])) {
         return;
     }
     $result = self::$_pricesLevel2[$cache_id_2][(int) $product_attribute_id];
     if (!$specific_price || $specific_price->price < 0) {
         $price = (double) $result['price'];
     } else {
         $price = (double) $specific_price->price;
     }
     // convert only if the specific price is in the default currency (id_currency = 0)
     if (!$specific_price || !($specific_price->price >= 0 && $specific_price->currency_id)) {
         $price = JeproshopTools::convertPrice($price, $currency_id);
     }
     // Attribute price
     if (is_array($result) && (!$specific_price || !$specific_price->product_attribute_id || $specific_price->price < 0)) {
         $attribute_price = JeproshopTools::convertPrice($result['attribute_price'] !== null ? (double) $result['attribute_price'] : 0, $currency_id);
         // If you want the default combination, please use NULL value instead
         if ($product_attribute_id !== false) {
             $price += $attribute_price;
         }
     }
     // Tax
     $address->country_id = $country_id;
     $address->state_id = $state_id;
     $address->postcode = $zipcode;
     $tax_manager = JeproshopTaxManagerFactory::getManager($address, JeproshopProductModelProduct::getTaxRulesGroupIdByProductId((int) $product_id, $context));
     $product_tax_calculator = $tax_manager->getTaxCalculator();
     // Add Tax
     if ($use_tax) {
         $price = $product_tax_calculator->addTaxes($price);
     }
     // Reduction
     $specific_price_reduction = 0;
     if (($only_reduction || $use_reduction) && $specific_price) {
         if ($specific_price->reduction_type == 'amount') {
             $reduction_amount = $specific_price->reduction;
             if (!$specific_price->currency_id) {
                 $reduction_amount = JeproshopTools::convertPrice($reduction_amount, $currency_id);
             }
             $specific_price_reduction = !$use_tax ? $product_tax_calculator->removeTaxes($reduction_amount) : $reduction_amount;
         } else {
             $specific_price_reduction = $price * $specific_price->reduction;
         }
     }
     if ($use_reduction) {
         $price -= $specific_price_reduction;
     }
     // Group reduction
     if ($use_group_reduction) {
         $reduction_from_category = JeproshopGroupReductionModelGroupReduction::getValueForProduct($product_id, $group_id);
         if ($reduction_from_category !== false) {
             $group_reduction = $price * (double) $reduction_from_category;
         } else {
             // apply group reduction if there is no group reduction for this category
             $group_reduction = ($reduction = JeproshopGroupModelGroup::getReductionByGroupId($group_id)) != 0 ? $price * $reduction / 100 : 0;
         }
     } else {
         $group_reduction = 0;
     }
     if ($only_reduction) {
         return JeproshopTools::roundPrice($group_reduction + $specific_price_reduction, $decimals);
     }
     if ($use_reduction) {
         $price -= $group_reduction;
     }
     // Eco Tax
     if (($result['ecotax'] || isset($result['attribute_ecotax'])) && $with_ecotax) {
         $ecotax = $result['ecotax'];
         if (isset($result['attribute_ecotax']) && $result['attribute_ecotax'] > 0) {
             $ecotax = $result['attribute_ecotax'];
         }
         if ($currency_id) {
             $ecotax = JeproshopTools::convertPrice($ecotax, $currency_id);
         }
         if ($use_tax) {
             // reinit the tax manager for ecotax handling
             $tax_manager = JeproshopTaxManagerFactory::getManager($address, (int) JeproshopSettingModelSetting::getValue('ecotax_tax_rules_group_id'));
             $ecotax_tax_calculator = $tax_manager->getTaxCalculator();
             $price += $ecotax_tax_calculator->addTaxes($ecotax);
         } else {
             $price += $ecotax;
         }
     }
     $price = JeproshopTools::roundPrice($price, $decimals);
     if ($price < 0) {
         $price = 0;
     }
     self::$_prices[$cache_id] = $price;
     return self::$_prices[$cache_id];
 }
Пример #3
0
 public function delete()
 {
     if ((int) $this->category_id === 0 || (int) $this->category_id === 1) {
         return false;
     }
     $this->clearCache();
     $allCategories = $this->getAllChildren();
     $allCategories[] = $this;
     foreach ($allCategories as $category) {
         $category->deleteLite();
         if (!$this->hasMultishopEntries()) {
             $category->deleteImage();
             $category->cleanGroups();
             $category->cleanAssoProducts();
             // Delete associated restrictions on cart rules
             JeproshopCartRuleModelCartRule::cleanProductRuleIntegrity('categories', array($category->category_id));
             JeproshopCategoryModelCategory::cleanPositions($category->parent_id);
             /* Delete Categories in GroupReduction */
             if (JeproshopGroupReductionModelGroupReduction::getGroupsReductionByCategoryId((int) $category->category_id)) {
                 JeproshopGroupReductionModelGroupReduction::deleteCategory($category->category_id);
             }
         }
     }
     /* Rebuild the nested tree */
     if (!$this->hasMultishopEntries() && (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree)) {
         JeproshopCategoryModelCategory::regenerateEntireNtree();
     }
     Hook::exec('actionCategoryDelete', array('category' => $this));
     return true;
 }
Пример #4
0
 /**
  * Assign price and tax to the template
  */
 protected function assignPriceAndTax()
 {
     $customer_id = isset($this->context->customer) ? (int) $this->context->customer->customer_id : 0;
     $group_id = (int) JeproshopGroupModelGroup::getCurrent()->group_id;
     $country_id = (int) $customer_id ? JeproshopCustomerModelCustomer::getCurrentCountry($customer_id) : JeproshopSettingModelSetting::getValue('default_country');
     $group_reduction = JeproshopGroupReductionModelGroupReduction::getValueForProduct($this->product->product_id, $group_id);
     if ($group_reduction === false) {
         $group_reduction = JeproshopGroupModelGroup::getReduction((int) $this->context->cookie->customer_id) / 100;
     }
     // Tax
     $tax = (double) $this->product->getTaxesRate(new JeproshopAddressModelAddress((int) $this->context->cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}));
     $this->assignRef('tax_rate', $tax);
     $product_price_with_tax = JeproshopProductModelProduct::getStaticPrice($this->product->product_id, true, null, 6);
     if (JeproshopProductModelProduct::$_taxCalculationMethod == COM_JEPROSHOP_TAX_INCLUDED) {
         $product_price_with_tax = JeproshopTools::roundPrice($product_price_with_tax, 2);
     }
     $product_price_without_eco_tax = (double) $product_price_with_tax - $this->product->ecotax;
     $ecotax_rate = (double) JeproshopTaxModelTax::getProductEcotaxRate($this->context->cart->{JeproshopSettingModelSetting::getValue('tax_address_type')});
     $ecotax_tax_amount = JeproshopTools::roundPrice($this->product->ecotax, 2);
     if (JeproshopProductModelProduct::$_taxCalculationMethod == COM_JEPROSHOP_TAX_INCLUDED && (int) JeproshopSettingModelSetting::getValue('use_tax')) {
         $ecotax_tax_amount = JeproshopTools::roundPrice($ecotax_tax_amount * (1 + $ecotax_rate / 100), 2);
     }
     $currency_id = (int) $this->context->cookie->currency_id;
     $product_id = (int) $this->product->product_id;
     $shop_id = $this->context->shop->shop_id;
     $quantity_discounts = JeproshopSpecificPriceModelSpecificPrice::getQuantityDiscounts($product_id, $shop_id, $currency_id, $country_id, $group_id, null, true, (int) $this->context->customer->customer_id);
     foreach ($quantity_discounts as &$quantity_discount) {
         if ($quantity_discount->product_attribute_id) {
             $combination = new JeproshopCombinationModelCombination((int) $quantity_discount->product_attribute_id);
             $attributes = $combination->getAttributesName((int) $this->context->language->lang_id);
             foreach ($attributes as $attribute) {
                 $quantity_discount->attributes = $attribute->name . ' - ';
             }
             $quantity_discount->attributes = rtrim($quantity_discount->attributes, ' - ');
         }
         if ((int) $quantity_discount->currency_id == 0 && $quantity_discount->reduction_type == 'amount') {
             $quantity_discount->reduction = JeproshopTools::convertPriceFull($quantity_discount->reduction, null, JeproshopContext::getContext()->currency);
         }
     }
     $product_price = $this->product->getPrice(JeproshopProductModelProduct::$_taxCalculationMethod == COM_JEPROSHOP_TAX_INCLUDED, false);
     $address = new JeproshopAddressModelAddress($this->context->cart->{JeproshopSettingModelSetting::getValue('tax_address_type')});
     $quantity_discounts = $this->formatQuantityDiscounts($quantity_discounts, $product_price, (double) $tax, $ecotax_tax_amount);
     $this->assignRef('quantity_discounts', $quantity_discounts);
     $this->assignRef('ecotax_tax_included', $ecotax_tax_amount);
     $ecotax_tax_excluded = JeproshopTools::roundPrice($this->product->ecotax, 2);
     $this->assignRef('ecotax_tax_excluded', $ecotax_tax_excluded);
     $this->assignRef('ecotaxTax_rate', $ecotax_rate);
     $display_price = JeproshopSettingModelSetting::getValue('display_price');
     $this->assignRef('display_price', $display_price);
     $product_price_without_eco_tax = (double) $product_price_without_eco_tax;
     $this->assignRef('product_price_without_ecotax', $product_price_without_eco_tax);
     $this->assignRef('group_reduction', $group_reduction);
     $no_tax = JeproshopTaxModelTax::taxExcludedOption() || !$this->product->getTaxesRate($address);
     $this->assignRef('no_tax', $no_tax);
     $ecotax = !count($this->errors) && $this->product->ecotax > 0 ? JeproshopTools::convertPrice((double) $this->product->ecotax) : 0;
     $this->assignRef('ecotax', $ecotax);
     $tax_enabled = JeproshopSettingModelSetting::getValue('use_tax');
     $this->assignRef('tax_enabled', $tax_enabled);
     $customer_group_without_tax = JeproshopGroupModelGroup::getPriceDisplayMethod($this->context->customer->default_group_id);
     $this->assignRef('customer_group_without_tax', $customer_group_without_tax);
 }
Пример #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
  **/
 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;
 }