function slct()
{
    db_conn('cubit');
    $sql = "SELECT count(stkid) FROM stock WHERE div='" . USER_DIV . "' AND units<=0";
    $Rx = db_exec($sql) or errDie("Unable to get stock from db.");
    db_conn("exten");
    $whs = "\n\t\t\t<select name='whid[]' multiple size='5'>\n\t\t\t\t<option value='0'>All</option>";
    $warehouses = qryWarehouse();
    if ($warehouses->num_rows() < 1) {
        return "There are no Warehouses found in Cubit.";
    } else {
        while ($wh = $warehouses->fetch_array()) {
            $whid = $wh['whid'];
            $whs .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    db_connect();
    $cats = "<select name='catid'>";
    $sql = "SELECT catid,cat,catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
    $catRslt = db_exec($sql);
    if (pg_numrows($catRslt) < 1) {
        return "<li>There are no stock categories in Cubit.</li>";
    } else {
        $cats .= "<option value='0'>All</option>";
        while ($cat = pg_fetch_array($catRslt)) {
            $catid = $cat['catid'];
            $cats .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
        }
    }
    $cats .= "</select>";
    # Select classification
    $class = "<select name='clasid' style='width: 167'>";
    $sql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
    $clasRslt = db_exec($sql);
    if (pg_numrows($clasRslt) < 1) {
        return "<li>There are no Classifications in Cubit.</li>";
    } else {
        $class .= "<option value='0'>All</option>";
        while ($clas = pg_fetch_array($clasRslt)) {
            $classid = $clas['clasid'];
            $class .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
        }
    }
    $class .= "</select>";
    if ($warehouses->num_rows() == 1) {
        $Sl = "SELECT stkid FROM stock";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) < 50) {
            $_POST["whid"] = 0;
            $_POST["catid"] = 0;
            $_POST["clasid"] = 0;
            $_POST["key"] = "view";
            return printStk($_POST);
        }
    }
    //layout
    $view = "\n\t\t\t<h3>View Stock</h3>\n\t\t\t<table cellpadding='5'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t\t\t\t\t<input type='hidden' name='key' value='view'>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Store</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td align='center'>{$whs}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>By Category</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td align='center'>{$cats}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>By Classification</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td align='center'>{$class}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Sort By</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='radio' name='sortby' value='normal' checked='yes'> Normal</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='radio' name='sortby' value='cat'> Category</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='radio' name='sortby' value='class'> Classification</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td align='right'><input type='submit' name='all' value='View &raquo;'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</form>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t<p>\n\t\t\t<table border='0' cellpadding='2' cellspacing='1' width='15%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><a href='stock-add.php'>Add Stock</a></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    return $view;
}
function slct($err = "")
{
    $defwhid = getSetting("DEF_WH");
    $warehouses = qryWarehouse();
    $whs = db_mksel($warehouses, "whid", $defwhid, "#whid", "(#whno) #whname");
    $stockcats = qryStockCat();
    $cats = db_mksel($stockcats, "catid", false, "#catid", "(#catcod) #cat");
    $stockclass = qryStockClass();
    $class = db_mksel($stockclass, "clasid", false, "#clasid", "#classname");
    $view = "\n\t\t\t\t<h3>Stock Sales Report</h3>\n\t\t\t\t{$err}\n\t\t\t\t<table cellpadding='5'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t<form action='" . SELF . "' method=post name=form>\n\t\t\t\t\t\t\t\t<input type=hidden name=key value=view>\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>Store</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align=center colspan=2>{$whs}</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>By Category</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center'>{$cats}</td>\n\t\t\t\t\t\t\t\t\t<td valign='bottom'><input type=submit name=cat value='View'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>By Classification</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center'>{$class}</td>\n\t\t\t\t\t\t\t\t\t<td valign='bottom'><input type='submit' name='class' value='View'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>All Categories and Classifications</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center' colspan='2'><input type='submit' name='all' value='View All'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t\t<p>\n\t\t\t\t<table border=0 cellpadding='2' cellspacing='1' width='15%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='stock-add.php'>Add Stock</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
    return $view;
}
function details($_GET)
{
    $showvat = TRUE;
    # get vars
    foreach ($_GET as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($sordid, "num", 1, 20, "Invalid invoice number.");
    # display errors, if any
    if ($v->isError()) {
        $err = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $err .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM corders WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
    $cordRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($cordRslt) < 1) {
        return "<i class=err>Not Found</i>";
    }
    $cord = pg_fetch_array($cordRslt);
    /* --- Start some checks --- */
    # check if stock was selected(yes = put done button)
    db_connect();
    $sql = "SELECT stkid FROM corders_items WHERE sordid = '{$cord['sordid']}' AND div = '" . USER_DIV . "'";
    $crslt = db_exec($sql);
    if (pg_numrows($crslt) < 1) {
        $error = "<li class=err> Error : Consignment number <b>{$sordid}</b> has no items.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    /* --- End some checks --- */
    /* --- Start Products Display --- */
    # Products layout
    $products = "";
    $disc = 0;
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM corders_items  WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    while ($stkd = pg_fetch_array($stkdRslt)) {
        if ($stkd['account'] == 0) {
            // determine warehouse name
            $wh = qryWarehouse($stkd["whid"], "whname");
            // get selected stock in this warehouse
            $stk = qryStock($stkd["stkid"]);
            $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'";
            $Ri = db_exec($Sl);
            $vd = pg_fetch_array($Ri);
            if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") {
                $showvat = FALSE;
            }
        } else {
            $wh['whname'] = "";
            $stk['stkcod'] = "";
            $stk['stkdes'] = $stkd['description'];
        }
        // totals of discounts
        $disc += $stkd['disc'];
        # put in product
        $products .= "\n\t\t<tr valign=top>\n\t\t\t<td>{$stk['stkcod']}</td>\n\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t<td>{$stkd['qty']}</td>\n\t\t\t<td>" . sprint($stk["selamt"]) . "</td>\n\t\t\t<td>" . CUR . "{$stkd['disc']}</td>\n\t\t\t<td>" . CUR . " " . sprint($stkd["amt"]) . "</td>\n\t\t</tr>";
    }
    /* --- Start Some calculations --- */
    # subtotal
    $SUBTOT = sprint($cord['subtot']);
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($cord['subtot']);
    $VAT = sprint($cord['vat']);
    $TOTAL = sprint($cord['total']);
    $cord['delchrg'] = sprint($cord['delchrg']);
    /* --- End Some calculations --- */
    # todays date
    $date = date("d-m-Y");
    $sdate = date("Y-m-d");
    # Avoid little box
    if (strlen($cord['comm']) > 0) {
        $cord['comm'] = "<table border=1 cellspacing='0' bordercolor='#000000'>\n\t\t\t<tr><td>" . nl2br($cord['comm']) . "</td></tr>\n\t\t</table>";
    }
    if ($cord['chrgvat'] == "inc") {
        $cord['chrgvat'] = "Inclusive";
    } elseif ($cord['chrgvat'] == "exc") {
        $cord['chrgvat'] = "Exclusive";
    } else {
        $cord['chrgvat'] = "No vat";
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    /* -- Final Layout -- */
    $details = "<center><h2>Consignment Order</h2>\n\t<table cellpadding='0' cellspacing='4' border=0 width=770>\n\t<tr><td valign=top width=30%>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><td>{$cord['surname']}</td></tr>\n\t\t\t<tr><td>" . nl2br($cord['cusaddr']) . "</td></tr>\n\t\t\t<tr><td>(VAT No. {$cord['cusvatno']})</td></tr>\n\t\t</table>\n\t</td><td valign=top width=30%>\n\t\t" . COMP_NAME . "<br>\n\t\t" . COMP_ADDRESS . "<br>\n\t\t" . COMP_TEL . "<br>\n\t\t" . COMP_FAX . "<br>\n\t\tReg No. " . COMP_REGNO . "<br>\n                VAT No. " . COMP_VATNO . "<br>\n\t</td><td align=left width=20%>\n\t\t<img src='compinfo/getimg.php' width=230 height=47>\n\t</td><td valign=bottom align=right width=20%>\n\t\t<table cellpadding='2' cellspacing='0' border=1 bordercolor='#000000'>\n\t\t\t<tr><td><b>Consignment Order No.</b></td><td valign=center>{$cord['sordid']}</td></tr>\n\t\t\t<tr><td><b>Order No.</b></td><td valign=center>{$cord['ordno']}</td></tr>\n\t\t\t<tr><td><b>Terms</b></td><td valign=center>{$cord['terms']} Days</td></tr>\n\t\t\t<tr><td><b>Date</b></td><td valign=center>{$cord['odate']}</td></tr>\n\t\t\t<tr><td><b>VAT</b></td><td valign=center>{$cord['chrgvat']}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=4>\n\t<table cellpadding='5' cellspacing='0' border=1 width=100% bordercolor='#000000'>\n\t\t<tr>\n\t\t\t<th>ITEM NUMBER</th>\n\t\t\t<th width=45%>DESCRIPTION</th>\n\t\t\t<th>QTY</th>\n\t\t\t<th>UNIT PRICE</th>\n\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t<th>AMOUNT</th>\n\t\t<tr>\n\t\t{$products}\n\t</table>\n\t</td></tr>\n\t<tr><td>\n\t{$cord['comm']}\n\t</td><td align=right colspan=3>\n\t\t<table cellpadding='5' cellspacing='0' border=1 width=50% bordercolor='#000000'>\n\t\t\t<tr><td><b>SUBTOTAL</b></td><td align=right>" . CUR . " {$SUBTOT}</td></tr>\n\t\t\t<tr><td><b>Trade Discount</b></td><td align=right>" . CUR . " {$cord['discount']}</td></tr>\n\t\t\t<tr><td><b>Delivery Charge</b></td><td align=right>" . CUR . " {$cord['delivery']}</td></tr>\n\t\t\t<tr><td><b>VAT {$vat14}</b></td><td align=right>" . CUR . " {$VAT}</td></tr>\n\t\t\t<tr><td><b>GRAND TOTAL<b></td><td align=right>" . CUR . " {$TOTAL}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t</table></center>";
    $OUTPUT = $details;
    require "tmpl-print.php";
}