You are here

function uc_ups_confirm_shipment_submit in Ubercart 7.3

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

Generates label and schedules pickup of the shipment.

See also

uc_ups_confirm_shipment()

File

shipping/uc_ups/uc_ups.ship.inc, line 634
UPS functions for label generation.

Code

function uc_ups_confirm_shipment_submit($form, &$form_state) {

  // Request pickup using parameters in form.
  $order_id = $_SESSION['ups']['order_id'];
  $packages = array_keys($_SESSION['ups']['packages']);
  $request = uc_ups_request_pickup($form_state['values']['digest'], $order_id, $packages);
  $result = drupal_http_request(variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/') . 'ShipAccept', array(
    'method' => 'POST',
    'data' => $request,
  ));
  $response = new SimpleXMLElement($result->data);
  $code = (string) $response->Response->ResponseStatusCode;
  if ($code == 0) {

    // failed request
    $error = $response->Response->Error;
    $error_severity = (string) $error->ErrorSeverity;
    $error_code = (string) $error->ErrorCode;
    $error_description = (string) $error->ErrorDescription;
    drupal_set_message(t('(@severity error @code) @description', array(
      '@severity' => $error_severity,
      '@code' => $error_code,
      '@description' => $error_description,
    )), 'error');
    if ($error_severity == 'HardError') {
      $form_state['redirect'] = 'admin/store/orders/' . $order_id . '/shipments/ups/' . implode('/', $packages);
      return;
    }
  }
  $shipment = new stdClass();
  $shipment->order_id = $order_id;
  $shipment->origin = clone $_SESSION['ups']['origin'];
  $shipment->destination = clone $_SESSION['ups']['destination'];
  $shipment->packages = $_SESSION['ups']['packages'];
  $shipment->shipping_method = 'ups';
  $shipment->accessorials = $_SESSION['ups']['service'];
  $shipment->carrier = t('UPS');

  // if NegotiatedRates exist, quote based on those, otherwise, use TotalCharges
  if (isset($response->ShipmentResults->ShipmentCharges)) {
    $charge = $response->ShipmentResults->ShipmentCharges->TotalCharges;
    if (isset($response->ShipmentResults->NegotiatedRates)) {
      $charge = $response->ShipmentResults->NegotiatedRates->NetSummaryCharges->GrandTotal;
    }
  }
  $cost = (string) $charge->MonetaryValue;
  $shipment->cost = $cost;
  $shipment->tracking_number = (string) $response->ShipmentResults->ShipmentIdentificationNumber;
  $ship_date = $_SESSION['ups']['ship_date'];
  $shipment->ship_date = gmmktime(12, 0, 0, $ship_date['month'], $ship_date['day'], $ship_date['year']);
  $exp_delivery = $_SESSION['ups']['expected_delivery'];
  $shipment->expected_delivery = gmmktime(12, 0, 0, $exp_delivery['month'], $exp_delivery['day'], $exp_delivery['year']);
  foreach ($response->ShipmentResults->PackageResults as $package_results) {
    $package =& current($shipment->packages);
    $package->tracking_number = (string) $package_results->TrackingNumber;
    $label_image = (string) $package_results->LabelImage->GraphicImage;

    // Save the label
    $directory = 'public://ups_labels';
    if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
      $label_path = $directory . '/label-' . $package->tracking_number . '.gif';
      if ($label_file = file_save_data(base64_decode($label_image), $label_path, FILE_EXISTS_REPLACE)) {
        file_usage_add($label_file, 'uc_shipping', 'package', $package->package_id);
        $package->label_image = $label_file;
      }
      else {
        drupal_set_message(t('Could not open a file to save the label image.'), 'error');
      }
    }
    else {
      drupal_set_message(t('Could not find or create the directory %directory in the file system path.', array(
        '%directory' => $directory,
      )), 'error');
    }
    unset($package);
    next($shipment->packages);
  }
  uc_shipping_shipment_save($shipment);
  unset($_SESSION['ups']);
  $form_state['redirect'] = 'admin/store/orders/' . $order_id . '/shipments';
}