<?php
//$Id: uc_discounts.install,v 1.9.2.2 2010/08/09 14:53:07 davexoxide Exp $


/**
 * @file
 * Install hooks for uc_discounts.module.
 */
function uc_discounts_schema() {
  $schema = array();

  $schema["uc_discounts"] = array(
    "fields" => array(
      "discount_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "name" => array(
        "type" => "varchar",
        "length" => 255,
        "not null" => TRUE,
        "default" => "",
      ),
      "short_description" => array(
        "type" => "varchar",
        "length" => 100,
        "not null" => TRUE,
        "default" => "",
      ),
      "description" => array(
        "type" => "varchar",
        "length" => 1000,
        "not null" => TRUE,
        "default" => "",
      ),
      "qualifying_type" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
      ),
      "qualifying_amount" => array(
        "type" => "float",
        "not null" => TRUE,
        "default" => 0.0,
        "description" => t("Minimum quantity or price required to qualify for this discount."),
      ),
      "has_qualifying_amount_max" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not this discount has a max qualifying amount."),
      ),
      "qualifying_amount_max" => array(
        "type" => "float",
        "not null" => TRUE,
        "default" => 0.0,
        "description" => t("Maximum quantity or price required to qualify for this discount."),
      ),
      "discount_type" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
      ),
      "discount_amount" => array(
        "type" => "float",
        "not null" => TRUE,
        "default" => 0.0,
        "description" => t("Amount to discount (i.e. 1 free item, 25%, or $2.00)"),
      ),
      "requires_code" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("Requires code to activate discount."),
      ),
      "filter_type" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("What type of object to filter on."),
      ),
      "has_role_filter" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not this discount filters based on role."),
      ),
      "requires_single_product_to_qualify" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("Requires qualifying amount to come from a single product."),
      ),
      "required_product" => array(
        "type" => "varchar",
        "length" => 255,
        "not null" => TRUE,
        "default" => "",
      ),
      "max_times_applied" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("Number of times this discount can be applied to a single cart (0 for unlimited)."),
      ),
      "can_be_combined_with_other_discounts" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not this discount will be applied if other discounts are."),
      ),
      "max_uses" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Number of times this discount can be applied (0 for no limit)."),
      ),
      "max_uses_per_user" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("Number of times this discount can be applied to a particular user (0 for unlimited)."),
      ),
      "max_uses_per_code" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Number of times this discount can be applied for a particular code (0 for unlimited)."),
      ),
      "has_expiration" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not discount has an expiration."),
      ),
      "expiration" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The exipration date and time as a unix timestamp."),
      ),
      "is_published" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => FALSE,
        "default" => 0,
        "description" => t("Convenience flag to state whether code is published on site or not."),
      ),
      "weight" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
      ),
      "insert_timestamp" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The insert date and time as a unix timestamp."),
      ),
    ),
    "primary key" => array("discount_id"),
  );

  return $schema;
}

function uc_discounts_codes_schema() {
  $schema = array();

  $schema["uc_discounts_codes"] = array(
    "fields" => array(
      "discount_code_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "code" => array(
        "type" => "varchar",
        "length" => 100,
        "not null" => TRUE,
        "default" => "",
      ),
    ),
    "primary key" => array("discount_code_id"),
  );

  return $schema;
}

function uc_discounts_products_schema() {
  $schema = array();

  $schema["uc_discounts_products"] = array(
    "fields" => array(
      "discount_product_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "product_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_products}.nid of the product being discounted."),
      ),
    ),
    "primary key" => array("discount_product_id"),
  );

  return $schema;
}

function uc_discounts_terms_schema() {
  $schema = array();

  $schema["uc_discounts_terms"] = array(
    "fields" => array(
      "discount_term_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "term_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {term}.tid of the term being discounted."),
      ),
    ),
    "primary key" => array("discount_term_id"),
  );

  return $schema;
}

function uc_discounts_skus_schema() {
  $schema = array();

  $schema["uc_discounts_skus"] = array(
    "fields" => array(
      "discount_sku_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "sku" => array(
        "type" => "varchar",
        "length" => 255,
        "not null" => TRUE,
        "description" => t("The {uc_products}.model of the product being discounted."),
      ),
    ),
    "primary key" => array("discount_sku_id"),
  );

  return $schema;
}

function uc_discounts_classes_schema() {
  $schema = array();

  $schema["uc_discounts_classes"] = array(
    "fields" => array(
      "discount_class_id" => array(
        "type" => "serial",
        "no null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "class" => array(
        "type" => "varchar",
        "length" => 32,
        "not null" => TRUE,
        "description" => t("The {node_type}.type of the product being discounted."),
      ),
    ),
    "primary key" => array("discount_class_id"),
  );

  return $schema;
}

function uc_discounts_roles_schema() {
  $schema = array();

  $schema["uc_discounts_roles"] = array(
    "fields" => array(
      "discount_role_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "role_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {role}.rid of the role required."),
      ),
    ),
    "primary key" => array("discount_role_id"),
  );

  return $schema;
}

function uc_discounts_uses_schema() {
  $schema = array();

  $schema["uc_discounts_uses"] = array(
    "fields" => array(
      "discount_use_id" => array(
        "type" => "serial",
        "not null" => TRUE,
      ),
      "discount_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The {uc_discounts}.discount_id of the discount."),
      ),
      "user_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The {users}.uid of the user who used the discount or (0 if anonymous)."),
      ),
      "order_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The {uc_orders}.order_id of the user's order."),
      ),
      "code" => array(
        "type" => "varchar",
        "length" => 100,
        "not null" => TRUE,
        "default" => "",
        "description" => t("Code used for discount."),
      ),
      "times_applied" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("Number of times this discount was applied."),
      ),
      "amount" => array(
        "type" => "float",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Total amount of discount."),
      ),
      "insert_timestamp" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The insert date and time as a unix timestamp."),
      ),
    ),
    "primary key" => array("discount_use_id"),
  );

  return $schema;
}

