public static function deleteByProductId($id_product)
 {
     if (Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_price` WHERE `id_product` = ' . (int) $id_product)) {
         // Refresh cache of feature detachable
         Configuration::updateGlobalValue('PS_SPECIFIC_PRICE_FEATURE_ACTIVE', AphCustomPrice::isCurrentlyUsed('custom_price'));
         return true;
     }
     return false;
 }
Example #2
0
 public static function priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency, $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $with_ecotax, &$specific_price, $use_group_reduction, $id_customer = 0, $use_customer_price = true, $id_cart = 0, $real_quantity = 0, $service_date = null, $service_time_from = null, $service_time_to = null)
 {
     require_once dirname(__FILE__) . '/../../modules/aphrodinet/classes/AphCustomPrice.php';
     static $address = null;
     static $context = null;
     if ($address === null) {
         $address = new Address();
     }
     if ($context == null) {
         $context = Context::getContext()->cloneContext();
     }
     if ($id_shop !== null && $context->shop->id != (int) $id_shop) {
         $context->shop = new Shop((int) $id_shop);
     }
     if (!$use_customer_price) {
         $id_customer = 0;
     }
     if ($id_product_attribute === null) {
         $id_product_attribute = Product::getDefaultAttribute($id_product);
     }
     if (!empty($id_cart)) {
         $res = Db::getInstance()->executeS('SELECT `delivery_date`, `delivery_time_from`, `delivery_time_to` ' . 'FROM ' . _DB_PREFIX_ . 'cart_product ' . 'WHERE `id_cart` = ' . (int) $id_cart . ' AND `id_product` = ' . (int) $id_product . ' AND `id_product_attribute` = ' . (int) $id_product_attribute . ' ' . 'LIMIT 1');
         if (!empty($res) && empty($service_date)) {
             $res = $res[0];
             if (!empty($res)) {
                 $service_date = $res['delivery_date'];
                 $service_time_from = $res['delivery_time_from'];
                 $service_time_to = $res['delivery_time_to'];
             }
         }
     }
     if (!empty($service_time_from) && strlen($service_time_from) == 5) {
         $service_time_from .= ':00';
     }
     if (!empty($service_time_to) && strlen($service_time_to) == 5) {
         $service_time_to .= ':00';
     }
     $cache_id = (int) $id_product . '-' . (int) $id_shop . '-' . (int) $id_currency . '-' . (int) $id_country . '-' . $id_state . '-' . $zipcode . '-' . (int) $id_group . '-' . (int) $quantity . '-' . (int) $id_product_attribute . '-' . (int) $with_ecotax . '-' . (int) $id_customer . '-' . (int) $use_group_reduction . '-' . (int) $id_cart . '-' . (int) $real_quantity . '-' . ($only_reduc ? '1' : '0') . '-' . ($use_reduc ? '1' : '0') . '-' . ($use_tax ? '1' : '0') . '-' . (int) $decimals . (!empty($service_date) && $service_date != '0000-00-00' ? '-' . $service_date : '') . (!empty($service_time_to) && $service_time_to != '00:00:00' ? '-' . $service_time_from : '') . (!empty($service_time_to) && $service_time_to != '00:00:00' ? '-' . $service_time_to : '');
     // reference parameter is filled before any returns
     $specific_price = SpecificPrice::getSpecificPrice((int) $id_product, $id_shop, $id_currency, $id_country, $id_group, $quantity, $id_product_attribute, $id_customer, $id_cart, $real_quantity);
     if (isset(self::$_prices[$cache_id])) {
         if (isset($specific_price['price']) && $specific_price['price'] > 0) {
             $specific_price['price'] = self::$_prices[$cache_id];
         }
         return self::$_prices[$cache_id];
     }
     $cache_id_2 = $id_product . '-' . $id_shop . (!empty($service_date) && $service_date != '0000-00-00' ? '-' . $service_date : '') . (!empty($service_time_to) && $service_time_to != '00:00:00' ? '-' . $service_time_from : '') . (!empty($service_time_to) && $service_time_to != '00:00:00' ? '-' . $service_time_to : '');
     if (!isset(self::$_pricesLevel2[$cache_id_2])) {
         $base_price_wt = 0;
         $service_date_ts = strtotime($service_date);
         $service_date_week_day = date('w', $service_date_ts);
         if (!empty($service_date) && $service_date != '0000-00-00') {
             // cerco il prezzo del giorno
             $custom_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1, NULL, 1);
             if (!empty($custom_prices)) {
                 $base_price_wt = $custom_prices[0]['price_wt'];
             }
         }
         if (!empty($service_date) && $service_date != '0000-00-00') {
             $offer_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1, 0, 1);
         }
         if (!empty($service_date) && $service_date != '0000-00-00') {
             $offer_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, $service_date_week_day, 0, 1);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, $service_date_week_day);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && !empty($service_time_to) && $service_time_to != '00:00:00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1, $service_time_from);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && !empty($service_time_to) && $service_time_to != '00:00:00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate($id_shop, $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, $service_date_week_day, $service_time_from);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate(Configuration::get('PS_SHOP_DEFAULT'), $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate(Configuration::get('PS_SHOP_DEFAULT'), $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, $service_date_week_day);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && !empty($service_time_to) && $service_time_to != '00:00:00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate(Configuration::get('PS_SHOP_DEFAULT'), $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, -1, $service_time_from);
         }
         if (!empty($service_date) && $service_date != '0000-00-00' && !empty($service_time_to) && $service_time_to != '00:00:00' && empty($custom_prices) && empty($offer_prices)) {
             $custom_prices = AphCustomPrice::getByDate(Configuration::get('PS_SHOP_DEFAULT'), $id_currency, $id_country, $id_group, $service_date, $id_customer, (int) $id_product, $service_date_week_day, $service_time_from);
         }
         if (!empty($offer_prices)) {
             $res = $offer_prices;
         } elseif (!empty($custom_prices)) {
             $res = $custom_prices;
         } else {
             $sql = new DbQuery();
             $sql->select('product_shop.`price`, product_shop.`ecotax`');
             $sql->from('product', 'p');
             $sql->innerJoin('product_shop', 'product_shop', '(product_shop.id_product=p.id_product AND product_shop.id_shop = ' . (int) $id_shop . ')');
             $sql->where('p.`id_product` = ' . (int) $id_product);
             if (Combination::isFeatureActive()) {
                 $sql->select('IFNULL(product_attribute_shop.id_product_attribute,0) id_product_attribute, product_attribute_shop.`price` AS attribute_price, product_attribute_shop.default_on');
                 $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.id_product = p.id_product AND product_attribute_shop.id_shop = ' . (int) $id_shop . ')');
             } else {
                 $sql->select('0 as id_product_attribute');
             }
             $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
         }
         if (is_array($res) && count($res)) {
             foreach ($res as $row) {
                 if ($row['price'] < 0) {
                     if ($row['reduction_type'] == 'amount') {
                         $row['price'] = 0;
                         //$base_price_wt - $row['reduction'];
                     } else {
                         $row['price'] = 0;
                         //$base_price_wt - (($row['reduction'] * $base_price_wt) / 100);
                     }
                 } else {
                     $row['price'] = 0;
                     //$row['price_wt'];
                 }
                 $array_tmp = array('price' => $row['price'], 'ecotax' => !empty($row['ecotax']), 'attribute_price' => isset($row['attribute_price']) ? $row['attribute_price'] : null);
                 self::$_pricesLevel2[$cache_id_2][(int) $row['id_product_attribute']] = $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) $id_product_attribute])) {
         return;
     }
     $result = self::$_pricesLevel2[$cache_id_2][(int) $id_product_attribute];
     if (!$specific_price || $specific_price['price'] < 0) {
         $price = (double) $result['price'];
     } else {
         $price = (double) $specific_price['price'];
     }
     if (!$specific_price || !($specific_price['price'] >= 0 && $specific_price['id_currency'])) {
         $price = Tools::convertPrice($price, $id_currency);
         if (isset($specific_price['price'])) {
             $specific_price['price'] = $price;
         }
     }
     if (is_array($result) && (!$specific_price || !$specific_price['id_product_attribute'] || $specific_price['price'] < 0)) {
         $attribute_price = Tools::convertPrice($result['attribute_price'] !== null ? (double) $result['attribute_price'] : 0, $id_currency);
         if ($id_product_attribute !== false) {
             $price += $attribute_price;
         }
     }
     $address->id_country = $id_country;
     $address->id_state = $id_state;
     $address->postcode = $zipcode;
     $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int) $id_product, $context));
     $product_tax_calculator = $tax_manager->getTaxCalculator();
     if ($use_tax) {
         $price = $product_tax_calculator->addTaxes($price);
     }
     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 ($id_currency) {
             $ecotax = Tools::convertPrice($ecotax, $id_currency);
         }
         if ($use_tax) {
             $tax_manager = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'));
             $ecotax_tax_calculator = $tax_manager->getTaxCalculator();
             $price += $ecotax_tax_calculator->addTaxes($ecotax);
         } else {
             $price += $ecotax;
         }
     }
     $specific_price_reduction = 0;
     if (($only_reduc || $use_reduc) && $specific_price) {
         if ($specific_price['reduction_type'] == 'amount') {
             $reduction_amount = $specific_price['reduction'];
             if (!$specific_price['id_currency']) {
                 $reduction_amount = Tools::convertPrice($reduction_amount, $id_currency);
             }
             $specific_price_reduction = $reduction_amount;
             if (!$use_tax && $specific_price['reduction_tax']) {
                 $specific_price_reduction = $product_tax_calculator->removeTaxes($specific_price_reduction);
             }
             if ($use_tax && !$specific_price['reduction_tax']) {
                 $specific_price_reduction = $product_tax_calculator->addTaxes($specific_price_reduction);
             }
         } else {
             $specific_price_reduction = $price * $specific_price['reduction'];
         }
     }
     if ($use_reduc) {
         $price -= $specific_price_reduction;
     }
     if ($use_group_reduction) {
         $reduction_from_category = GroupReduction::getValueForProduct($id_product, $id_group);
         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 = ($reduc = Group::getReductionByIdGroup($id_group)) != 0 ? $price * $reduc / 100 : 0;
         }
         $price -= $group_reduction;
     }
     if ($only_reduc) {
         return Tools::ps_round($specific_price_reduction, $decimals);
     }
     $price = Tools::ps_round($price, $decimals);
     if ($price < 0) {
         $price = 0;
     }
     self::$_prices[$cache_id] = $price;
     return self::$_prices[$cache_id];
 }
Example #3
0
 public function changeCustomPrices($shops_id, $products_id)
 {
     if (!empty($shops_id) && !empty($products_id) && is_array($shops_id) && is_array($products_id)) {
         foreach ($shops_id as $shop_id) {
             $store = new AphStore($shop_id);
             $id_country = $store->id_country;
             foreach ($products_id as $product_id) {
                 $customPrice = new AphCustomPrice();
                 $customPrice->id_shop = $shop_id;
                 $customPrice->id_shop_group = 0;
                 $customPrice->id_specific_price_rule = 0;
                 $customPrice->id_product = $product_id;
                 $customPrice->id_product_attribute = 0;
                 $customPrice->id_offer = $this->id;
                 $customPrice->id_currency = Context::getContext()->currency->id;
                 $customPrice->id_country = $id_country;
                 $customPrice->id_customer = 0;
                 $customPrice->id_group = 0;
                 $customPrice->from_quantity = 1;
                 $customPrice->price = -1;
                 $customPrice->price_wt = $this->price_wt;
                 $customPrice->reduction = $this->reduction;
                 $customPrice->reduction_tax = true;
                 $customPrice->reduction_type = $this->reduction_type;
                 $customPrice->from = $this->available_from_date;
                 $customPrice->to = $this->available_to_date;
                 $customPrice->time_start = $this->time_start;
                 $customPrice->time_end = $this->time_end;
                 $customPrice->day_0 = $this->day_0;
                 $customPrice->day_1 = $this->day_1;
                 $customPrice->day_2 = $this->day_2;
                 $customPrice->day_3 = $this->day_3;
                 $customPrice->day_4 = $this->day_4;
                 $customPrice->day_5 = $this->day_5;
                 $customPrice->day_6 = $this->day_6;
                 $res = $customPrice->add();
                 $customPrice = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
             SELECT `id_custom_price`
             FROM `' . _DB_PREFIX_ . 'custom_price`
             WHERE `id_shop` =' . $customPrice->id_shop . ' AND
                 `id_currency` =' . $customPrice->id_currency . ' AND
                 `id_country` =' . $customPrice->id_country . ' AND
                 `id_group` =' . $customPrice->id_group . ' AND
                 `id_product` =' . $customPrice->id_product . ' AND
                 `from_quantity` = 1 AND
                 `from` ="' . $this->available_from_date . '" AND
                 `to` ="' . $this->available_to_date . '" LIMIT 1');
             }
         }
     }
     return !empty($customPrice[0]['id_custom_price']) ? $customPrice[0]['id_custom_price'] : 0;
 }
 public function ajaxProcessDeleteCustomPrice()
 {
     $id_custom_price = Tools::getValue('id_custom_price');
     $customPrice = new AphCustomPrice($id_custom_price);
     $res = $customPrice->delete();
     die(Tools::jsonEncode(array('result' => $res)));
 }