<?php

/**
 * @file
 */

function storminvoice_install() {
  drupal_install_schema('storminvoice');
  variable_set('node_options_storminvoice', array('status'));
  variable_set('node_options_storminvoiceitem', array('status'));
}

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

function storminvoice_uninstall() {
  drupal_uninstall_schema('storminvoice');
}

function storminvoice_schema() {
  $schema['storminvoice'] = array(
    'fields'                => array(
      'vid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'nid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'number'              => array('type' => 'varchar', 'length' => 50),
      'reference'           => array('type' => 'varchar', 'length' => 150),
      'organization_nid'    => array('type' => 'int'),
      'organization_title'  => array('type' => 'varchar', 'length' => 128),
      'project_nid'         => array('type' => 'int'),
      'project_title'       => array('type' => 'varchar', 'length' => 128),
      'requestdate'         => array('type' => 'int'),
      'duedate'             => array('type' => 'int'),
      'paymentdate'         => array('type' => 'int'),
      'amount'              => array('type' => 'float'),
      'tax1'                => array('type' => 'float'),
      'tax2'                => array('type' => 'float'),
      'total'               => array('type' => 'float'),
      'totalcustomercurr'   => array('type' => 'float'),
      'taxexempt'           => array('type' => 'int', 'size' => 'tiny'),
      'src_nid'             => array('type' => 'int'),
      'src_vid'             => array('type' => 'int'),
    ),
    'primary key' => array('vid'),
    'indexes'     => array(
      'nid'               => array('nid'),
      'organization_nid'  => array('organization_nid'),
      'project_nid'       => array('project_nid'),
    ),
  );

  $schema['storminvoice_items'] = array(
    'fields'                => array(
      'invoice_nid'         => array('type' => 'int'),
      'invoice_vid'         => array('type' => 'int'),
      'weight'              => array('type' => 'int'),
      'description'         => array('type' => 'varchar', 'length' => 128),
      'amount'              => array('type' => 'float'),
      'tax1app'             => array('type' => 'int'),
      'tax1percent'         => array('type' => 'float'),
      'tax1'                => array('type' => 'float'),
      'tax2app'             => array('type' => 'int'),
      'tax2percent'         => array('type' => 'float'),
      'tax2'                => array('type' => 'float'),
      'total'               => array('type' => 'float'),
      'src_nid'             => array('type' => 'int'),
      'src_vid'             => array('type' => 'int'),
    ),
    'indexes'     => array(
      'invoice_nid'  => array('invoice_nid'),
      'invoice_vid'  => array('invoice_vid'),
    ),
  );

  return $schema;
}

function storminvoice_update_1() {
  $ret = array();
  db_change_field($ret, 'storminvoice', 'vat', 'tax', array('type' => 'float'));
  db_change_field($ret, 'storminvoice', 'vatexempt', 'taxexempt', array('type' => 'int', 'length' => 1));
  return $ret;
}

function storminvoice_update_6101() {
  $ret = array();

  if (!db_table_exists('storminvoiceitem')) { // if Storminvoiceitem was not active
    // add table
    $schema['storminvoiceitem'] = array(
      'fields'                => array(
        'vid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
        'nid'                 => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
        'invoice_nid'         => array('type' => 'int'),
        'weight'              => array('type' => 'int'),
        'amount'              => array('type' => 'float'),
        'taxpercent'          => array('type' => 'float'),
        'tax'                 => array('type' => 'float'),
        'total'               => array('type' => 'float'),
      ),
      'primary key' => array('vid', 'nid'),
    );

    db_create_table($ret, 'storminvoiceitem', $schema['storminvoiceitem']);

  }
  else { // if Storminvoiceitem was active
    // if needed, run storminvoiceitem update
    if (db_column_exists('storminvoiceitem', 'vatpercent')) {
      db_change_field($ret, 'storminvoiceitem', 'vatpercent', 'taxpercent', array('type' => 'float'));
    }
    if (db_column_exists('storminvoiceitem', 'vat')) {
      db_change_field($ret, 'storminvoiceitem', 'vat', 'tax', array('type' => 'float'));
    }

    // deactivate storminvoiceitem
    $sql = "DELETE FROM {system} WHERE 'name' = 'storminvoiceitem'";
    $ret[] = update_sql($sql);
  }

  return $ret;
}

