You are here

function commerce_gc_cron in Commerce GC 7

Implements hook_cron().

File

./commerce_gc.module, line 1223
Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.

Code

function commerce_gc_cron() {
  if (variable_get('commerce_gc_disable_empty_cards', FALSE)) {

    // Despite using a sum and subselect this query is extremely fast.
    $query = db_update('commerce_coupon');
    $query
      ->fields(array(
      'status' => 0,
    ));
    $query
      ->where('(SELECT SUM(amount) AS balance FROM {commerce_gc_transaction} t WHERE t.coupon_id = commerce_coupon.coupon_id AND status IN (:status)) = 0', array(
      ':status' => commerce_gc_balance_total_statuses(),
    ));
    $query
      ->condition('type', 'giftcard_coupon');
    $query
      ->condition('status', 1);
    $num_updated = $query
      ->execute();
    if ($num_updated) {
      watchdog('commerce_gc', 'Disabled @num giftcards with no remaining balance.', array(
        '@num' => $num_updated,
      ));
    }
  }
}