<?php

/**
 * @file
 * Install, update and uninstall functions for the uc_payment_pack module.
 */

/**
 * Implements hook_schema().
 */
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'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_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'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => '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'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_id'),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_payment_pack_uninstall() {
  db_delete('variable')
    ->condition(db_or()
      ->condition('name', 'uc_check_%', 'LIKE')
      ->condition('name', 'uc_cod_%', 'LIKE')
    )
    ->execute();
}

/**
 * Implements hook_update_last_removed().
 */
function uc_payment_pack_update_last_removed() {
  return 6000;
}
