-
Notifications
You must be signed in to change notification settings - Fork 2
/
salesreport.inc
370 lines (331 loc) · 13.3 KB
/
salesreport.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* @file salesreport.inc
* @brief salesreport.inc is a library of functions used to display the sales report
* on salesreport.php.
*
* This file uses but does not include:
* funcs.inc:
* - Used for the config.inc include
* - dateDiff()
* - money()
* - lastDayOfMonth()
* - checkDateNum()
* - dateStringVar()
*
* @link http://www.worldsapartgames.org/fc/salesreport.php @endlink
*
* @author Michael Whitehouse
* @author Creidieki Crouch
* @author Desmond Duval
* @copyright 2009-2014 Pioneer Valley Gaming Collective
* @version 1.8b
* @since Project has existed since time immemorial.
*/
/**
* displayList() displays all items sold in specified time frame
* @param string $start is a datestring for the beginning of the desired period.
* @param string $end is a datestring for the end of the desired period.
* @param int $order describes for the function how to sort the returned data.
* @param int $staff is an optional memberID to restrict returned data by
* the staff who checked out the sale.
* @param int $cust is an optional memberID to restrict returned data by
* the customer in the sale.
*/
function displayList($start, $end, $order, $staff, $cust) {
$cxn = open_stream();
$datestart = $start;
$dateend = (date_create() < date_create($end)) ? date("Y-m-d") : $end;
$start .= " 00:00:00";
$end .= " 23:59:59";
switch ($order) {
case 1 :
case 'NAME' :
$sorder = 'I.description';
break;
case 2 :
case 'DEPT' :
$sorder = 'I.department, I.description';
break;
case 3 :
case 'MANU' :
$sorder = 'I.manufacturer, I.description';
break;
case 4 :
$sorder = 'I.cost, I.description';
break;
default :
$sorder = 'I.description';
}
$staffSQL = ($staff > 0) ? "AND T.staffID = '$staff'" : "";
$custSQL = ($cust > 0) ? "AND T.customerID = '$cust'" : "";
$staffSQL2 = ($staff > 0) ? "AND staffID = '$staff'" : "";
$custSQL2 = ($cust > 0) ? "AND customerID = '$cust'" : "";
$sql = "SELECT I.description dsc,
I.department dept,
I.manufacturer manu,
T.whensale whensale,
T.ID tid,
SI.itemID ID,
SI.price price,
SI.cost cost,
SI.qty qty,
SI.tax tax
FROM soldItem SI
JOIN transactions T
ON SI.transactionID = T.ID
JOIN items I
ON SI.itemID = I.ID
WHERE T.whensale >= '$start'
AND T.whensale <= '$end'
$staffSQL
$custSQL
ORDER BY $sorder, T.whensale";
$result = query($cxn, $sql);
$onesale = false;
$totalTax = 0;
while ($row = mysqli_fetch_assoc($result)) {
$onesale = true;
extract($row);
$thisPrice = round(($price * $qty), 2);
$Tprice[$ID] += $thisPrice;
$Tcost[$ID] += round(($cost * $qty), 2);
$Ttaxable[$ID] += ($tax == 1) ? $thisPrice : 0;
$Ttax[$ID] = $tax;
$Tqty[$ID] += $qty;
$Tdesc[$ID] = $dsc;
$Tdept[$ID] = $dept;
$Tmanu[$ID] = $manu;
$Ttid[$ID] = $tid;
$Tdate[$ID] = $whensale;
$deptQty[$dept] += $qty;
$deptPrice[$dept] += $thisPrice;
$manuQty[$manu] += $qty;
$manuPrice[$manu] += $thisPrice;
}
// Determine total tax collected
$sql = "SELECT SUM(tax)
FROM transactions
WHERE whensale >= '$start'
AND whensale <= '$end'
$staffSQL2
$custSQL2
ORDER BY whensale";
$totalTax = queryOnce($cxn, $sql);
if (!$onesale) {
echo "<font size=+2>No sales during this period</font>";
include('footer.php');
exit();
}
// Get Payment Methods
$sql = "SELECT sum(cash) payca, sum(creditcard) paycc, sum(checkpay) paych, sum(account) payac, sum(giftCert) paygc
FROM transactions
WHERE whensale >= '$start'
AND whensale <= '$end'
$staffSQL
$custSQL";
$result = query($cxn, $sql);
$row = mysqli_fetch_assoc($result);
extract($row);
// Get Account Change Info
$sql = "SELECT sum(amount) FROM storeAccount WHERE amount > 0 AND whenAcct >= '$start' AND whenAcct <= '$end' $staffSQL $custSQL";
$accountUp = queryOnce($cxn, $sql);
$sql = "SELECT sum(amount) FROM storeAccount WHERE amount < 0 AND whenAcct >= '$start' AND whenAcct <= '$end' $staffSQL $custSQL";
$accountDown = queryOnce($cxn, $sql);
$sql = "SELECT sum(amount) FROM storeAccount";
$accountTotal = queryOnce($cxn, $sql);
// determine number of days in period
$days = dateDiff('-', $datestart, $dateend) + 1;
echo "Days: $days";
$totalPrice = round(array_sum($Tprice), 2);
$totalCost = round(array_sum($Tcost), 2);
$totalTaxable = round(array_sum($Ttaxable), 2);
$totalNontaxable = $totalPrice - $totalTaxable;
$totalGross = $totalPrice - $totalCost;
$margin = ($totalPrice != 0) ? (100 * $totalGross / $totalPrice) : 'NO SALES';
$margin = round($margin);
$sixty = round(($totalPrice * .6), 2);
echo "<table cellpadding=4><tr><td>
<b>Total Sales: " . money($totalPrice) . "<br>
Total Cost: " . money($totalCost) . "<br>
Gross Profit: " . money($totalGross) . "<br>
Margin: $margin%<br>
60% of Sales: " . money($sixty) . "
</td><td valign=top width=200>
Sales/Day: " . money($totalPrice / $days) . "<br>
Cost/Day: " . money($totalCost / $days) . "<br>
Profit/Day: " . money($totalGross / $days) . "
</td><td valign=top>
Est Final Sales: " . money($totalPrice * lastDayOfMonth(date('n')) / $days) . "<br>
Est Final Cost: " . money($totalCost * lastDayOfMonth(date('n')) / $days) . "<br>
Est Final Profit: " . money($totalGross * lastDayOfMonth(date('n')) / $days) . "<br>
</td><td valign=top>
Total Taxable Sales: " . money($totalTaxable) . "<br>
Total Non-Taxable Sales: " . money($totalNontaxable) . "<br>
Total Sales Tax: " . money($totalTax) . "
</td></tr></table><hr>";
if ($_SESSION['mem'] == 1)
echo "<table cellpadding=5><tr><td>Cash:<br>" . money($payca) . "</td>
<td>Account:<br>" . money($payac) . "</td>
<td>Gift Certificates:<br>" . money($paygc) . "</td>
<td>Account Added:<br>" . money($accountUp) . "</td>
<td>Account Change:<br>" . money($accountUp + $accountDown) . "</td></tr>
<tr><td>Credit Card:<br>" . money($paycc) . "</td>
<td>Checks:<br>" . money($paych) . "</td>
<td></td><td>Account Spent:<br>" . money($accountDown) . "</td>
<td>Total Account Balance:<br>" . money($accountTotal) . "</td></tr></table><hr>
";
echo "<table cellpadding=5 border><tr><td><b>Name</b></td><td>Qty<br>Sold</td><td>Total<br>Price</td><td>Tax</td><td>Total<br>Cost</td><td>Total<br>Gross</td>
<td>Department<br>Manufacturer</td><td>Last Sale</td></tr>\n";
switch ($order) {
case 'DEPT' : asort($Tdept);
foreach ($Tdept as $ID => $d) {
// set date for last sale
$date = date_create($Tdate[$ID]);
// if its a new department, we put a header there
if ($d != $lastD) {
echo "<tr><td colspan=5><b>$d</b></td></tr>\n";
}
$lastD = $d; // set it for next time
echo "<tr><td>D" . $Tdesc[$ID] . "</td>
<td>" . $Tqty[$ID] . "</td>
<td>" . $Tprice[$ID] . "</td>
<td>" . (($Ttax[$ID] == 1) ? "Yes" : "No") . "</td>
<td>" . $Tcost[$ID] . "</td>
<td>" . ($Tprice[$ID] - $Tcost[$ID]) . "</td>
<td>" . $d . "<br>" . $Tmanu[$ID] . "</td>
<td><a href='viewreceipts.php?view={$Ttid[$ID]}' target='viewlast'>" . $date->format("M-j") . "</td></tr>";
} // end foreach
break;
case 'MANU' : asort($Tmanu);
foreach ($Tmanu as $ID => $m) {
// set date for last sale
$date = date_create($Tdate[$ID]);
// if its a new department, we put a header there
if ($m != $lastM) {
echo "<tr><td colspan=5><b>$d</b></td></tr>\n";
}
$lastM = $m; // set it for next time
echo "<tr><td>M" . $Tdesc[$ID] . "</td>
<td>" . $Tprice[$ID] . "</td>
<td>" . (($Ttax[$ID] == 1) ? "Yes" : "No") . "</td>
<td>" . $Tcost[$ID] . "</td>
<td>" . $Tprice[$ID] - $Tcost[$ID] . "</td>
<td>" . $Tqty[$ID] . "</td>
<td>" . $Tdept[$ID] . "<br>" . $m . "</td>
<td><a href='viewreceipts.php?view={$Ttid[$ID]}' target='viewlast'>" . $date->format("M-j-y") . "</td></tr>\n";
} // end foreach
break;
case 'NAME':
default :
foreach ($Tdesc as $ID => $desc) {
// set date for last sale
$date = date_create($Tdate[$ID]);
echo "<tr><td>" . $desc . "</td>";
echo "<td>" . $Tqty[$ID] . "</td>";
echo "<td>" . $Tprice[$ID] . "</td>";
echo "<td>" . (($Ttax[$ID] == 1) ? "Yes" : "No") . "</td>";
echo "<td>" . $Tcost[$ID] . "</td>";
echo "<td>" . ($Tprice[$ID] - $Tcost[$ID]) . "</td>";
echo "<td>" . $Tdept[$ID] . "<br>" . $Tmanu[$ID] . "</td>
<td><a href='viewreceipts.php?view={$Ttid[$ID]}' target='viewlast'>" . $date->format('M-j') . "</td></tr>\n";
}
break;
} // end switch
echo "</table>";
// Display totals by Manufacturuer and Department
echo "<b>Sales By Department</b><br>
<table border cellpadding=3><td>Department</td><td>Price</td><td>Quantity</td><td>Proportion</td></tr>\n";
arsort($deptPrice);
foreach ($deptPrice as $dept => $price) {
$qty = $deptQty[$dept];
echo "<tr><td>$dept</td><td>" . money($price) . "</td><td>$qty</td><td>" . round(($price / $totalPrice * 100), 0) . "%</td></tr>";
}
echo "</table>";
echo "<p><b>Sales By Manufacturer</b><br>
<table border cellpadding=3><td>Manufacturer</td><td>Price</td><td>Quantity</td></tr>\n";
arsort($manuPrice);
foreach ($manuPrice as $manu => $price) {
$qty = $manuQty[$manu];
echo "<tr><td>$manu</td><td>" . money($price) . "</td><td>$qty</td><td>" . round(($price / $totalPrice * 100), 0) . "%</td></tr>";
}
echo "</table>";
}
/**
* displayTableByDay() prints a table of sales organised by day.
* @param string $start is a datestring for the start of the desired period.
* @param string $end is a datestring for the end of the desired period.
*/
function displayTableByDay($start, $end) {
if (checkDateNum($start) && checkDateNum($end)) {
$cxn = open_stream();
$sql = "SELECT totalPrice,
whensale
FROM transactions
WHERE whensale >= '$start'
AND whensale <= '$end'
ORDER BY whensale";
$result = query($cxn, $sql);
$curdate = 0;
echo "<table>\n";
while ($row = mysqli_fetch_assoc($result)) {
extract($row);
if ($curdate != 0 && dateStringVar($curdate) != dateStringVar($whensale)) {
$date = date_create($curdate);
$datestr = $date->format("F jS");
$day = $date->format("l");
echo "<tr><td>$datestr</td><td>$day</td><td>" . money($total) . "</td>";
$barlength = round($total / 5);
for ($i = 0; $i < $barlength; $i++)
echo "<td bgcolor=BLUE></td>";
echo "</tr>\n";
$total = 0;
}
$curdate = $whensale;
$total += $totalPrice;
}
// print last day
$date = date_create($curdate);
$datestr = $date->format("F jS");
$day = $date->format("l");
echo "<tr><td>$datestr</td><td>$day</td><td>" . money($total) . "</td>";
$barlength = round($total / 5);
for ($i = 0; $i < $barlength; $i++)
echo "<td bgcolor=BLUE></td>";
echo "</tr>\n";
$total = 0;
echo "</table>\n";
} else {
displayError("Invalid Date for displayTableByDay in salesreport.inc");
}
}
/**
* displayTableByHour() prints a table of sales organised by hour.
* @param string $start is a datestring for the start of the desired period.
* @param string $end is a datestring for the end of the desired period.
*/
function displayTableByHour($start, $end) {
$cxn = open_stream();
$sql = "SELECT totalPrice,
extract(hour FROM whensale) hr
FROM transactions
WHERE whensale >= '$start'
AND whensale <= '$end'
ORDER BY extract(hour FROM whensale)";
$result = query($cxn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
extract($row);
$hour[$hr] += $totalPrice;
}
echo "<table>\n";
for ($count = 0; $count < 24; $count++) {
echo "<tr><td>$count:00</td><td>\$" . $hour[$count] . "</td>\n";
$barlength = round($hour[$count] / 5);
for ($i = 0; $i < $barlength; $i++) {
echo "<td bgcolor=BLUE></td>";
}
echo "</tr>\n";
}
echo "</table>\n";
}
?>