PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Shipping/Model/Carrier/Abstract.php

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 358 lines | 209 code | 37 blank | 112 comment | 49 complexity | 95bb9c7ae35ee43312ea7f419c075ede MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  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@magentocommerce.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Shipping
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. abstract class Mage_Shipping_Model_Carrier_Abstract extends Varien_Object
  27. {
  28. protected $_code;
  29. protected $_rates = null;
  30. protected $_numBoxes = 1;
  31. /**
  32. * Whether this carrier has fixed rates calculation
  33. *
  34. * @var bool
  35. */
  36. protected $_isFixed = false;
  37. const HANDLING_TYPE_PERCENT = 'P';
  38. const HANDLING_TYPE_FIXED = 'F';
  39. const HANDLING_ACTION_PERPACKAGE = 'P';
  40. const HANDLING_ACTION_PERORDER = 'O';
  41. /**
  42. * Fields that should be replaced in debug with '***'
  43. *
  44. * @var array
  45. */
  46. protected $_debugReplacePrivateDataKeys = array();
  47. public function __construct()
  48. {
  49. }
  50. /**
  51. * Retrieve information from carrier configuration
  52. *
  53. * @param string $field
  54. * @return mixed
  55. */
  56. public function getConfigData($field)
  57. {
  58. if (empty($this->_code)) {
  59. return false;
  60. }
  61. $path = 'carriers/'.$this->_code.'/'.$field;
  62. return Mage::getStoreConfig($path, $this->getStore());
  63. }
  64. public function getConfigFlag($field)
  65. {
  66. if (empty($this->_code)) {
  67. return false;
  68. }
  69. $path = 'carriers/'.$this->_code.'/'.$field;
  70. return Mage::getStoreConfigFlag($path, $this->getStore());
  71. }
  72. abstract public function collectRates(Mage_Shipping_Model_Rate_Request $request);
  73. public function checkAvailableShipCountries(Mage_Shipping_Model_Rate_Request $request)
  74. {
  75. $speCountriesAllow = $this->getConfigData('sallowspecific');
  76. /*
  77. * for specific countries, the flag will be 1
  78. */
  79. if($speCountriesAllow && $speCountriesAllow==1){
  80. $showMethod = $this->getConfigData('showmethod');
  81. $availableCountries = array();
  82. if( $this->getConfigData('specificcountry') ) {
  83. $availableCountries = explode(',',$this->getConfigData('specificcountry'));
  84. }
  85. if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) {
  86. return $this;
  87. } elseif ($showMethod && (!$availableCountries || ($availableCountries && !in_array($request->getDestCountryId(), $availableCountries)))){
  88. $error = Mage::getModel('shipping/rate_result_error');
  89. $error->setCarrier($this->_code);
  90. $error->setCarrierTitle($this->getConfigData('title'));
  91. $errorMsg = $this->getConfigData('specificerrmsg');
  92. $error->setErrorMessage($errorMsg?$errorMsg:Mage::helper('shipping')->__('The shipping module is not available for selected delivery country.'));
  93. return $error;
  94. } else {
  95. /*
  96. * The admin set not to show the shipping module if the devliery country is not within specific countries
  97. */
  98. return false;
  99. }
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Processing additional validation to check is carrier applicable.
  105. *
  106. * @param Mage_Shipping_Model_Rate_Request $request
  107. * @return Mage_Shipping_Model_Carrier_Abstract|Mage_Shipping_Model_Rate_Result_Error|boolean
  108. */
  109. public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
  110. {
  111. return $this;
  112. }
  113. public function isActive()
  114. {
  115. $active = $this->getConfigData('active');
  116. return $active==1 || $active=='true';
  117. }
  118. /**
  119. * Whether this carrier has fixed rates calculation
  120. *
  121. * @return bool
  122. */
  123. public function isFixed()
  124. {
  125. return $this->_isFixed;
  126. }
  127. /**
  128. * Check if carrier has shipping tracking option available
  129. *
  130. * @return boolean
  131. */
  132. public function isTrackingAvailable()
  133. {
  134. return false;
  135. }
  136. public function getSortOrder()
  137. {
  138. return $this->getConfigData('sort_order');
  139. }
  140. /**
  141. * @param Mage_Shipping_Model_Rate_Request $request
  142. * @return void
  143. */
  144. protected function _updateFreeMethodQuote($request)
  145. {
  146. if ($request->getFreeMethodWeight()==$request->getPackageWeight()
  147. || !$request->hasFreeMethodWeight()) {
  148. return;
  149. }
  150. if (!$freeMethod = $this->getConfigData('free_method')) {
  151. return;
  152. }
  153. $freeRateId = false;
  154. if (is_object($this->_result)) {
  155. foreach ($this->_result->getAllRates() as $i=>$item) {
  156. if ($item->getMethod()==$freeMethod) {
  157. $freeRateId = $i;
  158. break;
  159. }
  160. }
  161. }
  162. if ($freeRateId===false) {
  163. return;
  164. }
  165. $price = null;
  166. if ($request->getFreeMethodWeight()>0) {
  167. $this->_setFreeMethodRequest($freeMethod);
  168. $result = $this->_getQuotes();
  169. if ($result && ($rates = $result->getAllRates()) && count($rates)>0) {
  170. if ((count($rates) == 1) && ($rates[0] instanceof Mage_Shipping_Model_Rate_Result_Method)) {
  171. $price = $rates[0]->getPrice();
  172. }
  173. if (count($rates) > 1) {
  174. foreach ($rates as $rate) {
  175. if ($rate instanceof Mage_Shipping_Model_Rate_Result_Method && $rate->getMethod() == $freeMethod) {
  176. $price = $rate->getPrice();
  177. }
  178. }
  179. }
  180. }
  181. } else {
  182. /**
  183. * if we can apply free shipping for all order we should force price
  184. * to $0.00 for shipping with out sending second request to carrier
  185. */
  186. $price = 0;
  187. }
  188. /**
  189. * if we did not get our free shipping method in response we must use its old price
  190. */
  191. if (!is_null($price)) {
  192. $this->_result->getRateById($freeRateId)->setPrice($price);
  193. }
  194. }
  195. public function getMethodPrice($cost, $method='')
  196. {
  197. if ($method == $this->getConfigData('free_method') &&
  198. $this->getConfigData('free_shipping_enable') &&
  199. $this->getConfigData('free_shipping_subtotal') <= $this->_rawRequest->getValueWithDiscount())
  200. {
  201. $price = '0.00';
  202. } else {
  203. $price = $this->getFinalPriceWithHandlingFee($cost);
  204. }
  205. return $price;
  206. }
  207. /**
  208. * get the handling fee for the shipping + cost
  209. *
  210. * @return final price for shipping emthod
  211. */
  212. public function getFinalPriceWithHandlingFee($cost)
  213. {
  214. $handlingFee = $this->getConfigData('handling_fee');
  215. $finalMethodPrice = 0;
  216. $handlingType = $this->getConfigData('handling_type');
  217. if (!$handlingType) {
  218. $handlingType = self::HANDLING_TYPE_FIXED;
  219. }
  220. $handlingAction = $this->getConfigData('handling_action');
  221. if (!$handlingAction) {
  222. $handlingAction = self::HANDLING_ACTION_PERORDER;
  223. }
  224. if($handlingAction == self::HANDLING_ACTION_PERPACKAGE)
  225. {
  226. if ($handlingType == self::HANDLING_TYPE_PERCENT) {
  227. $finalMethodPrice = ($cost + ($cost * $handlingFee/100)) * $this->_numBoxes;
  228. } else {
  229. $finalMethodPrice = ($cost + $handlingFee) * $this->_numBoxes;
  230. }
  231. } else {
  232. if ($handlingType == self::HANDLING_TYPE_PERCENT) {
  233. $finalMethodPrice = ($cost * $this->_numBoxes) + ($cost * $this->_numBoxes * $handlingFee/100);
  234. } else {
  235. $finalMethodPrice = ($cost * $this->_numBoxes) + $handlingFee;
  236. }
  237. }
  238. return $finalMethodPrice;
  239. }
  240. /**
  241. * Return weight in pounds
  242. *
  243. * @param integer Weight in someone measure
  244. * @return float Weight in pounds
  245. */
  246. public function convertWeightToLbs($weight)
  247. {
  248. return $weight;
  249. }
  250. /**
  251. * set the number of boxes for shipping
  252. *
  253. * @return weight
  254. */
  255. public function getTotalNumOfBoxes($weight)
  256. {
  257. /*
  258. reset num box first before retrieve again
  259. */
  260. $this->_numBoxes = 1;
  261. $weight = $this->convertWeightToLbs($weight);
  262. $maxPackageWeight = $this->getConfigData('max_package_weight');
  263. if($weight > $maxPackageWeight && $maxPackageWeight != 0) {
  264. $this->_numBoxes = ceil($weight/$maxPackageWeight);
  265. $weight = $weight/$this->_numBoxes;
  266. }
  267. return $weight;
  268. }
  269. public function isStateProvinceRequired()
  270. {
  271. return false;
  272. }
  273. public function isCityRequired()
  274. {
  275. return false;
  276. }
  277. public function isZipCodeRequired()
  278. {
  279. return false;
  280. }
  281. /**
  282. * Log debug data to file
  283. *
  284. * @param mixed $debugData
  285. */
  286. protected function _debug($debugData)
  287. {
  288. if ($this->getDebugFlag()) {
  289. Mage::getModel('core/log_adapter', 'shipping_' . $this->getCarrierCode() . '.log')
  290. ->setFilterDataKeys($this->_debugReplacePrivateDataKeys)
  291. ->log($debugData);
  292. }
  293. }
  294. /**
  295. * Define if debugging is enabled
  296. *
  297. * @return bool
  298. */
  299. public function getDebugFlag()
  300. {
  301. return $this->getConfigData('debug');
  302. }
  303. /**
  304. * Used to call debug method from not Paymant Method context
  305. *
  306. * @param mixed $debugData
  307. */
  308. public function debugData($debugData)
  309. {
  310. $this->_debug($debugData);
  311. }
  312. /**
  313. * Getter for carrier code
  314. *
  315. * @return string
  316. */
  317. public function getCarrierCode()
  318. {
  319. return $this->_code;
  320. }
  321. }