Extension ext-coupon continued, naming convention, many improvements:
[mailer.git] / inc / modules / member / what-cash_coupon.php
index 2984ec788dcf755dd9cdcc11c4fc297514455573..90280846df88b3def065737e16b1e4f0a7a11d33 100644 (file)
@@ -53,7 +53,100 @@ if ((!isExtensionActive('coupon')) && (!isAdmin())) {
 
 // Is the form sent?
 if (isFormSent()) {
-       // @TODO
+       // Is the coupon code entered?
+       if (isPostRequestParameterSet('coupon_code')) {
+               // Search for the coupon
+               $result = SQL_QUERY_ESC("SELECT
+       c.`coupon_id`,
+       UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
+       UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
+       d.`coupon_type`,
+       d.`total_created`,
+       d.`total_used`,
+       d.`points`,
+       d.`coupon_description`
+FROM
+       `{?_MYSQL_PREFIX?}_user_coupons` AS c
+INNER JOIN
+       `{?_MYSQL_PREFIX?}_coupon_data` AS d
+ON
+       c.`coupon_id`=d.`id`
+WHERE
+       c.`coupon_code`='%s' AND
+       c.`userid`=%s AND
+       c.`cashed_on` IS NULL AND
+       UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
+LIMIT 1",
+                       array(
+                               postRequestParameter('coupon_code'),
+                               getMemberId()
+                       ), __FILE__, __LINE__);
+
+               // Do we have an entry?
+               if (SQL_NUMROWS($result) == 1) {
+                       // Load data
+                       $content = SQL_FETCHARRAY($result);
+
+                       // Mark it as "cashed"
+                       SQL_QUERY_ESC("UPDATE
+       `{?_MYSQL_PREFIX?}_user_coupons`
+SET
+       `cashed_on`=NOW(),
+       `coupon_code`=NULL
+WHERE
+       `coupon_code`='%s' AND
+       `userid`=%s
+LIMIT 1",
+                               array(
+                                       postRequestParameter('coupon_code'),
+                                       getMemberId()
+                               ), __FILE__, __LINE__);
+
+                       // Does it work?
+                       if (SQL_HASZEROAFFECTED()) {
+                               // Abort it here, it always must work
+                               debug_report_bug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
+                       } // END - if
+
+                       // Update count as well
+                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_coupon_data` SET `total_used`=`total_used`+1 WHERE `id`=%s AND `total_used` < `total_created` LIMIT 1",
+                               array(bigintval($content['coupon_id'])), __FILE__, __LINE__);
+
+                       // Does it work?
+                       if (SQL_HASZEROAFFECTED()) {
+                               // Abort it here, it always must work
+                               debug_report_bug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
+                       } // END - if
+
+                       // Now we just need to book that points on the user's account
+                       addPointsThroughReferalSystem('coupon_cashed', getMemberId(), $content['points'], false, 0, getConfig('coupon_payment_method'));
+
+                       // Subtract points from member account if the admin has selected one
+                       if (isValidUserId(getConfig('coupon_userid'))) {
+                               // Subtract from this account
+                               subtractPoints('cashed_coupon', getConfig('coupon_userid'), $content['points']);
+                       } // END - if
+
+                       // Translate some data
+                       $content['coupon_expired'] = generateDateTime($content['coupon_expired'], '0');
+                       $content['coupon_created'] = generateDateTime($content['coupon_created'], '0');
+
+                       // Send admin notification
+                       sendAdminNotification('{--ADMIN_COUPON_CASHED_SUBJECT--}', 'admin_coupon_cashed', $content, getMemberId());
+
+                       // Display message
+                       displayMessage(getMaskedMessage('MEMBER_COUPON_CODE_CASHED', $content['points']));
+               } else {
+                       // No coupon found
+                       displayMessage('{--MEMBER_COUPON_CODE_ALREADY_USED--}');
+               }
+
+               // Free the result
+               SQL_FREERESULT($result);
+       } else {
+               // No code entered!
+               displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
+       }
 } else {
        // Load form template
        loadTemplate('member_cash_coupon', false);