Extension ext-grade continued, a lot renames:
[mailer.git] / inc / modules / member / what-cash_coupon.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/28/2011 *
4  * ===================                          Last change: 06/28/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-cash_coupon.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : To cash a coupon                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Einen Gutschein einloesen                        *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
47
48 // Continue only if the proper extension is active (admins can always continue)
49 if ((!isExtensionActive('coupon')) && (!isAdmin())) {
50         displayMessage(generateExtensionInactiveNotInstalledMessage('coupon'));
51         return;
52 } // END - if
53
54 // Is the form sent?
55 if (isFormSent()) {
56         // Is the coupon code entered?
57         if (isPostRequestParameterSet('coupon_code')) {
58                 // Search for the coupon
59                 $result = SQL_QUERY_ESC("SELECT
60         c.`coupon_id`,
61         UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
62         UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
63         d.`coupon_type`,
64         d.`total_created`,
65         d.`total_cashed`,
66         d.`points`,
67         d.`coupon_description`
68 FROM
69         `{?_MYSQL_PREFIX?}_user_coupons` AS c
70 INNER JOIN
71         `{?_MYSQL_PREFIX?}_coupon_data` AS d
72 ON
73         c.`coupon_id`=d.`id`
74 WHERE
75         c.`coupon_code`='%s' AND
76         c.`userid`=%s AND
77         c.`cashed_on` IS NULL AND
78         UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
79 LIMIT 1",
80                         array(
81                                 postRequestParameter('coupon_code'),
82                                 getMemberId()
83                         ), __FILE__, __LINE__);
84
85                 // Do we have an entry?
86                 if (SQL_NUMROWS($result) == 1) {
87                         // Load data
88                         $content = SQL_FETCHARRAY($result);
89
90                         // Mark it as "cashed"
91                         SQL_QUERY_ESC("UPDATE
92         `{?_MYSQL_PREFIX?}_user_coupons`
93 SET
94         `cashed_on`=NOW(),
95         `coupon_code`=NULL
96 WHERE
97         `coupon_code`='%s' AND
98         `userid`=%s
99 LIMIT 1",
100                                 array(
101                                         postRequestParameter('coupon_code'),
102                                         getMemberId()
103                                 ), __FILE__, __LINE__);
104
105                         // Does it work?
106                         if (SQL_HASZEROAFFECTED()) {
107                                 // Abort it here, it always must work
108                                 debug_report_bug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
109                         } // END - if
110
111                         // Update count as well
112                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_coupon_data` SET `total_cashed`=`total_cashed`+1 WHERE `id`=%s AND `total_cashed` < `total_created` LIMIT 1",
113                                 array(bigintval($content['coupon_id'])), __FILE__, __LINE__);
114
115                         // Does it work?
116                         if (SQL_HASZEROAFFECTED()) {
117                                 // Abort it here, it always must work
118                                 debug_report_bug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
119                         } // END - if
120
121                         // Now we just need to book that points on the user's account
122                         addPointsThroughReferalSystem('coupon_cashed', getMemberId(), $content['points'], false, 0, getConfig('coupon_payment_method'));
123
124                         // Subtract points from member account if the admin has selected one
125                         if (isValidUserId(getConfig('coupon_userid'))) {
126                                 // Subtract from this account
127                                 subtractPoints('cashed_coupon', getConfig('coupon_userid'), $content['points']);
128                         } // END - if
129
130                         // Translate some data
131                         $content['coupon_expired'] = generateDateTime($content['coupon_expired'], '0');
132                         $content['coupon_created'] = generateDateTime($content['coupon_created'], '0');
133
134                         // Send admin notification
135                         sendAdminNotification('{--ADMIN_COUPON_CASHED_SUBJECT--}', 'admin_coupon_cashed', $content, getMemberId());
136
137                         // Display message
138                         displayMessage(getMaskedMessage('MEMBER_COUPON_CODE_CASHED', $content['points']));
139                 } else {
140                         // No coupon found
141                         displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
142                 }
143
144                 // Free the result
145                 SQL_FREERESULT($result);
146         } else {
147                 // No code entered!
148                 displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
149         }
150 } elseif (isFormSent('change')) {
151         // Change receive status, depending on its current state
152         if (isUserDataEnabled('receiving_coupons')) {
153                 // Unsubscribe from coupons
154                 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_UNSUBSCRIBED--}';
155                 $newStatus = 'N';
156         } else {
157                 // Subscribe to coupons
158                 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
159                 $newStatus = 'Y';
160         }
161
162         // And change it in the database
163         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receiving_coupons`='%s' WHERE `userid`=%s LIMIT 1",
164                 array($newStatus, getMemberId()), __FILE__, __LINE__);
165
166         // Did something change?
167         if (!SQL_HASZEROAFFECTED()) {
168                 // Records have been updated
169                 displayMessage($message);
170         } else {
171                 // Nothing changed!
172                 displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
173         }
174 } else {
175         // Load form template
176         loadTemplate('member_cash_coupon', false);
177 }
178
179 // [EOF]
180 ?>