You are here

function merci_reserved_bucket_items in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6.2

Same name and namespace in other branches
  1. 7.2 includes/database.inc \merci_reserved_bucket_items()
3 calls to merci_reserved_bucket_items()
merci_get_available_bucket_count in includes/database.inc
Calculates the total number of available bucket items for a reservation.
merci_get_suggested_bucket_item in includes/api.inc
merci_load_reservations_for_node_in_timespan in includes/database.inc
Builds an array representing reservations for a Resource within a given timespan

File

includes/database.inc, line 465
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_reserved_bucket_items($content_type, $start = NULL, $end = NULL, $exclude_nid = NULL, $overdue = TRUE) {

  // Determine CCK table and columns the date data is stored in.
  $field = content_fields('field_merci_date');
  $db_info = content_database_info($field);
  $table = $db_info['table'];
  $column_start_date = $db_info['columns']['value']['column'];
  $column_end_date = $db_info['columns']['value2']['column'];

  // Get all assignable nodes for this bucket item.
  $total_items_nodes = db_query("SELECT n.nid FROM {node} n INNER JOIN {merci_reservation_item_node} m ON n.vid = m.vid WHERE n.type = '%s' AND m.merci_sub_type = %d AND m.merci_default_availability IN (%d, %d)", $content_type, MERCI_SUB_TYPE_ITEM, MERCI_AVA_F);
  $total_items_array = array();
  while ($ctnodes = db_fetch_array($total_items_nodes)) {
    $total_items_array[$ctnodes['nid']] = array();
  }
  $args = array(
    $start,
    $end,
    $start,
    $end,
    $start,
    $end,
    $content_type,
    MERCI_ITEM_STATUS_AVAILABLE,
  );

  // If we're checking an existing reservation, exclude it from the
  // reserved items.
  if (isset($exclude_nid)) {
    $where = ' AND ct.nid <> %d';
    $args[] = $exclude_nid;
  }
  else {
    $where = '';
  }

  // pull reservations with assigned nodes and status of MERCI_ITEM_STATUS_RESERVED or MERCI_ITEM_STATUS_CHECKED_OUT
  $reserved_nodes = db_query("\n    SELECT ct.nid,{$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 ,md.merci_item_nid FROM {" . $table . "} ct\n      INNER JOIN {merci_reservation_detail} md on ct.vid = md.vid\n      INNER JOIN {merci_bucket_node} m on md.merci_placeholder_nid = m.nid\n      INNER JOIN {node} ctn on ct.vid = ctn.vid\n      INNER JOIN {node} mn on m.vid = mn.vid\n        WHERE (\n                ({$column_start_date} >= '%s' and {$column_start_date} <= '%s')\n            OR  ({$column_end_date} >= '%s' and {$column_end_date} <= '%s')\n            OR  ({$column_start_date} <= '%s' and {$column_end_date} >= '%s')\n          )\n          AND mn.type = '%s'\n          AND md.merci_item_nid !=0\n          AND NOT md.merci_item_status <= %d\n          {$where}", $args);

  // Use up items for assigned nodes.
  while ($node = db_fetch_object($reserved_nodes)) {

    // If item is assigned then item is in use by this node.
    $total_items_array[$node->merci_item_nid][$node->nid] = $node;
  }
  if ($overdue) {
    $overdue_items_array = merci_overdue_items($content_type, $start, $exclude_nid);
    if (!empty($overdue_items_array)) {
      foreach ($overdue_items_array as $merci_item_nid => $nodes) {
        foreach ($nodes as $nid => $node) {
          $total_items_array[$node->merci_item_nid][$node->nid] = $node;
        }
      }
    }
  }

  // pull reservations without assigned nodes and not status of MERCI_ITEM_STATUS_CHECKED_IN
  $reserved_nodes = db_query("\n    SELECT ct.nid,{$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 ,md.merci_item_nid FROM {" . $table . "} ct\n        INNER JOIN {merci_reservation_detail} md ON ct.vid = md.vid\n        INNER JOIN {merci_bucket_node} m ON md.merci_placeholder_nid = m.nid\n        INNER JOIN {node} ctn on ct.vid = ctn.vid\n        INNER JOIN {node} mn ON m.vid = mn.vid\n          WHERE (\n                  ({$column_start_date} >= '%s' AND {$column_start_date} <= '%s')\n              OR  ({$column_end_date} >= '%s' AND {$column_end_date} <= '%s')\n              OR  ({$column_start_date} <= '%s' AND {$column_end_date} >= '%s')\n            )\n            AND mn.type = '%s'\n            AND md.merci_item_nid = 0\n            AND NOT md.merci_item_status < %d\n            {$where}", $args);
  uasort($total_items_array, '_merci_sort_array');

  // Temporarily assign an item for these nodes.
  while ($node = db_fetch_object($reserved_nodes)) {

    // Eat up a bucket item for this node.
    // If item is not assigned then temporarily add one.
    foreach ($total_items_array as $item_nid => $reservations) {
      $willitfit = TRUE;
      foreach ($reservations as $oldnode) {

        // Does the start date overlap this reservation.
        if (date_create($node->field_merci_date_value) > date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value) < date_create($oldnode->field_merci_date_value2) or date_create($node->field_merci_date_value2) > date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value2) < date_create($oldnode->field_merci_date_value2) or date_create($node->field_merci_date_value) <= date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value2) >= date_create($oldnode->field_merci_date_value2)) {

          // Can't use this item for this reservation. So try another.
          $willitfit = FALSE;
          break;
        }
      }
      if ($willitfit) {
        $total_items_array[$item_nid][$node->nid] = $node;
        break;
      }
    }
  }
  return $total_items_array;
}