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

/upload/catalog/model/shipping/auspost.php

https://github.com/MariusRugan/OpenCart-Secured
PHP | 144 lines | 115 code | 29 blank | 0 comment | 41 complexity | 8fdf7b6cacf020356b875679c0cb73cb MD5 | raw file
  1. <?php
  2. class ModelShippingAuspost extends Model {
  3. public function getQuote($address) {
  4. $this->load->language('shipping/auspost');
  5. if ($this->config->get('auspost_status')) {
  6. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('auspost_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
  7. if (!$this->config->get('auspost_geo_zone_id')) {
  8. $status = TRUE;
  9. } elseif ($query->num_rows) {
  10. $status = TRUE;
  11. } else {
  12. $status = FALSE;
  13. }
  14. } else {
  15. $status = FALSE;
  16. }
  17. $weight = intval($this->weight->convert($this->cart->getWeight(), $this->config->get('config_weight_class'), 2));
  18. $method_data = array();
  19. if ($this->config->get('auspost_status') && ($this->config->get('auspost_standard') || $this->config->get('auspost_express')) && $address['iso_code_2'] == 'AU') {
  20. $quote_data = array();
  21. $error = FALSE;
  22. if (!preg_match('/^[0-9]{4}$/', $address['postcode'])) {
  23. $error = 'Your postcode is not valid in Australia';
  24. } else {
  25. if ($this->config->get('auspost_standard')) {
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, 'http://drc.edeliver.com.au/ratecalc.asp?pickup_postcode=' . $this->config->get('auspost_postcode') . '&destination_postcode=' . $address['postcode'] . '&height=70&width=70&length=70&country=AU&service_type=standard&quantity=1&weight=' . $weight);
  28. curl_setopt($ch, CURLOPT_HEADER, 0);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30. $get_standard = curl_exec($ch);
  31. curl_close($ch);
  32. if (strstr($get_standard, 'err_msg=OK') == FALSE) {
  33. $error = 'Error interfacing with Australia Post (connection)';
  34. } else {
  35. $get_standard_charge = preg_match('/^charge=([0-9]{1,3}\.?[0-9]{0,2})/', $get_standard, $post_charge_standard);
  36. if (!isset($post_charge_standard[1])) {
  37. $error = 'Error interfacing with Australia Post (charge)';
  38. } else {
  39. $post_charge_standard = sprintf('%.2f', $post_charge_standard[1]);
  40. if (preg_match('/^[0-9]{1,2}\.[0-9]{2,2}$/', $this->config->get('auspost_handling')) && $this->config->get('auspost_handling') > 0) {
  41. $post_charge_standard = sprintf('%.2f', $post_charge_standard + $this->config->get('auspost_handling'));
  42. }
  43. $get_days_standard = preg_match('/days=([0-9]{1,2})/', $get_standard, $post_days_standard);
  44. $post_days_standard_append = '';
  45. if ($this->config->get('auspost_display_estimate') && isset($post_days_standard[1])) {
  46. if (is_numeric($post_days_standard[1])) {
  47. if($post_days_standard[1] == 1) {
  48. $post_days_standard_append = ' (est. ' . $post_days_standard[1] . ' day delivery)';
  49. } else {
  50. $post_days_standard_append = ' (est. ' . $post_days_standard[1] . ' days delivery)';
  51. }
  52. }
  53. }
  54. $quote_data['auspost_standard'] = array(
  55. 'id' => 'auspost.auspost_standard',
  56. 'title' => $this->language->get('text_standard') . $post_days_standard_append,
  57. 'cost' => $post_charge_standard,
  58. 'tax_class_id' => 0,
  59. 'text' => '$' . $post_charge_standard
  60. );
  61. }
  62. }
  63. }
  64. if ($this->config->get('auspost_express') && $error == FALSE) {
  65. $ch = curl_init();
  66. curl_setopt($ch, CURLOPT_URL, 'http://drc.edeliver.com.au/ratecalc.asp?pickup_postcode=' . $this->config->get('auspost_postcode') . '&destination_postcode=' . $address['postcode'] . '&height=70&width=70&length=70&country=AU&service_type=express&quantity=1&weight=' . $weight);
  67. curl_setopt($ch, CURLOPT_HEADER, 0);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  69. $get_express = curl_exec($ch);
  70. curl_close($ch);
  71. if (strstr($get_express, 'err_msg=OK') == FALSE) {
  72. $error = 'Error interfacing with Australia Post';
  73. } else {
  74. $get_express_charge = preg_match('/^charge=([0-9]{1,3}\.?[0-9]{0,2})/', $get_express, $post_charge_express);
  75. if (!isset($post_charge_express[1])) {
  76. $error = 'Error interfacing with Australia Post (charge)';
  77. } else {
  78. $post_charge_express = sprintf('%.2f', $post_charge_express[1]);
  79. if (preg_match('/^[0-9]{1,2}\.[0-9]{2,2}$/', $this->config->get('auspost_handling')) && $this->config->get('auspost_handling') > 0) {
  80. $post_charge_express = sprintf('%.2f', $post_charge_express + $this->config->get('auspost_handling'));
  81. }
  82. $get_days_express = preg_match('/days=([0-9]{1,2})/', $get_express, $post_days_express);
  83. $post_days_express_append = '';
  84. if ($this->config->get('auspost_display_estimate') && isset($post_days_express[1])) {
  85. if (is_numeric($post_days_express[1])) {
  86. if ($post_days_express[1] == 1) {
  87. $post_days_express_append = ' (est. ' . $post_days_express[1] . ' day delivery)';
  88. } else {
  89. $post_days_express_append = ' (est. ' . $post_days_express[1] . ' days delivery)';
  90. }
  91. }
  92. }
  93. $quote_data['auspost_express'] = array(
  94. 'id' => 'auspost.auspost_express',
  95. 'title' => $this->language->get('text_express') . $post_days_express_append,
  96. 'cost' => $post_charge_express,
  97. 'tax_class_id' => 0,
  98. 'text' => '$' . $post_charge_express
  99. );
  100. }
  101. }
  102. }
  103. }
  104. $method_data = array(
  105. 'id' => 'auspost_express',
  106. 'title' => $this->language->get('text_title'),
  107. 'quote' => $quote_data,
  108. 'sort_order' => $this->config->get('auspost_sort_order'),
  109. 'error' => $error
  110. );
  111. }
  112. return $method_data;
  113. }
  114. }
  115. ?>