You are here

function uc_shipping_package_edit in Ubercart 6.2

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

Rearranges the products in or out of a package.

See also

theme_uc_shipping_edit_package_fieldset()

uc_shipping_package_edit_submit()

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

File

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

Code

function uc_shipping_package_edit($form_state, $order, $package) {
  $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('Packages'), 'admin/store/orders/' . $order->order_id . '/packages');
  drupal_set_breadcrumb($breadcrumb);
  $products = array();
  $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->order_product_id] = $product;
      $products[$product->order_product_id] = $product;
    }
  }
  $result = db_query("SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} AS pp LEFT JOIN {uc_packages} AS p ON pp.package_id = p.package_id WHERE p.order_id = %d GROUP BY order_product_id", $order->order_id);
  while ($packaged_product = db_fetch_object($result)) {

    //Make already packaged products unavailable, except those in this package.
    $products[$packaged_product->order_product_id]->qty = $products[$packaged_product->order_product_id]->qty - $packaged_product->quantity + $package->products[$packaged_product->order_product_id]->qty;
  }
  $form = array();
  $form['#tree'] = TRUE;
  $form['package_id'] = array(
    '#type' => 'hidden',
    '#value' => $package->package_id,
  );
  $form['products'] = array();
  foreach ($products as $product) {
    if ($product->qty > 0) {
      $product_row = array();
      $product_row['checked'] = array(
        '#type' => 'checkbox',
        '#default_value' => isset($package->products[$product->order_product_id]),
      );
      $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, $product->qty)),
        '#default_value' => $package->products[$product->order_product_id]->qty,
      );
      $form['products'][$product->order_product_id] = $product_row;
    }
  }
  $form['products']['#theme'] = 'uc_shipping_edit_package_fieldset';
  $options = array();
  $shipping_type_options = uc_quote_shipping_type_options();
  foreach (array_keys($shipping_types_products) as $type) {
    $options[$type] = isset($shipping_type_options[$type]) ? $shipping_type_options[$type] : ucwords(str_replace('_', ' ', $type));
  }
  $form['shipping_type'] = array(
    '#type' => 'select',
    '#title' => t('Shipping type'),
    '#options' => $options,
    '#default_value' => $package->shipping_type,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}