<?php

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

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

  $schema['uc_shipments'] = array(
    'description' => 'Stores shipment information.',
    'fields' => array(
      'sid' => array(
        'description' => 'Primary key: the shipment ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'order_id' => array(
        'description' => 'The {uc_orders}.order_id of the order associated with the shipment.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'o_first_name' => array(
        'description' => 'Origin address: First name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_last_name' => array(
        'description' => 'Origin address: Last name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_company' => array(
        'description' => 'Origin address: Company name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_street1' => array(
        'description' => 'Origin address: Street line 1.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_street2' => array(
        'description' => 'Origin address: Street line 2.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_city' => array(
        'description' => 'Origin address: City.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_zone' => array(
        'description' => 'Origin address: State/province, from {uc_zones}.zone_id.',
        'type' => 'int',
        'size' => 'medium',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'o_postal_code' => array(
        'description' => 'Origin address: Postal code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'o_country' => array(
        'description' => 'Origin address: Country, from {uc_countries}.country_id.',
        'type' => 'int',
        'size' => 'medium',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'd_first_name' => array(
        'description' => 'Destination address: First name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_last_name' => array(
        'description' => 'Destination address: Last name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_company' => array(
        'description' => 'Destination address: Company name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_street1' => array(
       'description' => 'Destination address: Street line 1.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_street2' => array(
        'description' => 'Destination address: Street line 2.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_city' => array(
        'description' => 'Destination address: City.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_zone' => array(
        'description' => 'Destination address: State/province, from {uc_zones}.zone_id.',
        'type' => 'int',
        'size' => 'medium',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'd_postal_code' => array(
        'description' => 'Destination address: Postal code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'd_country' => array(
        'description' => 'Destination address: Country, from {uc_countries}.country_id.',
        'type' => 'int',
        'size' => 'medium',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'shipping_method' => array(
        'description' => 'The shipping method.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'accessorials' => array(
        'description' => 'Shipping options and special instructions.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'carrier' => array(
        'description' => 'The company making the delivery.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'transaction_id' => array(
        'description' => "The carrier's shipment identifier.",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'tracking_number' => array(
        'description' => 'The number used by the carrier to locate the shipment while it is in transit.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ship_date' => array(
        'description' => 'The Unix timestamp indicating when the shipment left the origin address.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'expected_delivery' => array(
        'description' => 'The Unix timestamp indicating the expected date of delivery.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'cost' => array(
        'description' => 'The cost of the shipment.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp indicating the last time the shipment was modified.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('sid'),
  );

  $schema['uc_packages'] = array(
    'description' => 'Stores shipment package information.',
    'fields' => array(
      'package_id' => array(
        'description' => 'Primary key: the package 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,
      ),
      'shipping_type' => array(
        'description' => 'The basic type of shipment, e.g.: small package, freight, etc.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'pkg_type' => array(
        'description' => 'The type of packaging.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'length' => array(
        'description' => 'The package length.',
        'type' => 'float',
        'not null' => FALSE,
      ),
      'width' => array(
        'description' => 'The pacakge width.',
        'type' => 'float',
        'not null' => FALSE,
      ),
      'height' => array(
        'description' => 'The package height.',
        'type' => 'float',
        'not null' => FALSE,
      ),
      'length_units' => array(
        'description' => 'The physical units of the length, width, and height.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'value' => array(
        'description' => 'The monetary value of the package contents.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => FALSE,
        'default' => 0.0,
      ),
      'sid' => array(
        'description' => 'The {uc_shimpents}.sid, if the package has been shipped.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'tracking_number' => array(
        'description' => 'The package-specific tracking number, if available.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'label_image' => array(
        'description' => 'The {file}.fid that refers to an image of the shipping label of the package.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('package_id'),
  );

  $schema['uc_packaged_products'] = array(
    'description' => 'Stores packaged product information.',
    'fields' => array(
      'package_id' => array(
        'description' => 'The {uc_packages}.package_id in which the product is shipped.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'order_product_id' => array(
        'description' => 'The {uc_order_products}.order_product_id of the ordered product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'qty' => array(
        'description' => 'The number of this product in this package.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('package_id', 'order_product_id'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function uc_shipping_install() {
  drupal_install_schema('uc_shipping');
}

/**
 * Implements hook_uninstall().
 */
function uc_shipping_uninstall() {
  drupal_uninstall_schema('uc_shipping');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_shipping_update_last_removed() {
  return 2;
}

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

  db_drop_primary_key($ret, 'uc_shipments');
  db_change_field($ret, 'uc_shipments', 'sid', 'sid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('sid')));
  db_change_field($ret, 'uc_shipments', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

  db_drop_primary_key($ret, 'uc_packages');
  db_change_field($ret, 'uc_packages', 'package_id', 'package_id', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('package_id')));
  db_change_field($ret, 'uc_packages', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_packages', 'value', 'value', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => FALSE));
  db_change_field($ret, 'uc_packages', 'sid', 'sid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE));

  db_drop_primary_key($ret, 'uc_packaged_products');
  db_change_field($ret, 'uc_packaged_products', 'package_id', 'package_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_packaged_products', 'order_product_id', 'order_product_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_packaged_products', array('package_id', 'order_product_id'));

  return $ret;
}

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

  if (db_column_exists('uc_packages', 'values')) {
    if ($GLOBALS['db_type'] == 'mysql' || $GLOBALS['db_type'] == 'mysqli') {
      $field = '`values`';
    }
    else {
      $field = 'values';
    }
    db_change_field($ret, 'uc_packages', $field, 'value', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => FALSE));
  }

  return $ret;
}

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

  db_change_field($ret, 'uc_packages', 'value', 'value', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'not null' => FALSE));

  return $ret;
}

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

  db_change_field($ret, 'uc_shipments', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_packages', 'value', 'value', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => FALSE));

  return $ret;
}

/**
 * Change to signed floats for package dimensions.
 */
function uc_shipping_update_6004() {
  $ret = array();

  $schema = array(
    'float_spec' => array(
      'type' => 'float',
      'not null' => TRUE,
      'default' => 0.0,
    ),
  );
  db_change_field($ret, 'uc_packages', 'length', 'length', $schema['float_spec'] + array('description' => 'Physical length of the packaging.'));
  db_change_field($ret, 'uc_packages', 'width', 'width', $schema['float_spec'] + array('description' => 'Physical width of the packaging.'));
  db_change_field($ret, 'uc_packages', 'height', 'height', $schema['float_spec'] + array('description' => 'Physical height of the packaging.'));

  return $ret;
}

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

  db_change_field($ret, 'uc_shipments', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_packages', 'value', 'value', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => FALSE));

  return $ret;
}

/**
 * Add 'changed' column to uc_shipments
 */
function uc_shipping_update_6006() {
  $ret = array();

  db_add_field($ret, 'uc_shipments', 'changed', array(
    'description' => 'The Unix timestamp indicating the last time the shipment was modified.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));

  return $ret;
}
