You are here

function uc_shipping_shipment_edit in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_shipping/uc_shipping.admin.inc \uc_shipping_shipment_edit()
  2. 7.3 shipping/uc_shipping/uc_shipping.admin.inc \uc_shipping_shipment_edit()

Create or edit a shipment.

See also

theme_uc_shipping_package_dimensions

theme_uc_shipping_address

uc_shipping_shipment_edit_submit

2 string references to 'uc_shipping_shipment_edit'
uc_shipping_make_shipment in shipping/uc_shipping/uc_shipping.module
Default method to send packages on a shipment.
uc_shipping_menu in shipping/uc_shipping/uc_shipping.module
Implementation of hook_shipping_menu().

File

shipping/uc_shipping/uc_shipping.module, line 830
Organizes ordered products into packages and sets them up for shipment. Shipping method modules may add functionality to generate shipping labels and tracking numbers.

Code

function uc_shipping_shipment_edit($order_id, $shipment) {
  drupal_add_css(drupal_get_path('module', 'uc_shipping') . '/uc_shipping.css');
  $order = uc_order_load($order_id);
  if (is_numeric($shipment)) {
    $shipment = uc_shipping_shipment_load($shipment);
  }
  $form = array();
  $form['order_id'] = array(
    '#type' => 'value',
    '#value' => $order_id,
  );
  if (isset($shipment->sid)) {
    $form['sid'] = array(
      '#type' => 'value',
      '#value' => $shipment->sid,
    );
    $methods = module_invoke_all('shipping_method');
    $method = $methods[$shipment->shipping_method];
  }
  $addresses = array();
  $form['packages'] = array(
    '#tree' => true,
    '#weight' => 1,
  );
  if ($shipment->o_street1) {
    $o_address = new stdClass();
    foreach ($shipment as $field => $value) {
      if (substr($field, 0, 2) == 'o_') {
        $o_address->{substr($field, 2)} = $value;
      }
    }
    $addresses[] = $o_address;
  }
  foreach ($shipment->packages as $id => $package) {
    foreach ($package->addresses as $address) {
      if (!in_array($address, $addresses)) {
        $addresses[] = $address;
      }
    }
    $declared_value = 0;
    $product_list = array();
    foreach ($package->products as $product) {
      $product_list[] = $product->qty . ' x ' . check_plain($product->model);
      $declared_value += $product->qty * $product->price;
    }
    $form['packages'][$id] = array(
      '#type' => 'fieldset',
      '#title' => t('Package @id', array(
        '@id' => $id,
      )),
      '#collapsible' => true,
    );
    $form['packages'][$id]['products'] = array(
      '#value' => theme('item_list', $product_list),
    );
    $form['packages'][$id]['pkg_type'] = array(
      '#type' => 'textfield',
      '#title' => t('Package type'),
      '#default_value' => $package->pkg_type,
      '#description' => t('E.g.: Box, pallet, tube, treasure chest, cocoon, etc.'),
    );
    if (isset($method) && is_array($method['ship']['pkg_types'])) {
      $form['packages'][$id]['pkg_type']['#type'] = 'select';
      $form['packages'][$id]['pkg_type']['#options'] = $method['ship']['pkg_types'];
      $form['packages'][$id]['pkg_type']['#description'] = '';
    }
    $form['packages'][$id]['dimensions'] = array(
      '#type' => 'fieldset',
      '#title' => t('Dimensions'),
      '#description' => t('Physical dimensions of the packaged product.'),
      '#theme' => 'uc_shipping_package_dimensions',
    );
    $form['packages'][$id]['dimensions']['units'] = array(
      '#type' => 'select',
      '#title' => t('Units of measurement'),
      '#options' => array(
        'in' => t('Inches'),
        'ft' => t('Feet'),
        'cm' => t('Centimeters'),
        'mm' => t('Millimeters'),
      ),
      '#default_value' => $package->length_units ? $package->length_units : variable_get('uc_length_unit', 'in'),
    );
    $form['packages'][$id]['dimensions']['length'] = array(
      '#type' => 'textfield',
      '#title' => t('Length'),
      '#default_value' => $package->length,
      '#size' => 8,
    );
    $form['packages'][$id]['dimensions']['width'] = array(
      '#type' => 'textfield',
      '#title' => t('Width'),
      '#default_value' => $package->width,
      '#size' => 8,
    );
    $form['packages'][$id]['dimensions']['height'] = array(
      '#type' => 'textfield',
      '#title' => t('Height'),
      '#default_value' => $package->height,
      '#size' => 8,
    );
    $form['packages'][$id]['declared_value'] = array(
      '#type' => 'textfield',
      '#title' => t('Declared value'),
      '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
      '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
      '#default_value' => isset($package->value) ? $package->value : $declared_value,
    );
    $form['packages'][$id]['tracking_number'] = array(
      '#type' => 'textfield',
      '#title' => t('Tracking number'),
      '#default_value' => $package->tracking_number,
    );
  }
  $form = array_merge($form, uc_shipping_address_form($addresses, $order));
  $form['shipment'] = array(
    '#type' => 'fieldset',
    '#title' => t('Shipment data'),
    '#collapsible' => true,
    '#weight' => 0,
  );
  $form['shipment']['shipping_method'] = array(
    '#type' => 'hidden',
    '#value' => isset($shipment->shipping_method) ? $shipment->shipping_method : 'manual',
  );
  $form['shipment']['carrier'] = array(
    '#type' => 'textfield',
    '#title' => t('Carrier'),
    '#default_value' => $shipment->carrier,
  );
  $form['shipment']['accessorials'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipment options'),
    '#default_value' => $shipment->accessorials,
    '#description' => t('Short notes about the shipment, e.g. residential, overnight, etc.'),
  );
  $form['shipment']['transaction_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Transaction ID'),
    '#default_value' => $shipment->transaction_id,
  );
  $form['shipment']['tracking_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Tracking number'),
    '#default_value' => $shipment->tracking_number,
  );
  if (isset($shipment->ship_date)) {
    $ship_date = getdate($shipment->ship_date);
  }
  else {
    $ship_date = getdate();
  }
  if (isset($shipment->expected_delivery)) {
    $exp_delivery = getdate($shipment->expected_delivery);
  }
  else {
    $exp_delivery = getdate();
  }
  $form['shipment']['ship_date'] = array(
    '#type' => 'date',
    '#title' => t('Ship date'),
    '#default_value' => array(
      'year' => $ship_date['year'],
      'month' => $ship_date['mon'],
      'day' => $ship_date['mday'],
    ),
  );
  $form['shipment']['expected_delivery'] = array(
    '#type' => 'date',
    '#title' => t('Expected delivery'),
    '#default_value' => array(
      'year' => $exp_delivery['year'],
      'month' => $exp_delivery['mon'],
      'day' => $exp_delivery['mday'],
    ),
  );
  $form['shipment']['cost'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipping cost'),
    '#default_value' => $shipment->cost,
    '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
    '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save shipment'),
    '#weight' => 10,
  );
  return $form;
}