You are here

function merci_get_available_bucket_count in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 includes/database.inc \merci_get_available_bucket_count()
  2. 7.2 includes/database.inc \merci_get_available_bucket_count()

Calculates the total number of available bucket items for a reservation.

Parameters

$content_type: The bucket content type.

$start: Start time in DATETIME format UTC timezone.

$end: End time in DATETIME format UTC timezone.

$reservation_nid: (Optional) A reservation nid to exclude from the reserved items.

Return value

The number of available bucket items.

1 call to merci_get_available_bucket_count()
merci_build_reservable_items in ./merci.module
Builds the list of all currently reservable items, filtered by date.

File

./merci.module, line 2078
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_get_available_bucket_count($content_type, $start, $end, $reservation = NULL) {

  // 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'];

  // Pull all active items for this bucket.
  $total_items = (int) db_result(db_query("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {merci_bucket_node} m ON n.vid = m.vid WHERE n.type = '%s' AND m.sub_type = %d AND m.default_availability IN (%d, %d)", $content_type, MERCI_SUB_TYPE_ITEM, MERCI_AVA_F, MERCI_AVA_S));
  $args = array(
    $start,
    $end,
    $start,
    $end,
    $start,
    $end,
    $content_type,
    MERCI_ITEM_STATUS_CHECKED_IN,
  );

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

  // Pull reserved bucket items for the period of the reservation.
  $reserved_items = (int) db_result(db_query("SELECT COUNT(md.nid) FROM {" . $table . "} ct INNER JOIN {merci_reservation_detail} md ON ct.vid = md.vid INNER JOIN {merci_bucket_node} m ON md.placeholder_nid = m.nid INNER JOIN {node} ctn ON ct.vid = ctn.vid INNER JOIN {node} mn ON m.vid = mn.vid WHERE (({$column_start_date} >= '%s' AND {$column_start_date} <= '%s') OR ({$column_end_date} >= '%s' AND {$column_end_date} <= '%s') OR ({$column_start_date} <= '%s' AND {$column_end_date} >= '%s')) AND mn.type = '%s' AND NOT (md.item_status <= %d){$where}", $args));
  return $total_items - $reserved_items;
}