You are here

function uc_ups_confirm_shipment_submit 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_confirm_shipment_submit()
  2. 6.2 shipping/uc_ups/uc_ups.ship.inc \uc_ups_confirm_shipment_submit()
  3. 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_confirm_shipment_submit()

Submit handler for uc_ups_confirm_shipment().

Generate label and schedule pickup of the shipment.

File

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

Code

function uc_ups_confirm_shipment_submit($form_id, $form_values) {
  include_once drupal_get_path('module', 'uc_store') . '/includes/simplexml.php';

  // Request pickup using parameters in form.
  $order_id = $_SESSION['ups']['order_id'];
  $packages = array_keys($_SESSION['ups']['packages']);
  $request = uc_ups_request_pickup($form_values['digest'], $order_id, $packages);
  $result = drupal_http_request(variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/') . 'ShipAccept', array(), 'POST', $request);
  $response = new JSimpleXML();
  $response
    ->loadString($result->data);
  $code = $response->document->response[0]->responsestatuscode[0]
    ->data();
  if ($code == 0) {

    // failed request
    $error = $response->document->response[0]->error[0];
    $error_severity = $error->errorseverity[0]
      ->data();
    $error_code = $error->errorcode[0]
      ->data();
    $error_description = $error->errordescription[0]
      ->data();
    drupal_set_message(t('(@severity error @code) @description', array(
      '@severity' => $error_severity,
      '@code' => $error_code,
      '@description' => $error_description,
    )), 'error');
    if ($error_severity == 'HardError') {
      return 'admin/store/orders/' . $order_id . '/shipments/ups/' . implode('/', $packages);
    }
  }
  $shipment = new stdClass();
  $shipment->order_id = $order_id;
  $shipment->origin = drupal_clone($_SESSION['ups']['origin']);
  $shipment->destination = drupal_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 (is_array($response->document->shipmentresults[0]->shipmentcharges)) {
    $charge = $response->document->shipmentresults[0]->shipmentcharges[0]->totalcharges[0];
    if (is_array($response->document->shipmentresults[0]->negotiatedrates)) {
      $charge = $response->document->shipmentresults[0]->negotiatedrates[0]->netsummarycharges[0]->grandtotal[0];
    }
  }
  $cost = $charge->monetaryvalue[0]
    ->data();
  $shipment->cost = $cost;
  $shipment->tracking_number = $response->document->shipmentresults[0]->shipmentidentificationnumber[0]
    ->data();
  $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->document->shipmentresults[0]->packageresults as $package_results) {
    $package =& current($shipment->packages);
    $package->tracking_number = $package_results->trackingnumber[0]
      ->data();
    $label_image = $package_results->labelimage[0]->graphicimage[0]
      ->data();
    if (file_check_directory(file_create_path('ups_labels'), FILE_CREATE_DIRECTORY)) {
      $label_path = file_create_path('ups_labels') . '/label' . $package->tracking_number . '.gif';
      if ($label_file = fopen($label_path, 'wb')) {
        fwrite($label_file, base64_decode($label_image));
        fclose($label_file);
        $package->label_image = $label_path;
      }
      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 "ups_labels" in the file system path.'), 'error');
    }
    unset($package);
    next($shipment->packages);
  }
  uc_shipping_shipment_save($shipment);
  unset($_SESSION['ups']);
  return 'admin/store/orders/' . $order_id . '/shipments';
}