You are here

function uc_shipping_new_package in Ubercart 5

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

Put ordered products into a package.

See also

theme_uc_shipping_new_package_fieldset

uc_shipping_new_package_validate

uc_shipping_new_package_submit

1 string reference to 'uc_shipping_new_package'
uc_shipping_menu in shipping/uc_shipping/uc_shipping.module
Implementation of hook_shipping_menu().

File

shipping/uc_shipping/uc_shipping.module, line 235
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_new_package($order_id) {
  $breadcrumb = drupal_get_breadcrumb();
  $breadcrumb[] = l(t('Packages'), 'admin/store/orders/' . $order_id . '/packages');
  drupal_set_breadcrumb($breadcrumb);
  $form = array(
    '#tree' => true,
  );
  $form['instructions'] = array(
    '#value' => t('Organize products into packages.
 Package numbers in multiple shipping types are of the first shipping type they appear in. All
 packages are given a unique ID when they are saved. Choose the default package "Sep." to
 automatically create a package for each of the selected quantity of products in that row.'),
  );
  $order = uc_order_load($order_id);
  $shipping_types_products = array();
  foreach ($order->products as $product) {
    if ($product->data['shippable']) {
      $product->shipping_type = uc_product_get_shipping_type($product);
      $shipping_types_products[$product->shipping_type][] = $product;
    }
  }
  $shipping_type_weights = variable_get('uc_quote_type_weight', array());
  $packaged_products = array();
  $result = db_query("SELECT op.order_product_id, SUM(pp.qty) AS quantity FROM {uc_packaged_products} AS pp LEFT JOIN {uc_packages} AS p ON pp.package_id = p.package_id LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id WHERE p.order_id = %d GROUP BY op.order_product_id", $order_id);
  while ($boxed_product = db_fetch_object($result)) {
    $packaged_products[$boxed_product->order_product_id] = $boxed_product->quantity;
  }
  $form['shipping_types'] = array();
  foreach ($shipping_types_products as $shipping_type => $products) {
    $form['shipping_types'][$shipping_type] = array(
      '#type' => 'fieldset',
      '#title' => ucwords(str_replace('_', ' ', $shipping_type)),
      '#collapsible' => true,
      '#collapsed' => false,
      '#weight' => $shipping_type_weights[$shipping_type],
    );
    foreach ($products as $product) {
      $unboxed_qty = $product->qty - $packaged_products[$product->order_product_id];
      if ($unboxed_qty > 0) {
        $product_row = array();
        $product_row['checked'] = array(
          '#type' => 'checkbox',
          '#default_value' => 0,
        );
        $product_row['model'] = array(
          '#type' => 'markup',
          '#value' => check_plain($product->model),
        );
        $product_row['name'] = array(
          '#type' => 'markup',
          '#value' => filter_xss_admin($product->title),
        );
        $product_row['qty'] = array(
          '#type' => 'select',
          '#options' => drupal_map_assoc(uc_range(1, $unboxed_qty)),
          '#default_value' => $unboxed_qty,
        );
        $options = drupal_map_assoc(uc_range(0, count($order->products)));
        $options[0] = t('Sep.');
        $product_row['package'] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => 0,
        );
        $form['shipping_types'][$shipping_type][$product->order_product_id] = $product_row;
      }
    }
    $form['shipping_types'][$shipping_type]['#theme'] = 'uc_shipping_new_package_fieldset';
  }
  $form['order_id'] = array(
    '#type' => 'hidden',
    '#value' => $order_id,
  );
  $form['create'] = array(
    '#type' => 'submit',
    '#value' => t('Make packages'),
  );
  $form['combine'] = array(
    '#type' => 'submit',
    '#value' => t('Create one package'),
  );
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );
  return $form;
}