You are here

function uc_ups_shipping_quote in Ubercart 5

Same name and namespace in other branches
  1. 8.4 shipping/uc_ups/uc_ups.module \uc_ups_shipping_quote()
  2. 6.2 shipping/uc_ups/uc_ups.module \uc_ups_shipping_quote()
  3. 7.3 shipping/uc_ups/uc_ups.module \uc_ups_shipping_quote()

Construct an XML quote request.

Parameters

$packages: Array of packages received from the cart.

$origin: Delivery origin address information.

$destination: Delivery destination address information.

$ups_service: UPS service code (refers to UPS Ground, Next-Day Air, etc.).

Return value

RatingServiceSelectionRequest XML document to send to UPS

1 call to uc_ups_shipping_quote()
uc_ups_quote in shipping/uc_ups/uc_ups.module
Callback for retrieving a UPS shipping quote.

File

shipping/uc_ups/uc_ups.module, line 410
Shipping quote module that interfaces with www.ups.com to get rates for small package shipments.

Code

function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) {
  $store['name'] = variable_get('uc_store_name', NULL);
  $store['owner'] = variable_get('uc_store_owner', NULL);
  $store['email'] = variable_get('uc_store_email', NULL);
  $store['email_from'] = variable_get('uc_store_email', NULL);
  $store['phone'] = variable_get('uc_store_phone', NULL);
  $store['fax'] = variable_get('uc_store_fax', NULL);
  $store['street1'] = variable_get('uc_store_street1', NULL);
  $store['street2'] = variable_get('uc_store_street2', NULL);
  $store['city'] = variable_get('uc_store_city', NULL);
  $store['zone'] = variable_get('uc_store_zone', NULL);
  $store['postal_code'] = variable_get('uc_store_postal_code', NULL);
  $store['country'] = variable_get('uc_store_country', 840);
  $account = variable_get('uc_ups_shipper_number', '');
  $ua = explode(' ', $_SERVER['HTTP_USER_AGENT']);
  $user_agent = $ua[0];
  $services = _uc_ups_service_list();
  $service = array(
    'code' => $ups_service,
    'description' => $services[$ups_service],
  );
  $pkg_types = _uc_ups_pkg_types();
  $shipper_zone = uc_get_zone_code($store['zone']);
  $shipper_country = uc_get_country_data(array(
    'country_id' => $store['country'],
  ));
  $shipper_country = $shipper_country[0]['country_iso_code_2'];
  $shipper_zip = $store['postal_code'];
  $shipto_zone = uc_get_zone_code($destination->zone);
  $shipto_country = uc_get_country_data(array(
    'country_id' => $destination->country,
  ));
  $shipto_country = $shipto_country[0]['country_iso_code_2'];
  $shipto_zip = $destination->postal_code;
  $shipfrom_zone = uc_get_zone_code($origin->zone);
  $shipfrom_country = uc_get_country_data(array(
    'country_id' => $origin->country,
  ));
  $shipfrom_country = $shipfrom_country[0]['country_iso_code_2'];
  $shipfrom_zip = $origin->postal_code;
  $ups_units = variable_get('uc_ups_unit_system', variable_get('uc_length_unit', 'in'));
  switch ($ups_units) {
    case 'in':
      $units = 'LBS';
      $unit_name = 'Pounds';
      break;
    case 'cm':
      $units = 'KGS';
      $unit_name = 'Kilograms';
      break;
  }
  $shipment_weight = 0;
  $package_schema = '';
  foreach ($packages as $package) {
    $qty = $package->qty;
    for ($i = 0; $i < $qty; $i++) {
      $package_type = array(
        'code' => $package->pkg_type,
        'description' => $pkg_types[$package->pkg_type],
      );
      $package_schema .= "<Package>";
      $package_schema .= "<PackagingType>";
      $package_schema .= "<Code>" . $package_type['code'] . "</Code>";

      //$package_schema .= "<Description>". $package_type['description'] ."</Description>";
      $package_schema .= "</PackagingType>";
      if ($package->pkg_type == '02' && $package->length && $package->width && $package->height) {
        if ($package->length < $package->width) {
          list($package->length, $package->width) = array(
            $package->width,
            $package->length,
          );
        }
        $package_schema .= "<Dimensions>";
        $package_schema .= "<UnitOfMeasurement>";
        $conversion = uc_weight_conversion($package->length_units, variable_get('uc_ups_unit_system', variable_get('uc_length_unit', 'in')));
        $package_schema .= "<Code>" . strtoupper(variable_get('uc_ups_unit_system', variable_get('uc_length_unit', 'in'))) . "</Code>";
        $package_schema .= "</UnitOfMeasurement>";
        $package_schema .= "<Length>" . number_format($package->length * $conversion, 2, '.', '') . "</Length>";
        $package_schema .= "<Width>" . number_format($package->width * $conversion, 2, '.', '') . "</Width>";
        $package_schema .= "<Height>" . number_format($package->height * $conversion, 2, '.', '') . "</Height>";
        $package_schema .= "</Dimensions>";
      }
      $size = $package->length * $conversion + 2 * $conversion * ($package->width + $package->height);
      switch ($ups_units) {
        case 'in':
          $conversion = uc_weight_conversion($package->weight_units, 'lb');
          break;
        case 'cm':
          $conversion = uc_weight_conversion($package->weight_units, 'kg');
          break;
      }
      $weight = max(1, $package->weight * $conversion);
      $shipment_weight += $weight;
      $package_schema .= "<PackageWeight>";
      $package_schema .= "<UnitOfMeasurement>";
      $package_schema .= "<Code>{$units}</Code>";
      $package_schema .= "<Description>{$unit_name}</Description>";
      $package_schema .= "</UnitOfMeasurement>";
      $package_schema .= "<Weight>" . number_format($weight, 1, '.', '') . "</Weight>";
      $package_schema .= "</PackageWeight>";
      if ($size > 130 && $size <= 165) {
        $package_schema .= "<LargePackageIndicator/>";
      }
      if (variable_get('uc_ups_insurance', TRUE)) {
        $package_schema .= "<PackageServiceOptions>";
        $package_schema .= "<InsuredValue>";
        $package_schema .= "<CurrencyCode>" . variable_get('uc_currency_code', 'USD') . "</CurrencyCode>";
        $package_schema .= "<MonetaryValue>" . $package->price . "</MonetaryValue>";
        $package_schema .= "</InsuredValue>";
        $package_schema .= "</PackageServiceOptions>";
      }
      $package_schema .= "</Package>";
    }
  }
  $schema = uc_ups_access_request() . "\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RatingServiceSelectionRequest xml:lang=\"en-US\">\n  <Request>\n    <TransactionReference>\n      <CustomerContext>Complex Rate Request</CustomerContext>\n      <XpciVersion>1.0001</XpciVersion>\n    </TransactionReference>\n    <RequestAction>Rate</RequestAction>\n    <RequestOption>rate</RequestOption>\n  </Request>\n  <PickupType>\n    <Code>" . variable_get('uc_ups_pickup_type', '01') . "</Code>\n  </PickupType>\n  <CustomerClassification>\n    <Code>" . variable_get('uc_ups_classification', '04') . "</Code>\n  </CustomerClassification>\n  <Shipment>\n    <Shipper>\n      <ShipperNumber>" . variable_get('uc_ups_shipper_number', '') . "</ShipperNumber>\n      <Address>\n        <City>" . $store['city'] . "</City>\n        <StateProvinceCode>{$shipper_zone}</StateProvinceCode>\n        <PostalCode>{$shipper_zip}</PostalCode>\n        <CountryCode>{$shipper_country}</CountryCode>\n      </Address>\n    </Shipper>\n    <ShipTo>\n      <Address>\n        <StateProvinceCode>{$shipto_zone}</StateProvinceCode>\n        <PostalCode>{$shipto_zip}</PostalCode>\n        <CountryCode>{$shipto_country}</CountryCode>\n      ";
  if (variable_get('uc_ups_residential_quotes', 0)) {
    $schema .= "<ResidentialAddressIndicator/>\n      ";
  }
  $schema .= "</Address>\n    </ShipTo>\n    <ShipFrom>\n      <Address>\n        <StateProvinceCode>{$shipfrom_zone}</StateProvinceCode>\n        <PostalCode>{$shipfrom_zip}</PostalCode>\n        <CountryCode>{$shipfrom_country}</CountryCode>\n      </Address>\n    </ShipFrom>\n    <ShipmentWeight>\n      <UnitOfMeasurement>\n        <Code>{$units}</Code>\n        <Description>{$unit_name}</Description>\n      </UnitOfMeasurement>\n      <Weight>" . number_format($shipment_weight, 1, '.', '') . "</Weight>\n    </ShipmentWeight>\n    <Service>\n      <Code>{$service[code]}</Code>\n      <Description>{$service[description]}</Description>\n    </Service>\n    ";
  $schema .= $package_schema;
  if (variable_get('uc_ups_negotiated_rates', false)) {
    $schema .= "<RateInformation>\n          <NegotiatedRatesIndicator/>\n        </RateInformation>";
  }
  $schema .= "</Shipment>\n</RatingServiceSelectionRequest>";
  return $schema;
}