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