You are here

function uc_ups_confirm_shipment_submit in Ubercart 8.4

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

Generates label and schedules pickup of the shipment.

See also

uc_ups_confirm_shipment()

File

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

Code

function uc_ups_confirm_shipment_submit($form, FormStateInterface $form_state) {
  $ups_config = \Drupal::config('uc_ups.setttings');

  // 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
    ->getValue('digest'), $order_id, $packages);
  $result = \Drupal::httpClient()
    ->post($ups_config
    ->get('connection_address') . 'ShipAccept', NULL, $request)
    ->send();
  $response = new \SimpleXMLElement($result
    ->getBody(TRUE));
  $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::messenger()
      ->addError(t('(@severity error @code) @description', [
      '@severity' => $error_severity,
      '@code' => $error_code,
      '@description' => $error_description,
    ]));
    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::messenger()
          ->addError(t('Could not open a file to save the label image.'));
      }
    }
    else {
      \Drupal::messenger()
        ->addError(t('Could not find or create the directory %directory in the file system path.', [
        '%directory' => $directory,
      ]));
    }
    unset($package);
    next($shipment->packages);
  }
  uc_shipping_shipment_save($shipment);
  unset($_SESSION['ups']);
  $form_state['redirect'] = 'admin/store/orders/' . $order_id . '/shipments';
}