✏️ 正在编辑: DiscountTabMEP.php
路径:
/home/bpioifn/www/pigmentse/modules/masseditproduct/classes/tabs/DiscountTabMEP.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * 2007-2019 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@prestashop.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author SeoSA <885588@bk.ru> * @copyright 2012-2019 SeoSA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class DiscountTabMEP extends BaseTabMEP { public $id_currency; public $id_country; public $id_group; public $from_quantity; public $reduction; public $delete_old_discount; public $reduction_type; public $from; public $price; public $leave_base_price; public $to; public $id_shop; public $currency; public $action_for_sp; public $action_discount; public function __construct() { parent::__construct(); $this->id_currency = Tools::getValue('sp_id_currency'); $this->id_country = Tools::getValue('sp_id_country'); $this->id_group = Tools::getValue('sp_id_group'); $this->from_quantity = Tools::getValue('sp_from_quantity'); $this->reduction = (float)Tools::getValue('sp_reduction'); $this->delete_old_discount = (int)Tools::getValue('delete_old_discount'); $this->delete_old_discount_all = (int)Tools::getValue('delete_old_discount_all'); $this->reduction_type = !$this->reduction ? 'amount' : Tools::getValue('sp_reduction_type'); $this->from = Tools::getValue('sp_from'); $this->to = Tools::getValue('sp_to'); $this->price = (float)Tools::getValue('price'); $this->leave_base_price = (int)Tools::getValue('leave_base_price'); $this->action_for_sp = Tools::getValue('action_for_sp'); $this->action_discount = Tools::getValue('action_discount'); if ($this->leave_base_price) { $this->price = -1; } if (!$this->from) { $this->from = '0000-00-00 00:00:00'; } if (!$this->to) { $this->to = '0000-00-00 00:00:00'; } $this->id_shop = $this->context->cookie->__get('shopContext'); $this->id_shop = !$this->id_shop ? 0 : $this->context->shop->id; $this->currency = Currency::getCurrency(Configuration::get('PS_CURRENCY_DEFAULT')); } public function applyChangeBoth($products, $combinations) { } public function deleteOldDiscount($id_product) { if ($this->delete_old_discount && $this->checkAccessField('delete_specific_price')) { SpecificPrice::deleteByProductId((int)$id_product); } } public function applyChangeForProducts($products) { if ($this->checkAccessField('specific_price') && $this->action_for_sp == 1) { return $this->deleteSpetificPrice($products); } if ($this->checkAccessField('specific_price') && $this->action_for_sp == 1) { return $this->deleteSpetificPrice($products); } if ($this->checkAccessField('delete_specific_price_all') && $this->delete_old_discount_all && $this->checkAccessField('specific_price') == false) { foreach ($products as $id_product) { SpecificPrice::deleteByProductId((int)$id_product); } } $return_products = array(); if($this->checkAccessField('specific_price') && $this->action_for_sp == 2){ foreach ($products as $id_product) { $result = Db::getInstance()->executeS(' SELECT reduction FROM '._DB_PREFIX_.'specific_price WHERE `id_product` = '.(int)$id_product.'' ); if(count($result) > 1){ LoggerMEP::getInstance()->error(sprintf($this->l('Product №%s: not changed, more than one discount'), $id_product)); continue; } $result = Db::getInstance()->getValue(' SELECT reduction FROM '._DB_PREFIX_.'specific_price WHERE `id_product` = '.(int)$id_product.'' ); $reduction_type = Db::getInstance()->getValue(' SELECT reduction_type FROM '._DB_PREFIX_.'specific_price WHERE `id_product` = '.(int)$id_product.'' ); $price_product = Db::getInstance()->getValue(' SELECT price FROM '._DB_PREFIX_.'product WHERE `id_product` = '.(int)$id_product.'' ); if(empty($result)){ continue; } if($this->action_discount == 0){ if($this->reduction_type != $reduction_type){ continue; } if($this->reduction_type == 'amount') { $reduction = $result + $this->reduction; } if($this->reduction_type == 'percentage') { $reduction = $result + $this->reduction/100; } } if($this->action_discount == 1){ if($this->reduction_type != $reduction_type){ continue; } if($this->reduction_type == 'amount') { $reduction = $result - $this->reduction; } if($this->reduction_type == 'percentage') { $reduction = $result - $this->reduction/100; } if($reduction < 0){ LoggerMEP::getInstance()->error( sprintf( $this->l('Discount is less than zero') ) ); return false; } } if($this->reduction_type == '-'){ LoggerMEP::getInstance()->error( sprintf( $this->l('Сurrency not specified.') ) ); return false; } if($this->action_discount == 2){ if($this->reduction_type == 'percentage') { $reduction = $this->reduction/100 ; } } if($this->reduction_type == 'amount'){ $reduction = $this->reduction; if($price_product < $this->reduction){ LoggerMEP::getInstance()->error( sprintf( $this->l('Price < 0'), $id_product ) ); } } if($this->reduction_type == 'percentage'){ if($this->reduction >= 1){ LoggerMEP::getInstance()->error( sprintf( $this->l('Price < 0'), $id_product ) ); } } $sql = 'UPDATE `'._DB_PREFIX_.'specific_price` SET `reduction` = ' . $reduction . ','; $sql .= '`reduction_type` = "'. $this->reduction_type .'",'; $sql .= '`id_currency` = '. (int)$this->id_currency .','; $sql .= '`id_country` = '. (int)$this->id_country .','; $sql .= '`id_group` = '. (int)$this->id_group .','; $sql .= '`from` = "'. $this->from .'",'; $sql .= '`to` = "'. $this->to .'",'; $sql .= '`price` = '. $this->price .','; $sql .= '`id_shop` = '. 0 . ','; $sql .= '`reduction_tax` = '. $this->leave_base_price .','; $sql .= '`from_quantity` = '. (int)$this->from_quantity .''; $sql .= ' WHERE `id_product` = '.(int)$id_product.' AND `id_product_attribute` = 0'; Db::getInstance()->execute($sql); } } if ($this->checkAccessField('specific_price') && $this->action_for_sp == 0) { foreach ($products as $id_product) { $this->deleteOldDiscount($id_product); if ($this->validateSpecificPrice( (int)$id_product, $this->id_shop, $this->id_currency, $this->id_country, $this->id_group, 0, $this->price, $this->from_quantity, $this->reduction, $this->reduction_type, $this->from, $this->to, 0 )) { $specific_price = new SpecificPrice(); $specific_price->id_product = (int)$id_product; $specific_price->id_product_attribute = (int)0; $specific_price->id_shop = (int)$this->id_shop; $specific_price->id_currency = (int)$this->id_currency; $specific_price->id_country = (int)$this->id_country; $specific_price->id_group = (int)$this->id_group; $specific_price->id_customer = 0; $specific_price->price = (float)$this->price; $specific_price->from_quantity = (int)$this->from_quantity; $sp_reduction = $this->reduction_type == 'percentage' ? $this->reduction / 100 : $this->reduction; $specific_price->reduction = (float)$sp_reduction; $specific_price->reduction_type = $this->reduction_type; $specific_price->from = $this->from; $specific_price->to = $this->to; if (!$specific_price->add()) { LoggerMEP::getInstance()->error( sprintf( $this->l('Product №%s: an error occurred while updating the specific price.'), $id_product ) ); } } $return_products[$id_product] = array( 'price' => Tools::displayPrice( Product::getPriceStatic($id_product, false), $this->currency ), 'price_final' => Tools::displayPrice( Product::getPriceStatic($id_product, true), $this->currency ), ); } } return array( 'products' => $return_products ); } public function applyChangeForCombinations($products) { if ($this->checkAccessField('specific_price') && $this->action_for_sp == 1) { return $this->deleteSpetificPrice($products); } $return_combinations = array(); if($this->checkAccessField('specific_price') && $this->action_for_sp == 2){ foreach ($products as $id_product => $combinations) { foreach ($combinations as $id_pa) { $reduction_type = Db::getInstance()->getValue(' SELECT reduction_type FROM '._DB_PREFIX_.'specific_price WHERE `id_product` = ' . (int)$id_product . ' AND `id_product_attribute` = '.(int)$id_pa .'' ); $result = Db::getInstance()->getValue(' SELECT reduction FROM ' . _DB_PREFIX_ . 'specific_price WHERE `id_product` = ' . (int)$id_product . ' AND `id_product_attribute` = '.(int)$id_pa .'' ); if(empty($result)){ continue; } $price_product = Db::getInstance()->getValue(' SELECT price FROM '._DB_PREFIX_.'product WHERE `id_product` = '.(int)$id_product.'' ); $price_combi = Db::getInstance()->getValue(' SELECT price FROM '._DB_PREFIX_.'product_attribute WHERE `id_product` = ' . (int)$id_product . ' AND `id_product_attribute` = '.(int)$id_pa .'' ); if ($this->action_discount == 0) { if($this->reduction_type != $reduction_type){ continue; } if ($this->reduction_type == 'amount') { $reduction = $result + $this->reduction; } if ($this->reduction_type == 'percentage') { $reduction = $result + $this->reduction/100; } if((($price_product + $price_combi) - $this->reduction) <= 0){ LoggerMEP::getInstance()->error( sprintf( $this->l('Price is less than zero') ) ); return false; } } if ($this->action_discount == 1) { if($this->reduction_type != $reduction_type){ continue; } if ($this->reduction_type == 'amount') { $reduction = $result - $this->reduction; } if ($this->reduction_type == 'percentage') { $reduction = $result - $this->reduction/100 ; } } if($this->action_discount == 2){ if($this->reduction_type == 'percentage') { $reduction = $this->reduction/100; } } if ($this->reduction_type == '-') { LoggerMEP::getInstance()->error( sprintf( $this->l('Сurrency not specified.') ) ); return false; } if($this->reduction_type == 'amount'){ if($price_product<$reduction || $reduction < 0){ LoggerMEP::getInstance()->error( sprintf( $this->l('Price < 0'), $id_product ) ); continue; } } if($this->reduction_type == 'percentage'){ if($reduction >= 1){ LoggerMEP::getInstance()->error( sprintf( $this->l('Price < 0'), $id_product ) ); } } $sql = 'UPDATE `' . _DB_PREFIX_ . 'specific_price` SET `reduction` = ' . $reduction . ','; $sql .= '`reduction_type` = "' . $this->reduction_type . '",'; $sql .= '`id_currency` = ' . (int)$this->id_currency . ','; $sql .= '`id_country` = ' . (int)$this->id_country . ','; $sql .= '`id_group` = ' . (int)$this->id_group . ','; $sql .= '`id_product_attribute` = ' . (int)$id_pa . ','; $sql .= '`from` = "' . $this->from . '",'; $sql .= '`to` = "' . $this->to . '",'; $sql .= '`price` = ' . $this->price . ','; $sql .= '`id_shop` = '. 0 . ','; $sql .= '`reduction_tax` = ' . $this->leave_base_price . ','; $sql .= '`from_quantity` = ' . (int)$this->from_quantity . ''; $sql .= ' WHERE `id_product` = ' . (int)$id_product . ' AND `id_product_attribute` = '.(int)$id_pa .''; Db::getInstance()->execute($sql); } } } if ($this->checkAccessField('specific_price') && $this->action_for_sp == 0) { foreach ($products as $id_product => $combinations) { foreach ($combinations as $id_pa) { if ($this->validateSpecificPrice( (int)$id_product, $this->id_shop, $this->id_currency, $this->id_country, $this->id_group, $id_pa, $this->price, $this->from_quantity, $this->reduction, $this->reduction_type, $this->from, $this->to, 0 )) { $specific_price = new SpecificPrice(); $specific_price->id_product = (int)$id_product; $specific_price->id_product_attribute = (int)$id_pa; $specific_price->id_shop = (int)$this->id_shop; $specific_price->id_currency = (int)$this->id_currency; $specific_price->id_country = (int)$this->id_country; $specific_price->id_group = (int)$this->id_group; $specific_price->id_customer = 0; $specific_price->price = (float)$this->price; $specific_price->from_quantity = (int)$this->from_quantity; $sp_reduction = $this->reduction_type == 'percentage' ? $this->reduction/100 : $this->reduction; $specific_price->reduction = (float)$sp_reduction; $specific_price->reduction_type = $this->reduction_type; $specific_price->from = $this->from; $specific_price->to = $this->to; if (!$specific_price->add()) { $logger = LoggerMEP::getInstance(); $logger->error( sprintf( $this->module->l( 'Product №%s: an error occurred while updating the specific price.' ), $id_product ) ); } } $return_combinations[$id_pa] = array( 'price' => Tools::displayPrice( Product::getPriceStatic($id_product, false, $id_pa), $this->currency ), 'price_final' => Tools::displayPrice( Product::getPriceStatic($id_product, true, $id_pa), $this->currency ), ); } } } return array( 'combinations' => $return_combinations, ); } protected function deleteSpetificPrice($products) { $where = 'WHERE 1'; if (count(Shop::getCompleteListOfShopsID()) > 1) { $where .= ' AND `id_shop` = '.(int)$this->id_shop; } if (!$this->checkOptionForCombination()) { $in = implode(array_map('intval', $products), ', '); $where .= ' AND `id_product` IN ('.pSQL($in).')'; } $return_combinations = array(); if ($this->checkOptionForCombination()) { $currency = Currency::getCurrency(Configuration::get('PS_CURRENCY_DEFAULT')); $ids_combination = array(); foreach ($products as $id_product => $combinations) { foreach ($combinations as $id_combination) { $ids_combination[] = $id_combination; $return_combinations[$id_combination] = array( 'price' => Tools::displayPrice( Product::getPriceStatic($id_product, false, $id_combination), $currency ), 'price_final' => Tools::displayPrice( Product::getPriceStatic($id_product, true, $id_combination), $currency ), ); } } if (count($ids_combination) == 1) { $where .= ' AND `id_product_attribute` = '.(int)$ids_combination[0]; } else { $in = implode($ids_combination, ', '); $where .= ' AND `id_product_attribute` IN ('.pSQL($in).')'; } } if ($this->id_currency) { $where .= ' AND `id_currency` = '.(int)$this->id_currency; } if ($this->id_country) { $where .= ' AND `id_country` = '.(int)$this->id_country; } if ($this->id_group) { $where .= ' AND `id_group` = '.(int)$this->id_group; } if ($this->from) { $where .= ' AND `from` >= "'.MassEditTools::roundFromDate($this->from).'"'; } if ($this->to) { $where .= ' AND `from` <= "'.MassEditTools::roundToDate($this->to).'"'; } if (Db::getInstance(_PS_USE_SQL_SLAVE_)->execute( ' DELETE FROM `'._DB_PREFIX_.'specific_price` '.$where )) { Configuration::updateGlobalValue( 'PS_SPECIFIC_PRICE_FEATURE_ACTIVE', ObjectModel::isCurrentlyUsed('specific_price') ); } return array( 'products' => ($this->checkOptionForCombination() ? array_keys($products) : $products), 'combinations' => $return_combinations, ); } public function checkBeforeChange() { if ($this->reduction_type == 'percentage' && ((float)$this->reduction <= 0 || (float)$this->reduction > 100)) { LoggerMEP::getInstance()->error($this->l('Product №%s: submitted reduction value (0-100) is out-of-range')); } if (LoggerMEP::getInstance()->hasError()) { return false; } return true; } public function checkOptionForCombination() { $change_for = (int)Tools::getValue('change_for'); if ($change_for == self::CHANGE_FOR_COMBINATION) { return true; } return false; } public function validateSpecificPrice( $id_product, $id_shop, $id_currency, $id_country, $id_group, $id_customer, $price, $from_quantity, $reduction, $reduction_type, $from, $to, $id_combination = 0 ) { if (!Validate::isUnsignedId($id_shop) || !Validate::isUnsignedId($id_currency) || !Validate::isUnsignedId($id_country) || !Validate::isUnsignedId($id_group) || !Validate::isUnsignedId( $id_customer )) { LoggerMEP::getInstance()->error(sprintf($this->l('Product №%s: wrong IDs'), $id_product)); } elseif ((!isset($price) && !isset($reduction)) || (isset($price) && !Validate::isNegativePrice($price)) || (isset($reduction) && !Validate::isPrice($reduction))) { LoggerMEP::getInstance()->error( sprintf($this->l('Product №%s: invalid price/discount amount'), $id_product) ); } elseif (!Validate::isUnsignedInt($from_quantity)) { LoggerMEP::getInstance()->error(sprintf($this->l('Product №%s: invalid quantity'), $id_product)); } elseif ($reduction && !Validate::isReductionType($reduction_type)) { LoggerMEP::getInstance()->error( sprintf( $this->l('Product №%s: please select a discount type (amount or percentage).'), $id_product ) ); } elseif ($from && $to && (!Validate::isDateFormat($from) || !Validate::isDateFormat($to))) { LoggerMEP::getInstance()->error( sprintf($this->l('Product №%s: the from/to date is invalid.'), $id_product) ); } elseif (SpecificPrice::exists( (int)$id_product, $id_combination, $id_shop, $id_group, $id_country, $id_currency, 0, $from_quantity, $from, $to, false )) { LoggerMEP::getInstance()->error( sprintf($this->l('Product №%s: speicifc price already exists.'), $id_product) ); return false; } else { return true; } return false; } public function getTitle() { return $this->l('Discount'); } public function assignVariables() { $variables = parent::assignVariables(); $variables['currencies'] = Currency::getCurrencies(false, true); $variables['countries'] = Country::getCountries($this->context->language->id, true); $variables['groups'] = Group::getGroups($this->context->language->id); return $variables; } }
💾 保存文件
← 返回文件管理器