<?php

/**
 * @file
 * Install hooks for uc_payment.module.
 */

function uc_payment_schema() {
  $schema = array();

  $schema['uc_payment_receipts'] = array(
    'description' => 'Stores completed payments.',
    'fields' => array(
      'receipt_id' => array(
        'description' => 'Primary key: the payment receipt ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'method' => array(
        'description' => 'The payment method.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'amount' => array(
        'description' => 'The payment amount in the store default currency.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'The {users}.uid who collected the payment.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'description' => 'A serialized array of extra payment data.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
      'comment' => array(
        'description' => 'A comment made on the payment.',
        'type' => 'text',
      ),
      'received' => array(
        'description' => 'The Unix timestamp indicating when the payment was received.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'order_id' => array('order_id'),
    ),
    'primary key' => array('receipt_id'),
  );

  return $schema;
}

function uc_payment_install() {
  drupal_install_schema('uc_payment');
  $t = get_t();
  db_query("INSERT INTO {uc_order_statuses} (order_status_id, title, state, weight, locked) VALUES ('payment_received', '". $t('Payment received') ."', 'payment_received', 10, 1);");
}

function uc_payment_uninstall() {
  drupal_uninstall_schema('uc_payment');

  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_pg_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_payment_%%'");
  variable_del('uc_default_payment_msg');
}

function uc_payment_update_last_removed() {
  return 2;
}

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

  db_drop_primary_key($ret, 'uc_payment_receipts');
  db_drop_index($ret, 'uc_payment_receipts', 'order_id');
  db_change_field($ret, 'uc_payment_receipts', 'receipt_id', 'receipt_id', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('receipt_id')));
  db_change_field($ret, 'uc_payment_receipts', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('indexes' => array('order_id' => array('order_id'))));
  db_change_field($ret, 'uc_payment_receipts', 'uid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

// Removed obsolete update 6001.

/**
 * Change currency fields to numeric(16,5).
 */
function uc_payment_update_6002() {
  $ret = array();

  db_change_field($ret, 'uc_payment_receipts', 'amount', 'amount', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Increase maximum length of comment field.
 */
function uc_payment_update_6003() {
  $ret = array();

  db_change_field($ret, 'uc_payment_receipts', 'comment', 'comment', array('type' => 'text'));

  return $ret;
}
