function uc_ups_confirm_shipment_submit in Ubercart 6.2
Same name and namespace in other branches
- 8.4 shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc \uc_ups_confirm_shipment_submit()
- 5 shipping/uc_ups/uc_ups.module \uc_ups_confirm_shipment_submit()
- 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_confirm_shipment_submit()
Generates label and schedules pickup of the shipment.
See also
File
- shipping/
uc_ups/ uc_ups.ship.inc, line 602 - 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(), 'POST', $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 = 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 (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;
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 = 'ups_labels/' . basename($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']);
$form_state['redirect'] = 'admin/store/orders/' . $order_id . '/shipments';
}