You are here

function uc_ups_shipment_request in Ubercart 5

Same name and namespace in other branches
  1. 8.4 shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc \uc_ups_shipment_request()
  2. 6.2 shipping/uc_ups/uc_ups.ship.inc \uc_ups_shipment_request()
  3. 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_shipment_request()

Construct an XML shippment 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

ShipConfirm XML document to send to UPS

1 call to uc_ups_shipment_request()
uc_ups_fulfill_order_validate in shipping/uc_ups/uc_ups.module
Validation handler for uc_ups_fulfill_order().

File

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

Code

function uc_ups_shipment_request($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'));
  $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 .= "</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 = constant(strtoupper($package->length_units) . '_TO_' . strtoupper(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>" . (floor($package->length * $conversion) + 1) . "</Length>";
        $package_schema .= "<Width>" . (floor($package->width * $conversion) + 1) . "</Width>";
        $package_schema .= "<Height>" . (floor($package->height * $conversion) + 1) . "</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 = $package->weight * $conversion;
      $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/>";
      }
      $package_schema .= "<PackageServiceOptions>";
      $package_schema .= "<InsuredValue>";
      $package_schema .= "<CurrencyCode>" . variable_get('uc_currency_code', 'USD') . "</CurrencyCode>";
      $package_schema .= "<MonetaryValue>" . number_format($package->price, 2, '.', '') . "</MonetaryValue>";
      $package_schema .= "</InsuredValue>";
      $package_schema .= "</PackageServiceOptions>";
      $package_schema .= "</Package>";
    }
  }
  $schema = uc_ups_access_request() . "\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ShipmentConfirmRequest xml:lang=\"en-US\">\n  <Request>\n    <TransactionReference>\n      <CustomerContext>Complex Rate Request</CustomerContext>\n      <XpciVersion>1.0001</XpciVersion>\n    </TransactionReference>\n    <RequestAction>ShipConfirm</RequestAction>\n    <RequestOption>validate</RequestOption>\n  </Request>\n  <Shipment>";
  $schema .= "<Shipper>";
  $schema .= "<Name>" . $store['name'] . "</Name>";
  $schema .= "<ShipperNumber>" . variable_get('uc_ups_shipper_number', '') . "</ShipperNumber>";
  if ($store['phone']) {
    $schema .= "<PhoneNumber>" . $store['phone'] . "</PhoneNumber>";
  }
  if ($store['fax']) {
    $schema .= "<FaxNumber>" . $store['fax'] . "</FaxNumber>";
  }
  if ($store['email']) {
    $schema .= "<EMailAddress>" . $store['email'] . "</EMailAddress>";
  }
  $schema .= "<Address>";
  $schema .= "<AddressLine1>" . $store['street1'] . "</AddressLine1>";
  if ($store['street2']) {
    $schema .= "<AddressLine2>" . $store['street2'] . "</AddressLine2>";
  }
  $schema .= "<City>" . $store['city'] . "</City>";
  $schema .= "<StateProvinceCode>{$shipper_zone}</StateProvinceCode>";
  $schema .= "<PostalCode>{$shipper_zip}</PostalCode>";
  $schema .= "<CountryCode>{$shipper_country}</CountryCode>";
  $schema .= "</Address>";
  $schema .= "</Shipper>";
  $schema .= "<ShipTo>";
  $schema .= "<CompanyName>" . $destination->company . "</CompanyName>";
  $schema .= "<AttentionName>" . $destination->first_name . ' ' . $destination->last_name . "</AttentionName>";
  $schema .= "<PhoneNumber>" . $destination->phone . "</PhoneNumber>";
  $schema .= "<EMailAddress>" . $destination->email . "</EMailAddress>";
  $schema .= "<Address>";
  $schema .= "<AddressLine1>" . $destination->street1 . "</AddressLine1>";
  if ($destination->street2) {
    $schema .= "<AddressLine2>" . $destination->street2 . "</AddressLine2>";
  }
  $schema .= "<City>" . $destination->city . "</City>";
  $schema .= "<StateProvinceCode>{$shipto_zone}</StateProvinceCode>";
  $schema .= "<PostalCode>{$shipto_zip}</PostalCode>";
  $schema .= "<CountryCode>{$shipto_country}</CountryCode>";
  if ($destination->residence) {
    $schema .= "<ResidentialAddressIndicator/>";
  }
  $schema .= "</Address>";
  $schema .= "</ShipTo>";
  $schema .= "<ShipFrom>";
  $schema .= "<CompanyName>" . $origin->company . "</CompanyName>";
  $schema .= "<AttentionName>" . $origin->first_name . ' ' . $origin->last_name . "</AttentionName>";
  $schema .= "<PhoneNumber>" . $origin->phone . "</PhoneNumber>";
  $schema .= "<EMailAddress>" . $origin->email . "</EMailAddress>";
  $schema .= "<Address>";
  $schema .= "<AddressLine1>" . $origin->street1 . "</AddressLine1>";
  if ($origin->street2) {
    $schema .= "<AddressLine2>" . $origin->street2 . "</AddressLine2>";
  }
  $schema .= "<City>" . $origin->city . "</City>";
  $schema .= "<StateProvinceCode>{$shipfrom_zone}</StateProvinceCode>";
  $schema .= "<PostalCode>{$shipfrom_zip}</PostalCode>";
  $schema .= "<CountryCode>{$shipfrom_country}</CountryCode>";
  $schema .= "</Address>";
  $schema .= "</ShipFrom>";
  $schema .= "<PaymentInformation>";
  $schema .= "<Prepaid>";
  $schema .= "<BillShipper>";
  $schema .= "<AccountNumber>{$account}</AccountNumber>";
  $schema .= "</BillShipper>";
  $schema .= "</Prepaid>";
  $schema .= "</PaymentInformation>";
  if (variable_get('uc_ups_negotiated_rates', false)) {
    $schema .= "<RateInformation>\n          <NegotiatedRatesIndicator/>\n        </RateInformation>";
  }
  $schema .= "<Service>";
  $schema .= "<Code>{$service[code]}</Code>";
  $schema .= "<Description>{$service[description]}</Description>";
  $schema .= "</Service>";
  $schema .= $package_schema;
  $schema .= "</Shipment>";
  $schema .= "<LabelSpecification>";
  $schema .= "<LabelPrintMethod>";
  $schema .= "<Code>GIF</Code>";
  $schema .= "</LabelPrintMethod>";
  $schema .= "<LabelImageFormat>";
  $schema .= "<Code>GIF</Code>";
  $schema .= "</LabelImageFormat>";
  $schema .= "</LabelSpecification>";
  $schema .= "</ShipmentConfirmRequest>";
  return $schema;
}