function storminvoice_update_6102() {
  $ret = array();

  db_add_column($ret, 'storminvoiceitem', 'src_nid', 'int');
  db_add_column($ret, 'storminvoiceitem', 'src_vid', 'int');
  variable_set('storm_tax1_percent', variable_get('storminvoice_default_taxpercent', 20));
  variable_del('storminvoice_default_taxpercent');

  return $ret;
}

function storminvoice_update_6103() {
  $ret = array();

  db_change_field($ret, 'storminvoice', 'tax', 'tax1', array('type' => 'float'));
  db_add_column($ret, 'storminvoiceitem', 'tax1app', 'int');
  db_change_field($ret, 'storminvoiceitem', 'taxpercent', 'tax1percent', array('type' => 'float'));
  db_change_field($ret, 'storminvoiceitem', 'tax', 'tax1', array('type' => 'float'));
  db_add_column($ret, 'storminvoiceitem', 'tax2app', 'int');
  db_add_column($ret, 'storminvoiceitem', 'tax2percent', 'float');
  db_add_column($ret, 'storminvoiceitem', 'tax2', 'float');

  return $ret;
}

function storminvoice_update_6104() {
  $ret = array();

  db_add_column($ret, 'storminvoice', 'src_nid', 'int');
  db_add_column($ret, 'storminvoice', 'src_vid', 'int');

  return $ret;
}

function storminvoice_update_6105() {
  $ret = array();

  db_add_column($ret, 'storminvoice', 'tax2', 'float');

  return $ret;
}

function storminvoice_update_6106() {
  $ret = array();

  // Fix naming convention
  db_rename_table($ret, 'storminvoiceitem', 'storminvoice_items');

  // New fields to cover details that were in the node table
  db_add_column($ret, 'storminvoice_items', 'invoice_vid', 'int');
  db_add_field($ret, 'storminvoice_items', 'description', array('type' => 'varchar', 'length' => 128));

  // Copy description from the node table into the storminvoice_items table
  $items = db_query("SELECT n.title, n.vid, n.nid, sit.invoice_nid FROM {node} AS n INNER JOIN {storminvoice_items} AS sit ON n.vid = sit.vid WHERE n.type = 'storminvoiceitem'");
  while ($item = db_fetch_object($items)) {
    // Find out vid of invoice
    $invoice = db_fetch_object(db_query("SELECT n.vid, n.nid FROM {node} AS n WHERE n.nid = %d", $item->invoice_nid));

    // Not using update_sql() as for large installations this would give an unwanted number of SQL queries shown on the update.php screen
    db_query("UPDATE {storminvoice_items} AS sit SET sit.description = '%s', sit.invoice_vid = %d WHERE sit.vid = %d", $item->title, $invoice->vid, $item->vid);

    // Remove nodes
    node_delete($item->nid);
  }

  // Remove content type
  $ret[] = update_sql("DELETE FROM {node_type} WHERE type = 'storminvoiceitem'");

  // Remove unnecessary fields from Storminvoice_items
  db_drop_primary_key($ret, 'storminvoice_items');
  db_drop_field($ret, 'storminvoice_items', 'nid');
  db_drop_field($ret, 'storminvoice_items', 'vid');

  return $ret;
}

/**
 * Improve primary keys and indexes
 */
function storminvoice_update_6201() {
  $return = array();
  db_drop_primary_key($return, 'storminvoice');
  db_add_primary_key($return, 'storminvoice', array('vid'));
  db_add_index($return, 'storminvoice', 'nid', array('nid'));
  db_add_index($return, 'storminvoice', 'organization_nid', array('organization_nid'));
  db_add_index($return, 'storminvoice', 'project_nid', array('project_nid'));
  db_add_index($return, 'storminvoice_items', 'invoice_nid', array('invoice_nid'));
  db_add_index($return, 'storminvoice_items', 'invoice_vid', array('invoice_vid'));
  return $return;
}
