2 /************************************************************************
3 * MXChange v0.2.1 Start: 04/12/2004 *
4 * ================ Last change: 12/01/2008 *
6 * -------------------------------------------------------------------- *
7 * File : what-payout.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Payout requests *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Auszahlungsanfragen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 // Don't call this directly!
43 } elseif (!isMember()) {
45 redirectToIndexMemberOnlyModule();
48 // Add description as navigation point
49 addMenuDescription('member', __FILE__);
51 if ((!isExtensionActive('payout')) && (!isAdmin())) {
52 loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('payout'));
56 $result_depths = SQL_QUERY("SELECT `level`, `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` ORDER BY level", __FILE__, __LINE__);
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__);
64 if (SQL_NUMROWS($result_points) == 1) {
66 list($points) = SQL_FETCHROW($result_points);
69 $totalPoints += $points;
73 SQL_FREERESULT($result_points);
77 SQL_FREERESULT($result_depths);
80 $usedPoints = countSumTotalData(getUserId(), 'user_data', 'used_points');
82 // Translate point into comma
83 $totalPoints = ($totalPoints - $usedPoints);
86 if (empty($totalPoints)) $totalPoints = '0.00000';
88 if (!isGetRequestElementSet('payout')) {
90 $result = SQL_QUERY_ESC("SELECT
91 `id`, `type`, `rate`, `min_points`, `allow_url`
93 `{?_MYSQL_PREFIX?}_payout_types`
98 array($totalPoints), __FILE__, __LINE__);
99 if (SQL_NUMROWS($result) > 0) {
101 SQL_FREERESULT($result);
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
107 `{?_MYSQL_PREFIX?}_user_payouts` AS p
109 `{?_MYSQL_PREFIX?}_payout_types` AS t
115 p.payout_timestamp DESC",
116 array(getUserId()), __FILE__, __LINE__);
117 if (SQL_NUMROWS($result_payouts) > 0) {
118 // List all his requests
120 while ($content = SQL_FETCHARRAY($result_payouts)) {
122 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
123 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
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'])) {
130 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
133 $content['target_account'] = $content['alt'];
135 $content['target_bank'] = "<a href=\"".generateDerefererUrl($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
137 // e-currency payout request
138 if (empty($content['target_account'])) $content['target_account'] = '---';
139 if (empty($content['target_bank'])) $content['target_bank'] = '---';
142 // Prepare data for the template
143 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
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']
153 // Load row template and switch colors
154 $OUT .= loadTemplate('member_payout_row', true, $content);
159 loadTemplate('member_payout', false, $OUT);
163 SQL_FREERESULT($result_payouts);
165 // Output payout list
166 outputPayoutList($totalPoints);
168 // No payout types setup
169 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_NO_PAYOUT_TYPES'));
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__);
176 if (SQL_NUMROWS($result) == 1) {
177 // ID is valid so load the data
178 $content = SQL_FETCHARRAY($result);
180 // Calculate maximum value
181 $max = round($totalPoints * $content['rate'] - 0.5);
184 // Calulcate points from submitted amount
186 if (isPostRequestElementSet('payout')) {
187 $points = bigintval(postRequestElement('payout')) / $content['rate'];
188 $PAY_MAX = $max / $content['rate'];
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']);
199 // Subtract points from member's account
200 subtractPoints('payout', getUserId(), $points);
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')",
209 bigintval(postRequestElement('payout')),
210 bigintval(getRequestElement('payout')),
211 postRequestElement('turl'),
212 postRequestElement('alt'),
213 postRequestElement('banner')
214 ), __FILE__, __LINE__);
217 $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getUserId());
218 if (getExtensionVersion('admins') >= '0.4.1') {
219 $adm_tpl = 'admin_payout_request_banner';
221 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getUserId());
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')",
229 bigintval(postRequestElement('payout')),
230 bigintval(postRequestElement('account')),
231 postRequestElement('bank'),
232 bigintval(getRequestElement('payout')),
233 postRequestElement('pass')
234 ), __FILE__, __LINE__);
237 $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getUserId());
238 $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getUserId());
241 // @TODO Rewrite this to a filter
242 if (getExtensionVersion('admins') >= '0.4.1') {
243 $admin_tpl = 'admin_payout_request';
248 createNewTask('[payout:] {--PAYOUT_REQUEST_ADMIN--}', $message_adm, 'PAYOUT_REQUEST', getUserId());
251 sendEmail(getUserId(), getMessage('PAYOUT_REQUEST_MEMBER'), $message_mem);
254 sendAdminNotification(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, postRequestArray(), getUserId());
256 // Load template and output it
257 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_REQUEST_SENT'));
258 } elseif ($content['allow'] == 'Y') {
262 'type' => $content['type'],
263 'payout' => bigintval(getRequestElement('payout'))
266 // Generate banner order form
267 loadTemplate('member_payout_form_banner', false, $content);
272 'type' => $content['type'],
273 'payout' => bigintval(getRequestElement('payout'))
276 // Generate normal form
277 loadTemplate('member_payout_form', false, $content);
280 // Not enougth points
281 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
285 loadTemplate('admin_settings_saved', false, getMessage('PAYOUT_ID_INVALID'));
289 SQL_FREERESULT($result);