#
#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
require "settings.php";
$OUTPUT = printSord($_POST);
require "template.php";
##
# Functions
##
# show Sales Orders
function printSord($_POST)
{
    extract($_POST);
    #nothing to remove ? set var anyway ...
    if (!isset($cancorderid)) {
        $cancorderid = array();
    }
    #get the entries to remove here ...
    foreach ($cancorderid as $each) {
        pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
#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 = printSord();
require "template.php";
##
# Functions
##
# show Sales Orders
function printSord()
{
    # Set up table to display in
    $printSord = "\n        <h3>View Canceled Sales Orders</h3>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n        <tr>\n        \t<th>Department</th>\n        \t<th>Sales Person</th>\n        \t<th>Sales Order No.</th>\n        \t<th>Sales Order Date</th>\n        \t<th>Customer Name</th>\n        \t<th>Order No</th>\n        \t<th>Grand Total</th>\n        \t<th colspan=6>Options</th>\n        </tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $sql = "SELECT * FROM cancelled_sord WHERE div = '" . USER_DIV . "' ORDER BY sordid DESC";
    $sordRslt = db_exec($sql) or errDie("Unable to retrieve Sales Orders from database.");
function cancel()
{
    extract($_REQUEST);
    if (isset($rem) && is_array($rem)) {
        foreach ($rem as $sordid) {
            # Get Sales Order info
            db_connect();
            $sql = "SELECT * FROM corders WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
            $sordRslt = db_exec($sql) or errDie("Unable to get Sales Order information");
            if (pg_numrows($sordRslt) < 1) {
                return "<i class=err>Not Found</i>";
            }
            $sord = pg_fetch_array($sordRslt);
            # begin updating
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            # Todays date (sql formatted)
            $date = date("Y-m-d");
            db_connect();
            # Record (sordid, username, date)
            $sql = "\n\t\t\tINSERT INTO cancelled_cord(sordid, deptid, username, date,\n\t\t\t\tdeptname, div)\n\t\t\tVALUES ('{$sordid}', '{$sord['deptid']}', '" . USER_NAME . "', '{$date}',\n\t\t\t\t'{$sord['deptname']}', '" . USER_DIV . "')";
            $rslt = db_exec($sql) or errDie("Unable to insert Sales Order record to Cubit.", SELF);
            $sql = "DELETE FROM corders WHERE sordid = '{$sordid}' AND done='n' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to remove Sales Order from Cubit.", SELF);
            # Get selected stock in this Sales Order
            db_connect();
            $sql = "SELECT * FROM corders_items WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
            $stkdRslt = db_exec($sql) or errDie("Unable to remove Sales Order from Cubit2.", SELF);
            # Delete Sales Order items
            $sql = "DELETE FROM corders_items WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to delete Sales Order 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']}' AND div = '" . USER_DIV . "'";
                $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 printSord();
}