You are here

function uc_ups_shipment_request in Ubercart 8.4

Same name and namespace in other branches
  1. 5 shipping/uc_ups/uc_ups.module \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()

Constructs an XML shipment 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/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc
Passes final information into shipment object.

File

shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc, line 358
UPS functions for label generation.

Code

function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) {
  $ups_config = \Drupal::config('uc_ups.settings');
  $account = $ups_config
    ->get('shipper_number');
  $ua = explode(' ', $_SERVER['HTTP_USER_AGENT']);
  $user_agent = $ua[0];
  $services = _uc_ups_service_list();
  $service = [
    'code' => $ups_service,
    'description' => $services[$ups_service],
  ];
  $pkg_types = UPSUtilities::packageTypes();
  $store_config = \Drupal::config('uc_store.settings');
  $shipper_zone = $store_config
    ->get('address.zone');
  $shipper_country = $store_config
    ->get('address.country');
  $shipper_zip = $store_config
    ->get('address.postal_code');
  $shipto_zone = $destination->zone;
  $shipto_country = $destination->country;
  $shipto_zip = $destination->postal_code;
  $shipfrom_zone = $origin->zone;
  $shipfrom_country = $origin->country;
  $shipfrom_zip = $origin->postal_code;
  $ups_units = $ups_config
    ->get('unit_system');
  $package_schema = '';
  foreach ($packages as $package) {

    // Determine length conversion factor and weight conversion factor
    // for this shipment.
    $length_factor = uc_length_conversion($package->length_units, $ups_config
      ->get('unit_system'));
    switch ($ups_units) {
      case 'in':
        $weight_factor = uc_weight_conversion($package->weight_units, 'lb');
        $units = 'LBS';
        $unit_name = 'Pounds';
        break;
      case 'cm':
        $weight_factor = uc_weight_conversion($package->weight_units, 'kg');
        $units = 'KGS';
        $unit_name = 'Kilograms';
        break;
    }
    $qty = $package->qty;
    for ($i = 0; $i < $qty; $i++) {
      $package_type = [
        '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) = [
            $package->width,
            $package->length,
          ];
        }
        $package_schema .= "<Dimensions>";
        $package_schema .= "<UnitOfMeasurement>";
        $package_schema .= "<Code>" . strtoupper($ups_config
          ->get('unit_system')) . "</Code>";
        $package_schema .= "</UnitOfMeasurement>";
        $package_schema .= "<Length>" . (floor($package->length * $length_factor) + 1) . "</Length>";
        $package_schema .= "<Width>" . (floor($package->width * $length_factor) + 1) . "</Width>";
        $package_schema .= "<Height>" . (floor($package->height * $length_factor) + 1) . "</Height>";
        $package_schema .= "</Dimensions>";
      }
      $size = $package->length * $length_factor + 2 * $length_factor * ($package->width + $package->height);
      $weight = max(1, $package->weight * $weight_factor);
      $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>" . $store_config
        ->get('currency.code') . "</CurrencyCode>";
      $package_schema .= "<MonetaryValue>" . number_format($package->value, 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_config
    ->get('name') . "</Name>";
  $schema .= "<ShipperNumber>" . $ups_config
    ->get('shipper_number') . "</ShipperNumber>";
  if ($phone = $store_config
    ->get('phone')) {
    $schema .= "<PhoneNumber>" . $phone . "</PhoneNumber>";
  }
  if ($fax = $store_config
    ->get('fax')) {
    $schema .= "<FaxNumber>" . $fax . "</FaxNumber>";
  }
  if ($mail = uc_store_email()) {
    $schema .= "<EMailAddress>" . $mail . "</EMailAddress>";
  }
  $schema .= "<Address>";
  $schema .= "<AddressLine1>" . $store_config
    ->get('address.street1') . "</AddressLine1>";
  if ($street2 = $store_config
    ->get('address.street2')) {
    $schema .= "<AddressLine2>" . $street2 . "</AddressLine2>";
  }
  $schema .= "<City>" . $store_config
    ->get('address.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->residential) {
    $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 ($ups_config
    ->get('negotiated_rates')) {
    $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;
}