You are here

function uc_shipping_new_package in Ubercart 7.3

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

Puts ordered products into a package.

See also

uc_shipping_new_package_validate()

uc_shipping_new_package_submit()

theme_uc_shipping_new_package_fieldset()

1 string reference to 'uc_shipping_new_package'
uc_shipping_menu in shipping/uc_shipping/uc_shipping.module
Implements hook_menu().

File

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

Code

function uc_shipping_new_package($form, &$form_state, $order) {
  $form['#tree'] = TRUE;
  $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());
  $result = db_query("SELECT op.order_product_id, SUM(pp.qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE p.order_id = :id GROUP BY op.order_product_id", array(
    ':id' => $order->order_id,
  ));
  $packaged_products = $result
    ->fetchAllKeyed();
  $form['shipping_types'] = array();
  $shipping_type_options = uc_quote_shipping_type_options();
  foreach ($shipping_types_products as $shipping_type => $products) {
    $form['shipping_types'][$shipping_type] = array(
      '#type' => 'fieldset',
      '#title' => isset($shipping_type_options[$shipping_type]) ? $shipping_type_options[$shipping_type] : ucwords(str_replace('_', ' ', $shipping_type)),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => isset($shipping_type_weights[$shipping_type]) ? $shipping_type_weights[$shipping_type] : 0,
    );
    foreach ($products as $product) {
      $unboxed_qty = $product->qty;
      if (isset($packaged_products[$product->order_product_id])) {
        $unboxed_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(
          '#markup' => check_plain($product->model),
        );
        $product_row['name'] = array(
          '#markup' => filter_xss_admin($product->title),
        );
        $product_row['qty'] = array(
          '#type' => 'select',
          '#title' => theme('uc_qty_label'),
          '#title_display' => 'invisible',
          '#options' => drupal_map_assoc(range(1, $unboxed_qty)),
          '#default_value' => $unboxed_qty,
        );
        $options = drupal_map_assoc(range(0, count($order->products)));
        $options[0] = t('Sep.');
        $product_row['package'] = array(
          '#type' => 'select',
          '#title' => t('Package'),
          '#title_display' => 'invisible',
          '#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->order_id,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['create'] = array(
    '#type' => 'submit',
    '#value' => t('Make packages'),
  );
  $form['actions']['combine'] = array(
    '#type' => 'submit',
    '#value' => t('Create one package'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );
  return $form;
}