<?php

/**
 * @file
 */

function stormexpense_help($path, $arg) {
  $o = '';

  switch ($path) {
    case "admin/help#stormexpense":
      $o = '<p>'. t("Provides expense support for SuiteDesk") .'</p>';
      break;
  }

  return $o;
}

function stormexpense_perm() {
  return array(
    'Storm expense: access',
    'Storm expense: add',
    'Storm expense: delete all',
    'Storm expense: delete own',
    'Storm expense: delete of user organization',
    'Storm expense: edit all',
    'Storm expense: edit own',
    'Storm expense: edit of user organization',
    'Storm expense: view all',
    'Storm expense: view own',
    'Storm expense: view of user organization',
    'Storm expense: view if assigned to project',
  );
}

function stormexpense_access($op, $node, $account = NULL) {
  if (empty($account)) {
    global $user;
    $account = $user;
  }

  if ($op == 'create') {
    return user_access('Storm expense: add');
  }

  if (is_numeric($node)) {
    $node = node_load($node);
  }

  if (!isset($account->stormorganization_nid) && module_exists('stormperson')) {
    _stormperson_user_load($account);
  }

  if ($op == 'delete') {
    if (user_access('Storm expense: delete all')) {
      return TRUE;
    }
    elseif (user_access('Storm expense: delete own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm expense: delete of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
  }

  if ($op == 'update') {
    if (user_access('Storm expense: edit all')) {
      return TRUE;
    }
    elseif (user_access('Storm expense: edit own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm expense: edit of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
  }

  if ($op == 'view') {
    if (user_access('Storm expense: view all')) {
      return TRUE;
    }
    elseif (user_access('Storm expense: view own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm expense: view of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm expense: view if assigned to project') && module_exists('stormteam')) {
      $project = node_load($node->project_nid);
      if (stormteam_user_belongs_to_team($project->assigned_nid, $account->stormperson_nid)) {
        return TRUE;
      }
      elseif (stormteam_user_belongs_to_team($project->assigned_nid, $account->stormorganization_nid)) {
        return TRUE;
      }
    }
  }

  return FALSE;
}

function stormexpense_access_sql($sql, $where = array()) {
  if (!user_access('Storm expense: view all')) {
    global $user;

    $cond = '';
    $join = array();
    if (user_access('Storm expense: view own')) {
      $cond .= 'n.uid = ' . $user->uid;
    }
    if (user_access('Storm expense: view of user organization')) {
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'sex.organization_nid = ' . $user->stormorganization_nid;
    }
    if (user_access('Storm expense: view if assigned to project')) {
      $join[] = 'LEFT JOIN {node} npr ON npr.nid = sex.project_nid';
      $join[] = 'LEFT JOIN {stormproject} spr ON spr.vid = npr.vid';

      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'spr.assigned_nid = ' . $user->stormperson_nid;

      if (module_exists('stormteam')) {
        // Load teams that the account belongs to:
        $belonged_teams = stormteam_user_return_teams();
        // Allow access if any of those teams is the one in question:
        foreach ($belonged_teams as $belonged_team) {
          $cond .= ' OR spr.assigned_nid = ' . $belonged_team;
        }
      }
    }
    $where[] = empty($cond) ? '0 = 1' : $cond;
  }

  $where[] = "'storm_access' = 'storm_access'";
  return storm_rewrite_sql($sql, $where, $join);
}

function stormexpense_storm_rewrite_where_sql($query, $primary_table, $account) {
  static $conds = array();

  if (isset($conds[$primary_table][$account->uid])) {
    return $conds[$primary_table][$account->uid];
  }

  $cond = '';
  if (!preg_match("/'storm_access' = 'storm_access'/", $query)) {
    if (user_access('Storm expense: view all', $account)) {
      return '';
    }
    $from = '{stormexpense} sex1';
    if (user_access('Storm expense: view own', $account)) {
      $cond .= "${primary_table}.uid = " . $account->uid;
    }
    if (user_access('Storm expense: view of user organization', $account)) {
      $cond .= !empty($cond) ? ' OR ' : '';
      # If function is called without viewing an organization, this variable
      # may not be set. These lines check for that and set the organization
      # node id to zero if not otherwise set.
      if (!isset($account->stormorganization_nid)) {
        $account->stormorganization_nid = 0;
      }
      $cond .= ' sex1.organization_nid = ' . $account->stormorganization_nid;
    }
    if (user_access('Storm expense: view if assigned to project')) {
      $from .= ' LEFT JOIN {node} npr1 ON npr1.nid = sex1.project_nid LEFT JOIN {stormproject} spr1 ON spr1.vid = npr1.vid';
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'spr1.assigned_nid = ' . $account->stormperson_nid;

      if (module_exists('stormteam')) {
        // Load teams that the account belongs to:
        $belonged_teams = stormteam_user_return_teams($account);
        // Allow access if any of those teams is the one in question:
        foreach ($belonged_teams as $belonged_team) {
          $cond .= ' OR spr1.assigned_nid = ' . $belonged_team;
        }
      }
    }
    if ($cond) {
      $cond = " WHEN 'stormexpense' THEN (SELECT IF($cond, 1, 0) FROM ${from} WHERE sex1.vid = ${primary_table}.vid) ";
    }
    else {
      $cond = " WHEN 'stormexpense' THEN 0 ";
    }
  }
  $conds[$primary_table][$account->uid] = $cond;
  return $cond;
}

function stormexpense_menu() {
  $items = array();

  $items['expenses'] = array(
    'title' => 'Expenses',
    'description' => 'SuiteDesk expenses',
    'page callback' => 'stormexpense_list',
    'access arguments' => array('Storm expense: access'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'stormexpense.admin.inc',
    'weight' => 9,
  );

  $items['expenses/report/%/%'] = array(
    'title' => 'Expenses',
    'description' => 'SuiteDesk expenses',
    'page arguments' => array(2, 3),
    'page callback' => 'stormexpense_list_report',
    'access arguments' => array('Storm expense: access'),
    'type' => MENU_CALLBACK,
    'file' => 'stormexpense.admin.inc',
  );

  $items['storm/expenses/provider_autocomplete'] = array(
    'title' => 'Provider autocomplete',
    'page callback' => 'stormexpense_autocomplete',
    'access arguments' => array('Storm expense: access'),
    'type' => MENU_CALLBACK,
    'file' => 'stormexpense.admin.inc',
  );

  return $items;
}

function stormexpense_theme() {
  return array(
    'stormexpense_list' => array(
      'file'      => 'stormexpense.theme.inc',
      'arguments' => array('header', 'tasks', 'duration'),
    ),
    'stormexpense_view' => array(
      'file'      => 'stormexpense.theme.inc',
      'arguments' => array('node', 'teaser', 'page'),
    ),
    'stormexpense_list_form_report_reports' => array(
      'file'      => 'stormexpense.theme.inc',
    ),
    'stormexpense_list_report' => array(
      'file'      => 'stormexpense.theme.inc',
      'arguments' => array('report', 'language', 'timetrackings'),
    ),
  );
}

function stormexpense_node_info() {
  return array(
    'stormexpense' => array(
      'name' => t('Expense'),
      'module' => 'stormexpense',
      'description' => t("An expense for SuiteDesk."),
      'title_label' => t("Title"),
      'body_label' => t("Description"),
    )
  );
}

function stormexpense_content_extra_fields($type_name) {
  if ($type_name == 'stormexpense') {
    return array(
      'group1' => array('label' => 'Organization/Project/Task/Ticket Group', 'weight' => -20),
      'group2' => array('label' => 'Date/Provider Group', 'weight' => -19),
      'group3' => array('label' => 'Amount', 'weight' => -18),
      'group4' => array('label' => 'Tax Group', 'weight' => -17),
    );
  }
}

function stormexpense_stormorganization_change($organization_nid, $organization_title) {
  $s = "UPDATE {stormexpense} SET organization_title='%s' WHERE organization_nid=%d AND organization_title <> '%s'";
  db_query($s, $organization_title, $organization_nid, $organization_title);
}

function stormexpense_stormproject_change($project_nid, $project_title) {
  $s = "UPDATE {stormexpense} SET project_title='%s' WHERE project_nid=%d AND project_title <> '%s'";
  db_query($s, $project_title, $project_nid, $project_title);
}

function stormexpense_stormtask_change($task_nid, $task_title, $task_stepno) {
  $s = "UPDATE {stormexpense} SET task_title='%s', task_stepno='%s' WHERE task_nid=%d AND
      (task_title<>'%s' OR task_stepno<>'%s')";
  db_query($s, $task_title, $task_stepno, $task_nid, $task_title, $task_stepno);
}

function stormexpense_stormticket_change($ticket_nid, $ticket_title) {
  $s = "UPDATE {stormexpense} SET ticket_title='%s' WHERE ticket_nid=%d AND ticket_title <> '%s'";
  db_query($s, $ticket_title, $ticket_nid, $ticket_title);
}

function stormexpense_stormproject_change_hierarchy($project_nid, $organization_nid, $organization_title) {
  $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s' WHERE project_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid);
}

function stormexpense_stormtask_change_hierarchy($task_nid, $organization_nid, $organization_title, $project_nid, $project_title) {
  $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s' WHERE task_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid);
}

function stormexpense_stormticket_change_hierarchy($ticket_nid, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title) {
  $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s', task_nid=%d, task_title='%s' WHERE ticket_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title, $ticket_nid);
}

function stormexpense_form(&$node) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Home'), '<front>');
  $breadcrumb[] = l(t('Expenses'), 'expenses');
  drupal_set_breadcrumb($breadcrumb);

  if (arg(1)=='add') {
    if (array_key_exists('organization_nid', $_GET) && !$node->organization_nid) {
      $node->organization_nid = $_GET['organization_nid'];
    }
    if (array_key_exists('project_nid', $_GET) && !$node->project_nid) {
      $node->project_nid = $_GET['project_nid'];
      $p = node_load($node->project_nid);
      $node->organization_nid = $p->organization_nid;
      if (!stormorganization_access('view', $node->organization_nid)) {
        drupal_set_message(t("You cannot add an expense for this project, as you do not have access to view the organization's profile"));
        drupal_goto('node/'. $node->project_nid);
      }
    }
    if (array_key_exists('task_nid', $_GET) && !$node->task_nid) {
      $node->task_nid = $_GET['task_nid'];
      $t = node_load($node->task_nid);
      $node->organization_nid = $t->organization_nid;
      $node->project_nid = $t->project_nid;
      // $project_access deals with the case whereby the project could be blank, hence access rules not required
      $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
      if (!stormorganization_access('view', $node->organization_nid) || !$project_access) {
        drupal_set_message(t("You cannot add an expense for this task, as you do not have access to view the both the organization and project's profile"));
        drupal_goto('node/'. $node->task_nid);
      }
    }
    if (array_key_exists('ticket_nid', $_GET) && !$node->ticket_nid) {
      $node->ticket_nid = $_GET['ticket_nid'];
      $t = node_load($node->ticket_nid);
      $node->organization_nid = $t->organization_nid;
      $node->project_nid = $t->project_nid;
      $node->task_nid = $t->task_nid;
      // $project_access deals with the case whereby the project could be blank, hence access rules not required
      $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
      $task_access = $node->task_nid ? stormtask_access('view', $node->task_nid) : TRUE;
      if (!stormorganization_access('view', $node->organization_nid) || !project_access || !task_access) {
        drupal_set_message(t("You cannot add an expense for this ticket, as you do not have access to view all of the organization, project and task's profile"));
        drupal_goto('node/'. $node->ticket_nid);
      }
    }

    if (!empty($_SESSION['stormexpense_list_filter']['organization_nid']) && !$node->organization_nid) {
      $node->organization_nid = $_SESSION['stormexpense_list_filter']['organization_nid'];
    }
    if (!empty($_SESSION['stormexpense_list_filter']['project_nid']) && !$node->project_nid) {
      $node->project_nid = $_SESSION['stormexpense_list_filter']['project_nid'];
    }
    if (!empty($_SESSION['stormexpense_list_filter']['task_nid']) && !$node->task_nid) {
      $node->task_nid = $_SESSION['stormexpense_list_filter']['task_nid'];
    }
    if (!empty($_SESSION['stormexpense_list_filter']['ticket_nid']) && !$node->ticket_nid) {
      $node->ticket_nid = $_SESSION['stormexpense_list_filter']['ticket_nid'];
    }
    if (array_key_exists('organization_nid', $_GET)) $node->organization_nid = $_GET['organization_nid'];
    if (array_key_exists('project_nid', $_GET)) $node->project_nid = $_GET['project_nid'];
    if (array_key_exists('task_nid', $_GET)) $node->task_nid = $_GET['task_nid'];
    if (array_key_exists('ticket_nid', $_GET)) $node->ticket_nid = $_GET['ticket_nid'];

    $node->expensedate = time();
    $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
              ON so.nid=n.nid WHERE n.status=1 AND so.isactive=1 AND n.type='stormorganization' ORDER BY n.title";

    // Load tax defaults
    $node->tax1app = variable_get('storm_tax1_app', 1);
    $node->tax1percent = variable_get('storm_tax1_percent', 20);
    $node->tax2app = variable_get('storm_tax2_app', 0);
    $node->tax2percent = variable_get('storm_tax2_percent', 20);
  }
  else {
    $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
              ON so.nid=n.nid WHERE n.status=1 AND n.type='stormorganization' ORDER BY n.title";
  }

  // 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_get_types('type', $node);
  $w = -100;

  $form['#attributes']['class'] = 'stormcomponent_node_form';

  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group1') : -20,
  );

  $s_org = stormorganization_access_sql($s_org);
  $s_org = db_rewrite_sql($s_org);
  $r = db_query($s_org);
  $organizations = array();
# if ($r->num_rows > 0) {
    while ($organization = db_fetch_object($r)) {
      $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,
    '#attributes' => array('onchange' => "stormticket_organization_project_task_tickets(this, 'edit-project-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
  );

  $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormproject} AS spr ON spr.vid=n.vid
  WHERE spr.organization_nid=%d AND n.status=1 AND n.type='stormproject' ORDER BY n.title";
  $s = stormproject_access_sql($s);
  $s = db_rewrite_sql($s);
  $r = db_query($s, $node->organization_nid);
  $projects = array();
  while ($project = db_fetch_object($r)) {
    $projects[$project->nid] = $project->title;
  }
  $projects = array(0 => '-') + $projects;
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => (isset($node->project_nid)) ? $node->project_nid : NULL,
    '#options' => $projects,
    '#process' => array('storm_dependent_select_process'),
    '#attributes' => array('onchange' => "stormticket_project_task_tickets(this, 'edit-organization-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
  );

  $tree = _stormtask_get_tree((isset($node->project_nid)) ? $node->project_nid : NULL);
  $tasks = _stormtask_plain_tree($tree);
  $tasks = array(0 => '-') + $tasks;
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => (isset($node->task_nid)) ? $node->task_nid : NULL,
    '#options' => (isset($tasks)) ? $tasks : NULL,
    '#process' => array('storm_dependent_select_process'),
    '#attributes' => array('onchange' => "stormticket_task_tickets(this, 'edit-organization-nid', 'edit-project-nid', 'edit-ticket-nid', true, '-')"),
  );

  $tickets = array();
  $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormticket} AS sti ON sti.vid=n.vid WHERE n.status=1 AND n.type='stormticket'
  AND sti.organization_nid=%d AND sti.project_nid=%d AND sti.task_nid=%d ORDER BY n.title";
  $s = stormticket_access_sql($s);
  $s = db_rewrite_sql($s);
  if (isset($node->organization_nid) && (isset($node->project_nid))) {
    $r = db_query($s, $node->organization_nid, $node->project_nid, $node->task_nid);
  }
  while ($ticket = db_fetch_object($r)) {
    $tickets[$ticket->nid] = $ticket->title;
  }
  $form['group1']['ticket_nid'] = array(
    '#type' => 'select',
    '#title' => t('Ticket'),
    '#default_value' => (isset($node->ticket_nid)) ? $node->ticket_nid : NULL,
    '#options' => array(0 => '-') + $tickets,
    '#process' => array('storm_dependent_select_process'),
  );

  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group2') : -19,
  );

  $form['group2']['expensedate'] = array(
    '#type' => 'dateext',
    '#title' => t('Date'),
    '#default_value' => $node->expensedate,
  );

  $form['group2']['provider_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Provider'),
    '#size' => 50,
    '#default_value' => (isset($node->provider_title)) ? $node->provider_title : NULL,
    '#autocomplete_path' => 'storm/expenses/provider_autocomplete',
  );

  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group3') : -18,
  );

  $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' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group4') : -17,
  );

  $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('storm_tax_display', TRUE)) {
    $form['group4']['#type'] = 'hidden';
  }
  if (!variable_get('storm_tax2_display', TRUE)) {
    $form['group4']['tax2app']['#type'] = 'hidden';
    $form['group4']['tax2percent']['#type'] = 'hidden';
  }

  $form['taxnotes'] = array(
    '#type' => 'markup',
    '#value' => t('Totals will be calculated automatically according to your tax selections.'),
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group4') : -17,
  );

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -16,
  );

  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }

  return $form;
}

