<?php

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

/**
 * Implements hook_uninstall().
 */
function uc_google_checkout_uninstall() {
  db_update('uc_order_statuses')
    ->fields(array(
      'locked' => 0,
    ))
    ->condition(db_or()
      ->condition('order_status_id', 'in_google_checkout')
      ->condition('order_status_id', 'chargeable')
    )
    ->execute();

  db_delete('variable')
    ->condition('name', 'uc_google_checkout_%', 'LIKE')
    ->execute();
}

/**
 * Implements hook_requirements().
 */
function uc_google_checkout_requirements($phase) {
  $t = get_t();

  $requirements['uc_google_checkout'] = array(
    'title' => $t('Google Checkout'),
    'description' => $t('Google Checkout is no longer in operation. This module cannot be installed, only uninstalled. If you already have it installed, you must uninstall it. Follow the instructions in the module\'s <a href="@readme">README.txt</a>', array('@readme' => '/' . drupal_get_path('module', 'uc_google_checkout') . '/README.txt')),
    'severity' => REQUIREMENT_ERROR,
  );

  return $requirements;
}

/**
 * Implements hook_schema().
 */
function uc_google_checkout_schema() {
  $schema = array();

  $schema['uc_gc_products'] = array(
    'description' => 'Stores Google Checkout information for products.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {uc_products}.vid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {uc_products}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'gc_salable' => array(
        'description' => 'A flag indicating whether the product can be sold with Google Checkout. 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('vid'),
    'foreign keys' => array(
      'uc_products' => array(
        'table' => 'uc_products',
        'columns' => array(
          'nid' => 'nid',
          'vid' => 'vid',
        ),
      )
    ),
  );

  $schema['uc_gc_orders'] = array(
    'description' => 'Stores Google Checkout orders information.',
    'fields' => array(
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'gc_order_number' => array(
        'description' => 'The Google Checkout order number.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0',
      ),
      'financial_state' => array(
        'description' => 'The order financial state.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'REVIEWING',
      ),
      'fulfillment_state' => array(
        'description' => 'The order fulfillment state.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'NEW',
      ),
      'gc_total' => array(
        'description' => 'The order total according to Google Checkout.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
    ),
    'indexes' => array(
      'gc_order_number' => array(array('gc_order_number', 20)),
    ),
    'primary key' => array('order_id'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_id'),
      ),
    ),
  );

  $schema['uc_gc_taxes'] = array(
    'description' => 'Stores tax information for Google Checkout.',
    'fields' => array(
      'zone' => array(
        'description' => 'The 2-letter state abberviation.',
        'type' => 'varchar',
        'length' => 2,
        'not null' => TRUE,
        'default' => '',
      ),
      'rate' => array(
        'description' => 'The tax rate.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'tax_shipping' => array(
        'description' => 'A flag indicating whether shipping costs are taxed.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('zone'),
  );

  return $schema;
}
