<?php

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

/**
 * Implmentation of hook_schema().
 */
function uc_flatrate_schema() {
  $schema = array();

  $schema['uc_flatrate_products'] = array(
    'description' => 'Stores product information for quantity-based shipping methods.',
    '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,
      ),
      'mid' => array(
        'description' => 'The {uc_flatrate_methods}.mid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'rate' => array(
        'description' => 'The rate multiplier in the store default currency per one of this product on the shipment.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('vid', 'mid'),
  );

  $schema['uc_flatrate_methods'] = array(
    'description' => 'Stores quantity-based shipping quote information.',
    'fields' => array(
      'mid' => array(
        'description' => 'Primary key: the shipping quote method ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'The method title, displayed on administration pages.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The user-facing label of the shipping method.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base_rate' => array(
        'description' => 'The amount of shipping cost before product quantity is applied.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'product_rate' => array(
        'description' => 'The default rate multiplier in the store default currency per product on the shipment.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
    ),
    'primary key' => array('mid'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function uc_flatrate_install() {
  drupal_install_schema('uc_flatrate');
}

/**
 * Implements hook_uninstall().
 */
function uc_flatrate_uninstall() {
  drupal_uninstall_schema('uc_flatrate');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_flatrate_update_last_removed() {
  return 4;
}

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

  db_drop_primary_key($ret, 'uc_flatrate_products');
  db_change_field($ret, 'uc_flatrate_products', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_flatrate_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_flatrate_products', 'mid', 'mid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_flatrate_products', array('vid', 'mid'));

  db_drop_primary_key($ret, 'uc_flatrate_methods');
  db_change_field($ret, 'uc_flatrate_methods', 'mid', 'mid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('mid')));

  return $ret;
}

// Removed obsolete updates 6001 and 6002.

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

  db_change_field($ret, 'uc_flatrate_products', 'rate', 'rate', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => FALSE));
  db_change_field($ret, 'uc_flatrate_methods', 'base_rate', 'base_rate', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => FALSE));
  db_change_field($ret, 'uc_flatrate_methods', 'product_rate', 'product_rate', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => FALSE));

  return $ret;
}
