forked from SecareLupus/fc_pos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiftcert.inc
255 lines (235 loc) · 9.01 KB
/
giftcert.inc
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* @file giftcert.inc
* @brief giftcert.inc is a library used for selling and converting gift certificates.
*
* @todo This file uses some functions, and they should be noted, even if the
* file doesn't include or require them.
*
* @link http://www.worldsapartgames.org/fc/index.php @endlink
*
* @author Michael Whitehouse
* @author Creidieki Crouch
* @author Desmond Duval
* @copyright 2009-2014 Pioneer Valley Gaming Collective
* @version 1.8d
* @since Project has existed since time immemorial.
*/
/**
* giftCertBalance takes a gift certificate ID, and returns the balance
* remaining on that gift certificate. If there is no cert with that ID,
* it will return false.
* @param int $ID is the ID of the gift certificate to be tested.
* @retval float Returns the balance on the certificate, or false, if there
* is no certificate at that ID, returns false instead.
*/
function giftCertBalance($ID)
{
$cxn = open_stream();
$sql = "SELECT SUM(amount) FROM giftCerts WHERE giftCertID='$ID'";
$result = query($cxn, $sql);
$row = mysqli_fetch_row($result);
return($row[0]);
}
/**
* giftCertToAccount allows members to inject their gift certificate
* balance directly into their store credit balance.
* @param int $giftCert is the ID of the gift certificate.
* @param int $member in the selected member's ID.
* @retval boolean Returns true if the gift certificate is successfully
* converted to the member's store credit, and false otherwise.
*/
function giftCertToAccount($giftCert, $member)
{
$cxn = open_stream();
$balance = giftCertBalance($giftCert);
$unbalance = -$balance;
if ($balance > 0) {
$stmt = $cxn->prepare(
"INSERT INTO giftCerts (giftCertID, transactionID, whenAcct, "
. "amount) VALUES (?, ?, NOW(), ?)"
);
$amount = -$amount;
$stmt->bind_param(
"iid", $giftCert, $_SESSION['transaction_ID'], $unbalance
);
if ($stmt->execute()) {
accountTransact(
$member, $balance, $_SESSION['transaction_ID'],
"Redeem gift cert #$giftCert"
);
return true;
} else {
displayError("Failed to create Gift Certificate transaction. Error #1");
return false;
}
} else {
return false;
}
}
/**
* showAllGiftCerts prints a list of all gift certificates in the system, and
* all relavent information for each.
*/
function showAllGiftCerts()
{
$giftCert = new giftCert;
//This doesn't work like you think it does...
for ($giftCert->certNum = 1; $giftCert->displayInfo(); $giftCert->certNum++);
}
/**
* giftCert is a class for managing gift certificates consistently.
*
* @author Michael Whitehouse
* @author Creidieki Crouch
* @author Desmond Duval
* @copyright 2009-2014 Pioneer Valley Gaming Collective
* @since Project has existed since time immemorial.
*/
class giftCert
{
var $name;
var $newCert;
var $notes;
var $spentCert;
var $certNum;
/**
* Constructor for GiftCert objects.
*/
function giftCert()
{
$this->name = (checkName($_POST['GCname'])) ? $_POST['GCname'] : '';
$this->newCert = greaterThanZero($_POST['GCnewCert']);
$this->certNum = greaterThanZero($_POST['GCcertNum']);
$this->notes = (checkName($_POST['GCnotes'])) ? $_POST['GCnotes'] : '';
}
/**
* displayForm prints a form which is compatible with the
* constructor giftCert(). Designed for register.php
*/
function displayForm()
{
echo "<b>Purchase Gift Certificate</b><br>
Recipient: <input type='text' name='GCname' value='" . $this->name . "' size=20 maxlength=40><br>
Value: \$<input type='text' name='GCnewCert' value='" . moneyND($this->newCert) . "' size=7 maxlength=7><br>";
}
/**
* displayInfo prints all relavent information for a given gift
* certificate to the page on which it is called.
* @retval boolean Returns true if successful, false otherwise.
*/
function displayInfo()
{
$cxn = open_stream();
$balance = giftCertBalance($this->certNum);
if ($balance) {
$sql = "SELECT * FROM giftCerts WHERE giftCertID='" . $this->certNum . "'";
if ($result = query($cxn, $sql)) {
// the first one is done differently because it is where it is set up
$row = mysqli_fetch_assoc($result);
extract($row);
echo "<b>Gift Certificate #" . $giftCertID . " details</b><br>
Recipient: $name<br>
Starting Value: " . money($amount) . "<br>";
// now we show any transactions if any
$first = true;
while ($row = mysqli_fetch_assoc($result)) {
if ($first) {
echo "Previous Gift Certificate Uses:<br>
<table border><tr><td>Date</td><td>Amount Spent</td></tr>";
$first=false;
}
extract($row);
$date = date_create("$whenAcct");
echo "<tr><td>" . $date->format("F jS, Y, g:i A")
. "</td><td>" . money(-$amount) . "</td></tr>";
} // end while
if (!$first) {
echo "</table><br>";
}
echo "Remaining Balance: " . money($balance) . "<p>";
} else {
displayError(
"Unable to retrieve Gift Certificate info<br>
SQL: $sql<br>
SQL Error" . $cxn->error
);
return false;
}
} else {
echo "<font color=RED>There is no gift certificate #" . $this->certNum . "<p>";
return false;
}
}
/**
* displaySelectForm prints a form for use in redeeming gift certificates.
*/
function displaySelectForm()
{
echo "Redeem Gift Certificate #: <input type='text' name='GCcertNum' size=3 maxlength=3 value='" . $this->certNum . "'><br>";
}
/**
* redeem takes an amount, and reduces the gift certificate's balance
* by that amount.
* @param float $amount is the amount being deducted from the balance.
* @retval boolean Returns true if amount is deducted, false otherwise.
*/
function redeem($amount)
{
$cxn = open_stream();
$balance = giftCertBalance($this->certNum);
if ($amount <= $balance) {
$stmt = $cxn->prepare(
"INSERT INTO giftCerts (giftCertID, transactionID, whenAcct, amount)
VALUES (?, ?, NOW(), ?)"
);
$amount = -$amount;
$stmt->bind_param("iid", $this->certNum, $_SESSION['transaction_ID'], $amount);
if ($stmt->execute()) {
return true;
} else {
displayError(
"Failed to create Gift Certificate transaction. Error #2<br>
GiftCertID: {$this->certNum}, Transaction number: {$_SESSION['transaction_ID']}, amount: $amount<br>
{$stmt->error}<p>"
);
return false;
}
} elseif ($balance) {
// if the gift cert exists but there is not enough in the account
echo "<font color=RED>Insufficient value in gift certificate.</font><br>
Gift Certificate #" . $this->certNum . " value is ". money($balance) ."<br>
You tried to spend " . money($amount) . "<p>";
return false;
} elseif (!$balance) {
echo "<font color='RED'>No gift certificate #" . $this->certNum . "</font><p>";
return false;
}
}
/**
* sell is called to create new gift certificates in the database. This
* function should not be called until after payment is concluded.
* @retval int Returns the gift certificate ID that has been created, or
* false if the creation failed.
*/
function sell()
{
$cxn = open_stream();
$sql = "SELECT MAX(giftCertID) FROM giftCerts";
$result = query($cxn, $sql);
$row = mysqli_fetch_row($result);
$max = $row[0] + 1;
$stmt = $cxn->prepare(
"INSERT INTO giftCerts (name, giftCertID, transactionID, whenAcct, amount)
VALUES (?, ?, ?, NOW(), ?)"
);
$stmt->bind_param("siid", $this->name, $max, $_SESSION['transaction_ID'], $this->newCert);
if ($stmt->execute()) {
return($max);
} else {
echo "<font color='RED'>Error: $stmt->error</font><p>";;
return (false);
}
}
}
?>