<?php

/**
 * @file
 */

function stormexpense_install() {
  drupal_install_schema('stormexpense');
  variable_set('node_options_stormexpense', array('status'));

  $attributes = array();

  $attributes['Expense status'] = array(
    'outstanding' => 'Outstanding',
    'paid' => 'Paid',
  );

  $attributes['Expense status search'] = array(
    'outstanding' => 'Outstanding',
    'paid' => 'Paid',
  );

  $s = "INSERT INTO {stormattribute} (domain, akey, avalue, weight) VALUES ('%s', '%s', '%s', %d)";
  $prevdomain = '';
  $weight = 0;
  foreach ($attributes as $domain => $attribute) {
    if ($domain != $prevdomain) $weight=0;
    foreach ($attribute as $key => $value) {
      db_query($s, $domain, $key, $value, $weight);
      $weight++;
    }
    $prevdomain = $domain;
  }
}

function stormexpense_disable() {
  drupal_set_message(t('Nodes of type "Expense" have not been deleted on disabling SuiteDesk Expense. Please note that they will now have reduced functionality, and will not be protected by SuiteDesk Expense access controls.'), 'warning');
}

function stormexpense_uninstall() {
  drupal_uninstall_schema('stormexpense');

  db_query($s = "DELETE FROM {stormattribute} WHERE domain IN ('Expense status', 'Expense status search')");
}

function stormexpense_schema() {
  $schema['stormexpense'] = array(
    'fields'                => array(
      'vid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'nid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'organization_nid'    => array('type' => 'int'),
      'organization_title'  => array('type' => 'varchar', 'length' => 128),
      'project_nid'         => array('type' => 'int'),
      'project_title'       => array('type' => 'varchar', 'length' => 128),
      'task_nid'            => array('type' => 'int'),
      'task_stepno'         => array('type' => 'varchar', 'length' => 128),
      'task_title'          => array('type' => 'varchar', 'length' => 128),
      'ticket_nid'          => array('type' => 'int'),
      'ticket_title'        => array('type' => 'varchar', 'length' => 128),
      'provider_nid'        => array('type' => 'int'),
      'provider_title'      => array('type' => 'varchar', 'length' => 128),
      'expensedate'         => array('type' => 'int'),
      'expensestatus'       => array('type' => 'varchar', 'length' => 128),
      'amount'              => array('type' => 'float'),
      'tax1app'             => array('type' => 'int'),
      'tax1percent'         => array('type' => 'float'),
      'tax1'                => array('type' => 'float'),
      'subtotal'            => array('type' => 'float'),
      'tax2app'             => array('type' => 'int'),
      'tax2percent'         => array('type' => 'float'),
      'tax2'                => array('type' => 'float'),
      'total'               => array('type' => 'float'),
    ),
    'primary key' => array('vid'),
    'indexes'     => array(
      'nid'               => array('nid'),
      'organization_nid'  => array('organization_nid'),
      'project_nid'       => array('project_nid'),
      'task_nid'          => array('task_nid'),
      'ticket_nid'        => array('ticket_nid'),
      'provider_nid'      => array('provider_nid'),
    ),
  );

  return $schema;
}

function stormexpense_update_1() {
  $ret = array();
  db_change_field($ret, 'stormexpense', 'vat', 'tax', array('type' => 'float'));
  return $ret;
}

function stormexpense_update_6101() {
  $ret = array();
  db_add_field($ret, 'stormexpense', 'expensestatus', array('type' => 'varchar', 'length' => 128));
  return $ret;
}

function stormexpense_update_6102() {
  $ret = array();
  db_change_field($ret, 'stormexpense', 'tax', 'tax1', array('type' => 'float'));
  db_add_field($ret, 'stormexpense', 'tax1app', array('type' => 'int'));
  db_add_field($ret, 'stormexpense', 'tax1percent', array('type' => 'float'));
  db_add_field($ret, 'stormexpense', 'tax2app', array('type' => 'int'));
  db_add_field($ret, 'stormexpense', 'tax2percent', array('type' => 'float'));
  db_add_field($ret, 'stormexpense', 'tax2', array('type' => 'float'));
  db_add_field($ret, 'stormexpense', 'subtotal', array('type' => 'float'));
  return $ret;
}

/**
 * Improve primary keys and indexes
 */
function stormexpense_update_6201() {
  $return = array();
  db_drop_primary_key($return, 'stormexpense');
  db_add_primary_key($return, 'stormexpense', array('vid'));
  $indexes = array(
    'nid'               => array('nid'),
    'organization_nid'  => array('organization_nid'),
    'project_nid'       => array('project_nid'),
    'task_nid'          => array('task_nid'),
    'ticket_nid'        => array('ticket_nid'),
    'provider_nid'      => array('provider_nid'),
  );
  foreach ($indexes as $name => $fields) {
    db_add_index($return, 'stormexpense', $name, $fields);
  }
  return $return;
}

/**
 * Move SuiteDesk Attribute module into component modules
 */
function stormexpense_update_6202() {
  // Only run this update if was not previously run as part of the legacy stormattribute module
  if (db_result(db_query("SELECT schema_version FROM {system} WHERE name = 'stormattribute'")) < 6108) {
    $attributes['Expense status'] = array(
      'outstanding' => 'Outstanding',
      'paid' => 'Paid',
    );

    $attributes['Expense status search'] = array(
      'outstanding' => 'Outstanding',
      'paid' => 'Paid',
    );

    $s = "INSERT INTO {stormattribute} (domain, akey, avalue, weight, isactive) VALUES ('%s', '%s', '%s', %d, 1)";
    $prevdomain = '';
    $weight = 0;
    foreach ($attributes as $domain => $attribute) {
      if ($domain != $prevdomain) $weight=0;
      foreach ($attribute as $key => $value) {
        db_query($s, $domain, $key, $value, $weight);
        $weight++;
      }
      $prevdomain = $domain;
    }
  }

  $ret = array();
  return $ret;
}
