You are here

function _pmexpense_beforesave in Drupal PM (Project Management) 7

Prepare expense date before saving it to the database.

2 calls to _pmexpense_beforesave()
pmexpense_insert in pmexpense/pmexpense.module
Implements hook_insert().
pmexpense_update in pmexpense/pmexpense.module
Implements hook_update().

File

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

Code

function _pmexpense_beforesave(&$node) {

  // Allow use of comma when inputting numerical values - str_replace with
  // period decimal.
  $node->amount = !empty($node->amount) ? $node->amount : 0;
  $node->amount = str_replace(',', '.', $node->amount);
  $node->tax1percent = str_replace(',', '.', $node->tax1percent);
  $node->tax2percent = str_replace(',', '.', $node->tax2percent);
  pm_taxation($node);
  $org_query = db_select('node', 'n');
  $org_query
    ->join('pmorganization', 'pmor', 'n.vid = pmor.vid');
  $org_result = $org_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmorganization')
    ->condition('n.nid', $node->organization_nid)
    ->execute();
  $o = $org_result
    ->fetchObject();
  $node->organization_title = $o->title;
  if (isset($node->project_nid)) {
    $pro_query = db_select('node', 'n');
    $pro_query
      ->join('pmproject', 'pmpr', 'n.vid = pmpr.vid');
    $pro_result = $pro_query
      ->fields('n', array(
      'title',
    ))
      ->condition('n.type', 'pmproject')
      ->condition('n.nid', $node->project_nid)
      ->execute();
    $p = $pro_result
      ->fetchObject();
    $node->project_title = isset($p->title) ? $p->title : '';
  }
  else {
    $node->project_title = '';
  }
  if (isset($node->task_nid)) {
    $task_query = db_select('node', 'n');
    $task_query
      ->join('pmtask', 'pmta', 'n.vid = pmta.vid');
    $task_result = $task_query
      ->fields('n', array(
      'title',
    ))
      ->fields('pmta', array(
      'stepno',
    ))
      ->condition('n.type', 'pmtask')
      ->condition('n.nid', $node->task_nid)
      ->execute();
    $ta = $task_result
      ->fetchObject();
    $node->task_title = isset($ta->title) ? $ta->title : '';
    $node->task_stepno = isset($ta->stepno) ? $ta->stepno : '';
  }
  else {
    $node->task_title = '';
    $node->task_stepno = '';
  }
  if (isset($node->ticket_nid)) {
    $ticket_query = db_select('node', 'n');
    $ticket_query
      ->join('pmticket', 'pmti', 'n.vid = pmti.vid');
    $ticket_result = $ticket_query
      ->fields('n', array(
      'title',
    ))
      ->condition('n.type', 'pmticket')
      ->condition('n.nid', $node->ticket_nid)
      ->execute();
    $ti = $ticket_result
      ->fetchObject();
    $node->ticket_title = isset($ti->title) ? $ti->title : '';
  }
  else {
    $node->ticket_title = '';
  }
}