function uc_discounts_order_codes_schema() {
  $schema = array();

  $schema["uc_discounts_order_codes"] = array(
    "fields" => array(
      "order_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {uc_orders}.order_id of the user's order."),
      ),
      "codes" => array(
        "type" => "text",
        "not null" => TRUE,
        "description" => t("Newline delimited codes string for order."),
      ),
    ),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_discounts_install() {
  drupal_install_schema("uc_discounts");
  drupal_install_schema("uc_discounts_codes");
  drupal_install_schema("uc_discounts_products");
  drupal_install_schema("uc_discounts_terms");
  drupal_install_schema("uc_discounts_skus");
  drupal_install_schema("uc_discounts_roles");
  drupal_install_schema("uc_discounts_uses");
  drupal_install_schema("uc_discounts_order_codes");
  drupal_install_schema("uc_discounts_classes");

  //Weight must be less than uc_payment's
  db_query("UPDATE {system} SET weight=-10 WHERE name='uc_discounts'");

  //Print out a nice message directing administrators towards the configuration screen.
  drupal_set_message(st("UC Discounts (Alternative) settings are available under !link",
      array("!link" => l("Administer > Store administration > Configuration", "admin/store/settings/uc_discounts"))
    ));
}

/**
 * Implementation of hook_uninstall().
 */
function uc_discounts_uninstall() {
  drupal_uninstall_schema("uc_discounts_classes");
  drupal_uninstall_schema("uc_discounts_order_codes");
  drupal_uninstall_schema("uc_discounts_uses");
  drupal_uninstall_schema("uc_discounts_products");
  drupal_uninstall_schema("uc_discounts_terms");
  drupal_uninstall_schema("uc_discounts_skus");
  drupal_uninstall_schema("uc_discounts_roles");
  drupal_uninstall_schema("uc_discounts_codes");
  drupal_uninstall_schema("uc_discounts");
}


//Add {uc_discounts_roles} table
function uc_discounts_update_1() {
  $queries = array();

  $schema = uc_discounts_roles_schema();
  db_create_table($queries, "uc_discounts_roles", $schema["uc_discounts_roles"]);

  switch ($GLOBALS["db_type"]) {
    case "mysql":
    case "mysqli":
      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD has_role_filter tinyint NOT NULL default 0 AFTER filter_type");
      break;

    case "pgsql":
      db_add_column($queries, "uc_discounts", "has_role_filter", "tinyint",
        array("size" => "tiny", "not null" => TRUE, "default" => 0)
      );
      break;
  }

  return $queries;
}

//Add has_qualifying_amount_max and qualifying_amount_max columns to uc_discounts table
function uc_discounts_update_2() {
  $queries = array();

  switch ($GLOBALS["db_type"]) {
    case "mysql":
    case "mysqli":
      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD has_qualifying_amount_max tinyint NOT NULL default 0 AFTER qualifying_amount");
      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD qualifying_amount_max float NOT NULL default 0 AFTER has_qualifying_amount_max");

      //Fixes update bug that existed in uc_discounts_update_1 (but is fixed now)
      $queries[] = update_sql("ALTER TABLE {uc_discounts} MODIFY has_role_filter tinyint NOT NULL");
      break;

    case "pgsql":
      db_add_column($queries, "uc_discounts", "has_qualifying_amount_max", "tinyint",
        array("not null" => TRUE, "default" => 0)
      );
      db_add_column($queries, "uc_discounts", "qualifying_amount_max", "float",
        array("not null" => TRUE, "default" => 0)
      );
      break;
  }

  return $queries;
}

//Add max_uses_per_code column to uc_discounts table
function uc_discounts_update_3() {
  $queries = array();

  switch ($GLOBALS["db_type"]) {
    case "mysql":
    case "mysqli":
      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD max_uses_per_code int NOT NULL default 0 AFTER max_uses_per_user");
      break;

    case "pgsql":
      db_add_column($queries, "uc_discounts", "max_uses_per_code", "int",
        array("not null" => TRUE, "default" => 0)
      );
      break;
  }

  return $queries;
}

//Add uc_discounts_skus table
function uc_discounts_update_4() {
  $schema = uc_discounts_skus_schema();
  db_create_table($queries, "uc_discounts_skus", $schema["uc_discounts_skus"]);
  return $queries;
}

//Add required_product column to uc_discounts table
function uc_discounts_update_5() {
  $queries = array();

  switch ($GLOBALS["db_type"]) {
    case "mysql":
    case "mysqli":
      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD required_product varchar(255) NOT NULL default '' AFTER requires_single_product_to_qualify");
      break;

    case "pgsql":
      db_add_column($queries, "uc_discounts", "required_product", "varchar(255)",
        array("not null" => TRUE, "default" => "")
      );
      break;
  }

  return $queries;
}

//Add uc_discounts_classes table
function uc_discounts_update_6() {
  $schema = uc_discounts_classes_schema();
  db_create_table($queries, "uc_discounts_classes", $schema["uc_discounts_classes"]);
  return $queries;
}

//Rename is_published to is_active
function uc_discounts_update_7() {
  db_change_field($ret, 'uc_discounts', 'is_published', 'is_active',array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE, 'default' => 0, ));
  
  return $ret;
}
