You are here

function pmexpense_form in Drupal PM (Project Management) 7

Same name and namespace in other branches
  1. 8 pmexpense/pmexpense.module \pmexpense_form()
  2. 7.3 pmexpense/pmexpense.module \pmexpense_form()
  3. 7.2 pmexpense/pmexpense.module \pmexpense_form()

Implements hook_pmexpense_form().

File

pmexpense/pmexpense.module, line 257
Hook implementations and main functions for PM Expense.

Code

function pmexpense_form($node, $form_state) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Expenses'), 'pm/expenses');
  drupal_set_breadcrumb($breadcrumb);
  if (arg(1) == 'add') {

    // Load tax defaults.
    $node->tax1app = variable_get('pm_tax1_app', 1);
    $node->tax1percent = variable_get('pm_tax1_percent', 20);
    $node->tax2app = variable_get('pm_tax2_app', 0);
    $node->tax2percent = variable_get('pm_tax2_percent', 20);
  }

  // Transition to compound tax - allow for nodes which may have been saved in
  // the past without a tax percentage.
  if (arg(2) == 'edit' && $node->tax1percent == NULL) {
    $node->tax1percent = $node->tax1 / $node->amount * 100;
  }
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmexpense', 'form');
  $w = -100;
  $form['#attributes']['class'] = 'pmcomponent_node_form';
  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group1']['weight'],
  );
  $query = db_select('node', 'n');
  $query
    ->join('pmorganization', 'org', 'n.vid = org.vid');
  $query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('n.type', 'pmorganization')
    ->addTag('node_access')
    ->orderBy('title', 'ASC');
  if (arg(1) == 'add') {
    $query
      ->condition('org.isactive', 1);
  }
  $result = $query
    ->execute();
  $organizations = array();
  foreach ($result as $organization) {
    $organizations[$organization->nid] = $organization->title;
    if (!isset($node->organization_nid)) {
      $node->organization_nid = $organization->nid;
    }
  }
  $form['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => $node->organization_nid,
    '#options' => $organizations,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'pmexpense_ajax_organization_nid',
      'wrapper' => 'pmexpense-project-nid',
    ),
  );
  if (isset($node->organization_nid)) {
    $organization_nid = isset($form_state['values']['organization_nid']) ? $form_state['values']['organization_nid'] : $node->organization_nid;
  }
  else {
    drupal_set_message(t('Please add an organization to the system before trying to add an expense.'), 'error');
  }
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'spr', 'n.vid = spr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('spr.organization_nid', $organization_nid)
    ->condition('n.status', 1)
    ->condition('n.type', 'pmproject')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $projects = array();
  foreach ($pro_result as $project) {
    $projects[$project->nid] = $project->title;
  }
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => isset($node->project_nid) ? $node->project_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $projects,
    '#prefix' => '<div id="pmexpense-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmexpense_ajax_project_nid',
      'wrapper' => 'pmexpense-task-nid',
    ),
  );
  $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : (isset($node->project_nid) ? $node->project_nid : 0);
  $tree = isset($project_nid) ? _pmtask_get_tree($project_nid) : array();
  $tasks = _pmtask_plain_tree($tree);
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $tasks,
    '#prefix' => '<div id="pmexpense-task-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmexpense_ajax_task_nid',
      'wrapper' => 'pmexpense-ticket-nid',
    ),
  );
  $task_nid = isset($form_state['values']['task_nid']) ? $form_state['values']['task_nid'] : (isset($node->task_nid) ? $node->task_nid : 0);
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmticket', 'pmti', 'n.vid = pmti.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('pmti.task_nid', $task_nid)
    ->condition('n.status', 1)
    ->condition('n.type', 'pmticket')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $tickets = array();
  foreach ($pro_result as $ticket) {
    $tickets[$ticket->nid] = $ticket->title;
  }
  $ticket_nid = isset($node->ticket_nid) ? $node->ticket_nid : NULL;
  $form['group1']['ticket_nid'] = array(
    '#type' => 'select',
    '#title' => t('Ticket'),
    '#default_value' => $ticket_nid,
    '#options' => array(
      0 => '-',
    ) + $tickets,
    '#prefix' => '<div id="pmexpense-ticket-nid">',
    '#suffix' => '</div>',
  );
  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group2']['weight'],
  );
  $form['group2']['provider_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Provider'),
    '#size' => 50,
    '#default_value' => isset($node->provider_title) ? $node->provider_title : NULL,
    '#autocomplete_path' => 'pm/expenses/provider_autocomplete',
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $form['group3']['amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Amount'),
    '#default_value' => isset($node->amount) ? $node->amount : NULL,
    '#size' => 15,
  );
  $form['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group4']['weight'],
  );
  $form['group4']['tax1app'] = array(
    '#type' => 'select',
    '#title' => t('Tax 1 Application'),
    '#options' => array(
      1 => t('Apply to item amount'),
      0 => t('Do not apply tax'),
    ),
    '#default_value' => $node->tax1app,
  );
  $form['group4']['tax1percent'] = array(
    '#type' => 'textfield',
    '#title' => t('Tax 1 Percentage'),
    '#default_value' => $node->tax1percent,
    '#size' => 20,
  );
  $form['group4']['tax2app'] = array(
    '#type' => 'select',
    '#title' => t('Tax 2 Application'),
    '#options' => array(
      2 => t('Apply to total of item amount plus previous tax'),
      1 => t('Apply to item amount'),
      0 => t('Do not apply tax'),
    ),
    '#default_value' => $node->tax2app,
  );
  $form['group4']['tax2percent'] = array(
    '#type' => 'textfield',
    '#title' => t('Tax 2 Percentage'),
    '#default_value' => $node->tax2percent,
    '#size' => 20,
  );
  if (!variable_get('pm_tax_display', TRUE)) {
    $form['group4']['#type'] = 'hidden';
  }
  if (!variable_get('pm_tax2_display', TRUE)) {
    $form['group4']['tax2app']['#type'] = 'hidden';
    $form['group4']['tax2percent']['#type'] = 'hidden';
  }
  $form['taxnotes'] = array(
    '#type' => 'markup',
    '#markup' => t('Totals will be calculated automatically according to your tax selections.'),
    '#weight' => $info['group4']['weight'],
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );

  // Check to see if the body field is still there, if so, display it.
  $body = field_get_items('pmexpense', $node, 'body');
  if ($body) {
    $form['body_field'] = $body;
  }
  return $form;
}