You are here

function uc_shipping_shipment_edit in Ubercart 7.3

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

Creates or edits a shipment.

See also

uc_shipping_shipment_edit_validate()

uc_shipping_shipment_edit_submit()

2 string references to 'uc_shipping_shipment_edit'
uc_shipping_make_shipment in shipping/uc_shipping/uc_shipping.admin.inc
Default method to send packages on a shipment.
uc_shipping_menu in shipping/uc_shipping/uc_shipping.module
Implements hook_menu().

File

shipping/uc_shipping/uc_shipping.admin.inc, line 753
Shipping administration menu items.

Code

function uc_shipping_shipment_edit($form, &$form_state, $order, $shipment) {
  $form['order_id'] = array(
    '#type' => 'value',
    '#value' => $order->order_id,
  );
  if (isset($shipment->sid)) {
    $form['sid'] = array(
      '#type' => 'value',
      '#value' => $shipment->sid,
    );
    $methods = module_invoke_all('uc_shipping_method');
    if (isset($methods[$shipment->shipping_method])) {
      $method = $methods[$shipment->shipping_method];
    }
  }
  $addresses = array();
  $form['packages'] = array(
    '#type' => 'fieldset',
    '#title' => t('Packages'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  if (isset($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;
      }
    }

    // Create list of products and get a representative product (last one in
    // the loop) to use for some default values.
    $product_list = array();
    $declared_value = 0;
    foreach ($package->products as $product) {
      $product_list[] = $product->qty . ' x ' . check_plain($product->model);
      $declared_value += $product->qty * $product->price;
    }
    $pkg_form = array(
      '#type' => 'fieldset',
      '#title' => t('Package @id', array(
        '@id' => $id,
      )),
    );
    $pkg_form['products'] = array(
      '#theme' => 'item_list',
      '#items' => $product_list,
    );
    $pkg_form['package_id'] = array(
      '#type' => 'hidden',
      '#value' => $id,
    );
    $pkg_form['pkg_type'] = array(
      '#type' => 'textfield',
      '#title' => t('Package type'),
      '#default_value' => $package->pkg_type,
      '#description' => t('For example: Box, pallet, tube, envelope, etc.'),
    );
    if (isset($method) && is_array($method['ship']['pkg_types'])) {
      $pkg_form['pkg_type']['#type'] = 'select';
      $pkg_form['pkg_type']['#options'] = $method['ship']['pkg_types'];
      $pkg_form['pkg_type']['#description'] = '';
    }
    $pkg_form['declared_value'] = array(
      '#type' => 'uc_price',
      '#title' => t('Declared value'),
      '#default_value' => isset($package->value) ? $package->value : $declared_value,
    );
    $pkg_form['weight'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'uc-inline-form',
          'clearfix',
        ),
      ),
      '#description' => t('Weight of the package. Default value is sum of product weights in the package.'),
      '#weight' => 15,
    );
    $pkg_form['weight']['weight'] = array(
      '#type' => 'textfield',
      '#title' => t('Weight'),
      '#default_value' => isset($package->weight) ? $package->weight : 0,
      '#size' => 10,
    );
    $pkg_form['weight']['units'] = array(
      '#type' => 'select',
      '#title' => t('Units'),
      '#options' => array(
        'lb' => t('Pounds'),
        'kg' => t('Kilograms'),
        'oz' => t('Ounces'),
        'g' => t('Grams'),
      ),
      '#default_value' => isset($package->weight_units) ? $package->weight_units : variable_get('uc_weight_unit', 'lb'),
    );
    $pkg_form['dimensions'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'uc-inline-form',
          'clearfix',
        ),
      ),
      '#title' => t('Dimensions'),
      '#description' => t('Physical dimensions of the packaged product.'),
      '#weight' => 20,
    );
    $pkg_form['dimensions']['length'] = array(
      '#type' => 'textfield',
      '#title' => t('Length'),
      '#default_value' => isset($package->length) ? $package->length : 1,
      '#size' => 8,
    );
    $pkg_form['dimensions']['width'] = array(
      '#type' => 'textfield',
      '#title' => t('Width'),
      '#default_value' => isset($package->width) ? $package->width : 1,
      '#size' => 8,
    );
    $pkg_form['dimensions']['height'] = array(
      '#type' => 'textfield',
      '#title' => t('Height'),
      '#default_value' => isset($package->height) ? $package->height : 1,
      '#size' => 8,
    );
    $pkg_form['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' => isset($package->length_units) ? $package->length_units : variable_get('uc_length_unit', 'in'),
    );
    $pkg_form['tracking_number'] = array(
      '#type' => 'textfield',
      '#title' => t('Tracking number'),
      '#default_value' => isset($package->tracking_number) ? $package->tracking_number : '',
    );
    $form['packages'][$id] = $pkg_form;
  }
  if (!empty($shipment->d_street1)) {
    foreach ($shipment as $field => $value) {
      if (substr($field, 0, 2) == 'd_') {
        $order->{'delivery_' . substr($field, 2)} = $value;
      }
    }
  }
  $form = uc_shipping_address_form($form, $form_state, $addresses, $order);
  $form['shipment'] = array(
    '#type' => 'fieldset',
    '#title' => t('Shipment data'),
    '#collapsible' => TRUE,
  );

  // Determine shipping option chosen by the customer.
  $message = '';
  if (isset($order->quote['method'])) {

    // Order has a quote attached.
    $method = $order->quote['method'];
    $methods = module_invoke_all('uc_shipping_method');
    if (isset($methods[$method])) {

      // Quote is from a currently-active shipping method.
      $services = $methods[$method]['quote']['accessorials'];
      $method = $services[$order->quote['accessorials']];
    }
    $message = t('Customer selected "@method" as the shipping method and paid @rate', array(
      '@method' => $method,
      '@rate' => uc_currency_format($order->quote['rate']),
    ));
  }
  else {

    // No quotes for this order.
    $message = t('There are no shipping quotes attached to this order. Customer was not charged for shipping.');
  }

  // Inform administrator of customer's shipping choice.
  $form['shipment']['shipping_choice'] = array(
    '#type' => 'markup',
    '#prefix' => '<div>',
    '#markup' => $message,
    '#suffix' => '</div>',
  );
  $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' => isset($shipment->carrier) ? $shipment->carrier : '',
  );
  $form['shipment']['accessorials'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipment options'),
    '#default_value' => isset($shipment->accessorials) ? $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' => isset($shipment->transaction_id) ? $shipment->transaction_id : '',
  );
  $form['shipment']['tracking_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Tracking number'),
    '#default_value' => isset($shipment->tracking_number) ? $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' => 'uc_price',
    '#title' => t('Shipping cost'),
    '#default_value' => isset($shipment->cost) ? $shipment->cost : 0,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save shipment'),
    '#weight' => 10,
  );
  return $form;
}