function confirm($_POST)
{
    # get vars
    extract($_POST);
    # Validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($purid, "num", 1, 20, "Invalid Purchase number.");
    $v->isOk($prd, "num", 1, 20, "Invalid period Database number.");
    $v->isOk($remarks, "string", 0, 255, "Invalid Remarks.");
    $v->isOk($refno, "string", 0, 255, "Invalid Delivery Reference No.");
    $pdate = $p_year . "-" . $p_month . "-" . $p_day;
    if (!checkdate($p_month, $p_day, $p_year)) {
        $v->isOk($date, "num", 1, 1, "Invalid Date.");
    }
    $v->isOk($subtot, "float", 1, 20, "Invalid Subtotal.");
    $vat = array_sum($rvat);
    foreach ($rvat as $rvat_k => $rvat_v) {
        $v->isOk($rvat_v, "float", 0, 40, "Invalid vat ({$rvat_v}).");
    }
    $v->isOk($total, "float", 1, 20, "Invalid total.");
    $error = "";
    $confirm = "";
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get purchase info
    db_conn($prd);
    $sql = "SELECT *,(shipchrg - rshipchrg) as shipchrg FROM purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    db_connect();
    $get_codes = "SELECT * FROM suppstock WHERE suppid = '{$pur['supid']}' ORDER BY stkid";
    $run_codes = db_exec($get_codes) or errDie("Unable to get supplier stock code information");
    if (pg_numrows($run_codes) > 0) {
        while ($codarr = pg_fetch_array($run_codes)) {
            if (strlen($codarr['stkcod']) > 0) {
                $stockcodes[$codarr['stkid']]['stkcod'] = $codarr['stkcod'];
            }
            if (strlen($codarr['stkdes']) > 0) {
                $stockcodes[$codarr['stkid']]['stkdes'] = $codarr['stkdes'];
            }
        }
    }
    # check if purchase has been printed
    if ($pur['received'] == "n") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has not been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($supRslt) < 1) {
        $sup['supname'] = "<li class='err'> Supplier not Found.</li>";
        $sup['supaddr'] = "<br><br><br>";
    } else {
        $sup = pg_fetch_array($supRslt);
        $supaddr = $sup['supaddr'];
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Warehouses found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Warehouse</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # days drop downs
    $days = array("30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY RETURNED</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_conn($prd);
    $sql = "SELECT *,ctqty as qty FROM pur_items  WHERE purid = '{$purid}' AND tqty > 0 AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
            $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
        }
        if (isset($stockcodes[$stk['stkid']]['stkdes'])) {
            $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
        }
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        $stkd['amt'] = sprint($stkd['unitcost'] * $stkd['qty']);
        # put in product
        if ($stk['serd'] == 'yes') {
            $sers = ext_getPurSerStk($pur['purnum'], $stkd['stkid']);
            for ($j = 0; $j < $stkd['qty']; $j++) {
                //$serial = $sers[$j]['serno'];
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t<td>1</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t</tr>";
                $key++;
            }
        } else {
            if ($stkd['account'] > 0) {
                $stk['stkdes'] = $stkd['description'];
                $stk['stkid'] = "";
                $stk['stkcod'] = "";
            }
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t<td>{$stkd['qty']}</td>\n\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    $subtot = sprint($subtot);
    $vat = sprint($vat);
    $total = sprint($subtot + $vat);
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl);
    $vd = "<table border='0' cellpadding='0' cellspacing='0'>";
    while ($vc = pg_fetch_array($Ri)) {
        $vid = $vc['id'];
        $vd .= "\n\t\t\t<tr>\n\t\t\t\t<td><input type='hidden' name='rvat[{$vid}]' value='{$rvat[$vid]}'></td>\n\t\t\t</tr>";
    }
    $vd .= "</table>";
    /* -- Final Layout -- */
    $confirm = "\n\t\t<center>\n\t\t<h3>Confirm Purchase Credit Note</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t\t{$vd}\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Account number</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supno']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</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\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Purchase Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Ref No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='refno' size='10' value='{$refno}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['terms']} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("p", $p_year, $p_month, $p_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['vatinc']}</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\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'><br></td>\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 class='" . bg_class() . "'><a href='purchase-new.php'>New purchase</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$remarks}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='purchase-view.php'>View purchases</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' size='10' value='{$subtot}'>{$subtot}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT @ " . TAX_VAT . " %</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='vat' size='10' value='{$vat}'>{$vat}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' size='10' value='{$total}'>{$total}</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\t<tr>\n\t\t\t\t<td align='right'><input type='submit' name='upBtn' value='Write'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t</center>";
    return $confirm;
}
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # Validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($purid, "num", 1, 20, "Invalid Purchase number.");
    $v->isOk($prd, "num", 1, 20, "Invalid period Database number.");
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get purchase info
    db_conn($prd);
    $sql = "SELECT *, (shipchrg - rshipchrg) as shipchrg, (fshipchrg - rfshipchrg) as fshipchrg FROM purch_int WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if purchase has been printed
    if ($pur['received'] == "n") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has not been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($supRslt) < 1) {
        $sup['supname'] = "<li class='err'> Supplier not Found.</li>";
        $sup['supaddr'] = "<br><br><br>";
    } else {
        $sup = pg_fetch_array($supRslt);
        $supaddr = $sup['supaddr'];
    }
    /* --- Start Drop Downs --- */
    # Days drop downs
    $days = array("30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # Format date
    list($p_year, $p_month, $p_day) = explode("-", $pur['pdate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t<tr>\n\t\t\t<th>STORE</th>\n\t\t\t<th>ITEM NUMBER</th>\n\t\t\t<th>DESCRIPTION</th>\n\t\t\t<th>SERIAL NO.</th>\n\t\t\t<th>QTY RETURNED</th>\n\t\t\t<th colspan='2'>UNIT PRICE</th>\n\t\t\t<th>DELIVERY DATE</th>\n\t\t\t<th>AMOUNT</th>\n\t\t\t<th>RETURNED</th>\n\t\t<tr>";
    # get selected stock in this purchase
    db_conn($prd);
    $sql = "SELECT *,(qty - tqty) as qty FROM purint_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # Get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # Get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        // $stkd['amt'] = ($stkd['unitcost'] * $stkd['qty']);
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        # put in product
        if ($stk['serd'] == 'yes') {
            $sers = ext_getPurSerStk($pur['purnum'], $stkd['stkid']);
            for ($j = 0; $j < $stkd['qty']; $j++) {
                $serial = $sers[$j]['serno'];
                $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='stkids[{$key}]' value='{$stkd['stkid']}'>\n\t\t\t\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t\t<td align='center'>\n\t\t\t\t\t\t\t\t<input type='hidden' name='sers[{$stkd['stkid']}][{$key}]' size='20' value='{$serial}'>{$serial}\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='qt[{$key}]' value='1'>\n\t\t\t\t\t\t\t\t<input type='hidden' size='5' name='qtys[{$key}]' value='1'>1\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t\t\t\t<td><input type='checkbox' name='recvd[]' value='{$key}' checked='yes'></td>\n\t\t\t\t\t\t</tr>";
                $key++;
            }
        } else {
            $products .= "<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='stkids[{$key}]' value='{$stkd['stkid']}'>\n\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t</td>\n\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='qt[{$key}]' value='{$stkd['qty']}'>\n\t\t\t\t\t<input type='text' size='5' name='qtys[{$key}]' value='{$stkd['qty']}'>\n\t\t\t\t</td>\n\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['amt']}</td>\n\t\t\t\t<td><input type='checkbox' name='recvd[]' value='{$key}' checked='yes'></td>\n\t\t\t</tr>";
            $key++;
        }
        # Put in product
        # $products .="<tr class='bg-odd'><td>$wh[whname]</td><td><input type=hidden name=stkids[] value='$stkd[stkid]'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid=$stk[stkid]\")'>$stk[stkcod]</a></td><td>$stk[stkdes]</td><td><input type=hidden name=qt[] value='$stkd[qty]'><input type=text size=5 name=qtys[] value='$stkd[qty]'></td><td>".CUR." $stkd[unitcost]</td><td>$pur[curr] $stkd[cunitcost]</td><td>$sday-$smon-$syear</td><td>".CUR." $stkd[amt]</td></tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Shipping charges
    $SHIPCHRG = sprint($pur['shipchrg']);
    # Get tax
    $TAX = sprint($pur['tax']);
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get Total
    $TOTAL = sprint($pur['total']);
    /* --- End Some calculations --- */
    /* -- Final Layout -- */
    $details = "\n\t\t<center>\n\t\t<h3>Stock Return</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='confirm'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t\t<input type='hidden' name='sct' value='{$pur['fshipchrg']}'>\n\t\t\t<input type='hidden' name='otax' value='{$pur['tax']}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Account number</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supno']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</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\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan=2> Purchase Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['terms']} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("p", $p_year, $p_month, $p_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} &nbsp;&nbsp;Exchange rate " . CUR . " {$pur['xrate']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} <input type='text' size='7' name='tax' value='{$pur['tax']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap>{$pur['curr']} <input type='text' size='7' name='shipchrg' value='{$pur['fshipchrg']}'></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\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\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 class='" . bg_class() . "'><a href='purch-int-view.php'>View International Orders</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$SHIPCHRG}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$TAX}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$TOTAL}</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\t<tr>\n\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input type='submit' name='upBtn' value='Write'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t</center>";
    return $details;
}
function confirm($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($purid, "num", 1, 20, "Invalid purchase number.");
    $v->isOk($remarks, "string", 0, 255, "Invalid Remarks.");
    $v->isOk($prd, "num", 1, 20, "Invalid period Database number.");
    // $v->isOk ($refno, "string", 0, 255, "Invalid Delivery Reference No.");
    $pdate = $p_year . "-" . $p_month . "-" . $p_day;
    if (!checkdate($p_month, $p_day, $p_year)) {
        $v->isOk($date, "num", 1, 1, "Invalid Date.");
    }
    $v->isOk($subtot, "float", 1, 20, "Invalid Subtotal.");
    $v->isOk($tax, "float", 0, 20, "Invalid Tax.");
    $v->isOk($total, "float", 1, 20, "Invalid total.");
    # Used to generate errors
    $error = "asa@";
    # display errors, if any
    $err = "";
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $err .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return details($_POST, $err);
    }
    # Get purchase info
    db_conn($prd);
    $sql = "SELECT * FROM purch_int WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if purchase has been printed
    if ($pur['received'] == "n") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has not been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($supRslt) < 1) {
        $sup['supname'] = "<li class='err'> Supplier not Found.</li>";
        $sup['supaddr'] = "<br><br><br>";
    } else {
        $sup = pg_fetch_array($supRslt);
        $supaddr = $sup['supaddr'];
    }
    # Format date
    list($p_year, $p_month, $p_day) = explode("-", $pur['pdate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\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>STORE</th>\n\t\t\t\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t\t\t\t<TH>SERIAL NO.</TH>\n\t\t\t\t\t\t\t<th>QTY RETURNED</th>\n\t\t\t\t\t\t\t<th colspan=2>UNIT PRICE</th>\n\t\t\t\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t\t\t<tr>";
    # get selected stock in this purchase
    db_conn($prd);
    $sql = "SELECT *,tqty as qty FROM purint_items  WHERE purid = '{$purid}' AND tqty > 0 AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # Get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # Get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        // $stkd['amt'] = ($stkd['unitcost'] * $stkd['qty']);
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        # put in product
        if ($stk['serd'] == 'yes') {
            $sers = ext_getPurSerStk($pur['purnum'], $stkd['stkid']);
            for ($j = 0; $j < $stkd['qty']; $j++) {
                $serial = "";
                //$serial = $sers[$j]['serno'];
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t\t\t\t<td align='center'>{$serial}</td>\n\t\t\t\t\t\t\t\t\t<td>1</td>\n\t\t\t\t\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t\t\t\t\t</tr>";
                $key++;
            }
        } else {
            $products .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t\t\t<td><br></td>\n\t\t\t\t\t\t\t\t<td>{$stkd['qty']}</td>\n\t\t\t\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['cunitcost']}</td>\n\t\t\t\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t\t\t\t<td nowrap>{$pur['curr']} {$stkd['amt']}</td>\n\t\t\t\t\t\t\t</tr>";
            $key++;
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    $subtot = sprint($subtot);
    $tax = sprint($tax);
    $total = sprint($subtot + $tax);
    /* --- End Some calculations --- */
    /* -- Final Layout -- */
    $details = "\n\t\t\t\t\t<center>\n\t\t\t\t\t<h3>Stock Return</h3>\n\t\t\t\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t\t\t\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td valign='top'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t   \t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t   \t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t   \t\t\t\t<td valign='center'>{$sup['supname']}</td>\n\t\t\t\t\t\t   \t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Account number</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$sup['supno']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Purchase Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$pur['terms']} Days</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("p", $p_year, $p_month, $p_day) . "</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} &nbsp;&nbsp;Exchange rate " . CUR . " {$pur['xrate']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} {$tax}</td>\n\t\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\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center' nowrap>{$pur['curr']} {$pur['fshipchrg']}</td>\n\t\t\t\t\t\t\t\t\t</tr>-->\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2'>{$products}</td>\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>\n\t\t\t\t\t\t\t\t<p>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='purch-int-view.php'>View International Orders</a></td>\n\t\t\t\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align=right>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} <input type='hidden' name='subtot' size='10' value='{$subtot}'>{$subtot}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} <input type='hidden' name='tax' size='10' value='{$tax}'>{$tax}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} <input type='hidden' name='total' size='10' value='{$total}'>{$total}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\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 align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input type='submit' name='upBtn' value='Write'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t</form>\n\t\t\t\t\t</center>";
    return $details;
}
function details($_POST, $error = "")
{
    $showvat = TRUE;
    # get vars
    extract($_POST);
    # Validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($purid, "num", 1, 20, "Invalid Purchase number.");
    $v->isOk($prd, "num", 1, 20, "Invalid period Database number.");
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get purchase info
    db_conn($prd);
    $sql = "SELECT *,(shipchrg - rshipchrg) as shipchrg FROM purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    db_connect();
    $get_codes = "SELECT * FROM suppstock WHERE suppid = '{$pur['supid']}' ORDER BY stkid";
    $run_codes = db_exec($get_codes) or errDie("Unable to get supplier stock code information");
    if (pg_numrows($run_codes) > 0) {
        while ($codarr = pg_fetch_array($run_codes)) {
            if (strlen($codarr['stkcod']) > 0) {
                $stockcodes[$codarr['stkid']]['stkcod'] = $codarr['stkcod'];
            }
            if (strlen($codarr['stkdes']) > 0) {
                $stockcodes[$codarr['stkid']]['stkdes'] = $codarr['stkdes'];
            }
        }
    }
    # check if purchase has been printed
    if ($pur['received'] == "n") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has not been received.";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($supRslt) < 1) {
        $sup['supname'] = "<li class='err'> Supplier not Found.";
        $sup['supaddr'] = "<br><br><br>";
    } else {
        $sup = pg_fetch_array($supRslt);
        $supaddr = $sup['supaddr'];
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Warehouses found in Cubit.";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Warehouse</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # days drop downs
    $days = array("30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($p_year, $p_month, $p_day) = explode("-", $pur['pdate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<TH>SERIAL NO.</TH>\n\t\t\t\t<th>QTY RETURNED</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>DISCOUNT</th>\n\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>RETURNED</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_conn($prd);
    $sql = "SELECT *,(qty - tqty) as qty FROM pur_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
            $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
        }
        if (isset($stockcodes[$stk['stkid']]['stkdes'])) {
            $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
        }
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        if ($stkd['udiscount'] > 0) {
            $discps = round($stkd['udiscount'] / 100 * $stkd['unitcost'], 2);
        } else {
            $discps = 0;
        }
        $amt[$keys] = sprint($qty[$stkd['id']] * ($stkd['unitcost'] - $discps));
        $stkd['amt'] = sprint(($stkd['unitcost'] - $discps) * $stkd['qty']);
        db_conn('cubit');
        $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;
        }
        # put in product
        if ($stk['serd'] == 'yes') {
            $sers = ext_getPurSerStk($pur['purnum'], $stkd['stkid']);
            for ($j = 0; $j < $stkd['qty']; $j++) {
                $serial = $sers[$j]['serno'];
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='hidden' name='ids[{$key}]' value='{$stkd['id']}'>\n\t\t\t\t\t\t\t<input type='hidden' name='stkids[{$key}]' value='{$stkd['stkid']}'>\n\t\t\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t<td align='center'><input type='hidden' name='sers[{$stkd['stkid']}][{$key}]' size='20' value='{$serial}'>{$serial}</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='hidden' name='qt[{$key}]' value='1'>\n\t\t\t\t\t\t\t<input type='hidden' size='5' name='qtys[{$key}]' value='1'>1\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td nowrap>{$stkd['unitcost']}</td>\n\t\t\t\t\t\t<td>{$stkd['udiscount']}</td>\n\t\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='recvd[]' value='{$key}' checked='yes'></td>\n\t\t\t\t\t</tr>";
                $key++;
            }
        } else {
            if ($stkd['stkid'] == 0) {
                $stk['stkdes'] = $stkd['description'];
                $stk['stkid'] = "";
                $stk['stkcod'] = "";
            }
            $stkd['qty'] = sprint3($stkd['qty']);
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='ids[{$key}]' value='{$stkd['id']}'>\n\t\t\t\t\t\t<input type='hidden' name='stkids[{$key}]' value='{$stkd['stkid']}'>\n\t\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t<td><br></td>\n\t\t\t\t\t<td><input type='hidden' name='qt[{$key}]' value='{$stkd['qty']}'><input type='text' size='5' name='qtys[{$key}]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td nowrap>{$stkd['unitcost']}</td>\n\t\t\t\t\t<td>{$stkd['udiscount']}</td>\n\t\t\t\t\t<td>{$sday}-{$smon}-{$syear}</td>\n\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='recvd[]' value='{$key}' checked='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get vat
    $VAT = sprint($pur['vat']);
    # Get Total
    // $TOTAL = sprint($pur['total']);
    $TOTAL = sprint($pur['total']);
    /* --- End Some calculations --- */
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (!isset($del)) {
        $del = $pur["shipchrg"];
    }
    /* -- Final Layout -- */
    $details = "\n\t\t<center>\n\t\t<h3>Stock Return</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='confirm'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Account number</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sup['supno']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</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\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Purchase Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Ref No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='refno' size='10' value='{$pur['refno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['terms']} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("p", $p_year, $p_month, $p_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['vatinc']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td valign='center'>" . CUR . " <input type='hidden' size='10' value='0' name='del'>{$del}</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\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\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 class='" . bg_class() . "'><a href='purchase-new.php'>New purchase</a></td>\n\t\t\t\t\t\t\t<td class='bg-odd' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='purchase-view.php'>View purchases</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align=right>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$pur['shipping']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$pur['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</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\t<tr>\n\t\t\t\t<td align='right'><input type='submit' name='upBtn' value='Confirm'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t</center>";
    return $details;
}