// NODE MANIPULATION FUNCTIONS
function stormexpense_insert($node) {
  _stormexpense_beforesave($node);

  db_query("INSERT INTO {stormexpense}
    (vid, nid, organization_nid, organization_title, project_nid, project_title,
    task_nid, task_title, task_stepno, ticket_nid, ticket_title,
    provider_nid, provider_title, expensedate, amount, tax1app, tax1percent,
    tax1, subtotal, tax2app, tax2percent, tax2, total) VALUES
    (%d, %d, %d, '%s', %d, '%s',
    %d, '%s', '%s', %d, '%s',
    %d, '%s', %d, %f, %d, %f,
    %f, %f, %d, %f, %f, %f)",
    $node->vid, $node->nid, $node->organization_nid, $node->organization_title, $node->project_nid, (isset($node->project_title)) ? $node->project_title : NULL,
    (isset($node->task_nid)) ? $node->task_nid : NULL, (isset($node->task_title)) ? $node->task_title : NULL, (isset($node->task_stepno)) ? $node->task_stepno : NULL, $node->ticket_nid, (isset($node->ticket_title)) ? $node->ticket_title : NULL,
    (isset($node->provider_nid)) ? $node->provider_nid : NULL, $node->provider_title, $node->expensedate, $node->amount, $node->tax1app, $node->tax1percent,
    $node->tax1, $node->subtotal, $node->tax2app, $node->tax2percent, $node->tax2, $node->total);
}

