You are here

function uc_coupon_purchase_create in Ubercart Discount Coupons 7.2

Same name and namespace in other branches
  1. 5 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()
  2. 6 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()
  3. 7.3 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()

Create newly purchased coupons, based on the supplied coupon object.

2 calls to uc_coupon_purchase_create()
uc_coupon_purchase_create_action in uc_coupon_purchase/uc_coupon_purchase.rules.inc
Action callback to give user a coupon
uc_coupon_purchase_uc_order in uc_coupon_purchase/uc_coupon_purchase.module
Implements hook_uc_order().

File

uc_coupon_purchase/uc_coupon_purchase.module, line 378

Code

function uc_coupon_purchase_create($coupon, $qty, $uid = NULL) {
  $suffix_length = isset($coupon->data['purchase_suffix_length']) ? $coupon->data['purchase_suffix_length'] : 12;

  // If more than one coupon will be generated, ensure it is a bulk coupon.
  if ($qty > 1 || $coupon->bulk) {
    $coupon->bulk = TRUE;
    if (!isset($coupon->data['bulk_number'])) {
      $coupon->data['bulk_number'] = 1;
    }
    $coupon->data['bulk_number'] *= $qty;
    if (!isset($coupon->data['bulk_length'])) {
      $coupon->data['bulk_length'] = 8;
    }

    // Adjust the suffix so single and bulk coupons are the same length where possible.
    $suffix_length = max($suffix_length - $coupon->data['bulk_length'], 4);
  }

  // Adjust the validity dates.
  if (isset($coupon->data['purchase_relative'])) {
    $diff = $coupon->valid_until - $coupon->valid_from;
    $coupon->valid_from = gmmktime();
    $coupon->valid_until = $coupon->valid_from + $diff;
  }

  // Create a new, active coupon with the suffix applied.
  $coupon->data['base_cid'] = $coupon->cid;
  unset($coupon->cid);
  $coupon->code .= strtoupper(user_password($suffix_length));
  $coupon->status = TRUE;
  uc_coupon_save($coupon);

  // Associate this coupon with its owner.
  if (!is_null($uid)) {
    $id = db_insert('uc_coupon_purchase_users')
      ->fields(array(
      'uid' => $uid,
      'cid' => $coupon->cid,
    ))
      ->execute();
  }
  return $coupon;
}