<?php

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

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

  $schema['uc_quote_shipping_types'] = array(
    'description' => 'Stores shipping information of products.',
    'fields' => array(
      'id_type' => array(
        'description' => 'Determines the table that id references. "product" => {uc_products}.nid.',
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'id' => array(
        'description' => 'The entity ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'shipping_type' => array(
        'description' => 'The basic type of shipment, e.g.: small package, freight.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('id_type', 'id'),
  );

  $schema['uc_quote_product_locations'] = array(
    'description' => 'Stores default product origin addresses.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {uc_products}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'first_name' => array(
        'description' => 'The address first name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'last_name' => array(
        'description' => 'The address last name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'company' => array(
        'description' => 'The address company.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'street1' => array(
        'description' => 'The address street line 1.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'street2' => array(
        'description' => 'The address street line 2.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'city' => array(
        'description' => 'The address city.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'zone' => array(
        'description' => 'The address state/province, from {uc_zones}.zone_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'postal_code' => array(
        'description' => 'The address postal code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'country' => array(
        'description' => 'The address country, from {uc_countries}.country_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'phone' => array(
        'description' => 'The address phone number.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('nid'),
    'foreign keys' => array(
      'uc_products' => array(
        'table' => 'uc_products',
        'columns' => array('nid' => 'nid'),
      ),
    ),
  );

  $schema['uc_order_quotes'] = array(
    'description' => 'Stores shipping quotes.',
    'fields' => array(
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'method' => array(
        'description' => 'The quoted shipping method.',
        'type' => 'varchar',
        'length' => 25,
        'not null' => TRUE,
        'default' => '',
      ),
      'accessorials' => array(
        'description' => 'Additional services or special instructions.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'rate' => array(
        'description' => 'The quoted shipping rate.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('order_id'),
    'unique keys' => array(
      'order_id_quote_method' => array('order_id', 'method'),
    ),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_id'),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_quote_uninstall() {
  db_delete('variable')
    ->condition('name', 'uc_quote_%', 'LIKE')
    ->execute();

  variable_del('uc_store_shipping_type');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_quote_update_last_removed() {
  return 6004;
}

/**
 * Drops {uc_order_quotes}.quote_form.
 */
function uc_quote_update_7001() {
  db_drop_field('uc_order_quotes', 'quote_form');
}
