From 9aca05177f6f966321fac7ca040837184bf45748 Mon Sep 17 00:00:00 2001
From: Neil Drumm <drumm@delocalizedham.com>
Date: Tue, 14 Feb 2012 16:34:30 -0800
Subject: [PATCH] [#1440466] If the coupon value is 0.00, set it to match the
 product price.

---
 uc_coupon_purchase/uc_coupon_purchase.module |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/uc_coupon_purchase/uc_coupon_purchase.module b/uc_coupon_purchase/uc_coupon_purchase.module
index d125eea..4e81b4a 100644
--- a/uc_coupon_purchase/uc_coupon_purchase.module
+++ b/uc_coupon_purchase/uc_coupon_purchase.module
@@ -200,6 +200,7 @@ function uc_coupon_purchase_feature_form_submit($form, &$form_state) {
  
   $description = (empty($form_state['values']['model'])) ? t('<strong>SKU:</strong> Any<br/>') : t('<strong>SKU:</strong> !sku<br/>', array('!sku' => $form_state['values']['model']));
   $description .=  t('<strong>Base coupon:</strong> !coupon<br/>', array('!coupon' => $coupon->name .' ('. $coupon->code .')'));
+  $description .= t('<strong>Value:</strong> !value<br/>', array('!value' => ($coupon->value === '0.00' ? t('Matches sell price') : uc_coupon_format_discount($coupon))));
 
   $data = array(
     'pfid' => $form_state['values']['pfid'],
@@ -342,7 +343,7 @@ function uc_coupon_purchase_order($op, $order, $status) {
               $coupon->data['order_id'] = $order->order_id;
               
               // Create new coupon and link to purchaser.
-              $new_coupon = uc_coupon_purchase_create($coupon, $product->qty, $order->uid);
+              $new_coupon = uc_coupon_purchase_create($coupon, $product, $order->uid);
               uc_order_comment_save($order->order_id, 0, t('Coupon created with code %code.', array('%code' => $new_coupon->code)));
               
               // Fire hook and conditional actions to send emails for this coupon purchase.
@@ -359,17 +360,17 @@ function uc_coupon_purchase_order($op, $order, $status) {
 /**
  * Create newly purchased coupons, based on the supplied coupon object.
  */
-function uc_coupon_purchase_create($coupon, $qty, $uid = NULL) {
+function uc_coupon_purchase_create($coupon, $product, $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) {
+  if ($product->qty > 1 || $coupon->bulk) {
     $coupon->bulk = TRUE;
 
     if (!isset($coupon->data['bulk_number'])) {
       $coupon->data['bulk_number'] = 1;
     }
-    $coupon->data['bulk_number'] *= $qty;
+    $coupon->data['bulk_number'] *= $product->qty;
 
     if (!isset($coupon->data['bulk_length'])) {
       $coupon->data['bulk_length'] = 8;
@@ -385,6 +386,11 @@ function uc_coupon_purchase_create($coupon, $qty, $uid = NULL) {
     $coupon->valid_from = gmmktime();
     $coupon->valid_until = $coupon->valid_from + $diff;
   }
+
+  // Adjust value.
+  if ($coupon->value === '0.00') {
+    $coupon->value = $product->price;
+  }
   
   // Create a new, active coupon with the suffix applied.
   $coupon->data['base_cid'] = $coupon->cid;
-- 
1.7.5