function stormexpense_update($node) {
  _stormexpense_beforesave($node);

  if ($node->revision) {
    stormexpense_insert($node);
  }
  else {
    db_query("UPDATE {stormexpense} SET
      organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s',
      task_nid=%d, task_title='%s', task_stepno='%s', ticket_nid=%d, ticket_title='%s',
      provider_nid=%d, provider_title='%s', expensedate=%d, amount=%f, tax1app=%d, tax1percent=%f,
      tax1=%f, subtotal=%f, tax2app=%d, tax2percent=%f, tax2=%f, total=%f WHERE vid = %d",
      $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title,
      $node->task_nid, $node->task_title, $node->task_stepno, $node->ticket_nid, $node->ticket_title,
      $node->provider_nid, $node->provider_title, $node->expensedate, $node->amount, $node->tax1app, $node->tax1percent,
      $node->tax1, $node->subtotal, $node->tax2app, $node->tax2percent, $node->tax2, $node->total, $node->vid);
  }
}

function _stormexpense_beforesave(&$node) {
  // Allow use of comma when inputting numerical values - str_replace with period decimal
  $node->amount = str_replace(',', '.', $node->amount);
  $node->tax1percent = str_replace(',', '.', $node->tax1percent);
  $node->tax2percent = str_replace(',', '.', $node->tax2percent);

  $node->expensedate = _storm_date_to_gmtimestamp($node->expensedate);

  storm_taxation($node);

  $s = "SELECT n.title FROM {node} AS n
    INNER JOIN {stormorganization} AS o ON n.nid=o.nid
    WHERE type='stormorganization' AND n.nid=%d";
  $r = db_query($s, $node->organization_nid);
  $o = db_fetch_object($r);
  $o ? $node->organization_title = $o->title : NULL;

  $s = "SELECT n.title, p.organization_title
    FROM {node} n INNER JOIN {stormproject} p ON n.nid=p.nid
    WHERE type='stormproject' AND n.nid=%d";
  $r = db_query($s, $node->project_nid);
  $p = db_fetch_object($r);
  $p ? $node->project_title = $p->title : NULL;

  $s = "SELECT title, stepno FROM {node} AS n INNER JOIN {stormtask} AS t ON n.vid=t.vid WHERE n.type='stormtask' AND n.nid=%d";
  (isset($node->task_nid)) ? $r = db_query($s, $node->task_nid) : NULL;
  $ta = db_fetch_object($r);
  (isset($ta->title)) ? $node->task_title = $ta->title : NULL;
  (isset($ta->stepno)) ? $node->task_stepno = $ta->stepno : NULL;

  $s = "SELECT title FROM {node} AS n INNER JOIN {stormticket} AS t ON n.vid=t.vid WHERE n.type='stormticket' AND n.nid=%d";
  (isset($node->ticket_nid)) ? $r = db_query($s, $node->ticket_nid) : NULL;

  $ti = db_fetch_object($r);
  (isset($ti->title)) ? $node->ticket_title = $ti->title : NULL;
}

