<?php

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

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

  $schema['uc_payment_check'] = array(
    'description' => 'Stores check payment information.',
    'fields' => array(
      'check_id' => array(
        'description' => 'Primary key: the check 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,
      ),
      'clear_date' => array(
        'description' => 'The Unix timestamp indicating the expected clear date for the check.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'order_id' => array('order_id'),
    ),
    'primary key' => array('check_id'),
  );

  $schema['uc_payment_cod'] = array(
    'description' => 'Stores COD payment information.',
    'fields' => array(
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'delivery_month' => array(
        'description' => 'The month of delivery. 1 => January, 2 => February, etc.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'delivery_day' => array(
        'description' => 'The day of the month of delivery.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'delivery_year' => array(
        'description' => 'The year of delivery.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('order_id'),
  );

  $schema['uc_payment_other'] = array(
    'description' => 'Stores Other payment type information.',
    'fields' => array(
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'description' => array(
        'description' => 'The description of the payment type.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('order_id'),
  );

  return $schema;
}

function uc_payment_pack_install() {
  drupal_install_schema('uc_payment_pack');
}

function uc_payment_pack_uninstall() {
  drupal_uninstall_schema('uc_payment_pack');

  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_check_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_cod_%%'");
}

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

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

  db_drop_primary_key($ret, 'uc_payment_cod');
  db_change_field($ret, 'uc_payment_cod', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('order_id')));
  db_change_field($ret, 'uc_payment_cod', 'delivery_month', 'delivery_month', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'small', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_payment_cod', 'delivery_day', 'delivery_day', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'small', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_payment_cod', 'delivery_year', 'delivery_year', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'small', 'not null' => TRUE, 'default' => 0));

  db_drop_primary_key($ret, 'uc_payment_other');
  db_change_field($ret, 'uc_payment_other', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('order_id')));

  return $ret;
}
