Exemplo n.º 1
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 ajaxProcessSaveCustomPrice()
 {
     if (Tools::getValue('id_product') < 1) {
         die(Tools::jsonEncode(array('result' => false, 'error' => 'Nessun prodotto selezionato')));
     }
     // Tools::ps_round($ecotax / (1 + Tax::getProductEcotaxRate() / 100), 6);
     $id_custom_price = (int) Tools::getValue('id_custom_price');
     $id_product = Tools::getValue('id_product');
     $id_shop = Tools::getValue('id_shop');
     $id_currency = Tools::getValue('id_currency');
     $id_country = Tools::getValue('id_country');
     $price = Tools::getValue('price_wt');
     // if ($price > 0){
     //     $product = new Product($id_product);
     //     $tax_rate = $product->getTaxesRate();
     //     $tax_rate = ($tax_rate>0) ? (($tax_rate / 100) + 1) : 1;
     //     $price = $price / $tax_rate;
     // }
     $from_quantity = Tools::getValue('from_quantity');
     $reduction = (double) Tools::getValue('reduction');
     $reduction_tax = Tools::getValue('reduction_tax');
     $reduction_type = !$reduction ? 'amount' : Tools::getValue('reduction_type');
     $reduction_type = $reduction_type == '-' ? 'amount' : $reduction_type;
     $from = Tools::getValue('from');
     if (!$from) {
         $from = '0000-00-00 00:00:00';
     } else {
         $from .= ' ' . Tools::getValue('time_from') . ':00';
     }
     $to = Tools::getValue('to');
     if (!$to) {
         $to = '0000-00-00 00:00:00';
     } else {
         $to .= ' ' . Tools::getValue('time_to') . ':00';
     }
     if ($this->context->currency->id) {
         $id_currency = (int) $this->context->currency->id;
     } else {
         $id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
     }
     $id_shop_group = Context::getContext()->shop->id_shop_group;
     $store = new AphStore(Context::getContext()->shop->id);
     $id_country = $store->id_country;
     $id_customer = 0;
     $id_group = 0;
     $id_product_attribute = 0;
     $res = false;
     if ($price == '-1' && (double) $reduction == '0') {
         die(Tools::jsonEncode(array('result' => false, 'error' => 'Non hai selezionato una riduzione')));
     } elseif ($to != '0000-00-00 00:00:00' && strtotime($to) < strtotime($from)) {
         die(Tools::jsonEncode(array('result' => false, 'error' => 'Hai selezionato un intervallo di date non valido')));
     } elseif ($reduction_type == 'percentage' && ((double) $reduction <= 0 || (double) $reduction > 100)) {
         die(Tools::jsonEncode(array('result' => false, 'error' => 'Il valore della riduzione non è valido')));
     } elseif ($this->_validateCustomPrice($id_shop, $id_product, $id_currency, null, null, null, $price, $from_quantity, $reduction, $reduction_type, $from, $to, $id_product_attribute)) {
         $customPrice = new AphCustomPrice($id_custom_price);
         $customPrice->id_product = (int) $id_product;
         $customPrice->id_product_attribute = (int) $id_product_attribute;
         $customPrice->id_shop = (int) $id_shop;
         $customPrice->id_shop_group = (int) $id_shop_group;
         $customPrice->id_currency = (int) $id_currency;
         $customPrice->id_country = (int) $id_country;
         $customPrice->id_customer = (int) $id_customer;
         $customPrice->id_group = (int) $id_group;
         $customPrice->price_wt = (double) $price;
         $customPrice->from_quantity = (int) $from_quantity;
         $customPrice->reduction = (double) $reduction;
         $customPrice->reduction_tax = $reduction_tax;
         $customPrice->reduction_type = $reduction_type;
         $customPrice->from = $from;
         $customPrice->to = $to;
         $customPrice->time_start = Tools::getValue('time_start') . ':00';
         $customPrice->time_end = Tools::getValue('time_end') . ':00';
         $customPrice->day_0 = (bool) Tools::getValue('day_0');
         $customPrice->day_1 = (bool) Tools::getValue('day_1');
         $customPrice->day_2 = (bool) Tools::getValue('day_2');
         $customPrice->day_3 = (bool) Tools::getValue('day_3');
         $customPrice->day_4 = (bool) Tools::getValue('day_4');
         $customPrice->day_5 = (bool) Tools::getValue('day_5');
         $customPrice->day_6 = (bool) Tools::getValue('day_6');
         $customPrice->removable = 1;
         if (!empty($id_custom_price)) {
             $res = $customPrice->update();
         } else {
             $res = $customPrice->add();
         }
     }
     die(Tools::jsonEncode(array('result' => $res, 'error' => 'Si e\' verificato un problema. Prego riprovare.')));
 }