You are here

function uc_shipping_new_shipment in Ubercart 6.2

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

Sets up a new shipment with the chosen packages.

See also

uc_shipping_new_shipment_submit()

theme_uc_shipping_new_shipment()

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

File

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

Code

function uc_shipping_new_shipment($form_state, $order) {
  $breadcrumb = drupal_get_breadcrumb();
  $breadcrumb[] = l(t('Administer'), 'admin');
  $breadcrumb[] = l(t('Store administration'), 'admin/store');
  $breadcrumb[] = l(t('Orders'), 'admin/store/orders');
  $breadcrumb[] = l(t('Order @id', array(
    '@id' => $order->order_id,
  )), 'admin/store/orders/' . $order->order_id);
  $breadcrumb[] = l(t('Shipments'), 'admin/store/orders/' . $order->order_id . '/shipments');
  drupal_set_breadcrumb($breadcrumb);
  $checked_pkgs = isset($_REQUEST['pkgs']) ? $_REQUEST['pkgs'] : array();
  $form = array(
    '#tree' => TRUE,
  );
  $form['order_id'] = array(
    '#type' => 'hidden',
    '#value' => $order->order_id,
  );
  $packages_by_type = array();
  $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = %d AND sid IS NULL", $order->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 = uc_quote_get_shipping_types();
  $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;
    }
  }
  $pkgs_exist = FALSE;
  foreach ($packages_by_type as $shipping_type => $packages) {
    $form['shipping_types'][$shipping_type] = array(
      '#type' => 'fieldset',
      '#title' => $shipping_types[$shipping_type]['title'],
    );
    $form['shipping_types'][$shipping_type]['packages'] = array();
    foreach ($packages as $package) {
      $pkgs_exist = TRUE;
      $package_row = array();
      $package_row['checked'] = array(
        '#type' => 'checkbox',
        '#default_value' => in_array($package->package_id, $checked_pkgs) ? 1 : 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_types'][$shipping_type]['packages'][$package->package_id] = $package_row;
    }
    if (isset($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'),
    );
  }
  return $form;
}