You are here

function merci_commerce_line_item_add_form in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

File

merci_commerce/merci_commerce.module, line 113

Code

function merci_commerce_line_item_add_form($element, &$form_state) {
  $form = array();
  $query = db_select('commerce_product', 'c');
  $query
    ->addField('c', 'product_id', 'product_id');
  $query
    ->addField('c', 'title', 'title');
  $query
    ->condition('c.type', 'product');
  $result = $query
    ->execute();
  $options = array();
  foreach ($result as $record) {
    $options[$record->product_id] = $record->title;
  }
  $form['fee_type_id'] = array(
    '#type' => 'select',
    '#title' => t('Fee type'),
    '#description' => t('Select the type of fee to add.'),
    '#options' => $options,
  );
  $entity = $form_state['complete form']['#entity'];
  $entity_type = $form_state['complete form']['#entity_type'];
  $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
  $merci_line_items = $entity_wrapper->{MERCI_CHECKOUT_REFERENCE}->{MERCI_LINE_ITEM_REFERENCE};
  $options = array();
  foreach ($merci_line_items
    ->getIterator() as $delta => $merci_line_item) {
    $options[$merci_line_item
      ->getIdentifier()] = t('!display_title (!resource_title)', array(
      '!display_title' => $merci_line_item->{MERCI_RESOURCE_DISPLAY}
        ->label(),
      '!resource_title' => $merci_line_item
        ->label(),
    ));
  }
  $form['merci_line_item_id'] = array(
    '#type' => 'select',
    '#title' => t('Resource'),
    '#description' => t('Select the resource this fee applies to.'),
    '#options' => $options,
    '#required' => TRUE,
  );
  $order = $element['#entity'];
  $line_item = commerce_line_item_new(MERCI_FEE, $order->order_id);
  return $form;
}