A QCodo powered CMS
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.

434 lines
14 KiB

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