You are here

uc_taxes.install in Ubercart 5

Same filename and directory in other branches
  1. 6.2 uc_taxes/uc_taxes.install
  2. 7.3 uc_taxes/uc_taxes.install

File

uc_taxes/uc_taxes.install
View source
<?php

function uc_taxes_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {uc_taxes} (\n        `id` mediumint(9) NOT NULL,\n        `name` varchar(255) NOT NULL,\n        `rate` float unsigned NOT NULL default '0',\n        `shippable` tinyint UNSIGNED NOT NULL default 0,\n        `taxed_product_types` text NOT NULL,\n        `taxed_line_items` text NOT NULL,\n        `weight` tinyint(2) NOT NULL default '0',\n        PRIMARY KEY  (`id`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {uc_taxes} (\n        id serial NOT NULL,\n        name varchar(255) NOT NULL default '',\n        rate float NOT NULL default 0,\n        shippable smallint_unsigned NOT NULL default 0,\n        taxed_product_types text NOT NULL default '',\n        taxed_line_items text NOT NULL default '',\n        weight smallint NOT NULL default 0,\n        PRIMARY KEY  (id)\n      );");
      db_query("CREATE INDEX {uc_taxes}_id ON {uc_taxes} (id)");
      break;
  }
}
function uc_taxes_uninstall() {
  db_query("DROP TABLE {uc_taxes}");
}
function uc_taxes_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP INDEX taxes");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP pcid");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN id mediumint(9) NOT NULL FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD PRIMARY KEY id (id)");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN name varchar(255) NOT NULL AFTER id");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN area area varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN type type enum('code','zone','country') NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN standalone cumulative tinyint(1) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN weight tinyint(2) NOT NULL default '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP CONSTRAINT {uc_taxes}_pcid_area_key");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP pcid");
      db_add_column($ret, 'uc_taxes', 'id', 'integer', array(
        'not null' => true,
        'default' => 0,
      ));
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD PRIMARY KEY (id)");
      $ret[] = update_sql("CREATE INDEX {uc_taxes}_id ON {uc_taxes} (id)");
      db_add_column($ret, 'uc_taxes', 'name', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      db_change_column($ret, 'uc_taxes', 'area', 'area', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      db_change_column($ret, 'uc_taxes', 'type', 'type', "enum('code','zone','country')", array(
        'not null' => true,
        'default' => 'code',
      ));
      db_change_column($ret, 'uc_taxes', 'standalone', 'cumulative', 'smallint', array(
        'not null' => true,
        'default' => 0,
      ));
      db_add_column($ret, 'uc_taxes', 'weight', 'smallint', array(
        'not null' => true,
        'default' => 0,
      ));
      break;
  }
  return $ret;
}
function uc_taxes_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN shipping taxed_line_items text NOT NULL");
      break;
    case 'pgsql':
      db_change_column($ret, 'uc_taxes', 'shipping', 'taxed_line_items', 'text', array(
        'not null' => true,
        'default' => "''",
      ));
      break;
  }
  return $ret;
}
function uc_taxes_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN conditions text NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'conditions', 'text', array(
        'not null' => true,
        'default' => "''",
      ));
      break;
  }
  return $ret;
}
function uc_taxes_update_4() {
  $result = db_query("SELECT id, taxed_line_items FROM {uc_taxes} WHERE cumulative != 0");
  while ($tax = db_fetch_object($result)) {
    if ($taxed_line_items = unserialize($tax->taxed_line_items) && is_array($taxed_line_items)) {
      $taxed_line_items['tax'] = 'tax';
      db_query("UPDATE {uc_taxes} SET taxed_line_items = '%s' WHERE id = %d", serialize($taxed_line_items), $tax->id);
    }
  }
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP cumulative");
  return $ret;
}
function uc_taxes_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN taxed_product_types text NOT NULL AFTER rate");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'taxed_product_types', 'text', array(
        'not null' => true,
        'default' => "''",
      ));
      break;
  }
  $types = array();
  foreach (module_invoke_all('product_types') as $type) {
    $types[$type] = $type;
  }
  db_query("UPDATE {uc_taxes} SET taxed_product_types = '%s'", serialize($types));
  return $ret;
}
function uc_taxes_update_6() {
  $ret = array();
  if (module_exists('workflow_ng_ui')) {
    $configurations = array();
    $taxes = uc_taxes_get_rates();
    foreach ($taxes as $tax) {
      $configurations['uc_taxes_' . $tax->id] = array(
        '#name' => 'uc_taxes_' . $tax->id,
        '#label' => $tax->name,
        '#event' => 'calculate_tax_' . $tax->id,
        '#module' => 'uc_taxes',
        '#active' => true,
      );
      $action = workflow_ng_use_action('uc_taxes_action_apply_tax', array(
        '#label' => t('Apply @tax', array(
          '@tax' => $tax->name,
        )),
      ));
      switch ($tax->type) {
        case 'code':
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_postal_code', array(
            '#label' => t('Is in postal code area @code', array(
              '@code' => $tax->area,
            )),
            '#settings' => array(
              'pattern' => $tax->area,
            ),
          ));
          break;
        case 'zone':
          $zone = uc_get_zone_code($tax->area);
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_zone', array(
            '#label' => t('Is in @zone', array(
              '@zone' => $zone,
            )),
            '#settings' => array(
              'zones' => array(
                $tax->area,
              ),
            ),
          ));
          break;
        case 'country':
          $country = uc_get_country_data(array(
            'country_id' => $tax->area,
          ));
          $country_name = $country[0]['country_name'];
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_country', array(
            '#label' => t('Is in @country', array(
              '@country' => $country_name,
            )),
            '#settings' => array(
              'countries' => array(
                $tax->area,
              ),
            ),
          ));
          break;
      }
      $configurations['uc_taxes_' . $tax->id] = workflow_ng_configure($configurations['uc_taxes_' . $tax->id], $action, $area_condition);
      workflow_ng_ui_save_configuration($configurations['uc_taxes_' . $tax->id]);
    }
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP area");
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP type");
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP conditions");
  }
  return $ret;
}
function uc_taxes_update_7() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN shippable tinyint UNSIGNED NOT NULL default 0");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'shippable', 'smallint_unsigned', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      break;
  }
  return $ret;
}