Huge rewrite of default parameters, ext-network continued:
[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(getMemberId(), 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 } // END - while
75
76 // Free memory
77 SQL_FREERESULT($result_depths);
78
79 // Get used points
80 $usedPoints = countSumTotalData(getMemberId(), '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
91         `id`, `type`, `rate`, `min_points`, `allow_url`
92 FROM
93         `{?_MYSQL_PREFIX?}_payout_types`
94 WHERE
95         %s >= `min_points`
96 ORDER BY
97         `type` ASC",
98                 array($totalPoints), __FILE__, __LINE__);
99         if (SQL_NUMROWS($result) > 0) {
100                 // Free memory
101                 SQL_FREERESULT($result);
102
103                 // Check for his payouts
104                 $result_payouts = SQL_QUERY_ESC("SELECT
105         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
106 FROM
107         `{?_MYSQL_PREFIX?}_user_payouts` AS p
108 LEFT JOIN
109         `{?_MYSQL_PREFIX?}_payout_types` AS t
110 ON
111         p.payout_id = t.id
112 WHERE
113         p.userid = %s
114 ORDER BY
115         p.payout_timestamp DESC",
116                         array(getMemberId()), __FILE__, __LINE__);
117                 if (SQL_NUMROWS($result_payouts) > 0) {
118                         // List all his requests
119                         $OUT = ''; $SW = 2;
120                         while ($content = SQL_FETCHARRAY($result_payouts)) {
121                                 // Translate status
122                                 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
123                                 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
124
125                                 // Nothing entered must be secured in member/what-payputs.php !
126                                 if ($content['allow'] == 'Y') {
127                                         // Banner/Textlink views/clicks request
128                                         if (!empty($content['banner'])) {
129                                                 // Banner
130                                                 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
131                                         } else {
132                                                 // Textlink
133                                                 $content['target_account'] = $content['alt'];
134                                         }
135                                         $content['target_bank'] = "<a href=\"".generateDerefererUrl($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
136                                 } else {
137                                         // e-currency payout request
138                                         if (empty($content['target_account'])) $content['target_account'] = '---';
139                                         if (empty($content['target_bank']))    $content['target_bank']    = '---';
140                                 }
141
142                                 // Prepare data for the template
143                                 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
144                                 $content = array(
145                                         'sw'     => $SW,
146                                         'acc'    => $content['target_account'],
147                                         'points' => translateComma($content['payout_total']) . ' ' . $content['type'],
148                                         'bank'   => $content['target_bank'],
149                                         'stamp'  => generateDateTime($content['payout_timestamp'], 2),
150                                         'status' => $content['status']
151                                 );
152
153                                 // Load row template and switch colors
154                                 $OUT .= loadTemplate('member_payout_row', true, $content);
155                                 $SW = 3 - $SW;
156                         }
157
158                         // Load template
159                         loadTemplate('member_payout', false, $OUT);
160                 }
161
162                 // Free memory
163                 SQL_FREERESULT($result_payouts);
164
165                 // Output payout list
166                 outputPayoutList($totalPoints);
167         } else {
168                 // No payout types setup
169                 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_NO_PAYOUT_TYPES'));
170         }
171 } else {
172         // Chedk if he can get paid by selected type
173         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{?_MYSQL_PREFIX?}_payout_types` WHERE `id`=%s LIMIT 1",
174                 array(bigintval(getRequestElement('payout'))), __FILE__, __LINE__);
175
176         if (SQL_NUMROWS($result) == 1) {
177                 // id is valid so load the data
178                 $content = SQL_FETCHARRAY($result);
179
180                 // Calculate maximum value
181                 $max     = round($totalPoints * $content['rate'] - 0.5);
182                 $PAY_MAX = '0';
183
184                 // Calulcate points from submitted amount
185                 $points = '0';
186                 if (isPostRequestElementSet('payout')) {
187                         $points  = bigintval(postRequestElement('payout')) / $content['rate'];
188                         $PAY_MAX = $max / $content['rate'];
189                 }
190
191                 // Has enougth points to payout?
192                 if ($totalPoints >= $content['min_points']) {
193                         // Ok, he can get be paid
194                         if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
195                                 // Remember points in array
196                                 setRequestPostElement('payout_points', translateComma($points));
197                                 setRequestPostElement('type'         , $content['type']);
198
199                                 // Subtract points from member's account
200                                 subtractPoints('payout', getMemberId(), $points);
201
202                                 // Add entry to his tranfer history
203                                 if ($content['allow'] == 'Y') {
204                                         // Banner/textlink ordered
205                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
206 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
207                                         array(
208                                                 getMemberId(),
209                                                 bigintval(postRequestElement('payout')),
210                                                 bigintval(getRequestElement('payout')),
211                                                 postRequestElement('turl'),
212                                                 postRequestElement('alt'),
213                                                 postRequestElement('banner')
214                                         ), __FILE__, __LINE__);
215
216                                         // Load templates
217                                         $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getMemberId());
218                                         if (getExtensionVersion('admins') >= '0.4.1') {
219                                                 $adm_tpl = 'admin_payout_request_banner';
220                                         } else {
221                                                 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getMemberId());
222                                         }
223                                 } else {
224                                         // e-currency payout requested
225                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
226 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
227                                         array(
228                                                 getMemberId(),
229                                                 bigintval(postRequestElement('payout')),
230                                                 bigintval(postRequestElement('account')),
231                                                 postRequestElement('bank'),
232                                                 bigintval(getRequestElement('payout')),
233                                                 postRequestElement('pass')
234                                         ), __FILE__, __LINE__);
235
236                                         // Load templates
237                                         $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getMemberId());
238                                         $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getMemberId());
239                                         $admin_tpl = '';
240
241                                         // @TODO Rewrite this to a filter
242                                         if (getExtensionVersion('admins') >= '0.4.1') {
243                                                 $admin_tpl = 'admin_payout_request';
244                                         } // END - if
245                                 }
246
247                                 // Generate task
248                                 createNewTask('[payout:] {--PAYOUT_REQUEST_ADMIN--}', $message_adm, 'PAYOUT_REQUEST', getMemberId());
249
250                                 // Send out mails
251                                 sendEmail(getMemberId(), getMessage('PAYOUT_REQUEST_MEMBER'), $message_mem);
252
253                                 // To admin(s)
254                                 sendAdminNotification(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, postRequestArray(), getMemberId());
255
256                                 // Load template and output it
257                                 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_REQUEST_SENT'));
258                         } elseif ($content['allow'] == 'Y') {
259                                 // Prepare content
260                                 $content = array(
261                                         'max'    => $max,
262                                         'type'   => $content['type'],
263                                         'payout' => bigintval(getRequestElement('payout'))
264                                 );
265
266                                 // Generate banner order form
267                                 loadTemplate('member_payout_form_banner', false, $content);
268                         } else {
269                                 // Prepare content
270                                 $content = array(
271                                         'max'    => $max,
272                                         'type'   => $content['type'],
273                                         'payout' => bigintval(getRequestElement('payout'))
274                                 );
275
276                                 // Generate normal form
277                                 loadTemplate('member_payout_form', false, $content);
278                         }
279                 } else {
280                         // Not enougth points
281                         loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
282                 }
283         } else {
284                 // id is invalid
285                 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_ID_INVALID'));
286         }
287
288         // Free result
289         SQL_FREERESULT($result);
290 }
291
292 // [EOF]
293 ?>