25aa636c78b4d803a2f61833e8c60c9d604e5dcb
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         // Don't call this directly!
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 } elseif (!IS_MEMBER()) {
40         // Not logged in
41         LOAD_URL("modules.php?module=index");
42 } elseif ((!EXT_IS_ACTIVE("payout")) && (!IS_ADMIN())) {
43         // Extension "payout" is not active
44         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "payout");
45         return;
46 }
47
48 // Add description as navigation point
49 ADD_DESCR("member", basename(__FILE__));
50
51 $result_depths = SQL_QUERY("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths ORDER BY level", __FILE__, __LINE__);
52 $TPTS = 0;
53 while (list($lvl, $per) = SQL_FETCHROW($result_depths))
54 {
55         // Load referral points
56         $result_points = SQL_QUERY_ESC("SELECT points FROM "._MYSQL_PREFIX."_user_points WHERE userid=%s AND ref_depth=%d LIMIT 1",
57          array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
58         if (SQL_NUMROWS($result_points) == 1)
59         {
60                 list($points) = SQL_FETCHROW($result_points);
61                 SQL_FREERESULT($result_points);
62                 $TPTS += $points;
63         }
64 }
65
66 // Free memory
67 SQL_FREERESULT($result_depths);
68
69 // Get used points
70 $USED = GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
71
72 // Translate point into comma
73 $TPTS = TRANSLATE_COMMA($TPTS - $USED);
74
75 // Sanity check...
76 if (empty($TPTS)) $TPTS = "0.00000";
77
78 if (empty($_GET['payout']))
79 {
80         // Load payout types
81         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
82 FROM "._MYSQL_PREFIX."_payout_types
83 WHERE %s >= min_points
84 ORDER BY type", array(REVERT_COMMA($TPTS)), __FILE__, __LINE__);
85         if (SQL_NUMROWS($result) > 0)
86         {
87                 // Free memory
88                 SQL_FREERESULT($result);
89
90                 // Check for his payouts
91                 $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 bannerm
92 FROM "._MYSQL_PREFIX."_user_payouts AS p
93 LEFT JOIN "._MYSQL_PREFIX."_payout_types AS t
94 ON p.payout_id = t.id
95 WHERE p.userid = %s
96 ORDER BY p.payout_timestamp DESC",
97                  array($GLOBALS['userid']), __FILE__, __LINE__);
98                 if (SQL_NUMROWS($result_payouts) > 0)
99                 {
100                         // List all his requests
101                         $SW = 2; $OUT = "";
102                         while (list($pid, $total, $account, $bank, $type, $tstamp, $status, $allow, $url, $alt, $banner) = SQL_FETCHROW($result_payouts))
103                         {
104                                 // Translate status
105                                 $evl = "\$status = PAYOUT_MEMBER_STATUS_".strtoupper($status).";";
106                                 eval($evl);
107                                 $status = "<FONT class=\"member_failed\">".$status."</FONT>";
108
109                                 // Nothing entered must be secured in member/what-payputs.php !
110                                 if ($allow == "Y")
111                                 {
112                                         // Banner/Textlink views/clicks request
113                                         if (!empty($banner))
114                                         {
115                                                 // Banner
116                                                 $account = "<IMG src=\"".$banner."\" alt=\"".$alt."\" title=\"".$alt."\" border=\"0\">";
117                                         }
118                                          else
119                                         {
120                                                 // Textlink
121                                                 $account = $alt;
122                                         }
123                                         $bank = "<A href=\"".DEREFERER($url)."\" target=\"_blank\">".CLICK_HERE."</A>";
124                                 }
125                                  else
126                                 {
127                                         // e-currency payout request
128                                         if (empty($account)) $account = "---";
129                                         if (empty($bank))    $bank    = "---";
130                                 }
131
132                                 // Prepare data for the template
133                                 $content = array(
134                                         'sw'     => $SW,
135                                         'acc'    => $account,
136                                         'points' => TRANSLATE_COMMA($total)." ".COMPILE_CODE($type),
137                                         'bank'   => $bank,
138                                         'stamp'  => MAKE_DATETIME($tstamp, "2"),
139                                         'status' => $status
140                                 );
141                                 // Load row template and switch colors
142                                 $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
143                                 $SW = 3 - $SW;
144                         }
145
146                         // Remember rows in constant
147                         define('__PAYOUT_ROWS', $OUT);
148
149                         // Load template
150                         LOAD_TEMPLATE("member_payout");
151                 }
152
153                 // Free memory
154                 SQL_FREERESULT($result_payouts);
155
156                 // Output payout list
157                 PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
158         }
159 }
160  else
161 {
162         // Chedk if he can get paid by selected type
163         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url FROM "._MYSQL_PREFIX."_payout_types WHERE id=%s LIMIT 1",
164          array(bigintval($_GET['payout'])), __FILE__, __LINE__);
165
166         if (SQL_NUMROWS($result) == 1)
167         {
168                 // ID is valid
169                 list($type, $rate, $min, $allow) = SQL_FETCHROW($result);
170                 SQL_FREERESULT($result);
171
172                 // Calculate maximum value
173                 $max = round($TPTS * $rate - 0.5);
174
175                 // Calulcate points from submitted amount
176                 $PAYOUT = 0;
177                 if (!empty($_POST['payout']))
178                 {
179                         $PAYOUT  = bigintval($_POST['payout']) / $rate;
180                         $PAY_MAX = $max / $rate;
181                 }
182
183                 // Move variables into constants for templates
184                 define('PAYOUT_MAX_VALUE' , $max);
185                 define('PAYOUT_TYPE_VALUE', COMPILE_CODE($type));
186
187                 if (REVERT_COMMA($TPTS) >= $min)
188                 {
189                         // Ok, he can get be paid
190                         if ((isset($_POST['ok'])) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $min))
191                         {
192                                 // Calculate exact value
193                                 define('PAYOUT_POINTS_VALUE', $PAYOUT);
194
195                                 // Subtract points from member's account
196                                 SUB_POINTS($GLOBALS['userid'], $PAYOUT);
197
198                                 // Add entry to his tranfer history
199                                 if ($allow == "Y")
200                                 {
201                                         // Banner/textlink ordered
202                                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
203 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
204  array(
205         $GLOBALS['userid'],
206         bigintval($_POST['payout']),
207         bigintval($_GET['payout']),
208         $_POST['turl'],
209         $_POST['alt'],
210         $_POST['banner']
211 ), __FILE__, __LINE__);
212
213                                         // Load templates
214                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), $GLOBALS['userid']);
215                                         if (GET_EXT_VERSION("admins") >= "0.4.1")
216                                         {
217                                                 $adm_tpl = "admin_payout_request_banner";
218                                         }
219                                          else
220                                         {
221                                                 $msg_adm = addslashes(LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), $GLOBALS['userid']));
222                                         }
223                                 }
224                                  else
225                                 {
226                                         // e-currency payout requested
227                                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
228 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
229  array(
230         $GLOBALS['userid'],
231         bigintval($_POST['payout']),
232         bigintval($_POST['account']),
233         $_POST['bank'],
234         bigintval($_GET['payout']),
235         $_POST['pass']
236 ), __FILE__, __LINE__);
237
238                                         // Load templates
239                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), $GLOBALS['userid']);
240                                         $msg_adm = addslashes(LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), $GLOBALS['userid']));
241                                         $admin_tpl = "";
242                                         if (GET_EXT_VERSION("admins") >= "0.4.1")
243                                         {
244                                                 $admin_tpl = "admin_payout_request";
245                                         }
246                                 }
247
248                                 // Generate task
249                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, status, task_type, subject, text, task_created, userid)
250 VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] ".PAYOUT_REQUEST_ADMIN."','%s', UNIX_TIMESTAMP(), %s)",
251  array(
252         $msg_adm,
253         $GLOBALS['userid']
254 ), __FILE__, __LINE__);
255
256                                 // Send out mails
257                                 SEND_EMAIL($GLOBALS['userid'], PAYOUT_REQUEST_MEMBER, $msg_mem);
258
259                                 // To admin(s)
260                                 SEND_ADMIN_NOTIFICATION(PAYOUT_REQUEST_ADMIN, $admin_tpl, array(), $GLOBALS['userid']);
261
262                                 // Load template and output it
263                                 LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_REQUEST_SENT);
264                         }
265                          elseif ($allow == "Y")
266                         {
267                                 // Generate banner order form
268                                 LOAD_TEMPLATE("member_payout_form_banner");
269                         }
270                          else
271                         {
272                                 // Generate normal form
273                                 LOAD_TEMPLATE("member_payout_form");
274                         }
275                 }
276                  else
277                 {
278                         // Not enougth points
279                         LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_POINTS_NOT_ENOUGTH);
280                 }
281         }
282          else
283         {
284                 // ID is invalid
285                 LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_ID_INVALID);
286         }
287 }
288 //
289 ?>