<?php

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

/**
 * Implements hook_schema().
 */
function uc_fulfillment_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_countries_zones}.zone_id.',
        'type' => 'varchar',
        'length' => '32',
        'default' => '',
      ),
      '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' => 'varchar',
        'length' => '2',
        'default' => '',
      ),
      'o_phone' => array(
        'description' => 'Origin address: Telephone number.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
        'default' => '',
      ),
      'o_email' => array(
        'description' => 'Origin address: E-mail address.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
        'default' => '',
      ),
      '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_countries_zones}.zone_id.',
        'type' => 'varchar',
        'length' => '32',
        'default' => '',
      ),
      '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' => 'varchar',
        'length' => '2',
        'default' => '',
      ),
      'd_phone' => array(
        'description' => 'Destination address: Telephone number.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
        'default' => '',
      ),
      'd_email' => array(
        'description' => 'Destination address: E-mail address.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
        'default' => '',
      ),
      '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 shipping cost for the shipment.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'currency' => array(
        'description' => 'The currency code for the currency used.',
        'type' => 'varchar',
        'length' => 3,
        'not null' => TRUE,
        'default' => '',
      ),
      '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'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_id'),
      ),
    ),
    'indexes' => array(
      'order_id' => array('order_id'),
     ),
  );

  $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 package 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' => 4,
        'not null' => FALSE,
      ),
      'weight' => array(
        'description' => 'The package weight.',
        'type' => 'float',
        'not null' => FALSE,
      ),
      'weight_units' => array(
        'description' => 'The physical units of the weight.',
        'type' => 'varchar',
        'length' => 4,
        '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,
      ),
      'currency' => array(
        'description' => 'The currency code for the currency used.',
        'type' => 'varchar',
        'length' => 3,
        'not null' => TRUE,
        'default' => '',
      ),
      'sid' => array(
        'description' => 'The {uc_shipments}.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' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('package_id'),
    'foreign keys' => array(
      'uc_orders' => array(
        'table' => 'uc_orders',
        'columns' => array('order_id' => 'order_id'),
      ),
      'uc_quote_shipping_types' => array(
        'table' => 'uc_quote_shipping_types',
        'columns' => array('shipping_type' => 'shipping_type'),
      ),
      'uc_shipments' => array(
        'table' => 'uc_shipments',
        'columns' => array('sid' => 'sid'),
      ),
      'file' => array(
        'table' => 'file',
        'columns' => array('label_image' => 'fid'),
      ),
    ),
    'indexes' => array(
      'order_id' => array('order_id'),
      'sid' => array('sid'),
    ),
  );

  $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'),
    'foreign keys' => array(
      'package_id' => array('uc_packages' => 'package_id'),
      'order_product_id' => array('uc_order_products' => 'order_product_id'),
    ),
    'indexes' => array(
      'order_product_id' => array('order_product_id'),
    ),
  );

  return $schema;
}
