config.php partly solved, see #117
[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 - 2008 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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
43         require($INC);
44 } elseif (!IS_MEMBER()) {
45         // Not logged in
46         redirectToUrl('modules.php?module=index');
47 } elseif ((!EXT_IS_ACTIVE('payout')) && (!IS_ADMIN())) {
48         addFatalMessage(__FILE__, __LINE__, sprintf(getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), 'payout'));
49         return;
50 }
51
52 // Add description as navigation point
53 ADD_DESCR('member', __FILE__);
54
55 $result_depths = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` ORDER BY level", __FILE__, __LINE__);
56 $TPTS = 0;
57 while ($content = SQL_FETCHARRAY($result_depths)) {
58         // Load referal points
59         $result_points = SQL_QUERY_ESC("SELECT points FROM `{!_MYSQL_PREFIX_user_points!}` WHERE userid=%s AND ref_depth=%d LIMIT 1",
60                 array(getUserId(), bigintval($content['level'])), __FILE__, __LINE__);
61
62         // Entry found?
63         if (SQL_NUMROWS($result_points) == 1) {
64                 // Load points
65                 list($points) = SQL_FETCHROW($result_points);
66
67                 // Add them to total
68                 $TPTS += $points;
69         }
70
71         // Free result
72         SQL_FREERESULT($result_points);
73 }
74
75 // Free memory
76 SQL_FREERESULT($result_depths);
77
78 // Get used points
79 $USED = GET_TOTAL_DATA(getUserId(), "user_data", "used_points");
80
81 // Translate point into comma
82 $TPTS = translateComma($TPTS - $USED);
83
84 // Sanity check...
85 if (empty($TPTS)) $TPTS = "0.00000";
86
87 if (!REQUEST_ISSET_GET(('payout'))) {
88         // Load payout types
89         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
90 FROM `{!_MYSQL_PREFIX!}_payout_types`
91 WHERE %s >= min_points
92 ORDER BY type ASC",
93                 array(convertCommaToDot($TPTS)), __FILE__, __LINE__);
94         if (SQL_NUMROWS($result) > 0) {
95                 // Free memory
96                 SQL_FREERESULT($result);
97
98                 // Check for his payouts
99                 $result_payouts = SQL_QUERY_ESC("SELECT DISTINCT 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
100 FROM `{!_MYSQL_PREFIX!}_user_payouts` AS p
101 LEFT JOIN `{!_MYSQL_PREFIX!}_payout_types` AS t
102 ON p.payout_id = t.id
103 WHERE p.userid = %s
104 ORDER BY p.payout_timestamp DESC",
105                  array(getUserId()), __FILE__, __LINE__);
106                 if (SQL_NUMROWS($result_payouts) > 0) {
107                         // List all his requests
108                         $OUT = ''; $SW = 2;
109                         while ($content = SQL_FETCHARRAY($result_payouts)) {
110                                 // Translate status
111                                 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
112                                 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
113
114                                 // Nothing entered must be secured in member/what-payputs.php !
115                                 if ($content['allow'] == 'Y') {
116                                         // Banner/Textlink views/clicks request
117                                         if (!empty($content['banner'])) {
118                                                 // Banner
119                                                 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
120                                         } else {
121                                                 // Textlink
122                                                 $content['target_account'] = $content['alt'];
123                                         }
124                                         $content['target_bank'] = "<a href=\"".DEREFERER($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
125                                 } else {
126                                         // e-currency payout request
127                                         if (empty($content['target_account'])) $content['target_account'] = '---';
128                                         if (empty($content['target_bank']))    $content['target_bank']    = '---';
129                                 }
130
131                                 // Prepare data for the template
132                                 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
133                                 $content = array(
134                                         'sw'     => $SW,
135                                         'acc'    => $content['target_account'],
136                                         'points' => translateComma($content['payout_total'])." ".COMPILE_CODE($content['type']),
137                                         'bank'   => $content['target_bank'],
138                                         'stamp'  => generateDateTime($content['payout_timestamp'], '2'),
139                                         'status' => $content['status']
140                                 );
141
142                                 // Load row template and switch colors
143                                 $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
144                                 $SW = 3 - $SW;
145                         }
146
147                         // Remember rows in constant
148                         // @TODO Rewrite this constant
149                         define('__PAYOUT_ROWS', $OUT);
150
151                         // Load template
152                         LOAD_TEMPLATE("member_payout");
153                 }
154
155                 // Free memory
156                 SQL_FREERESULT($result_payouts);
157
158                 // Output payout list
159                 PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
160         }
161 } else {
162         // Chedk if he can get paid by selected type
163         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{!_MYSQL_PREFIX!}_payout_types` WHERE `id`=%s LIMIT 1",
164                 array(bigintval(REQUEST_GET('payout'))), __FILE__, __LINE__);
165
166         if (SQL_NUMROWS($result) == 1) {
167                 // ID is valid so load the data
168                 $content = SQL_FETCHARRAY($result);
169                 SQL_FREERESULT($result);
170
171                 // Calculate maximum value
172                 $max = round($TPTS * $content['rate'] - 0.5);
173
174                 // Calulcate points from submitted amount
175                 $PAYOUT = 0;
176                 if (REQUEST_ISSET_POST(('payout'))) {
177                         $PAYOUT  = bigintval(REQUEST_POST('payout')) / $content['rate'];
178                         $PAY_MAX = $max / $content['rate'];
179                 }
180
181                 // Move variables into constants for templates
182                 // @TODO Rewrite these constants
183                 define('PAYOUT_MAX_VALUE' , $max);
184                 define('PAYOUT_TYPE_VALUE', COMPILE_CODE($content['type']));
185
186                 if (convertCommaToDot($TPTS) >= $content['min_points']) {
187                         // Ok, he can get be paid
188                         if ((IS_FORM_SENT()) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $content['min_points'])) {
189                                 // Calculate exact value
190                                 // @TODO Rewrite this constant
191                                 define('PAYOUT_POINTS_VALUE', $PAYOUT);
192
193                                 // Subtract points from member's account
194                                 SUB_POINTS('payout', getUserId(), $PAYOUT);
195
196                                 // Add entry to his tranfer history
197                                 if ($content['allow'] == 'Y') {
198                                         // Banner/textlink ordered
199                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
200 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
201  array(
202         getUserId(),
203         bigintval(REQUEST_POST('payout')),
204         bigintval(REQUEST_GET('payout')),
205         REQUEST_POST('turl'),
206         REQUEST_POST('alt'),
207         REQUEST_POST('banner')
208 ), __FILE__, __LINE__);
209
210                                         // Load templates
211                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), getUserId());
212                                         if (GET_EXT_VERSION('admins') >= '0.4.1') {
213                                                 $adm_tpl = "admin_payout_request_banner";
214                                         } else {
215                                                 $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), getUserId());
216                                         }
217                                 } else {
218                                         // e-currency payout requested
219                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
220 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
221  array(
222         getUserId(),
223         bigintval(REQUEST_POST('payout')),
224         bigintval(REQUEST_POST('account')),
225         REQUEST_POST('bank'),
226         bigintval(REQUEST_GET('payout')),
227         REQUEST_POST('pass')
228 ), __FILE__, __LINE__);
229
230                                         // Load templates
231                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), getUserId());
232                                         $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), getUserId());
233                                         $admin_tpl = '';
234
235                                         // @TODO Rewrite this to a filter
236                                         if (GET_EXT_VERSION('admins') >= '0.4.1') {
237                                                 $admin_tpl = "admin_payout_request";
238                                         }
239                                 }
240
241                                 // Generate task
242                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, status, task_type, subject, text, task_created, userid)
243 VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] {--PAYOUT_REQUEST_ADMIN--}','%s', UNIX_TIMESTAMP(), %s)",
244  array(
245         $msg_adm,
246         getUserId()
247 ), __FILE__, __LINE__);
248
249                                 // Send out mails
250                                 sendEmail(getUserId(), getMessage('PAYOUT_REQUEST_MEMBER'), $msg_mem);
251
252                                 // To admin(s)
253                                 sendAdminNotification(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, array(), getUserId());
254
255                                 // Load template and output it
256                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_REQUEST_SENT'));
257                         } elseif ($content['allow'] == 'Y') {
258                                 // Prepare content
259                                 $content = array(
260                                         'payout' => bigintval(REQUEST_GET('payout'))
261                                 );
262
263                                 // Generate banner order form
264                                 LOAD_TEMPLATE("member_payout_form_banner", false, $content);
265                         } else {
266                                 // Prepare content
267                                 $content = array(
268                                         'payout' => bigintval(REQUEST_GET('payout'))
269                                 );
270
271                                 // Generate normal form
272                                 LOAD_TEMPLATE("member_payout_form", false, $content);
273                         }
274                 } else {
275                         // Not enougth points
276                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
277                 }
278         } else {
279                 // ID is invalid
280                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_ID_INVALID'));
281         }
282 }
283
284 //
285 ?>