function stormexpense_nodeapi(&$node, $op, $teaser, $page) {
  if ($node->type != 'stormexpense') {
    return;
  }
  switch ($op) {
    case 'delete revision':
      // Notice that we're matching a single revision based on the node's vid.
      db_query('DELETE FROM {stormexpense} WHERE vid = %d', $node->vid);
      break;
  }
}

function stormexpense_delete($node) {
  db_query('DELETE FROM {stormexpense} WHERE nid = %d', $node->nid);
}

function stormexpense_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {stormexpense} WHERE vid = %d', $node->vid));
  return $additions;
}

function stormexpense_view($node, $teaser = FALSE, $page = FALSE) {
  return theme('stormexpense_view', $node, $teaser, $page);
}

// INVOICE AUTO ADD HANDLER
function stormexpense_storminvoice_auto_add($node, $invoice_nid = NULL) {
  if (!module_exists('storminvoice')) {
    drupal_set_message(t('This function should only be called from within SuiteDesk Invoice'));
    return;
  }
  else {
    global $user;

    if (!$invoice_nid) {

      $new_invoice = new StdClass;

      // Code copied with edits from node form
      $new_invoice->requestdate = time();
      $new_invoice->duedate = $new_invoice->requestdate + (variable_get('storminvoice_payment_days', 30) * 86400);
      $s = "SELECT MAX(CAST(SUBSTRING_INDEX(sin.number, '/', 1) AS SIGNED)) FROM {node} n INNER JOIN {storminvoice} sin ON n.nid=sin.nid
        WHERE n.type='storminvoice' AND YEAR(FROM_UNIXTIME(sin.requestdate))=YEAR(FROM_UNIXTIME(%d))";
      $date = getdate($new_invoice->requestdate);
      $new_invoice->number = (db_result(db_query($s, $new_invoice->requestdate)) + 1) .'/'. $date['year'];

      $new_invoice->title = $node->title;
      $new_invoice->uid = $user->uid;
      $new_invoice->type = 'storminvoice';
      // $new_invoice->reference
      $new_invoice->organization_nid = $node->organization_nid;
      $new_invoice->organization_title = $node->organization_title;
      $new_invoice->project_nid = $node->project_nid;
      $new_invoice->project_title = $node->project_title;
      // $new_invoice->amount
      // $new_invoice->tax
      // $new_invoice->total
      // $new_invoice->totalcustomercurr
      // $new_invoice->taxexempt
      $new_invoice->src_nid = $node->nid;
      $new_invoice->src_vid = $node->vid;

      node_save($new_invoice);
      $invoice_nid = $new_invoice->nid;
    }
    else {
      $new_invoice = node_load($invoice_nid);
    }

    if ($node->ticket_nid) {
      $parent_ticket = node_load($node->ticket_nid);
    }
    elseif ($node->task_nid) {
      $parent_task = node_load($node->task_nid);
    }
    elseif ($node->project_nid) {
      $parent_project = node_load($node->project_nid);
    }
    else {
      $parent_organization = node_load($node->organization_nid);
    }

    $count = count($new_invoice->items);

    $new_invoice->items[$count] = new StdClass;

    $new_invoice->items[$count]->description = $node->title;
    $new_invoice->items[$count]->amount = $node->amount;
    // Tax percent uses the values set on the expense.
    $new_invoice->items[$count]->tax1app = $node->tax1app;
    $new_invoice->items[$count]->tax1percent = $node->tax1percent;
    $new_invoice->items[$count]->tax1 = $node-> tax1;
    $new_invoice->items[$count]->tax2app = $node->tax2app;
    $new_invoice->items[$count]->tax2percent = $node->tax2percent;
    $new_invoice->items[$count]->tax2 = $node->tax2;
    $new_invoice->items[$count]->total = $node->total;
    $new_invoice->items[$count]->src_nid = $node->nid;
    $new_invoice->items[$count]->src_vid = $node->vid;

    // storm_taxation($new_invoice->items[$count]);
    storminvoice_update($new_invoice);
  }
  return $invoice_nid;
}


function stormexpense_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'stormexpense'),
  );
}

function stormexpense_storm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'storm_dashboard_link',
      'title' => t('Expenses'),
      'icon' => 'stormexpense-item',
      'path' => 'expenses',
      'params' => array(),
      'access_arguments' => 'Storm expense: access',
      'node_type' => 'stormexpense',
      'add_type' => 'stormexpense',
      'map' => array(),
      'weight' => 110,
    );
  }
  return $links;
}
