-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrefund_service_charge.php
211 lines (159 loc) · 5.74 KB
/
refund_service_charge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
include_once("includes/inc.global.php");
$p->site_section = ADMINISTRATION;
$p->page_title = _("Refund Service Charge");
// *** Starts main() ***
$cUser->MustBeLevel(2);
if (isset($_GET["TID"]) && is_numeric($_GET["TID"]))
{
$page = transfer_fee($_GET["TID"], $_GET["trade_time"]);
}
else if (isset($_GET["CID"]) && is_numeric($_GET["CID"]))
{
$page = confirmation($_GET["CID"], $_GET["selected_time"]);
}
else
{
$page = select_time();
}
$p->DisplayPage($page);
// *** Ends main() ***
function select_time()
{
global $cDB;
$system_account_id = SYSTEM_ACCOUNT_ID;
$ts = time();
$year = strftime("%Y", $ts);
$month = strftime("%m", $ts);
$trade_type = "S";
$refunded = TRADE_MONTHLY_FEE_REVERSAL;
// We'll show only the monthly fees taken during the last 12 months.
$date_year_ago = ($year - 1) . "-$month-01";
$sql = "select distinct(trade_date) from trades where
trade_date > '$date_year_ago' and type='$trade_type' and
member_id_to = '$system_account_id' and status != '$refunded'";
//echo $sql;
$result = $cDB->Query($sql);
$selection_list = "";
while ($row = mysql_fetch_object($result))
{
$selection_list .=
"<option value=\"$row->trade_date\">$row->trade_date</option>";
}
if (empty($selection_list))
{
return _("No service charges found.");
}
global $_;
$html = <<<ENDHTML
<form method="GET" action="">
<input type="hidden" name="CID" value="$ts" />
<select name="selected_time">
$selection_list
</select>
<input type="submit" value={$_("Refund")} />
</form>
ENDHTML;
return $html;
}
/*
* Displays a confirmation dialogue. Also shows a warning msg if monthly
* fee has been already taken this month.
*/
function confirmation($cid, $selected_time)
{
if( !defined("TAKE_SERVICE_FEE"))
{
return _("This system isn't setup to process service fees.");
}
if($ignore==true && isset($_SESSION["LAST_CID"]) && $cid <= $_SESSION["LAST_CID"])
{
return _("Action already executed. Start from the administration page for a new refund.");
}
else
{
$_SESSION["LAST_CID"] = $cid;
}
$ts = time();
global $_;
$html = <<<ENDHTML
{$_("You are about to refund the service charge taken on")}
<em>$selected_time</em>.
<form method="GET" action="">
<input type="hidden" name="TID" value="$ts" />
<input type="hidden" name="trade_time" value="$selected_time">
<input type="submit" value={$_("Refund now")} />
</form>
<p><strong>Or</strong></p>
<form method="GET" action="admin_menu.php">
<input type="submit" value={$_("Cancel")} />
</form>
ENDHTML;
return $html;
}
/*
* Does the actual fee transfer from member accounts to the system account.
*/
function transfer_fee($tid, $trade_time) {
global $cDB, $monthly_fee_exempt_list;
// Make sure this transaction has not been done before.
if(isset($_SESSION["LAST_TID"]) && $tid <= $_SESSION["LAST_TID"])
{
return _("Already transfered. Start from the administration page for a new transfer.");
}
else
{
// Store the current transaction id for later checks.
$_SESSION["LAST_TID"] = $tid;
}
$sql = "select * from trades where trade_date='$trade_time'";
//echo $sql;
$result = $cDB->Query($sql);
$row = mysql_fetch_array($result);
$monthly_fee = $row["amount"];
$system_account_id = SYSTEM_ACCOUNT_ID;
$member_table = DATABASE_MEMBERS;
$trade_table = DATABASE_TRADES;
$trade_type_monthly = TRADE_MONTHLY_FEE;
$trade_type = TRADE_MONTHLY_FEE_REVERSAL;
$desc = _("Refund for")." ".$row["description"]." "._("taken on")." $trade_time";
// Transaction starts.
$cDB->Query("BEGIN");
$trade_time = $cDB->EscTxt($trade_time);
// We don't want to charge inactive accounts.
$query0 = "select trade_id, member_id_from from " . DATABASE_TRADES .
" where trade_date = $trade_time
and type='S'
and member_id_to = '$system_account_id'";
$result0 = $cDB->Query($query0);
// This single timestamp will be applied to every transfer done in
// this transaction. This is for the ease of identification of this
// batch of transfer later.
while ($row = mysql_fetch_object($result0))
{
if ( !in_array($row->member_id, $monthly_fee_exempt_list)) {
// Category 12 is "Miscellaneous".
$query1 = "insert into $trade_table set member_id_to='".$row->member_id_from."',
member_id_from='$system_account_id', amount=$monthly_fee, category=12,
description='$desc', type='$trade_type', trade_date=$trade_time";
$result1 = $cDB->Query($query1);
$query2 = "update $member_table set balance = balance + $monthly_fee
where member_id = '".$row->member_id_from."'";
$result2 = $cDB->Query($query2);
// echo $query2."<br>";
$query3 = "update $member_table set balance = balance - $monthly_fee
where member_id = '$system_account_id'";
$result3 = $cDB->Query($query3);
$query4 = "update $trade_table set status = '$trade_type' where
trade_id = ".$row->trade_id."";
$result4 = $cDB->Query($query4);
if ( !( $result2 && $result3 && $result4))
{
$cDB->Query("rollback");
return _("Couldn't transfer.");
}
}
}
$cDB->Query("COMMIT");
return _("Done");
}