<?php

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

/**
 * 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(
      'nid' => array('uc_products' => 'nid'),
      'vid' => array('uc_products' => '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(
      'order_id' => array('uc_orders' => '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;
}

/**
 * Implements hook_install().
 */
function uc_google_checkout_install() {
  $t = get_t();

  db_merge('uc_order_statuses')
    ->key(array('order_status_id' => 'in_google_checkout'))
    ->insertFields(array(
      'order_status_id' => 'in_google_checkout',
      'title' => $t('In Google Checkout'),
      'state' => 'in_checkout',
      'weight' => -7,
      'locked' => 1,
    ))
    ->updateFields(array(
      'state' => 'in_checkout',
      'locked' => 1,
    ))
    ->execute();

  db_merge('uc_order_statuses')
    ->key(array('order_status_id' => 'chargeable'))
    ->insertFields(array(
      'order_status_id' => 'chargeable',
      'title' => $t('Chargeable'),
      'state' => 'post_checkout',
      'weight' => 2,
      'locked' => 1,
    ))
    ->updateFields(array(
      'state' => 'post_checkout',
      'locked' => 1,
    ))
    ->execute();
}

/**
 * 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_update_last_removed().
 */
function uc_google_checkout_update_last_removed() {
  // 7.x-3.0-beta2 and earlier were installed with schema version 0,
  // which causes update.php to fail.
  return drupal_get_installed_schema_version('uc_google_checkout') == 0 ? 0 : 6004;
}

/**
 * Fix incorrect order status configuration.
 */
function uc_google_checkout_update_7000() {
  db_delete('uc_order_statuses')
    ->condition('order_status_id', '')
    ->execute();

  db_merge('uc_order_statuses')
    ->key(array('order_status_id' => 'in_google_checkout'))
    ->insertFields(array(
      'order_status_id' => 'in_google_checkout',
      'title' => t('In Google Checkout'),
      'state' => 'in_checkout',
      'weight' => -7,
      'locked' => 1,
    ))
    ->updateFields(array(
      'state' => 'in_checkout',
      'locked' => 1,
    ))
    ->execute();

  db_merge('uc_order_statuses')
    ->key(array('order_status_id' => 'chargeable'))
    ->insertFields(array(
      'order_status_id' => 'chargeable',
      'title' => t('Chargeable'),
      'state' => 'post_checkout',
      'weight' => 2,
      'locked' => 1,
    ))
    ->updateFields(array(
      'state' => 'post_checkout',
      'locked' => 1,
    ))
    ->execute();
}
