<?php

/*
 * @file
 * Install percentage coupon types and its fields.
 */

/**
 * Implements hook_install().
 */
function commerce_coupon_pct_install() {
  drupal_load('module', 'commerce');
  $values = array(
    'type' => 'commerce_coupon_pct',
    'label' => st('Percentage coupon'),
  );

  $coupon_type = commerce_coupon_type_create($values);
  commerce_coupon_type_save($coupon_type, TRUE);
}

/**
 * Implements hook_uninstall().
 */
function commerce_coupon_pct_uninstall() {
  // Make sure commerce.module is loaded so that commerce_delete_instances()
  // is available.
  drupal_load('module', 'commerce');
  // The function commerce_coupon_type_delete() can't be used here because
  // commerce_coupon module might not be enabled anymore at this point.
  db_delete('commerce_coupon_type')
    ->condition('type', 'commerce_coupon_pct')
    ->execute();

  // Delete all fields that belong to the module's coupon type.
  commerce_delete_instances('commerce_coupon', 'commerce_coupon_pct');

  // Also delete coupons.
  db_delete('commerce_coupon')
    ->condition('type', 'commerce_coupon_pct')
    ->execute();
}

/**
 * Implements hook_disable().
 */
function commerce_coupon_pct_disable() {
  drupal_load('module', 'commerce_coupon');
  commerce_coupon_type_disable('commerce_coupon_pct');
}

/**
 * Implements hook_enable().
 */
function commerce_coupon_pct_enable() {
  drupal_load('module', 'commerce_coupon');
  commerce_coupon_type_enable('commerce_coupon_pct');
}


/**
 * Implements hook_update_N().
 */
function commerce_coupon_pct_update_7001(&$sandbox) {
  // The update system is going to flush all caches anyway, so nothing to do.
}