function display()
{
    extract($_REQUEST);
    $fields = array();
    $fields["from_year"] = date("Y");
    $fields["from_month"] = date("m");
    $fields["from_day"] = "01";
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("d");
    $fields["prd"] = "daily";
    $fields["increase"] = 0;
    $fields["decrease"] = 0;
    extract($fields, EXTR_SKIP);
    $from_date = "{$from_year}-{$from_month}-{$from_day}";
    $to_date = "{$to_year}-{$to_month}-{$to_day}";
    $sql = "SELECT stkid, stkcod, stkdes, minlvl, maxlvl, units FROM cubit.stock";
    $stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
    $stock_out = "";
    $tot_avail = 0;
    while ($stock_data = pg_fetch_array($stock_rslt)) {
        $tmp_min = $suggested_min = averageSalesQty($stock_data["stkid"], $from_date, $to_date, $prd);
        $tmp_max = $suggested_max = maxSalesQty($stock_data["stkid"], $prd);
        $suggested_min += $tmp_min / 100 * $increase;
        $suggested_min -= $tmp_min / 100 * $decrease;
        $suggested_max += $tmp_max / 100 * $increase;
        $suggested_max -= $tmp_max / 100 * $decrease;
        $stock_out .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$stock_data['stkcod']}</td>\n\t\t\t\t<td>{$stock_data['stkdes']}</td>\n\t\t\t\t<td align='center'><b>{$stock_data['minlvl']}</b></td>\n\t\t\t\t<td align='center'><b>{$stock_data['maxlvl']}</b></td>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<b>{$suggested_min}</b>\n\t\t\t\t</td>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<b>" . sprint3($suggested_max) . "</b>\n\t\t\t\t</td>\n\t\t\t\t<td align='center'><b>" . sprint3($stock_data['units']) . "</b></td>\n\t\t\t</tr>";
        if ($stock_data['units'] > 0) {
            $tot_avail += $stock_data['units'];
        }
    }
    $stock_out .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='6'><b>Total: (Positive amounts only)</b></td>\n\t\t\t\t\t\t<td align='center'><b>" . sprint3($tot_avail) . "</b></td>\n\t\t\t\t\t</tr>\n\t\t\t\t";
    $prds = array("daily", "weekly", "monthly");
    $prd_sel = "<select name='prd'>";
    foreach ($prds as $prd_val) {
        if ($prd_val == $prd) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $prd_sel .= "<option value='{$prd_val}' {$sel}>" . ucfirst($prd_val) . "</option>";
    }
    $OUTPUT = "\n\t\t<center>\n\t\t<h3>Minimum and Maximum Stock Levels</h3>\n\t\t<form method='POST' action='" . SELF . "'>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th colspan='4'>Date Range</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>" . mkDateSelect("from", $from_year, $from_month, $from_day) . "</td>\n\t\t\t\t<td>&nbsp; <b> To </b> &nbsp;</td>\n\t\t\t\t<td>" . mkDateSelect("to", $to_year, $to_month, $to_day) . "</td>\n\t\t\t\t<td rowspan='2'>\n\t\t\t\t\t<input type='submit' value='Calculate' style='height: 100%; font-weight: bold' />\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='3' align='center'>{$prd_sel}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='4' align='center'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Increase</th>\n\t\t\t\t\t\t\t<th>Decrease</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='50%' align='center'>\n\t\t\t\t\t\t\t\t<input type='text' name='increase' value='{$increase}' size='3' />%\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td width='50%' align='center'>\n\t\t\t\t\t\t\t\t<input type='text' name='decrease' value='{$decrease}' size='3' />%\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Stock Code</th>\n\t\t\t\t<th>Stock Description</th>\n\t\t\t\t<th>Current Minimum</th>\n\t\t\t\t<th>Current Maximum</th>\n\t\t\t\t<th>Suggested Minimum</th>\n\t\t\t\t<th>Suggested Maximum</th>\n\t\t\t\t<th>Currently On Hand</th>\n\t\t\t</tr>\n\t\t\t{$stock_out}\n\t\t</table>\n\t\t</center>";
    return $OUTPUT;
}
function orderDate($stkid, $from_date, $to_date)
{
    $sql = "SELECT minlvl FROM cubit.stock WHERE stkid='{$stkid}'";
    $min_rslt = db_exec($sql) or errDie("Unable to retrieve stock minimum stock level.");
    $min = pg_fetch_result($min_rslt, 0);
    if (empty($min)) {
        $min = 0;
    }
    $from_time = strtotime($from_date);
    $to_time = strtotime(date("Y-m-t", $from_time));
    $sales = averageSalesQty($stkid, date("Y-m-d", $from_time), date("Y-m-d", $to_time), "DAILY");
    if ($sales <= $min) {
        return date("Y-m-d");
    } else {
        $tmp_sales = $sales;
        $ret = 0;
        while ($tmp_sales > $min) {
            $to_time = $from_time * (1 * (60 * 60 * 24));
            $tmp_sales = averageSalesQty($stkid, date("Y-m-d", $from_time), date("Y-m-d", $to_time), "MONTHLY");
            $ret = 1;
        }
        if ($ret) {
            return date("Y-m-d", $to_time);
        }
    }
    return;
}