AbstractCarrier.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ SgM3K2um/7TRdy59C86nJAU8+eg/BFhWfdMVkXoKSbM=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.02]
  11. */
  12. /**
  13. * shipping carrier model abstract class
  14. *
  15. * IMPORTANT: all methods that extend this class must have the name identical with the
  16. * 'name' field in the shipping_carriers table.
  17. */
  18. namespace Ppb\Model\Shipping\Carrier;
  19. use Cube\Db\Table\Row\AbstractRow,
  20. Ppb\Model\Shipping as ShippingModel,
  21. Ppb\Service;
  22. abstract class AbstractCarrier extends AbstractRow
  23. {
  24. /**
  25. * methods array constants
  26. */
  27. const DOM = 'domestic';
  28. const INTL = 'international';
  29. /**
  30. * dimensions constants;
  31. */
  32. const W = 'width';
  33. const L = 'length';
  34. const H = 'height';
  35. /**
  36. * weight conversion rate
  37. * 1 lbs = 0.4536 kg
  38. */
  39. const LBS_TO_KG = 0.4536;
  40. /**
  41. * dimensions conversion rate
  42. * 1 cm = 0.3937 inches
  43. */
  44. const CM_TO_INCHES = 0.3937;
  45. /**
  46. *
  47. * shipping carrier description
  48. * (to be used in the admin area - functionality description)
  49. *
  50. * @var string
  51. */
  52. protected $_description;
  53. /**
  54. *
  55. * carrier methods array - defined by each carrier class
  56. *
  57. * @var array
  58. */
  59. protected $_methods = array(
  60. self::DOM => array(),
  61. self::INTL => array(),
  62. );
  63. /**
  64. *
  65. * package weight
  66. * (lbs or kg, depending on shipping carrier)
  67. *
  68. * @var int
  69. */
  70. protected $_weight;
  71. /**
  72. *
  73. * package weight uom (accepted values: kg, lbs)
  74. *
  75. * @var string
  76. */
  77. protected $_weightUom;
  78. /**
  79. *
  80. * carrier weight uom
  81. *
  82. * @var string
  83. */
  84. protected static $_carrierWeightUom = ShippingModel::UOM_LBS;
  85. /**
  86. *
  87. * carrier dimensions uom
  88. *
  89. * @var string
  90. */
  91. protected static $_carrierDimensionsUom = ShippingModel::UOM_INCHES;
  92. /**
  93. *
  94. * error message resulted from a getPrice operation
  95. *
  96. * @var string
  97. */
  98. protected $_error = null;
  99. /**
  100. *
  101. * package dimensions
  102. *
  103. * @var array
  104. */
  105. protected $_dimensions = array(
  106. self::W => null,
  107. self::H => null,
  108. self::L => null,
  109. );
  110. /**
  111. *
  112. * package dimensions uom (accepted values: inches, cm)
  113. *
  114. * @var string
  115. */
  116. protected $_dimensionsUom;
  117. /**
  118. *
  119. * carrier currency
  120. *
  121. * @var string
  122. */
  123. protected $_currency;
  124. /**
  125. *
  126. * source zip code
  127. *
  128. * @var string
  129. */
  130. protected $_sourceZip;
  131. /**
  132. *
  133. * source country
  134. *
  135. * @var string
  136. */
  137. protected $_sourceCountry;
  138. /**
  139. *
  140. * destination zip code
  141. *
  142. * @var string
  143. */
  144. protected $_destZip;
  145. /**
  146. *
  147. * destination country
  148. *
  149. * @var string
  150. */
  151. protected $_destCountry;
  152. /**
  153. *
  154. * class constructor
  155. *
  156. * @param string $carrierName carrier name
  157. * @param string $currency
  158. *
  159. * @throws \RuntimeException
  160. */
  161. public function __construct($carrierName, $currency)
  162. {
  163. $carriersService = new Service\Table\ShippingCarriers();
  164. $carrier = $carriersService->findBy('name', $carrierName);
  165. if (!$carrier['id']) {
  166. $translate = $this->getTranslate();
  167. throw new \RuntimeException(
  168. sprintf($translate->_("The shipping carrier you are trying to use, '%s', does not exist."), $carrierName));
  169. }
  170. $data = array(
  171. 'table' => $carriersService->getTable(),
  172. 'data' => $carriersService->getData($carrier['id']),
  173. );
  174. parent::__construct($data);
  175. $this->setCurrency($currency);
  176. }
  177. /**
  178. *
  179. * get carrier description string
  180. *
  181. * @return string
  182. */
  183. public function getDescription()
  184. {
  185. $translate = $this->getTranslate();
  186. if (null !== $translate) {
  187. return $translate->_($this->_description);
  188. }
  189. return $this->_description;
  190. }
  191. /**
  192. *
  193. * set carrier description string
  194. *
  195. * @param string $description
  196. *
  197. * @return $this
  198. */
  199. public function setDescription($description)
  200. {
  201. $this->_description = (string)$description;
  202. return $this;
  203. }
  204. /**
  205. *
  206. * get package weight
  207. *
  208. * @return float|int
  209. */
  210. public function getWeight()
  211. {
  212. return $this->_weight;
  213. }
  214. /**
  215. *
  216. * set package weight
  217. *
  218. * @param float $weight
  219. *
  220. * @return $this
  221. * @throws \InvalidArgumentException
  222. */
  223. public function setWeight($weight)
  224. {
  225. if (empty($this->_weightUom)) {
  226. throw new \InvalidArgumentException("Please set the weight UOM before setting the weight value.");
  227. }
  228. if ($this->_weightUom != self::$_carrierWeightUom) {
  229. $weight = ($this->_weightUom == ShippingModel::UOM_KG) ?
  230. ($weight / self::LBS_TO_KG) : ($weight * self::LBS_TO_KG);
  231. }
  232. $this->_weight = round($weight, 1);
  233. return $this;
  234. }
  235. /**
  236. *
  237. * get weight uom
  238. *
  239. * @return string
  240. */
  241. public function getWeightUom()
  242. {
  243. return $this->_weightUom;
  244. }
  245. /**
  246. *
  247. * set weight uom
  248. *
  249. * @param string $weightUom
  250. *
  251. * @return $this
  252. * @throws \InvalidArgumentException
  253. */
  254. public function setWeightUom($weightUom)
  255. {
  256. $weightUom = strtolower($weightUom);
  257. if (!in_array($weightUom, array(ShippingModel::UOM_KG, ShippingModel::UOM_LBS))) {
  258. throw new \InvalidArgumentException("Invalid weight UOM submitted.");
  259. }
  260. $this->_weightUom = $weightUom;
  261. return $this;
  262. }
  263. /**
  264. *
  265. * get error message
  266. *
  267. * @return string
  268. */
  269. public function getError()
  270. {
  271. $translate = $this->getTranslate();
  272. if (null !== $translate) {
  273. return $translate->_($this->_error);
  274. }
  275. return $this->_error;
  276. }
  277. /**
  278. *
  279. * set error message
  280. *
  281. * @param string $error
  282. *
  283. * @return $this
  284. */
  285. public function setError($error)
  286. {
  287. $this->_error = (string)$error;
  288. return $this;
  289. }
  290. /**
  291. *
  292. * get dimensions array
  293. *
  294. * @return array
  295. */
  296. public function getDimensions()
  297. {
  298. return $this->_dimensions;
  299. }
  300. /**
  301. *
  302. * set dimensions array
  303. *
  304. * @param array $dimensions
  305. *
  306. * @return $this
  307. * @throws \InvalidArgumentException
  308. */
  309. public function setDimensions($dimensions)
  310. {
  311. if (empty($this->_dimensionsUom)) {
  312. throw new \InvalidArgumentException("Please set the dimensions UOM before setting the dimensions.");
  313. }
  314. if (is_array($dimensions)) {
  315. if (
  316. array_key_exists(self::L, $dimensions) &&
  317. array_key_exists(self::W, $dimensions) &&
  318. array_key_exists(self::H, $dimensions)
  319. ) {
  320. if ($this->_dimensionsUom != self::$_carrierDimensionsUom) {
  321. if ($this->_dimensionsUom == ShippingModel::UOM_CM) {
  322. $dimensions = array(
  323. self::L => $dimensions[self::L] * self::CM_TO_INCHES,
  324. self::W => $dimensions[self::W] * self::CM_TO_INCHES,
  325. self::H => $dimensions[self::H] * self::CM_TO_INCHES,
  326. );
  327. }
  328. else {
  329. $dimensions = array(
  330. self::L => $dimensions[self::L] / self::CM_TO_INCHES,
  331. self::W => $dimensions[self::W] / self::CM_TO_INCHES,
  332. self::H => $dimensions[self::H] / self::CM_TO_INCHES,
  333. );
  334. }
  335. }
  336. $this->_dimensions = $dimensions;
  337. }
  338. }
  339. return $this;
  340. }
  341. /**
  342. *
  343. * get dimensions uom
  344. *
  345. * @return string
  346. */
  347. public function getDimensionsUom()
  348. {
  349. return $this->_dimensionsUom;
  350. }
  351. /**
  352. *
  353. * set dimensions uom
  354. *
  355. * @param string $dimensionsUom
  356. *
  357. * @return $this
  358. * @throws \InvalidArgumentException
  359. */
  360. public function setDimensionsUom($dimensionsUom)
  361. {
  362. $dimensionsUom = strtolower($dimensionsUom);
  363. if (!in_array($dimensionsUom, array(ShippingModel::UOM_INCHES, ShippingModel::UOM_CM))) {
  364. throw new \InvalidArgumentException("Invalid dimensions UOM submitted.");
  365. }
  366. $this->_dimensionsUom = $dimensionsUom;
  367. return $this;
  368. }
  369. /**
  370. *
  371. * get carrier currency iso code
  372. *
  373. * @return string
  374. */
  375. public function getCurrency()
  376. {
  377. return $this->_currency;
  378. }
  379. /**
  380. *
  381. * set carrier currency iso code
  382. *
  383. * @param string $currency
  384. *
  385. * @return $this
  386. */
  387. public function setCurrency($currency)
  388. {
  389. $this->_currency = (string)$currency;
  390. return $this;
  391. }
  392. /**
  393. *
  394. * get source zip code
  395. *
  396. * @return string
  397. */
  398. public function getSourceZip()
  399. {
  400. return $this->_sourceZip;
  401. }
  402. /**
  403. *
  404. * set source zip code
  405. *
  406. * @param string $sourceZip
  407. *
  408. * @return $this
  409. */
  410. public function setSourceZip($sourceZip)
  411. {
  412. $this->_sourceZip = (string)$sourceZip;
  413. return $this;
  414. }
  415. /**
  416. *
  417. * get source country
  418. *
  419. * @return string
  420. */
  421. public function getSourceCountry()
  422. {
  423. return $this->_sourceCountry;
  424. }
  425. /**
  426. *
  427. * set source country
  428. *
  429. * @param string $sourceCountry
  430. *
  431. * @return $this
  432. */
  433. public function setSourceCountry($sourceCountry)
  434. {
  435. $this->_sourceCountry = (string)$sourceCountry;
  436. return $this;
  437. }
  438. /**
  439. *
  440. * get destination zip code
  441. *
  442. * @return string
  443. */
  444. public function getDestZip()
  445. {
  446. return $this->_destZip;
  447. }
  448. /**
  449. *
  450. * set destination zip code
  451. *
  452. * @param string $destZip
  453. *
  454. * @return $this
  455. */
  456. public function setDestZip($destZip)
  457. {
  458. $this->_destZip = (string)$destZip;
  459. return $this;
  460. }
  461. /**
  462. *
  463. * get destination country
  464. *
  465. * @return string
  466. */
  467. public function getDestCountry()
  468. {
  469. return $this->_destCountry;
  470. }
  471. /**
  472. *
  473. * set destination country
  474. *
  475. * @param string $destCountry
  476. *
  477. * @return $this
  478. */
  479. public function setDestCountry($destCountry)
  480. {
  481. $this->_destCountry = (string)$destCountry;
  482. return $this;
  483. }
  484. /**
  485. *
  486. * get form elements, used to create the form needed to add the shipping carrier settings
  487. *
  488. * @return array
  489. */
  490. public function getElements()
  491. {
  492. return array();
  493. }
  494. /**
  495. *
  496. * dummy function used as a placeholder for translatable sentences
  497. *
  498. * @param $string
  499. *
  500. * @return string
  501. */
  502. protected function _($string)
  503. {
  504. return $string;
  505. }
  506. abstract public function getPrice($methodName = null);
  507. }