* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7541 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class AdminShipping extends AdminTab { private $_fieldsHandling; public function __construct() { $this->table = 'delivery'; $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT')); $this->_fieldsHandling = array( 'PS_SHIPPING_HANDLING' => array('title' => $this->l('Handling charges'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'), 'PS_SHIPPING_FREE_PRICE' => array('title' => $this->l('Free shipping starts at'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'), 'PS_SHIPPING_FREE_WEIGHT' => array('title' => $this->l('Free shipping starts at'), 'suffix' => Configuration::get('PS_WEIGHT_UNIT'), 'validation' => 'isUnsignedFloat', 'cast' => 'floatval'), 'PS_SHIPPING_METHOD' => array('title' => $this->l('Billing'), 'validation' => 'isBool', 'cast' => 'intval')); parent::__construct(); } public function postProcess() { global $currentIndex; /* Handling settings */ if (isset($_POST['submitHandling'.$this->table])) { if ($this->tabAccess['edit'] === '1') { /* Check required fields */ foreach ($this->_fieldsHandling AS $field => $values) if (($value = Tools::getValue($field)) == false AND (string)$value != '0') $this->_errors[] = Tools::displayError('field').' '.$values['title'].' '.Tools::displayError('is required.'); /* Check field validity */ foreach ($this->_fieldsHandling AS $field => $values) if (Tools::getValue($field)) { $function = $values['validation']; if (!Validate::$function(Tools::getValue($field))) $this->_errors[] = Tools::displayError('field').' '.$values['title'].' '.Tools::displayError('is invalid.'); } /* Update configuration values */ if (!sizeof($this->_errors)) { foreach ($this->_fieldsHandling AS $field => $values) { $function = $values['cast']; Configuration::updateValue($field, call_user_func($function, Tools::getValue($field))); } Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token); } } else $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } /* Shipping fees */ elseif (isset($_POST['submitFees'.$this->table])) { if ($this->tabAccess['edit'] === '1') { if (($id_carrier = (int)(Tools::getValue('id_carrier'))) AND $id_carrier == ($id_carrier2 = (int)(Tools::getValue('id_carrier2')))) { $carrier = new Carrier($id_carrier); if (Validate::isLoadedObject($carrier)) { /* Get configuration values */ $shipping_method = $carrier->getShippingMethod(); $rangeTable = $carrier->getRangeTable(); $carrier->deleteDeliveryPrice($rangeTable); /* Build prices list */ $priceList = ''; foreach ($_POST AS $key => $value) if (strstr($key, 'fees_')) { $tmpArray = explode('_', $key); $priceList .= '('.($shipping_method == Carrier::SHIPPING_METHOD_PRICE ? (int)($tmpArray[2]) : 'NULL').', '.($shipping_method == Carrier::SHIPPING_METHOD_WEIGHT ? (int)($tmpArray[2]) : 'NULL').', '.$carrier->id.', '.(int)($tmpArray[1]).', '.number_format(abs(preg_replace("#,#", '.', $value)), 2, '.', '').'),'; unset($tmpArray); } $priceList = rtrim($priceList, ','); /* Update delivery prices */ $carrier->addDeliveryPrice($priceList); Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token); } else $this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).'); } elseif (isset($id_carrier2)) { $_POST['id_carrier'] = $id_carrier2; } else $this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).'); } else $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } } public function display() { $this->displayFormHandling(); $this->displayFormFees(); } public function displayFormHandling() { global $currentIndex; $confKeys = $this->_fieldsHandling; foreach ($confKeys AS $key => $confKey) $getConf[] = $key; $confValues = Configuration::getMultiple($getConf); unset($confKeys['PS_SHIPPING_METHOD']); echo '
'.$this->l('Handling').''; foreach ($confKeys AS $key => $confKey) { $postValue = Tools::getValue($key); $sign_left = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('left') : ''); $sign_right = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('right') : (is_string($confKey['suffix']) ? ' '.$confKey['suffix'] : '')); echo '
'; echo $sign_left; echo ''; echo $sign_right.' '.($key == 'PS_SHIPPING_HANDLING' ? $this->l('(tax excl.)') : ''); echo '
'; } echo '

'.$this->l('Tips:').'


'.$this->l('Billing').'

'; } public function displayFormFees() { global $currentIndex; $carrierArray = array(); $id_carrier = Tools::getValue('id_carrier'); $carriers = Carrier::getCarriers((int)(Configuration::get('PS_LANG_DEFAULT')), true , false,false, NULL, PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE); foreach ($carriers AS $carrier) if (!$carrier['is_free']) $carrierArray[] = array( 'id' => $carrier['id_carrier'], 'display' => '' ); if (count($carrierArray)) { if (!$id_carrier) $id_carrier = (int)$carrierArray[0]['id']; $carrierSelected = new Carrier($id_carrier); } echo '

'.$this->l('Fees by carrier, geographical zone, and ranges').'

'.$this->l('Fees').''; if (!count($carrierArray)) echo $this->l('You only have free carriers, there is no need to configure your delivery prices.'); else { echo ''.$this->l('Carrier:').'
'; $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT')); $rangeObj = $carrierSelected->getRangeObject(); $rangeTable = $carrierSelected->getRangeTable(); $suffix = $carrierSelected->getRangeSuffix(); $rangeIdentifier = 'id_'.$rangeTable; $ranges = $rangeObj->getRanges($id_carrier); $delivery = Carrier::getDeliveryPriceByRanges($rangeTable, $id_carrier); foreach ($delivery AS $deliv) $deliveryArray[$deliv['id_zone']][$deliv['id_carrier']][$deliv[$rangeIdentifier]] = $deliv['price']; if (!$carrierSelected->is_free) foreach ($ranges AS $range) echo ''; echo ''; $zones = $carrierSelected->getZones(); if (sizeof($ranges) && !$carrierSelected->is_free) { if (sizeof($zones) > 1) { echo ' '; foreach ($ranges AS $range) echo ''; echo ''; } foreach ($zones AS $zone) { echo ' '; foreach ($ranges AS $range) { if (isset($deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]])) $price = $deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]]; else $price = '0.00'; echo ''; } echo ' '; } } echo ''; echo '
'.$this->l('Zone / Range').''.(float)($range['delimiter1']).$suffix.' '.$this->l('to').' '.(float)($range['delimiter2']).$suffix.'
'.$this->l('All').''.$currency->getSign('left').''.$currency->getSign('right').'
'.$zone['name'].''.$currency->getSign('left').''.$currency->getSign('right').'
'; if (sizeof($ranges) && !$carrierSelected->is_free) echo ' '; elseif ($carrierSelected->is_free) echo $this->l('This is a free carrier'); else echo $this->l('No ranges set for this carrier'); echo '

'.$this->l('Prices do not include tax.').'

'; } echo '
'; } }