You are here

function uc_fedex_quote in FedEx Shipping 5

Same name and namespace in other branches
  1. 6.2 uc_fedex.module \uc_fedex_quote()
  2. 6 uc_fedex.module \uc_fedex_quote()
  3. 7.2 uc_fedex.module \uc_fedex_quote()
  4. 7 uc_fedex.module \uc_fedex_quote()

Callback for retrieving a FedEx shipping quote.

Requests a quote of all available FedEx services. Quote returned from the FedEx server is parsed and only the selected services are presented to the user.

Parameters

$products: Array of cart contents.

$details: Order details other than product information.

Return value

JSON object containing rate, error, and debugging information.

1 string reference to 'uc_fedex_quote'
uc_fedex_shipping_method in ./uc_fedex.module
Implementation of Ubercart's hook_shipping_method().

File

./uc_fedex.module, line 424
FedEx Web Services Rate / Available Services Quote

Code

function uc_fedex_quote($products, $details) {

  // For now, everything is put in one package, so there is
  // only one element in the $packages array.

  /* Assign products to one or more packages for quoting */
  $packages = _uc_fedex_package_products($products);

  /* Create and fill object with info needed about origin */
  $origin = variable_get('uc_quote_store_default_address', new stdClass());
  $country = db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $origin->country);
  $country_data = db_fetch_object($country);
  $origin->country_iso_code_2 = $country_data->country_iso_code_2;

  /* Create and fill object with info needed about destination */
  $destination = (object) $details;
  if ($origin->country == $destination->country) {

    // Try to save a DB query
    $destination->country_iso_code_2 = $origin->country_iso_code_2;
  }
  else {
    $country = db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $destination->country);
    $country_data = db_fetch_object($country);
    $destination->country_iso_code_2 = $country_data->country_iso_code_2;
  }

  /* Load preference for Residence/Commercial destination address */
  if (variable_get('uc_fedex_residential_quotes', 1)) {
    $destination->residence = TRUE;
  }
  else {
    $destination->residence = FALSE;
  }

  /* Call the method that does the actual SOAP request to the FedEx Server */

  /* Response contains all available services and rates                    */
  $response = uc_fedex_rate_request($packages, $origin, $destination);

  /* Construct an array containing only those services that the   */

  /* store admin has allowed in admin/store/settings/quotes/edit  */
  $fedex_services = array_filter(variable_get('uc_fedex_services', _uc_fedex_services()));
  $quotes = array();
  $method = uc_fedex_shipping_method();
  if (!isset($response->Options)) {

    // Memphis, we have a problem ...
    // Error returned from FedEx server - will print in $message box
    // Don't even try to extract a quote from the response, just return
    // empty quote array.
    return array();
  }

  /* Test responses to see if we are interested in that service */
  foreach ($response->Options as $options) {
    $service = $options->ServiceDetail->ServiceType;
    if (in_array($service, $fedex_services)) {

      // Check to see if we're quoting ACCOUNT or LIST rates
      if (variable_get('uc_fedex_quote_type', 'list') == 'list') {

        // LIST rate
        // LIST quotes return both ACCOUNT rates (in RatedShipmentDetails[0])
        // and LIST rates (in RatedShipmentDetails[1])
        $ratedetail = $options->RatedShipmentDetails[1];
      }
      else {

        // ACCOUNT rate
        // ACCOUNT quotes return only ACCOUNT rates
        $ratedetail = $options->RatedShipmentDetails[0];
      }

      // need to handle dimensional rates, other modifiers.
      // Markup rate before customer sees it
      $rate = uc_fedex_rate_markup($ratedetail->ShipmentRateDetail->TotalNetCharge->Amount);
      $quotes[$service] = array(
        'rate' => $rate,
        'format' => uc_currency_format($rate),
        'option_label' => theme('uc_fedex_option_label', $method['fedex']['quote']['accessorials'][$service], $packages),
      );
    }
  }
  if (user_access('configure quotes') && variable_get('uc_quote_display_debug', FALSE)) {

    //  $quotes['data']['debug'] = htmlentities($response).'<br />';
  }

  // Sort rate quotes in order of increasing price
  uasort($quotes, 'uc_quote_price_sort');
  return $quotes;
}