PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/adminstrator/tabs/AdminShipping.php

http://marocmall.googlecode.com/
PHP | 305 lines | 243 code | 29 blank | 33 comment | 32 complexity | dd549fe0b3cd94f8fa574034f65bdc79 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 8861 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class AdminShipping extends AdminTab
  28. {
  29. private $_fieldsHandling;
  30. public function __construct()
  31. {
  32. $this->table = 'delivery';
  33. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  34. $this->_fieldsHandling = array(
  35. 'PS_SHIPPING_HANDLING' => array('title' => $this->l('Handling charges'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'),
  36. 'PS_SHIPPING_FREE_PRICE' => array('title' => $this->l('Free shipping starts at'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'),
  37. 'PS_SHIPPING_FREE_WEIGHT' => array('title' => $this->l('Free shipping starts at'), 'suffix' => Configuration::get('PS_WEIGHT_UNIT'), 'validation' => 'isUnsignedFloat', 'cast' => 'floatval'),
  38. 'PS_SHIPPING_METHOD' => array('title' => $this->l('Billing'), 'validation' => 'isBool', 'cast' => 'intval'));
  39. parent::__construct();
  40. }
  41. public function postProcess()
  42. {
  43. global $currentIndex;
  44. /* Handling settings */
  45. if (isset($_POST['submitHandling'.$this->table]))
  46. {
  47. if ($this->tabAccess['edit'] === '1')
  48. {
  49. /* Check required fields */
  50. foreach ($this->_fieldsHandling AS $field => $values)
  51. if (($value = Tools::getValue($field)) == false AND (string)$value != '0')
  52. $this->_errors[] = Tools::displayError('field').' <b>'.$values['title'].'</b> '.Tools::displayError('is required.');
  53. /* Check field validity */
  54. foreach ($this->_fieldsHandling AS $field => $values)
  55. if (Tools::getValue($field))
  56. {
  57. $function = $values['validation'];
  58. if (!Validate::$function(Tools::getValue($field)))
  59. $this->_errors[] = Tools::displayError('field').' <b>'.$values['title'].'</b> '.Tools::displayError('is invalid.');
  60. }
  61. /* Update configuration values */
  62. if (!sizeof($this->_errors))
  63. {
  64. foreach ($this->_fieldsHandling AS $field => $values)
  65. {
  66. $function = $values['cast'];
  67. Configuration::updateValue($field, call_user_func($function, Tools::getValue($field)));
  68. }
  69. Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
  70. }
  71. }
  72. else
  73. $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
  74. }
  75. /* Shipping fees */
  76. elseif (isset($_POST['submitFees'.$this->table]))
  77. {
  78. if ($this->tabAccess['edit'] === '1')
  79. {
  80. if (($id_carrier = (int)(Tools::getValue('id_carrier'))) AND $id_carrier == ($id_carrier2 = (int)(Tools::getValue('id_carrier2'))))
  81. {
  82. $carrier = new Carrier($id_carrier);
  83. if (Validate::isLoadedObject($carrier))
  84. {
  85. /* Get configuration values */
  86. $shipping_method = $carrier->getShippingMethod();
  87. $rangeTable = $carrier->getRangeTable();
  88. $carrier->deleteDeliveryPrice($rangeTable);
  89. /* Build prices list */
  90. $priceList = '';
  91. foreach ($_POST AS $key => $value)
  92. if (strstr($key, 'fees_'))
  93. {
  94. $tmpArray = explode('_', $key);
  95. $priceList .= '('.($shipping_method == Carrier::SHIPPING_METHOD_PRICE ? (int)($tmpArray[2]) : 'NULL').',
  96. '.($shipping_method == Carrier::SHIPPING_METHOD_WEIGHT ? (int)($tmpArray[2]) : 'NULL').', '.(int)$carrier->id.',
  97. '.(int)$tmpArray[1].', '.number_format(abs(preg_replace("#,#", '.', $value)), 6, '.', '').'),';
  98. unset($tmpArray);
  99. }
  100. $priceList = rtrim($priceList, ',');
  101. /* Update delivery prices */
  102. $carrier->addDeliveryPrice($priceList);
  103. Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
  104. }
  105. else
  106. $this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).');
  107. }
  108. elseif (isset($id_carrier2))
  109. {
  110. $_POST['id_carrier'] = $id_carrier2;
  111. }
  112. else
  113. $this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).');
  114. }
  115. else
  116. $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
  117. }
  118. }
  119. public function display()
  120. {
  121. $this->displayFormHandling();
  122. $this->displayFormFees();
  123. }
  124. public function displayFormHandling()
  125. {
  126. global $currentIndex;
  127. $confKeys = $this->_fieldsHandling;
  128. foreach ($confKeys AS $key => $confKey)
  129. $getConf[] = $key;
  130. $confValues = Configuration::getMultiple($getConf);
  131. unset($confKeys['PS_SHIPPING_METHOD']);
  132. echo '
  133. <form action="'.$currentIndex.'&submitHandling'.$this->table.'=1&token='.$this->token.'" method="post">
  134. <fieldset>
  135. <legend><img src="../img/admin/delivery.gif" />'.$this->l('Handling').'</legend>';
  136. foreach ($confKeys AS $key => $confKey)
  137. {
  138. $postValue = Tools::getValue($key);
  139. $sign_left = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('left') : '');
  140. $sign_right = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('right') : (is_string($confKey['suffix']) ? '&nbsp;'.$confKey['suffix'] : ''));
  141. echo '
  142. <label class="clear">'.$confKey['title'].':</label>
  143. <div class="margin-form">';
  144. echo $sign_left;
  145. echo '<input size="6" type="text" name="'.$key.'" value="'.(($postValue != false OR (string)$postValue == '0') ? $postValue : $confValues[$key]).'" />';
  146. echo $sign_right.' '.($key == 'PS_SHIPPING_HANDLING' ? $this->l('(tax excl.)') : '');
  147. echo '</div>';
  148. }
  149. echo '
  150. <div class="margin-form" style="margin-top: 20px;">
  151. <input type="submit" value="'.$this->l(' Save ').'" name="submitHandling'.$this->table.'" class="button" />
  152. </div>
  153. <p style="font-weight: bold; font-size: 11px;">'.$this->l('Tips:').'</p>
  154. <ul style="list-style-type: disc; font-size: 11px; color:#7F7F7F; margin-left: 30px; line-height: 20px;">
  155. <li>'.$this->l('If you set these parameters to 0, they will be disabled').'</li>
  156. <li>'.$this->l('Coupons are not taken into account when calculating free shipping').'</li>
  157. </ul>
  158. </fieldset>
  159. <br />
  160. <fieldset>
  161. <legend><img src="../img/admin/money.gif" />'.$this->l('Billing').'</legend>
  162. <label class="clear">'.$this->l('Choice of range:').' </label>
  163. <div class="margin-form">
  164. <input type="radio" name="PS_SHIPPING_METHOD" value="0" id="total_price"
  165. '.((isset($confValues['PS_SHIPPING_METHOD']) AND $confValues['PS_SHIPPING_METHOD'] == 0) ? 'checked="checked"' : '').'/>
  166. <label class="t" for="total_price"> '.$this->l('According to total price').'</label><br />
  167. <input type="radio" name="PS_SHIPPING_METHOD" value="1" id="total_weight"
  168. '.((!isset($confValues['PS_SHIPPING_METHOD']) OR $confValues['PS_SHIPPING_METHOD'] == 1) ? 'checked="checked"' : '').'/>
  169. <label class="t" for="total_weight"> '.$this->l('According to total weight').'</label>
  170. </div>
  171. <div class="margin-form">
  172. <input type="submit" value="'.$this->l(' Save ').'" name="submitHandling'.$this->table.'" class="button" />
  173. </div>
  174. </fieldset>
  175. </form>';
  176. }
  177. public function displayFormFees()
  178. {
  179. global $currentIndex;
  180. $carrierArray = array();
  181. $id_carrier = Tools::getValue('id_carrier');
  182. $carriers = Carrier::getCarriers((int)(Configuration::get('PS_LANG_DEFAULT')), true , false,false, NULL, Carrier::PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE);
  183. foreach ($carriers AS $carrier)
  184. if (!$carrier['is_free'])
  185. $carrierArray[] = array(
  186. 'id' => $carrier['id_carrier'],
  187. 'display' => '<option value="'.(int)($carrier['id_carrier']).'"'.(($carrier['id_carrier'] == $id_carrier) ? ' selected="selected"' : '').'>'.$carrier['name'].'</option>'
  188. );
  189. if (count($carrierArray))
  190. {
  191. if (!$id_carrier)
  192. $id_carrier = (int)$carrierArray[0]['id'];
  193. $carrierSelected = new Carrier($id_carrier);
  194. }
  195. echo '<br /><br />
  196. <h2>'.$this->l('Fees by carrier, geographical zone, and ranges').'</h2>
  197. <form action="'.$currentIndex.'&token='.$this->token.'" id="fees" name="fees" method="post">
  198. <fieldset>
  199. <legend><img src="../img/admin/delivery.gif" />'.$this->l('Fees').'</legend>';
  200. if (!count($carrierArray))
  201. echo $this->l('You only have free carriers, there is no need to configure your delivery prices.');
  202. else
  203. {
  204. echo '<b>'.$this->l('Carrier:').' </b>
  205. <select name="id_carrier2" onchange="document.fees.submit();">';
  206. foreach ($carrierArray AS $carrierOption)
  207. echo $carrierOption['display'];
  208. echo '
  209. </select><br />
  210. <table class="table space" cellpadding="0" cellspacing="0">
  211. <tr>
  212. <th>'.$this->l('Zone / Range').'</th>';
  213. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  214. $rangeObj = $carrierSelected->getRangeObject();
  215. $rangeTable = $carrierSelected->getRangeTable();
  216. $suffix = $carrierSelected->getRangeSuffix();
  217. $rangeIdentifier = 'id_'.$rangeTable;
  218. $ranges = $rangeObj->getRanges($id_carrier);
  219. $delivery = Carrier::getDeliveryPriceByRanges($rangeTable, $id_carrier);
  220. foreach ($delivery AS $deliv)
  221. $deliveryArray[$deliv['id_zone']][$deliv['id_carrier']][$deliv[$rangeIdentifier]] = $deliv['price'];
  222. if (!$carrierSelected->is_free)
  223. foreach ($ranges AS $range)
  224. echo '<th style="font-size: 11px;">'.(float)($range['delimiter1']).$suffix.' '.$this->l('to').' '.(float)($range['delimiter2']).$suffix.'</th>';
  225. echo '</tr>';
  226. $zones = $carrierSelected->getZones();
  227. if (sizeof($ranges) && !$carrierSelected->is_free)
  228. {
  229. if (sizeof($zones) > 1)
  230. {
  231. echo '
  232. <tr>
  233. <th style="height: 30px;">'.$this->l('All').'</th>';
  234. foreach ($ranges AS $range)
  235. echo '<td class="center">'.$currency->getSign('left').'<input type="text" id="fees_all_'.$range[$rangeIdentifier].'" onchange="this.value = this.value.replace(/,/g, \'.\');" onkeyup="if ((event.keyCode||event.which) != 9){ spreadFees('.$range[$rangeIdentifier].') }" style="width: 45px;" />'.$currency->getSign('right').'</td>';
  236. echo '</tr>';
  237. }
  238. foreach ($zones AS $zone)
  239. {
  240. echo '
  241. <tr>
  242. <th style="height: 30px;">'.$zone['name'].'</th>';
  243. foreach ($ranges AS $range)
  244. {
  245. if (isset($deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]]))
  246. $price = $deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]];
  247. else
  248. $price = '0.00';
  249. echo '<td class="center">'.$currency->getSign('left').'<input type="text" class="fees_'.$range[$rangeIdentifier].'" onchange="this.value = this.value.replace(/,/g, \'.\');" name="fees_'.$zone['id_zone'].'_'.$range[$rangeIdentifier].'" onkeyup="clearAllFees('.$range[$rangeIdentifier].')" value="'.$price.'" style="width: 45px;" />'.$currency->getSign('right').'</td>';
  250. }
  251. echo '
  252. </tr>';
  253. }
  254. }
  255. echo '<tr>
  256. <td colspan="'.(sizeof($ranges) + 1).'" class="center" style="border-bottom: none; height: 40px;">
  257. <input type="hidden" name="submitFees'.$this->table.'" value="1" />';
  258. if (sizeof($ranges) && !$carrierSelected->is_free)
  259. echo ' <input type="submit" value="'.$this->l(' Save ').'" class="button" />';
  260. elseif ($carrierSelected->is_free)
  261. echo $this->l('This is a free carrier');
  262. else
  263. echo $this->l('No ranges set for this carrier');
  264. echo '
  265. </td>
  266. </tr>';
  267. echo '
  268. </table>
  269. <p>'.$this->l('Prices do not include tax.').'</p>';
  270. }
  271. echo '
  272. </fieldset>
  273. <input type="hidden" name="id_carrier" value="'.$id_carrier.'" />
  274. </form>';
  275. }
  276. }