]> git.mxchange.org Git - mailer.git/blob - inc/modules/member/what-payout.php
SQL fix :(
[mailer.git] / inc / modules / member / what-payout.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         // Don't call this directly!
42         die();
43 } elseif (!isMember()) {
44         // Not logged in
45         redirectToIndexMemberOnlyModule();
46 }
47
48 // Add description as navigation point
49 addMenuDescription('member', __FILE__);
50
51 if ((!isExtensionActive('payout')) && (!isAdmin())) {
52         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('payout'));
53         return;
54 } // END - if
55
56 $result_depths = SQL_QUERY("SELECT level, percents FROM `{?_MYSQL_PREFIX?}_refdepths` ORDER BY level", __FILE__, __LINE__);
57 $totalPoints = 0;
58 while ($content = SQL_FETCHARRAY($result_depths)) {
59         // Load referal points
60         $result_points = SQL_QUERY_ESC("SELECT `points` FROM `{?_MYSQL_PREFIX?}_user_points` WHERE `userid`=%s AND `ref_depth`='%s' LIMIT 1",
61                 array(getUserId(), bigintval($content['level'])), __FILE__, __LINE__);
62
63         // Entry found?
64         if (SQL_NUMROWS($result_points) == 1) {
65                 // Load points
66                 list($points) = SQL_FETCHROW($result_points);
67
68                 // Add them to total
69                 $totalPoints += $points;
70         } // END - if
71
72         // Free result
73         SQL_FREERESULT($result_points);
74 }
75
76 // Free memory
77 SQL_FREERESULT($result_depths);
78
79 // Get used points
80 $usedPoints = countSumTotalData(getUserId(), 'user_data', 'used_points');
81
82 // Translate point into comma
83 $totalPoints = ($totalPoints - $usedPoints);
84
85 // Sanity check...
86 if (empty($totalPoints)) $totalPoints = '0.00000';
87
88 if (!isGetRequestElementSet('payout')) {
89         // Load payout types
90         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
91 FROM `{?_MYSQL_PREFIX?}_payout_types`
92 WHERE %s >= min_points
93 ORDER BY type ASC",
94                 array($totalPoints), __FILE__, __LINE__);
95         if (SQL_NUMROWS($result) > 0) {
96                 // Free memory
97                 SQL_FREERESULT($result);
98
99                 // Check for his payouts
100                 $result_payouts = SQL_QUERY_ESC("SELECT
101         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 AS alt, p.banner_url AS banner
102 FROM
103         `{?_MYSQL_PREFIX?}_user_payouts` AS p
104 LEFT JOIN
105         `{?_MYSQL_PREFIX?}_payout_types` AS t
106 ON
107         p.payout_id = t.id
108 WHERE
109         p.userid = %s
110 ORDER BY
111         p.payout_timestamp DESC",
112                         array(getUserId()), __FILE__, __LINE__);
113                 if (SQL_NUMROWS($result_payouts) > 0) {
114                         // List all his requests
115                         $OUT = ''; $SW = 2;
116                         while ($content = SQL_FETCHARRAY($result_payouts)) {
117                                 // Translate status
118                                 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
119                                 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
120
121                                 // Nothing entered must be secured in member/what-payputs.php !
122                                 if ($content['allow'] == 'Y') {
123                                         // Banner/Textlink views/clicks request
124                                         if (!empty($content['banner'])) {
125                                                 // Banner
126                                                 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
127                                         } else {
128                                                 // Textlink
129                                                 $content['target_account'] = $content['alt'];
130                                         }
131                                         $content['target_bank'] = "<a href=\"".generateDerefererUrl($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
132                                 } else {
133                                         // e-currency payout request
134                                         if (empty($content['target_account'])) $content['target_account'] = '---';
135                                         if (empty($content['target_bank']))    $content['target_bank']    = '---';
136                                 }
137
138                                 // Prepare data for the template
139                                 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
140                                 $content = array(
141                                         'sw'     => $SW,
142                                         'acc'    => $content['target_account'],
143                                         'points' => translateComma($content['payout_total']) . ' ' . $content['type'],
144                                         'bank'   => $content['target_bank'],
145                                         'stamp'  => generateDateTime($content['payout_timestamp'], 2),
146                                         'status' => $content['status']
147                                 );
148
149                                 // Load row template and switch colors
150                                 $OUT .= loadTemplate('member_payout_row', true, $content);
151                                 $SW = 3 - $SW;
152                         }
153
154                         // Load template
155                         loadTemplate('member_payout', false, $OUT);
156                 }
157
158                 // Free memory
159                 SQL_FREERESULT($result_payouts);
160
161                 // Output payout list
162                 outputPayoutList($totalPoints);
163         }
164 } else {
165         // Chedk if he can get paid by selected type
166         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{?_MYSQL_PREFIX?}_payout_types` WHERE `id`=%s LIMIT 1",
167                 array(bigintval(getRequestElement('payout'))), __FILE__, __LINE__);
168
169         if (SQL_NUMROWS($result) == 1) {
170                 // ID is valid so load the data
171                 $content = SQL_FETCHARRAY($result);
172
173                 // Calculate maximum value
174                 $max     = round($totalPoints * $content['rate'] - 0.5);
175                 $PAY_MAX = 0;
176
177                 // Calulcate points from submitted amount
178                 $points = 0;
179                 if (isPostRequestElementSet('payout')) {
180                         $points  = bigintval(postRequestElement('payout')) / $content['rate'];
181                         $PAY_MAX = $max / $content['rate'];
182                 }
183
184                 // Has enougth points to payout?
185                 if ($totalPoints >= $content['min_points']) {
186                         // Ok, he can get be paid
187                         if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
188                                 // Remember points in array
189                                 setRequestPostElement('payout_points', translateComma($points));
190                                 setRequestPostElement('type'         , $content['type']);
191
192                                 // Subtract points from member's account
193                                 subtractPoints('payout', getUserId(), $points);
194
195                                 // Add entry to his tranfer history
196                                 if ($content['allow'] == 'Y') {
197                                         // Banner/textlink ordered
198                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
199 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
200                                         array(
201                                                 getUserId(),
202                                                 bigintval(postRequestElement('payout')),
203                                                 bigintval(getRequestElement('payout')),
204                                                 postRequestElement('turl'),
205                                                 postRequestElement('alt'),
206                                                 postRequestElement('banner')
207                                         ), __FILE__, __LINE__);
208
209                                         // Load templates
210                                         $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getUserId());
211                                         if (getExtensionVersion('admins') >= '0.4.1') {
212                                                 $adm_tpl = 'admin_payout_request_banner';
213                                         } else {
214                                                 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getUserId());
215                                         }
216                                 } else {
217                                         // e-currency payout requested
218                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
219 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
220                                         array(
221                                                 getUserId(),
222                                                 bigintval(postRequestElement('payout')),
223                                                 bigintval(postRequestElement('account')),
224                                                 postRequestElement('bank'),
225                                                 bigintval(getRequestElement('payout')),
226                                                 postRequestElement('pass')
227                                         ), __FILE__, __LINE__);
228
229                                         // Load templates
230                                         $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getUserId());
231                                         $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getUserId());
232                                         $admin_tpl = '';
233
234                                         // @TODO Rewrite this to a filter
235                                         if (getExtensionVersion('admins') >= '0.4.1') {
236                                                 $admin_tpl = 'admin_payout_request';
237                                         } // END - if
238                                 }
239
240                                 // Generate task
241                                 createNewTask('[payout:] {--PAYOUT_REQUEST_ADMIN--}', $message_adm, 'PAYOUT_REQUEST', getUserId());
242
243                                 // Send out mails
244                                 sendEmail(getUserId(), getMessage('PAYOUT_REQUEST_MEMBER'), $message_mem);
245
246                                 // To admin(s)
247                                 sendAdminNotification(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, postRequestArray(), getUserId());
248
249                                 // Load template and output it
250                                 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_REQUEST_SENT'));
251                         } elseif ($content['allow'] == 'Y') {
252                                 // Prepare content
253                                 $content = array(
254                                         'max'    => $max,
255                                         'type'   => $content['type'],
256                                         'payout' => bigintval(getRequestElement('payout'))
257                                 );
258
259                                 // Generate banner order form
260                                 loadTemplate('member_payout_form_banner', false, $content);
261                         } else {
262                                 // Prepare content
263                                 $content = array(
264                                         'max'    => $max,
265                                         'type'   => $content['type'],
266                                         'payout' => bigintval(getRequestElement('payout'))
267                                 );
268
269                                 // Generate normal form
270                                 loadTemplate('member_payout_form', false, $content);
271                         }
272                 } else {
273                         // Not enougth points
274                         loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
275                 }
276         } else {
277                 // ID is invalid
278                 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_ID_INVALID'));
279         }
280
281         // Free result
282         SQL_FREERESULT($result);
283 }
284
285 // [EOF]
286 ?>