You are here

uc_discounts.install in Ubercart Discounts (Alternative) 6.2

Same filename and directory in other branches
  1. 7.2 uc_discounts/uc_discounts.install

Install hooks for uc_discounts.module.

File

uc_discounts/uc_discounts.install
View source
<?php

//$Id: uc_discounts.install,v 1.9.2.6 2010/12/28 00:06:23 jrust 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" => 100,
        "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."),
      ),
      "use_only_discounted_products_to_qualify" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Calculate the order subtotal using only discounted products."),
      ),
      "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_type" => array(
        "type" => "int",
        'size' => 'tiny',
        "not null" => TRUE,
        "default" => 0,
      ),
      "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)."),
      ),
      "limit_max_times_applied" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not to further limit the maximum times applied to the number of qualifying products."),
      ),
      "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."),
      ),
      'add_to_cart' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
        'default' => 0,
        'description' => t('Indicates whether free items are added to cart by the module.'),
      ),
      "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_activation" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("Whether or not discount has an activation date."),
      ),
      "activates_on" => array(
        "type" => "int",
        "not null" => TRUE,
        "default" => 0,
        "description" => t("The activation date and time as a unix timestamp."),
      ),
      "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_active" => 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",
    ),
  );
  $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",
    ),
  );
  $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."),
      ),
      "grouping" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("The grouping of this entry since required products be used in discount qualification or application."),
      ),
    ),
    'indexes' => array(
      'discount_id_grouping' => array(
        'discount_id',
        'grouping',
      ),
    ),
    "primary key" => array(
      "discount_product_id",
    ),
  );
  $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."),
      ),
      "grouping" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("The grouping of this entry since required products be used in discount qualification or application."),
      ),
    ),
    'indexes' => array(
      'discount_id_grouping' => array(
        'discount_id',
        'grouping',
      ),
    ),
    "primary key" => array(
      "discount_term_id",
    ),
  );
  $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."),
      ),
      "grouping" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("The grouping of this entry since required products be used in discount qualification or application."),
      ),
    ),
    'indexes' => array(
      'discount_id_grouping' => array(
        'discount_id',
        'grouping',
      ),
    ),
    "primary key" => array(
      "discount_sku_id",
    ),
  );
  $schema["uc_discounts_classes"] = array(
    "fields" => array(
      "discount_class_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."),
      ),
      "class" => array(
        "type" => "varchar",
        "length" => 32,
        "not null" => TRUE,
        "description" => t("The {node_type}.type of the product being discounted."),
      ),
      "grouping" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("The grouping of this entry since required products be used in discount qualification or application."),
      ),
    ),
    'indexes' => array(
      'discount_id_grouping' => array(
        'discount_id',
        'grouping',
      ),
    ),
    "primary key" => array(
      "discount_class_id",
    ),
  );
  $schema["uc_discounts_authors"] = array(
    "fields" => array(
      "discount_author_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."),
      ),
      "author_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {users}.uid of the author being discounted."),
      ),
      "grouping" => array(
        "type" => "int",
        "size" => "tiny",
        "not null" => TRUE,
        "default" => 1,
        "description" => t("The grouping of this entry since required products be used in discount qualification or application."),
      ),
    ),
    'indexes' => array(
      'discount_id_grouping' => array(
        'discount_id',
        'grouping',
      ),
    ),
    "primary key" => array(
      "discount_author_id",
    ),
  );
  $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",
    ),
  );
  $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",
    ),
  );
  $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");

  //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 > Discounts", "admin/store/uc_discounts"),
  )));
}

/**
 * Implementation of hook_uninstall().
 */
function uc_discounts_uninstall() {
  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;
}

/**
 * #984718, Add an activates_on date to discounts.
 */
function uc_discounts_update_6001() {
  $schema = uc_discounts_schema();
  $ret = array();
  db_add_field($ret, 'uc_discounts', 'has_activation', $schema['uc_discounts']['fields']['has_activation']);
  db_add_field($ret, 'uc_discounts', 'activates_on', $schema['uc_discounts']['fields']['activates_on']);
  return $ret;
}

/**
 * #919730, Add the ability for a discount to qualify based on subtotal of all products
 */
function uc_discounts_update_6002() {
  $schema = uc_discounts_schema();
  $ret = array();
  db_add_field($ret, 'uc_discounts', 'use_only_discounted_products_to_qualify', $schema['uc_discounts']['fields']['use_only_discounted_products_to_qualify']);

  // This was the default before this feature was added
  db_query("UPDATE {uc_discounts} SET use_only_discounted_products_to_qualify=1");
  return $ret;
}

