A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 lines
18 KiB

13 years ago
  1. <?php
  2. /**
  3. * This file is a part of Quasi CMS
  4. *@package Quasi
  5. */
  6. if(!defined('QUASICMS') ) die('No Quasi.');
  7. if (!defined("SHIPPINGREQUESTBASE.CLASS.PHP")){
  8. define("SHIPPINGREQUESTBASE.CLASS.PHP",1);
  9. /**
  10. * Class ShippingRequestType - enumerator class for types of shipping requests
  11. *@package Quasi
  12. * @subpackage Classes
  13. */
  14. class ShippingRequestType
  15. {
  16. ///@var const - a request for a rate
  17. const Rate = 1;
  18. ///@var const - a request for a rate availability
  19. const Availability = 2;
  20. ///@var const - a request for a printable label image
  21. const Label = 3;
  22. ///@var const - a request for the status of the account
  23. const AccountStatus = 4;
  24. ///@var const - a request to make a credit payment
  25. const CreditAccount = 5;
  26. public static function ToString($intFlag)
  27. {
  28. switch($intFlag)
  29. {
  30. case ShippingRequestType::Label:
  31. return 'LabelRequest';
  32. case ShippingRequestType::Availability:
  33. return 'AvailabilityRequest';
  34. case ShippingRequestType::Rate:
  35. return 'RateRequest';
  36. case ShippingRequestType::AccountStatus:
  37. return 'AccountStatusRequest';
  38. case ShippingRequestType::CreditAccount:
  39. return 'CreditAccountRequest';
  40. default:
  41. return 'None';
  42. }
  43. }
  44. }
  45. /**
  46. * Class ExtraDocumentImage - convenience class for extra customs image documents
  47. *@package Quasi
  48. * @subpackage Classes
  49. */
  50. class ExtraDocumentImage
  51. {
  52. public $Copies;
  53. public $Image;
  54. public $Type;
  55. public function __construct($mixImage, $strType, $intCopies=1)
  56. {
  57. $this->Image = $mixImage;
  58. $this->Type = $strType;
  59. $this->Copies = $intCopies;
  60. }
  61. }
  62. /**
  63. * Class ShippingRequest - base class for performing shipping requests via Shipping API web services
  64. *
  65. * This class provides an interface to shipping requests. Abstract methods MUST be implemented by
  66. * subclasses, others may be overridden as needed. Subclasses are instantiated by a call to
  67. * ShippingMethod::GetRequest($strRequestType). The primary function of this base class is to associate
  68. * the request with the ShippingMethod
  69. *
  70. * This class also connects to the specified server via the methods provided by extending WebServiceRequest.
  71. *
  72. *@author Erik Winn <erikwinnmail@yahoo.com>
  73. *
  74. * $Id: ShippingRequest.class.php 502 2009-02-10 22:09:53Z erikwinn $
  75. *@version 0.1
  76. *
  77. *@copyright (C) 2008 by Erik Winn
  78. *@license GPL v.2
  79. This program is free software; you can redistribute it and/or modify
  80. it under the terms of the GNU General Public License as published by
  81. the Free Software Foundation; either version 2 of the License, or
  82. (at your option) any later version.
  83. This program is distributed in the hope that it will be useful,
  84. but WITHOUT ANY WARRANTY; without even the implied warranty of
  85. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  86. GNU General Public License for more details.
  87. You should have received a copy of the GNU General Public License
  88. along with this program; if not, write to the Free Software
  89. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  90. *
  91. *@package Quasi
  92. * @subpackage Classes
  93. */
  94. abstract class ShippingRequest extends WebServiceRequest
  95. {
  96. /**
  97. *@var ShippingMethod method that created this calculator
  98. */
  99. protected $objShippingMethod;
  100. /**
  101. *@var ShippingRequestType - the type of request to create
  102. */
  103. protected $intShippingRequestType;
  104. /**
  105. * An array of CustomsInformation objects containing data for customs
  106. * declarations
  107. *@var array aryCustomsInformation
  108. */
  109. protected $aryCustomsInformation = null;
  110. /**
  111. * An array of extra document images for customs declarations
  112. *@var array aryExtraDocumentImages
  113. */
  114. protected $aryExtraDocumentImages;
  115. /**
  116. *@var resource objShippingLabelImage - the shipping label image as GD image resource
  117. */
  118. protected $objShippingLabelImage;
  119. /**
  120. *@var array aryCustomsFormImages - image(s) for printing out customs forms ..
  121. */
  122. protected $aryCustomsFormImages;
  123. /**
  124. *@var boolean blnIsAvailable - flag indicating if the method is avaiable for the address in Order ..
  125. */
  126. protected $blnIsAvailable;
  127. /**
  128. * ShippingRequest Constructor
  129. *
  130. * This sets some defaults for the shipping requests.
  131. *
  132. *NOTE: It is assumed that the shipping origin is the same as the store address.
  133. *@todo Support remote shipping options ..
  134. *
  135. * @param ShippingMethod objShippingMethod - the method to be used for the request
  136. */
  137. public function __construct(ShippingMethod $objShippingMethod)
  138. {
  139. $this->objShippingMethod =& $objShippingMethod;
  140. $this->blnTestMode = $objShippingMethod->TestMode;
  141. }
  142. /**
  143. * Connects to web service and submits the request. Note that
  144. * this function merely constructs a request URL from internal variables
  145. * that are set in createRequest, it may therefor contain a GET query
  146. * string or a POST depending on the subclass requirements.
  147. * Note: If the request transaction succeeds we also call handleResponse here.
  148. *@return boolean true on success
  149. */
  150. protected function submitRequest()
  151. {
  152. if(! parent::submitRequest())
  153. return false;
  154. if(! $this->handleResponse())
  155. return false;
  156. return true;
  157. }
  158. /**
  159. * This function attempts to create a request string for the method of the given ShippingRequestType.
  160. * The WebRequestType (POST, GET, etc ..) may also be optionally set here.
  161. *
  162. * @param ShippingRequestType intShippingRequestType - the type of shipping request object to create
  163. * @param WebRequestType intWebRequestType - the type of web request to submit
  164. */
  165. protected function createRequest($intShippingRequestType, $intWebRequestType = WebRequestType::POST)
  166. {
  167. $this->intShippingRequestType = $intShippingRequestType;
  168. parent::createRequest($intWebRequestType);
  169. /* switch($intShippingRequestType)
  170. {
  171. case ShippingRequestType::Label:
  172. return $this->createLabelRequest();
  173. break;
  174. case ShippingRequestType::Availability:
  175. return $this->createAvailabilityRequest();
  176. break;
  177. case ShippingRequestType::Rate:
  178. return $this->createRateRequest();
  179. break;
  180. case ShippingRequestType::AccountStatus:
  181. return $this->createAccountStatusRequest();
  182. break;
  183. case ShippingRequestType::CreditAccount:
  184. return $this->createCreditAccountRequest();
  185. break;
  186. default:
  187. throw new QCallerException('Shipping request type unsupported: ' . $intRequestType );
  188. }*/
  189. }
  190. /**
  191. * This function attempts to create a POST request string for the method of a given ShippingRequestType.
  192. */
  193. protected function createPOSTRequest()
  194. {
  195. switch($this->intShippingRequestType)
  196. {
  197. case ShippingRequestType::Label:
  198. return $this->createLabelRequest();
  199. break;
  200. case ShippingRequestType::Availability:
  201. return $this->createAvailabilityRequest();
  202. break;
  203. case ShippingRequestType::Rate:
  204. return $this->createRateRequest();
  205. break;
  206. case ShippingRequestType::AccountStatus:
  207. return $this->createAccountStatusRequest();
  208. break;
  209. case ShippingRequestType::CreditAccount:
  210. return $this->createCreditAccountRequest();
  211. break;
  212. default:
  213. throw new QCallerException('Shipping request type unsupported: ' . $this->intRequestType );
  214. }
  215. }
  216. /**
  217. * This function attempts to create a GET request string for the method of a given ShippingRequestType.
  218. */
  219. protected function createGETRequest()
  220. {
  221. switch($this->intShippingRequestType)
  222. {
  223. case ShippingRequestType::Label:
  224. return $this->createLabelRequest();
  225. break;
  226. case ShippingRequestType::Availability:
  227. return $this->createAvailabilityRequest();
  228. break;
  229. case ShippingRequestType::Rate:
  230. return $this->createRateRequest();
  231. break;
  232. case ShippingRequestType::AccountStatus:
  233. return $this->createAccountStatusRequest();
  234. break;
  235. case ShippingRequestType::CreditAccount:
  236. return $this->createCreditAccountRequest();
  237. break;
  238. default:
  239. throw new QCallerException('Shipping request type unsupported: ' . $this->intRequestType );
  240. }
  241. }
  242. /**
  243. * This function handles the response string for the method of a given ShippingRequestType.
  244. */
  245. protected function handleResponse()
  246. {
  247. switch($this->intShippingRequestType)
  248. {
  249. case ShippingRequestType::Label:
  250. return $this->handleLabelResponse();
  251. break;
  252. case ShippingRequestType::Availability:
  253. return $this->handleAvailabilityResponse();
  254. break;
  255. case ShippingRequestType::Rate:
  256. return $this->handleRateResponse();
  257. break;
  258. case ShippingRequestType::AccountStatus:
  259. return $this->handleAccountStatusResponse();
  260. break;
  261. case ShippingRequestType::CreditAccount:
  262. return $this->handleCreditAccountResponse();
  263. break;
  264. default:
  265. throw new QCallerException('Shipping request type unsupported: ' . $this->intRequestType );
  266. }
  267. }
  268. protected function initCustomsInformationArray()
  269. {
  270. $this->aryCustomsInformation = array();
  271. $aryOrderItems = OrderItem::LoadArrayByOrderId($this->objShippingMethod->Order->Id);
  272. foreach($aryOrderItems as $objOrderItem)
  273. {
  274. $objCustomsInfo = new CustomsInformation();
  275. $objCustomsInfo->Quantity = $objOrderItem->Quantity;
  276. $objCustomsInfo->Description = $objOrderItem->Product->Name;
  277. $objCustomsInfo->Weight = $objOrderItem->Product->Weight * $objOrderItem->Quantity;
  278. $objCustomsInfo->Value = $objOrderItem->Product->RetailPrice * $objOrderItem->Quantity;
  279. $this->aryCustomsInformation[] = $objCustomsInfo;
  280. }
  281. }
  282. /**
  283. * Returns a rate for this method to the order address
  284. *@return float containing the rate for the order address
  285. */
  286. abstract public function GetRate();
  287. /**
  288. * Returns a shipping label image suitable for printing
  289. *@return string containing the image code
  290. */
  291. abstract public function GetLabel();
  292. /**
  293. * Returns an account status report
  294. *@return string containing the status report
  295. */
  296. abstract public function GetAccountStatus();
  297. /**
  298. * Returns whether this method is available for the order address
  299. *@return boolean true if method is available
  300. */
  301. abstract public function GetAvailability();
  302. /**
  303. * Submits an account credit payment
  304. */
  305. // abstract public function CreditAccount();
  306. //Request string creators
  307. /**
  308. * Creates a rate request
  309. */
  310. abstract protected function createRateRequest();
  311. /**
  312. * Creates a label image request
  313. */
  314. abstract protected function createLabelRequest();
  315. /**
  316. * Creates an account status request
  317. */
  318. abstract protected function createAccountStatusRequest();
  319. /**
  320. * Creates a request submitting an account credit payment
  321. */
  322. abstract protected function createCreditAccountRequest();
  323. /**
  324. * Creates a method available request
  325. */
  326. abstract protected function createAvailabilityRequest();
  327. //Response handlers
  328. /**
  329. * Creates a rate request
  330. */
  331. abstract protected function handleRateResponse();
  332. /**
  333. * Creates a label image request
  334. */
  335. abstract protected function handleLabelResponse();
  336. /**
  337. * Creates an account status request
  338. */
  339. abstract protected function handleAccountStatusResponse();
  340. /**
  341. * Creates a request submitting an account credit payment
  342. */
  343. abstract protected function handleCreditAccountResponse();
  344. /**
  345. * Creates a method available request
  346. */
  347. abstract protected function handleAvailabilityResponse();
  348. ///Gettors
  349. public function __get($strName)
  350. {
  351. switch ($strName)
  352. {
  353. case 'ShippingLabelImage':
  354. return $this->objShippingLabelImage;
  355. case 'ExtraDocumentImages':
  356. return (array) $this->aryExtraDocumentImages;
  357. case 'CustomsFormImages':
  358. return (array) $this->aryCustomsFormImages;
  359. case 'IsAvailable':
  360. return $this->blnIsAvailable;
  361. case 'Order':
  362. return $this->objShippingMethod->Order ;
  363. case 'Carrier':
  364. return $this->objShippingMethod->Carrier ;
  365. case 'ServiceType':
  366. return $this->objShippingMethod->ServiceType ;
  367. case 'Rate':
  368. return $this->objShippingMethod->Rate ;
  369. case 'TotalValue':
  370. return $this->objShippingMethod->Order->ProductTotalCharged;
  371. case 'Pounds':
  372. return $this->objShippingMethod->Pounds ;
  373. case 'Ounces':
  374. return $this->objShippingMethod->Ounces ;
  375. case 'Container':
  376. return $this->objShippingMethod->Container ;
  377. case 'IsMachinable':
  378. return $this->objShippingMethod->IsMachinable ;
  379. case 'OriginZip':
  380. return $this->objShippingMethod->OriginZip ;
  381. case 'DestinationZip':
  382. return $this->objShippingMethod->DestinationZip ;
  383. case 'DestinationCountryId':
  384. return $this->objShippingMethod->DestinationCountryId ;
  385. case 'OriginCountryId':
  386. return $this->objShippingMethod->OriginCountryId ;
  387. case 'OriginStateId':
  388. case 'OriginZoneId':
  389. return $this->objShippingMethod->OriginZoneId ;
  390. case 'DestinationStateId':
  391. case 'DestinationZoneId':
  392. return $this->objShippingMethod->DestinationZoneId ;
  393. ///string representation of country and state names ..
  394. case 'DestinationCountry':
  395. return $this->objShippingMethod->DestinationCountry ;
  396. case 'OriginCountry':
  397. return $this->objShippingMethod->OriginCountry ;
  398. case 'OriginState':
  399. case 'OriginZone':
  400. return $this->objShippingMethod->OriginZone ;
  401. case 'DestinationState':
  402. case 'DestinationZone':
  403. return $this->objShippingMethod->DestinationZone ;
  404. ///string representation of country and state ISO 2 letter codes ..
  405. case 'DestinationCountryCode':
  406. return $this->objShippingMethod->DestinationCountryCode ;
  407. case 'OriginCountryCode':
  408. return $this->objShippingMethod->OriginCountryCode ;
  409. case 'OriginStateCode':
  410. case 'OriginZoneCode':
  411. return $this->objShippingMethod->OriginZoneCode ;
  412. case 'DestinationStateCode':
  413. case 'DestinationZoneCode':
  414. return $this->objShippingMethod->DestinationZoneCode ;
  415. default:
  416. try {
  417. return parent::__get($strName);
  418. } catch (QCallerException $objExc) {
  419. $objExc->IncrementOffset();
  420. throw $objExc;
  421. }
  422. }
  423. }
  424. ///Settors
  425. public function __set($strName, $mixValue)
  426. {
  427. switch ($strName)
  428. {
  429. case 'DestinationZip':
  430. return ($this->objShippingMethod->DestinationZip = $mixValue);
  431. case 'OriginZip':
  432. return ($this->objShippingMethod->OriginZip = $mixValue);
  433. case 'DestinationCountryId':
  434. return ($this->objShippingMethod->DestinationCountryId = $mixValue);
  435. case 'OriginCountryId':
  436. return ($this->objShippingMethod->OriginCountryId = $mixValue);
  437. case 'Container':
  438. return ($this->objShippingMethod->Container = $mixValue);
  439. case 'Pounds':
  440. return ($this->objShippingMethod->Pounds = $mixValue);
  441. case 'Rate':
  442. return ($this->objShippingMethod->Rate = $mixValue);
  443. case 'Ounces':
  444. return ($this->objShippingMethod->Ounces = $mixValue);
  445. default:
  446. try {
  447. return (parent::__set($strName, $mixValue));
  448. } catch (QCallerException $objExc) {
  449. $objExc->IncrementOffset();
  450. throw $objExc;
  451. }
  452. }
  453. }
  454. }//end class
  455. }//end define
  456. ?>