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