/**
 * #1100790, Add the ability for a discount max applications to be limited to number of required products in the cart
 */
function uc_discounts_update_6003() {
  $schema = uc_discounts_schema();
  $ret = array();
  db_add_field($ret, 'uc_discounts', 'limit_max_times_applied', $schema['uc_discounts']['fields']['limit_max_times_applied']);
  db_query("UPDATE {uc_discounts} SET required_product = '' WHERE required_product = 'none'");
  return $ret;
}

//Add uc_discounts_authors table
function uc_discounts_update_6004() {
  $schema["uc_discounts_authors"] = array(
    "fields" => array(
      "discount_author_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."),
      ),
      "author_id" => array(
        "type" => "int",
        "not null" => TRUE,
        "description" => t("The {users}.uid of the author being discounted."),
      ),
    ),
    "primary key" => array(
      "discount_author_id",
    ),
  );
  $ret = array();
  db_create_table($ret, 'uc_discounts_authors', $schema['uc_discounts_authors']);
  return $ret;
}

/**
 * #999778, Allow multiple products to be selected for discount qualification
 */
function uc_discounts_update_6005() {
  $schema = uc_discounts_schema();
  $ret = array();
  db_add_field($ret, 'uc_discounts', 'required_product_type', $schema['uc_discounts']['fields']['required_product_type']);
  db_add_field($ret, 'uc_discounts_products', 'grouping', $schema['uc_discounts_products']['fields']['grouping']);
  db_add_index($ret, 'uc_discounts_products', 'discount_id_grouping', array(
    'discount_id',
    'grouping',
  ));
  db_add_field($ret, 'uc_discounts_terms', 'grouping', $schema['uc_discounts_terms']['fields']['grouping']);
  db_add_index($ret, 'uc_discounts_terms', 'discount_id_grouping', array(
    'discount_id',
    'grouping',
  ));
  db_add_field($ret, 'uc_discounts_skus', 'grouping', $schema['uc_discounts_skus']['fields']['grouping']);
  db_add_index($ret, 'uc_discounts_skus', 'discount_id_grouping', array(
    'discount_id',
    'grouping',
  ));
  db_add_field($ret, 'uc_discounts_classes', 'grouping', $schema['uc_discounts_classes']['fields']['grouping']);
  db_add_index($ret, 'uc_discounts_classes', 'discount_id_grouping', array(
    'discount_id',
    'grouping',
  ));
  db_add_field($ret, 'uc_discounts_authors', 'grouping', $schema['uc_discounts_authors']['fields']['grouping']);
  db_add_index($ret, 'uc_discounts_authors', 'discount_id_grouping', array(
    'discount_id',
    'grouping',
  ));
  $result = db_query("SELECT * FROM {uc_discounts} WHERE required_product != ''");
  while ($discount = db_fetch_object($result)) {
    db_query("INSERT INTO {uc_discounts_skus} SET discount_id = %d, sku = '%s', grouping = %d", $discount->discount_id, $discount->required_product, DISCOUNT_FILTER_GROUPING_QUALIFICATION);
    db_query("UPDATE {uc_discounts} SET required_product_type = %d WHERE discount_id = %d", FILTER_TYPE_SKUS, $discount->discount_id);
  }
  db_drop_field($ret, 'uc_discounts', 'required_product');
  return $ret;
}

/**
 * #1220388, Optionally allow free items to be automatically added to cart.
 */
function uc_discounts_update_6006() {
  $schema = uc_discounts_schema();
  $ret = array();
  db_add_field($ret, 'uc_discounts', 'add_to_cart', $schema['uc_discounts']['fields']['add_to_cart']);
  return $ret;
}

Functions

Namesort descending Description
uc_discounts_install Implementation of hook_install().
uc_discounts_schema @file Install hooks for uc_discounts.module.
uc_discounts_uninstall Implementation of hook_uninstall().
uc_discounts_update_1
uc_discounts_update_2
uc_discounts_update_3
uc_discounts_update_4
uc_discounts_update_5
uc_discounts_update_6
uc_discounts_update_6001 #984718, Add an activates_on date to discounts.
uc_discounts_update_6002 #919730, Add the ability for a discount to qualify based on subtotal of all products
uc_discounts_update_6003 #1100790, Add the ability for a discount max applications to be limited to number of required products in the cart
uc_discounts_update_6004
uc_discounts_update_6005 #999778, Allow multiple products to be selected for discount qualification
uc_discounts_update_6006 #1220388, Optionally allow free items to be automatically added to cart.
uc_discounts_update_7