You are here

function uc_ups_shipping_quote in Ubercart 8.4

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

Constructs an XML quote request.

Parameters

array $packages: Array of packages received from the cart.

\Drupal\uc_store\AddressInterface $origin: Delivery origin address information.

\Drupal\uc_store\AddressInterface $destination: Delivery destination address information.

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

Return value

string 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 353
UPS shipping quote module.

Code

function uc_ups_shipping_quote(array $packages, AddressInterface $origin, AddressInterface $destination, $ups_service) {
  $ups_config = \Drupal::config('uc_ups.settings');
  $ua = explode(' ', $_SERVER['HTTP_USER_AGENT']);
  $user_agent = $ua[0];
  $services = UPSUtilities::services();
  $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
    ->getZone();
  $shipto_country = $destination
    ->getCountry();
  $shipto_zip = $destination
    ->getPostalCode();
  $shipfrom_zone = $origin
    ->getZone();
  $shipfrom_country = $origin
    ->getCountry();
  $shipfrom_zip = $origin
    ->getPostalCode();
  $ups_units = $ups_config
    ->get('unit_system');
  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) {

    // 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');
        break;
      case 'cm':
        $weight_factor = uc_weight_conversion($package->weight_units, 'kg');
        break;
    }

    // Loop over quantity of packages in this shipment.
    $qty = $package->qty;
    for ($i = 0; $i < $qty; $i++) {

      // Build XML for this package.
      $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>" . number_format($package->length * $length_factor, 2, '.', '') . "</Length>";
        $package_schema .= "<Width>" . number_format($package->width * $length_factor, 2, '.', '') . "</Width>";
        $package_schema .= "<Height>" . number_format($package->height * $length_factor, 2, '.', '') . "</Height>";
        $package_schema .= "</Dimensions>";
      }
      $weight = max(1, $package->weight * $weight_factor);
      $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>";
      $size = $package->length * $length_factor + 2 * $length_factor * ($package->width + $package->height);
      if ($size > 130 && $size <= 165) {
        $package_schema .= "<LargePackageIndicator/>";
      }
      if ($ups_config
        ->get('insurance')) {
        $package_schema .= "<PackageServiceOptions>";
        $package_schema .= "<InsuredValue>";
        $package_schema .= "<CurrencyCode>" . $store_config
          ->get('currency.code') . "</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>" . $ups_config
    ->get('pickup_type') . "</Code>\n  </PickupType>\n  <CustomerClassification>\n    <Code>" . $ups_config
    ->get('classification') . "</Code>\n  </CustomerClassification>\n  <Shipment>\n    <Shipper>\n      <ShipperNumber>" . $ups_config
    ->get('shipper_number') . "</ShipperNumber>\n      <Address>\n        <City>" . $store_config
    ->get('address.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 ($ups_config
    ->get('residential_quotes')) {
    $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 ($ups_config
    ->get('negotiated_rates')) {
    $schema .= "<RateInformation>\n          <NegotiatedRatesIndicator/>\n        </RateInformation>";
  }
  $schema .= "</Shipment>\n</RatingServiceSelectionRequest>";
  return $schema;
}