#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
require "settings.php";
# show current stock
$OUTPUT = printQuo();
require "template.php";
##
# Functions
##
# show quotes
function printQuo()
{
    # Set up table to display in
    $printQuo = "\n        <h3>Incomplete POS Quotes</h3>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n        <tr><th>Username</th><th>Department</th><th>Sales Person</th><th>Quote No.</th><th>Quote Date</th><th>Customer Name</th><th>Order No</th><th>Grand Total</th><th>Balance</th><th colspan=3>Options</th></tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $sql = "SELECT * FROM pos_quotes WHERE done = 'n' AND div = '" . USER_DIV . "' ORDER BY quoid DESC";
    $quoRslt = db_exec($sql) or errDie("Unable to retrieve quotes from database.");
#
#
#
#
#
#
#
#
#
#
#
require "settings.php";
if (isset($_POST["key"])) {
    switch ($_POST["key"]) {
        case "view":
            $OUTPUT = printQuo($_POST);
            break;
        default:
            $OUTPUT = slct();
            break;
    }
} else {
    $OUTPUT = slct();
}
require "template.php";
# Default view
function slct()
{
    //layout
    $slct = "\n\t\t<h3>View Cancelled POS Quotes<h3>\n\t\t<table " . TMPL_tblDflts . " width='460'>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='view'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>By Date Range</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center' nowrap>\n\t\t\t\t\t" . mkDateSelect("from", date("Y"), date("m"), "01") . "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;TO&nbsp;&nbsp;&nbsp;\n\t\t\t\t\t" . mkDateSelect("to") . "\n\t\t\t\t</td>\n\t\t\t\t<td valign='bottom'><input type='submit' value='Search'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<p>\n\t\t<table border='0' cellpadding='2' cellspacing='1'>\n\t\t\t<tr>\n\t\t\t\t<th>Quick Links</th>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td><a href='pos-quote-unf-view.php'>View Incomplete POS Quotes</td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td><a href='pos-quote-new.php'>New POS Quote</td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td><a href='main.php'>Main Menu</td>\n\t\t\t</tr>\n\t\t</table>";
    return $slct;
function cancel()
{
    extract($_REQUEST);
    if (isset($rem) && is_array($rem)) {
        foreach ($rem as $quoid) {
            # Get Quote info
            db_connect();
            $sql = "SELECT * FROM quotes WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
            $quoRslt = db_exec($sql) or errDie("Unable to get Quote information");
            if (pg_numrows($quoRslt) < 1) {
                return "<i class=err>Not Found</i>";
            }
            $quo = pg_fetch_array($quoRslt);
            # begin updating
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            db_connect();
            # Todays date (sql formatted)
            $date = date("Y-m-d");
            # Record (quoid, username, date)
            $sql = "\n\t\t\tINSERT INTO cancelled_quo (quoid, deptid, username, date,\n\t\t\t\tdeptname, div)\n\t\t\tVALUES ('{$quoid}', '{$quo['deptid']}', '" . USER_NAME . "', '{$date}',\n\t\t\t\t'{$quo['deptname']}', '" . USER_DIV . "')";
            $rslt = db_exec($sql) or errDie("Unable to insert Quote record to Cubit.", SELF);
            # Update the Quote (make balance less)
            $sql = "DELETE FROM quotes WHERE quoid = '{$quoid}' AND done='n' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to remove Quote from Cubit.", SELF);
            # Get selected stock in this Quote
            db_connect();
            $sql = "SELECT * FROM quote_items WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
            $stkdRslt = db_exec($sql);
            # Delete Quote items
            $sql = "DELETE FROM quote_items WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to delete Quote items from Cubit.", SELF);
            # while($stkd = pg_fetch_array($stkdRslt)){
            #	# Update stock(alloc - qty)
            #	$sql = "UPDATE stock SET alloc = (alloc - '$stkd[qty]') WHERE stkid = '$stkd[stkid]'";
            #	$rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.",SELF);
            # }
            # commit updating
            pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
        }
    }
    return printQuo();
}