You are here

function uc_shipping_new_shipment in Ubercart 5

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

Set up a new shipment with the chosen packages.

See also

theme_uc_shipping_new_shipment

uc_shipping_new_shipment_submit

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

File

shipping/uc_shipping/uc_shipping.module, line 624
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_shipment($order_id) {
  $breadcrumb = drupal_get_breadcrumb();
  $breadcrumb[] = l(t('Shipments'), 'admin/store/orders/' . $order_id . '/shipments');
  drupal_set_breadcrumb($breadcrumb);
  $form = array(
    '#tree' => true,
  );
  $form['order_id'] = array(
    '#type' => 'hidden',
    '#value' => $order_id,
  );
  $packages_by_type = array();
  $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = %d AND sid IS NULL", $order_id);
  while ($package = db_fetch_object($result)) {
    $products = array();
    $weight = 0;
    $result2 = db_query("SELECT pp.order_product_id, pp.qty, pp.qty * op.weight AS weight, op.title, op.model FROM {uc_packaged_products} AS pp LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = %d", $package->package_id);
    while ($product = db_fetch_object($result2)) {
      $weight += $product->weight;
      $products[$product->order_product_id] = $product;
    }
    $package->weight = $weight;
    $package->products = $products;
    $packages_by_type[$package->shipping_type][$package->package_id] = $package;
  }
  $option_methods = array();
  $shipping_types = module_invoke_all('shipping_type');
  $shipping_methods = module_invoke_all('shipping_method');
  $shipping_methods_by_type = array();
  foreach ($shipping_methods as $method) {
    if (isset($method['ship'])) {
      $shipping_methods_by_type[$method['ship']['type']][] = $method;
    }
  }
  foreach ($packages_by_type as $shipping_type => $packages) {
    $form[$shipping_type] = array(
      '#type' => 'fieldset',
      '#title' => $shipping_types[$shipping_type]['title'],
    );
    $form[$shipping_type]['packages'] = array();
    foreach ($packages as $package) {
      $pkgs_exist = true;
      $package_row = array();
      $package_row['checked'] = array(
        '#type' => 'checkbox',
        '#default_value' => 0,
      );
      $package_row['package_id'] = array(
        '#value' => $package->package_id,
      );
      $product_list = array();
      foreach ($package->products as $product) {
        $product_list[] = $product->qty . ' x ' . check_plain($product->model);
      }
      $package_row['products'] = array(
        '#value' => '<ul><li>' . implode('</li><li>', $product_list) . '</li></ul>',
      );
      $package_row['weight'] = array(
        '#value' => $package->weight,
      );
      $form[$shipping_type]['packages'][$package->package_id] = $package_row;
    }
    if (is_array($shipping_methods_by_type[$shipping_type])) {
      foreach ($shipping_methods_by_type[$shipping_type] as $method) {
        $option_methods += array(
          $method['id'] => $method['title'],
        );
      }
    }
  }
  if ($pkgs_exist) {
    $option_methods = array(
      'all' => t('Ship Manually'),
    ) + $option_methods;
    $form['method'] = array(
      '#type' => 'select',
      '#title' => t('Shipping method'),
      '#options' => $option_methods,
      '#default_value' => 'all',
    );
    $form['ship'] = array(
      '#type' => 'submit',
      '#value' => t('Ship packages'),
    );
  }

  //drupal_set_message('<pre>'. print_r($form, true) .'</pre>');
  return $form;
}