You are here

function uc_coupon_purchase_create in Ubercart Discount Coupons 6

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

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

1 call to uc_coupon_purchase_create()
uc_coupon_purchase_order in uc_coupon_purchase/uc_coupon_purchase.module
Implementation of hook_order().

File

uc_coupon_purchase/uc_coupon_purchase.module, line 362

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)) {
    db_query("INSERT INTO {uc_coupon_purchase_users} (uid, cid) VALUES (%d, %d)", $uid, $coupon->cid);
  }
  return $coupon;
}