You are here

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

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

Adds items to reservation on creation/update.

Parameters

$node: The reservation node.

2 calls to merci_add_reservation_items()
merci_insert in ./merci.module
Implementation of hook_insert().
merci_update in ./merci.module
Implementation of hook_update().

File

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

Code

function merci_add_reservation_items($node) {

  // Update existing items.
  if (isset($node->existing_items['items'])) {
    foreach ($node->existing_items['items'] as $did => $item_nid) {

      // Only selected items get their status bumped to reserved.
      if ($item_nid) {
        $item_status = MERCI_ITEM_STATUS_RESERVED;
      }
      else {
        $item_status = MERCI_ITEM_STATUS_AVAILABLE;
      }
      if ($node->revision) {
        db_query("INSERT INTO {merci_reservation_detail} (nid, vid, placeholder_nid, item_nid, item_status) VALUES (%d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->existing_items['placeholders'][$did], $item_nid, $item_status);
      }
      else {
        db_query("UPDATE {merci_reservation_detail} SET item_nid = %d, item_status = %d WHERE did = %d", $item_nid, $item_status, $did);
      }
    }
  }

  // New items.
  foreach ($node->choice as $num => $choice) {

    // Resource.
    if (is_numeric($choice['item'])) {
      $item = db_fetch_object(db_query("SELECT type, title FROM {node} WHERE nid = %d", $choice['item']));
      $type = $item->type;
      $title = $item->title;
      $item_nid = $choice['item'];
      $item_node = node_load($item_nid);
      $item_status = MERCI_ITEM_STATUS_RESERVED;
    }
    elseif ($choice['item']) {
      $type = $choice['item'];
      $title = db_result(db_query("SELECT name FROM {node_type} WHERE type = '%s'", $type));
      $item_nid = 0;
      $item_node = FALSE;
      $item_status = MERCI_ITEM_STATUS_AVAILABLE;
    }
    else {

      // Nothing selected -- move along...
      continue;
    }

    // Build the item's placeholder node.
    $reservation = new stdClass();
    $reservation->type = $type;
    $reservation->name = $node->name;
    $reservation->uid = $node->uid;
    $reservation->title = "{$title} " . t('(Reservation)');
    $reservation->body = '';
    $reservation->status = 0;
    $reservation->promote = 0;
    $reservation->sticky = 0;

    // MERCI specific data.
    $merci_settings = merci_load_content_type_settings($type);
    $reservation->merci_default_availability = MERCI_AVA_F;
    $reservation->merci_sub_type = MERCI_SUB_TYPE_RESERVATION;

    // Use the item specific accounting data if an item is assigned,
    // otherwise fall back to the content type defaults.
    $reservation->merci_late_fee_per_hour = $item_node ? $item_node->merci['late_fee_per_hour'] : $merci_settings->late_fee_per_hour;
    $reservation->merci_rate_per_hour = $item_node ? $item_node->merci['rate_per_hour'] : $merci_settings->rate_per_hour;
    $reservation->merci_fee_free_hours = $item_node ? $item_node->merci['fee_free_hours'] : $merci_settings->fee_free_hours;
    $reservation->merci_min_cancel_hours = $item_node ? $item_node->merci['min_cancel_hours'] : $merci_settings->min_cancel_hours;
    $reservation->merci_autocheckout = $item_node ? $item_node->merci['autocheckout'] : $merci_settings->autocheckout;
    $reservation->merci_autocheckin = $item_node ? $item_node->merci['autocheckin'] : $merci_settings->autocheckin;
    $reservation->merci_selfcheckout = $item_node ? $item_node->merci['selfcheckout'] : $merci_settings->selfcheckout;
    $reservation = node_submit($reservation);
    node_save($reservation);
    db_query("INSERT INTO {merci_reservation_detail} (nid, vid, placeholder_nid, item_nid, item_status) VALUES (%d, %d, %d, %d, %d)", $node->nid, $node->vid, $reservation->nid, $item_nid, $item_status);
  }

  // Update the state of all items with associations.
  switch ((int) $node->merci_status) {
    case MERCI_STATUS_UNCONFIRMED:
    case MERCI_STATUS_PENDING:
      $item_status = MERCI_ITEM_STATUS_RESERVED;
      break;
    case MERCI_STATUS_CHECKED_OUT:
      $item_status = MERCI_ITEM_STATUS_CHECKED_OUT;
      break;
    case MERCI_STATUS_CHECKED_IN:
      $item_status = MERCI_ITEM_STATUS_CHECKED_IN;
      break;
    case MERCI_STATUS_CANCELLED:
    case MERCI_STATUS_DENIED:
      $item_status = MERCI_ITEM_STATUS_AVAILABLE;
      break;
  }
  db_query("UPDATE {merci_reservation_detail} SET item_status = %d WHERE vid = %d AND item_nid <> 0", $item_status, $node->vid);
}