Removed comment introduced by Profi-Concept, this comment should fine (in a much...
[mailer.git] / inc / modules / member / what-payout.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 04/12/2004 *
4  * ===================                          Last change: 12/01/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-payout.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Payout requests                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auszahlungsanfragen                              *
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, 2010 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         // Don't call this directly!
41         die();
42 } elseif (!isMember()) {
43         // Not logged in
44         redirectToIndexMemberOnlyModule();
45 }
46
47 // Add description as navigation point
48 addMenuDescription('member', __FILE__);
49
50 if ((!isExtensionActive('payout')) && (!isAdmin())) {
51         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('payout'));
52         return;
53 } // END - if
54
55 // Translate point into comma
56 $totalPoints = getTotalPoints(getMemberId());
57
58 if (!isGetRequestParameterSet('payout')) {
59         // Load payout types
60         $result = SQL_QUERY_ESC("SELECT
61         `id`, `type`, `rate`, `min_points`, `allow_url`
62 FROM
63         `{?_MYSQL_PREFIX?}_payout_types`
64 WHERE
65         %s >= `min_points`
66 ORDER BY
67         `type` ASC",
68                 array($totalPoints), __FILE__, __LINE__);
69         if (!SQL_HASZERONUMS($result)) {
70                 // Free memory
71                 SQL_FREERESULT($result);
72
73                 // Check for his payouts
74                 $result_payouts = SQL_QUERY_ESC("SELECT
75         p.id, p.payout_total, p.target_account, p.target_bank, t.type, p.payout_timestamp, p.status, t.allow_url AS allow, p.target_url AS url, p.link_text, p.banner_url AS banner
76 FROM
77         `{?_MYSQL_PREFIX?}_user_payouts` AS p
78 LEFT JOIN
79         `{?_MYSQL_PREFIX?}_payout_types` AS t
80 ON
81         p.payout_id = t.id
82 WHERE
83         p.userid = %s
84 ORDER BY
85         p.payout_timestamp DESC",
86                         array(getMemberId()), __FILE__, __LINE__);
87                 if (!SQL_HASZERONUMS($result_payouts)) {
88                         // List all his requests
89                         $OUT = '';
90                         while ($content = SQL_FETCHARRAY($result_payouts)) {
91                                 // Nothing entered must be secured in member/what-payputs.php !
92                                 if ($content['allow'] == 'Y') {
93                                         // Banner/Textlink views/clicks request
94                                         if (!empty($content['banner'])) {
95                                                 // Banner
96                                                 $content['target_account'] = '<img src="' . $content['banner'] . '" alt="' . $content['link_text'] . '" title="' . $content['link_text'] . '" border="0" />';
97                                         } else {
98                                                 // Textlink
99                                                 $content['target_account'] = $content['link_text'];
100                                         }
101                                         $content['target_bank'] = '<a href="' . generateDerefererUrl($content['url']) . '" target="_blank">{--CLICK_HERE--}</a>';
102                                 } // END - if
103
104                                 // Prepare data for the template
105                                 $content['payout_timestamp'] = generateDateTime($content['payout_timestamp'], 2);
106
107                                 // Load row template and switch colors
108                                 $OUT .= loadTemplate('member_payout_row', true, $content);
109                         } // END - while
110
111                         // Load template
112                         loadTemplate('member_payout', false, $OUT);
113                 } // END - if
114
115                 // Free memory
116                 SQL_FREERESULT($result_payouts);
117
118                 // Output payout list
119                 outputPayoutList($totalPoints);
120         } else {
121                 // No payout types setup
122                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_SETUP_INCOMPLETE--}');
123         }
124 } else {
125         // Chedk if he can get paid by selected type
126         $result = SQL_QUERY_ESC("SELECT `type`, `rate`, `min_points`, `allow_url` AS allow FROM `{?_MYSQL_PREFIX?}_payout_types` WHERE `id`=%s LIMIT 1",
127                 array(bigintval(getRequestParameter('payout'))), __FILE__, __LINE__);
128
129         if (SQL_NUMROWS($result) == 1) {
130                 // id is valid so load the data
131                 $content = SQL_FETCHARRAY($result);
132
133                 // Calculate maximum value
134                 $max     = round($totalPoints * $content['rate'] - 0.5);
135                 $PAY_MAX = '0';
136
137                 // Calulcate points from submitted amount
138                 $points = '0';
139                 if (isPostRequestParameterSet('payout')) {
140                         $points  = bigintval(postRequestParameter('payout')) / $content['rate'];
141                         $PAY_MAX = $max / $content['rate'];
142                 }
143
144                 // Has enougth points to payout?
145                 if ($totalPoints >= $content['min_points']) {
146                         // Ok, he can get be paid
147                         if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
148                                 // Remember points in array
149                                 setPostRequestParameter('payout_points', translateComma($points));
150                                 setPostRequestParameter('type'         , $content['type']);
151
152                                 // Subtract points from member's account
153                                 subtractPoints('payout', getMemberId(), $points);
154
155                                 // Add entry to his tranfer history
156                                 if ($content['allow'] == 'Y') {
157                                         // Banner/textlink ordered
158                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
159 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
160                                         array(
161                                                 getMemberId(),
162                                                 bigintval(postRequestParameter('payout')),
163                                                 bigintval(getRequestParameter('payout')),
164                                                 postRequestParameter('turl'),
165                                                 postRequestParameter('link_text'),
166                                                 postRequestParameter('banner')
167                                         ), __FILE__, __LINE__);
168
169                                         // Load templates
170                                         $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getMemberId());
171                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
172                                                 $adm_tpl = 'admin_payout_request_banner';
173                                         } else {
174                                                 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getMemberId());
175                                         }
176                                 } else {
177                                         // e-currency payout requested
178                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
179 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
180                                         array(
181                                                 getMemberId(),
182                                                 bigintval(postRequestParameter('payout')),
183                                                 bigintval(postRequestParameter('account')),
184                                                 postRequestParameter('bank'),
185                                                 bigintval(getRequestParameter('payout')),
186                                                 postRequestParameter('password')
187                                         ), __FILE__, __LINE__);
188
189                                         // Load templates
190                                         $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getMemberId());
191                                         $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getMemberId());
192                                         $admin_tpl = '';
193
194                                         // @TODO Rewrite this to a filter
195                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
196                                                 $admin_tpl = 'admin_payout_request';
197                                         } // END - if
198                                 }
199
200                                 // Generate task (we ignore the task id here)
201                                 createNewTask('[payout:] {--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $message_adm, 'PAYOUT_REQUEST', getMemberId());
202
203                                 // Send out mails
204                                 sendEmail(getMemberId(), '{--MEMBER_PAYOUT_REQUEST_SUBJECT--}', $message_mem);
205
206                                 // To admin(s)
207                                 sendAdminNotification('{--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $admin_tpl, postRequestArray(), getMemberId());
208
209                                 // Load template and output it
210                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_REQUEST_SENT--}');
211                         } elseif ($content['allow'] == 'Y') {
212                                 // Prepare content
213                                 $content = array(
214                                         'max'    => $max,
215                                         'type'   => $content['type'],
216                                         'payout' => bigintval(getRequestParameter('payout'))
217                                 );
218
219                                 // Generate banner order form
220                                 loadTemplate('member_payout_form_banner', false, $content);
221                         } else {
222                                 // Prepare content
223                                 $content = array(
224                                         'max'    => $max,
225                                         'type'   => $content['type'],
226                                         'payout' => bigintval(getRequestParameter('payout'))
227                                 );
228
229                                 // Generate normal form
230                                 loadTemplate('member_payout_form', false, $content);
231                         }
232                 } else {
233                         // Not enougth points
234                         loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_POINTS_NOT_ENOUGTH--}');
235                 }
236         } else {
237                 // id is invalid
238                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_ID_INVALID--}');
239         }
240
241         // Free result
242         SQL_FREERESULT($result);
243 }
244
245 // [EOF]
246 ?>