<?php
// $Id: coupon.install,v 1.2.4.6 2007/02/15 05:58:01 gordon Exp $

require_once(drupal_get_path('module', 'store') . '/store.deps.inc');

/**
 * E-Commerce coupon module schema
 */
function coupon_install() {
  $deps = array('ec_mail');
  if (!store_deps_install('coupon', $deps)) {
    return;
  }
    
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {ec_coupon} (
          coupon varchar(20) NOT NULL default '',
          operator enum('+','-','%') NOT NULL default '+',
          operand decimal(10,2) NOT NULL default '0.00',
          created int(10) NOT NULL default '0',
          created_txnid int(10) unsigned default NULL,
          created_vid int(10) unsigned default NULL,
          PRIMARY KEY  (coupon),
          KEY created_txnid (created_txnid, created_vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {ec_transaction_coupon} (
        txnid int(10) NOT NULL,
        coupon varchar(20) NOT NULL,
        discount decimal(10,2) NOT NULL,
        PRIMARY KEY  (txnid,coupon)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {ec_coupon} (
          coupon varchar(20) NOT NULL default '',
          operator varchar(1) check (operator in ('+','-','*')) NOT NULL default '+',
          operand decimal(10,2) NOT NULL default '0.00',
          created integer NOT NULL default '0',
          created_txnid integer default NULL,
          created_vid integer default NULL,
          PRIMARY KEY  (coupon)
      )");
      db_query("CREATE INDEX {ec_coupon}_created_txnid ON {ec_coupon} (created_txnid, created_vid)");
      db_query("CREATE TABLE {ec_transaction_coupon} (
        txnid integer NOT NULL,
        coupon varchar(20) NOT NULL,
        discount decimal(10,2) NOT NULL,
        PRIMARY KEY  (txnid,coupon)
      )");
     break;
  }
  db_query("UPDATE {system} SET weight = 5 WHERE name = 'coupon'");
  
  require_once(drupal_get_path('module', 'coupon') . '/coupon.module');
  require_once(drupal_get_path('module', 'ec_mail') . '/ec_mail.module');
  ec_mail_reset_to_defaults(ECMAIL_TYPE_COUPON, 'coupon');
}

/**
 * Update the coupon mail to the new system.
 * The ec_mail module becomes mandatory from this update onwards. 
 */
function coupon_update_1() {
  $ret = array();
  $deps = array('ec_mail');
  if (store_deps_update($ret, 'coupon', $deps)) {
    ec_mail_reset_to_defaults(ECMAIL_TYPE_COUPON);
  }
  return $ret;
}

function coupon_update_2() {
  $ret[] = update_sql("UPDATE {system} SET weight = 5 WHERE name = 'coupon'");
  return $ret;
}
