/
sales_forecast_compare.php
executable file
·276 lines (232 loc) · 6.77 KB
/
sales_forecast_compare.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
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
<?php
require ("settings.php");
require ("sales_forecast.lib.php");
if (isset($_REQUEST["key"])) {
switch ($_REQUEST["key"]) {
case "enter":
$OUTPUT = enter();
break;
case "compare":
$OUTPUT = compare();
break;
}
} else {
$OUTPUT = enter();
}
require ("template.php");
function enter()
{
extract($_REQUEST);
$fields = array();
$fields["prd"] = "month";
extract ($fields, EXTR_SKIP);
if ($prd == "month") {
$prd_month = "checked";
$prd_week = "";
} else {
$prd_month = "";
$prd_week = "checked";
}
// Retrieve list of sales forecasts
$sql = "SELECT forecasts.id, prd, prd_val, inc_perc, dec_perc, timestamp,
username, extract('epoch' FROM timestamp) AS e_time
FROM cubit.forecasts
LEFT JOIN cubit.users ON forecasts.user_id = users.userid
ORDER BY timestamp DESC";
$fc_rslt = db_exec($sql) or errDie("Unable to retrieve sales forecasts.");
$fc_sel = "<select name='forecast_id'
onchange='javascript:document.form.submit()'>
<option value='0'>[None]</option>";
while ($fc_data = pg_fetch_array($fc_rslt)) {
if ($fc_data["prd"] == "monthly") {
$fc_prd = "Month";
} else {
$fc_prd = "Week";
}
$fc_sel .= "<option value='$fc_data[id]'>
[Created: ".date("d-m-Y G:i:s", $fc_data["e_time"])."]
[$fc_prd: $fc_data[prd_val]]
[Increase: $fc_data[inc_perc]%]
[Decrease: $fc_data[dec_perc]%]
</option>";
}
$fc_sel .= "</select>";
$OUTPUT = "<center>
<h3>Compare Sales Forecasts</h3>
<form method='post' action='".SELF."'>
<input type='hidden' name='key' value='compare' />
<table ".TMPL_tblDflts.">
<tr>
<th colspan='4'>Saved Sales Forecast</th>
</tr>
<tr class='".bg_class()."'>
<td colspan='3'>$fc_sel</td>
<td rowspan='3'>
<input type='submit' value='Compare »'
style='width: 100%; height: 100%; font-weight: bold' />
</td>
</tr>
<tr class='".bg_class()."'>
<td>Compare With</td>
<td>
Last 7 Days
<input type='radio' name='prd' value='week' $prd_week />
</td>
<td>
Last 30 Days
<input type='radio' name='prd' value='month' $prd_month />
</td>
</tr>
<tr class='".bg_class()."'>
<td colspan='3'><table ".TMPL_tblDflts." width='100%'>
<tr>
<td><b>Increase</b></td>
<td>
+<input type='text' name='inc_perc' value='0' size='5'>%
</td>
<td><b>Decrease</b></td>
<td>
-<input type='text' name='dec_perc' value='0' size='5'>%
</td>
</tr>
</td></table>
</tr>
</table>
</center>";
return $OUTPUT;
}
function compare()
{
extract($_REQUEST);
if ($prd == "week") {
$dates = lastWeekDates();
} else {
$dates = lastMonthDates();
}
$start_date = $dates["start_date"];
$end_date = $dates["end_date"];
// Store headings
$sql = "SELECT whid, whname FROM exten.warehouses ORDER BY whname ASC";
$wh_rslt = db_exec($sql) or errDie("Unable to retrieve stores.");
$stores_th_lv1 = $stores_th_lv2 = "";
while ($wh_data = pg_fetch_array($wh_rslt)) {
$stores_th_lv1 .= "<th colspan='2'>$wh_data[whname]</th>";
$stores_th_lv2 .= "<th>Actual</th><th>Projected</th>";
}
// Retrieve unique stock
$sql = "SELECT DISTINCT(stkcod) FROM cubit.stock ORDER BY stkcod ASC";
$stkcod_rslt = db_exec($sql) or errDie("Unable to retrieve stock codes.");
$stock_out = "";
while ($stkcod = pg_fetch_array($stkcod_rslt)) {
$stkcod = $stkcod["stkcod"];
$sql = "SELECT stkdes FROM cubit.stock
WHERE stkcod='$stkcod'";
$stkdes_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
$stkdes = pg_fetch_result($stkdes_rslt, 0);
$stock_out .= "<tr class='".bg_class()."'>
<td>$stkcod</td>
<td>$stkdes</td>
<td>
".totalActual($stkcod, $start_date, $end_date, $forecast_id)."
</td>
<td>
".totalProjected($stkcod, $inc_perc, $dec_perc, $start_date,
$end_date, $forecast_id)."
</td>";
pg_result_seek($wh_rslt, 0);
while ($wh_data = pg_fetch_array($wh_rslt)) {
$sql = "SELECT stkid, units FROM cubit.stock
WHERE stkcod='$stkcod' AND whid='$wh_data[whid]'";
$stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
$stock_data = pg_fetch_array($stock_rslt);
$stkid = $stock_data["stkid"];
// Don't go beyond this point unless the stock exists in this store
if (empty($stkid)) {
$stock_out .= "<td>0.00</td><td>0.00</td>";
continue;
}
$sql = "SELECT actual, projected FROM cubit.forecast_items
WHERE stkid='$stkid' AND forecast_id='$forecast_id'";
$fci_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
$fci_data = pg_fetch_array($fci_rslt);
// Total sales for the selected period
$current_actual = actualSales($stkid, $start_date, $end_date,
$forecast_id);
$current_projected = projectedSales($stkid, $inc_perc, $dec_perc,
$start_date, $end_date,
$forecast_id);
if (empty($current_actual)) $current_actual = "0.00";
if (empty($current_projected)) $current_projected = "0.00";
if (empty($fci_data["actual"])) $fci_data["actual"] = "0.00";
if (empty($fci_data["projected"])) $fci_data["projected"] = "0.00";
$actual_sales = sprint($current_actual - $fci_data["actual"]);
$projected_sales = sprint($current_projected - $fci_data["projected"]);
if ($actual_sales > 0) {
$actual_color = "green";
} elseif ($actual_sales < 0) {
$actual_color = "red";
} else {
$actual_color = "black";
}
if ($projected_sales > 0) {
$projected_color = "green";
} elseif ($projected_sales < 0) {
$projected_color = "red";
} else {
$projected_color = "black";
}
$stock_out .= "
<input type='hidden' name='stkid[]' value='$stkid' />
<td>
<input type='hidden' name='actual_sales[$stkid]'
value='$actual_sales' />
<span style='color: $actual_color'>$actual_sales</span>
</td>
<td>
<input type='hidden' name='projected_sales[$stkid]'
value='$projected_sales' />
<span style='color: $projected_color'>$projected_sales</span>
</td>";
}
}
$OUTPUT = "
<center>
<h3>Compare Sales Forecast</h3>
<table ".TMPL_tblDflts.">
<tr>
<th rowspan='2'>Stock Code</th>
<th rowspan='2'>Stock Item</th>
<th colspan='2'>Total</th>
$stores_th_lv1
</tr>
<tr>
<th>Actual</th>
<th>Projected</th>
$stores_th_lv2
</tr>
$stock_out
<tr>
</table>
</center>";
return $OUTPUT;
}
function lastWeekDates()
{
$days = 60 * 60 * 24;
$start_time = time() - ($days * 7);
$end_time = time();
$start_date = date("Y-m-d", $start_time);
$end_date = date("Y-m-d", $end_time);
$dates = array("start_date"=>$start_date, "end_date"=>$end_date);
return $dates;
}
function lastMonthDates()
{
$days = 60 * 60 * 24;
$start_time = time() - ($days * 30);
$end_time = time();
$start_date = date("Y-m-d", $start_time);
$end_date = date("Y-m-d", $end_time);
$dates = array("start_date"=>$start_date, "end_date"=>$end_date);
return $dates;
}