function slctAcc()
{
    // Layout
    $slctAcc = "\n\t\t\t<h3>Select Account</h3>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t\t<input type='hidden' name='key' value='viewtran'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Field</th>\n\t\t\t\t\t<th>Value</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Name</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<select name='accid'>";
    core_connect();
    $sql = "SELECT * FROM accounts WHERE div = '" . USER_DIV . "' ORDER BY accname ASC";
    $accRslt = db_exec($sql) or errDie("Could not retrieve Categories Information from the Database.", SELF);
    if (pg_numrows($accRslt) < 1) {
        return "<li class='err'> There are no Accounts in Cubit.</li>";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        $slctAcc .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
    }
    $slctAcc .= "\n\t\t\t\t\t\t</select>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='submit' name='details' value='View Transactions'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='3'><br></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Number</td>\n\t\t\t\t\t<td><input type='text' name='topacc' size='3' maxlength='3'> / <input type='text' name='accnum' size='3' maxlength='3'></td>\n\t\t\t\t\t<td><input type='submit' value='View Transactions'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Period</td>\n\t\t\t\t\t<td valign='center' colspan='3'>\n\t\t\t\t\t\t<select name='prd'>";
    db_conn(YR_DB);
    $sql = "SELECT * FROM info WHERE prdname !=''";
    $prdRslt = db_exec($sql);
    if (pg_numrows($prdRslt) < 1) {
        return "<li class='err'>ERROR : There are no periods set for the current year</li>";
    }
    while ($prd = pg_fetch_array($prdRslt)) {
        if ($prd['prddb'] == PRD_DB) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $slctAcc .= "<option value='{$prd['prddb']}' {$sel}>{$prd['prdname']}</option>";
    }
    $slctAcc .= "\n\t\t\t\t\t\t</select>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td align='center'><input type='button' value='&laquo Back' onClick='javascript:history.back()'></td>\n\t\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<table " . TMPL_tblDflts . " width='100'>\n\t\t\t<tr><th>Quick Links</th></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $slctAcc;
}
function printBran()
{
    # Set up table to display in
    $printBran = "\r\n    <h3>View Branches</h3>\r\n    <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n    <tr><th>Branch Code</th><th>Branch Name</th><th>Details</th><th colspan=2>Options</th></tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $sql = "SELECT * FROM branches ORDER BY branname ASC";
    $branRslt = db_exec($sql) or errDie("Unable to retrieve branches from database.");
    if (pg_numrows($branRslt) < 1) {
        return "<li>There are no branches in Cubit.";
    }
    while ($bran = pg_fetch_array($branRslt)) {
        $printBran .= "<tr class='" . bg_class() . "'><td>{$bran['brancod']}</td><td align=center>{$bran['branname']}</td><td>{$bran['brandet']}</td><td><a href='admin-branedit.php?div={$bran['div']}'>Edit</a></td>";
        core_connect();
        $sql = "SELECT accid FROM accounts WHERE div = '{$bran['div']}' AND accnum != '999'";
        $cRslt = db_exec($sql) or errDie("Unable to retrieve branches from database.");
        if (pg_numrows($cRslt) < 1) {
            $printBran .= "<td><a href='admin-branrem.php?div={$bran['div']}'>Remove</a></td>";
        } else {
            $printBran .= "<td><br></td></tr>";
        }
        $printBran .= "</tr>";
        $i++;
    }
    $printBran .= "</table>\r\n    <p>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=15%>\r\n        <tr><td><br></td></tr>\r\n        <tr><th>Quick Links</th></tr>\r\n\t\t<tr class='bg-odd'><td><a href='admin-branadd.php'>Add Branch</a></td></tr>\r\n\t\t<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>\r\n\t</table>";
    return $printBran;
}
function bal($id)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid Cash Flow Statement number.");
    # display errors, if any
    if ($v->isError()) {
        $theseErrors = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $theseErrors .= "<li class=err>" . $e["msg"];
        }
        $theseErrors .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $theseErrors;
    }
    # connect to core DB
    core_connect();
    # get the Trial Balance
    $cf = new dbSelect("save_cashflow", "core", grp(m("cols", "output, date_trunc('day', gentime) as gentime"), m("where", "id='{$id}'")));
    $cf->run();
    if ($cf->num_rows() < 1) {
        return "<center><li> Invalid Cash Flow Statement Number.</li></center>";
    }
    $stmnt = $cf->fetch_array();
    $OUTPUT = base64_decode($stmnt['output']);
    if (isset($_GET["xls"])) {
        $cftime = preg_replace("/ 00:00.*/", "", $stmnt["gentime"]);
        require_lib("xls");
        Stream("cashflow-{$cftime}", $OUTPUT);
    } else {
        require "../tmpl-print.php";
    }
}
function enterAllow($id)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid reimbursement ID.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class=err>" . $e["msg"];
        }
        $confirmCust .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    # get deduction info
    db_connect();
    $sql = "SELECT * FROM rbs WHERE id='{$id}' AND div = '" . USER_DIV . "'";
    $allowRslt = db_exec($sql) or errDie("Unable to select allowance info from database.");
    if (pg_numrows($allowRslt) < 1) {
        return "Invalid reimbursement ID.";
    }
    $myAllow = pg_fetch_array($allowRslt);
    # get ledger account name
    core_connect();
    $sql = "SELECT accname FROM accounts WHERE accid = '{$myAllow['account']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $acc = pg_fetch_array($accRslt);
    $enterAllow = "<h3>Edit reimbursement</h3>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<form action='" . SELF . "' method=post>\n\t<input type=hidden name=key value=confirm>\n\t<input type=hidden name=id value='{$id}'>\n\t<tr><th>Field</th><th>Value</th></tr>\n\t<tr class='bg-odd'><td>Name of reimbursement</td><td align=center><input type=text size=20 name=name value='{$myAllow['name']}'></td></tr>\n\t<tr class='bg-even'><td>Account</td><td align=center>{$acc['accname']}</td></tr>\n\t<tr><td colspan=2 align=right><input type=submit value='Confirm &raquo;'></td></tr>\n\t</form>\n\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $enterAllow;
}
function inc($id)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid Income Statement number.");
    # display errors, if any
    if ($v->isError()) {
        $theseErrors = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $theseErrors .= "<li class=err>" . $e["msg"];
        }
        $theseErrors .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $theseErrors;
    }
    # connect to core DB
    core_connect();
    # get the income statement
    $sql = "SELECT * FROM save_income_stmnt WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $incRslt = db_exec($sql) or errDie("Unable to retrieve income statement from the Database", SELF);
    if (pg_numrows($incRslt) < 1) {
        return "<center><li> Invalid Income Statement Number.";
    }
    $inc = pg_fetch_array($incRslt);
    $income = base64_decode($inc['output']);
    $OUTPUT = $income;
    require "../tmpl-print.php";
}
function printDept()
{
    # Set up table to display in
    $printDept = "\r\n    <h3>Departments</h3>\r\n    <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n    <tr><th>Dept No</th><th>Department</th><th>Income Account</th><th>Debtors Control Account</th><th>Creditors Control Account</th></tr>";
    # connect to database
    db_conn("exten");
    # Query server
    $i = 0;
    $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
    $deptRslt = db_exec($sql) or errDie("Unable to retrieve Departments from database.");
    if (pg_numrows($deptRslt) < 1) {
        return "<li>There are no Departments in Cubit.";
    }
    while ($dept = pg_fetch_array($deptRslt)) {
        # get ledger account name
        core_connect();
        $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['incacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $accinc = pg_fetch_array($accRslt);
        # get debtors account name
        $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['debtacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $accdebt = pg_fetch_array($accRslt);
        # get creditors account name
        $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['credacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acccred = pg_fetch_array($accRslt);
        $printDept .= "<tr class='" . bg_class() . "'><td>{$dept['deptno']}</td><td>{$dept['deptname']}</td><td>{$accinc['accname']}</td><td>{$accdebt['accname']}</td><td>{$acccred['accname']}</td><td><a href='dept-edit.php?deptid={$dept['deptid']}'>Edit</a></td>";
        $printDept .= "<td><a href='dept-rem.php?deptid={$dept['deptid']}'>Remove</a></td></tr>";
        $i++;
    }
    $printDept .= "</table>\r\n    <p>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=15%>\r\n        <tr><td><br></td></tr>\r\n        <tr><th>Quick Links</th></tr>\r\n\t\t<tr class='bg-odd'><td><a href='dept-add.php'>Add Department</a></td></tr>\r\n\t\t<tr class='bg-odd'><td><a href='../main.php'>Main Menu</a></td></tr>\r\n\t</table>";
    return $printDept;
}
function view()
{
    $view = "<center><table width=90%>\n        <tr><td width=80%><h3>View Returned Purchases</h3></td>\n        <td class='bg-even'><a href='purchase-new.php'>Add New Purchase</a><br>\n        <a href='purchase-ret.php'>Return Purchases</a>\n        </td></tr>\n        </table><br>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=90%>\n        <tr><th>Item Name</th><th>Description</th><th>Quantity</th><th>Total Cost</th><th>Payment Method</th><th>Item Account</th><th>Account Used</th></tr>";
    core_connect();
    $sql = "SELECT * FROM purch_ret ORDER BY purchid DESC";
    $purchRslt = db_exec($sql);
    if (pg_numrows($purchRslt) < 1) {
        return "<li class=err> There are no returned purchases in Cubit yet";
    } else {
        $i = 0;
        // for bgcolor
        while ($purch = pg_fetch_array($purchRslt)) {
            foreach ($purch as $key => $value) {
                ${$key} = $value;
            }
            # get used account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$usedacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $usedaccname = $acc['accname'];
            # get paid account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$paidacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $paidaccname = $acc['accname'];
            $view .= "<tr class='" . bg_class() . "'><td>{$itemname}</td><td>{$descript}</td><td>{$quantity}</td><td>" . CUR . " {$tlcost}</td><td>{$paytype}</td><td>{$paidaccname}</td><td>{$usedaccname}</td></tr>";
            $i++;
        }
    }
    $view .= "</table>\n\t\t<p>\n\t\t<table border=0 cellpadding='2' cellspacing='1'>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $view;
}
function add($id)
{
    db_conn('cubit');
    $sql = "SELECT * FROM batch_cashbook WHERE cashid='{$id}'";
    $accntRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve bank deposits details from database.", SELF);
    $cd = pg_fetch_array($accntRslt);
    extract($cd);
    # Accounts Drop down selections
    core_connect();
    # Income accounts ($inc)
    $glacc = "<select name='accinv'>";
    $sql = "SELECT * FROM accounts WHERE accid='{$accinv}'";
    $accRslt = db_exec($sql);
    $numrows = pg_numrows($accRslt);
    $acc = pg_fetch_array($accRslt);
    # Check Disable
    $glacc = $acc['accname'];
    db_connect();
    $sql = "SELECT * FROM bankacct WHERE bankid='{$bankid}'";
    $banks = db_exec($sql);
    if (pg_numrows($banks) < 1) {
        return "<li class=err> There are no accounts held at the selected Bank.\n                <p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct Selection'>";
    }
    $acc = pg_fetch_array($banks);
    $bank = "{$acc['accname']} - {$acc['bankname']} ({$acc['acctype']})";
    # layout
    $add = "\n        \t\t\t<h3>Delete BAtch cashbook entry</h3>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\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='remove'>\n\t\t\t\t\t\t<input type='hidden' name='id' value='{$id}'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Field</th>\n\t\t\t\t\t\t\t<th>Value</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>Bank Account</td>\n\t\t\t\t\t\t\t<td valign='center'>{$bank}</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>Paid to/Received From</td>\n\t\t\t\t\t\t\t<td valign='center'>{$name}</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'>Description</td>\n\t\t\t\t\t\t\t<td>{$descript}</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>Cheque Number</td>\n\t\t\t\t\t\t\t<td valign='center'>{$cheqnum}</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>Amount</td>\n\t\t\t\t\t\t\t<td valign='center'>" . CUR . " {$amount}</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'>Contra Account</td>\n\t\t\t\t\t\t\t<td>{$glacc}</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></td>\n\t\t\t\t\t\t\t<td valign='center' align='right'><input type='submit' value='Delete &raquo;'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>";
    # main table (layout with menu)
    $OUTPUT = "\n        \t\t\t<center>\n\t\t\t        <table width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='65%' align='left'>{$add}</td>\n\t\t\t\t\t\t\t<td valign='top' align='center'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='65%'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th>Quick Links</th>\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</tr>\n\t\t\t        </table>";
    return $OUTPUT;
}
function bal($id)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid Trial Balance number.");
    # display errors, if any
    if ($v->isError()) {
        $theseErrors = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $theseErrors .= "<li class=err>" . $e["msg"];
        }
        $theseErrors .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $theseErrors;
    }
    # connect to core DB
    core_connect();
    # get the Trial Balance
    $sql = "SELECT * FROM save_trial_bal WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $balRslt = db_exec($sql) or errDie("Unable to retrieve Trial Balance from the Database", SELF);
    if (pg_numrows($balRslt) < 1) {
        return "<center><li> Invalid Trial Balance Number.";
    }
    $bal = pg_fetch_array($balRslt);
    $balance = base64_decode($bal['output']);
    $OUTPUT = $balance;
    include "temp.xls.php";
    Stream("TB", $OUTPUT);
}
function viewDeducts()
{
    # connect to db
    db_connect();
    # select entries from db
    $i = 0;
    $viewDeducts = "\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Reference no</th>\n\t\t\t\t<th>Deduction name</th>\n\t\t\t\t<th>Creditor name</th>\n\t\t\t\t<th>Account</th>\n\t\t\t\t<th>Expense Account</th>\n\t\t\t\t<th>Creditor details</th>\n\t\t\t\t<th>Type</th>\n\t\t\t</tr>";
    $sql = "SELECT * FROM salded WHERE div = '" . USER_DIV . "' ORDER BY refno";
    $salRslt = db_exec($sql) or errDie("Unable to select salary deductions from database.");
    if (pg_numrows($salRslt) < 1) {
        return "\n\t\t\t<li class='err'>No salary deductions found in database.</li><br>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    }
    while ($mySal = pg_fetch_array($salRslt)) {
        # get ledger account name
        core_connect();
        $sql = "SELECT accname FROM accounts WHERE accid = '{$mySal['accid']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acc = pg_fetch_array($accRslt);
        if (isset($mySal["expaccid"])) {
            $sql = "SELECT accname FROM accounts WHERE accid='{$mySal['expaccid']}' AND div = '" . USER_DIV . "'";
            $expRslt = db_exec($sql);
            $exp = pg_fetch_array($expRslt);
        } else {
            $exp["accname"] = "";
        }
        $viewDeducts .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$mySal['refno']}</td>\n\t\t\t\t<td>{$mySal['deduction']}</td>\n\t\t\t\t<td>{$mySal['creditor']}</td>\n\t\t\t\t<td>{$acc['accname']}</td>\n\t\t\t\t<td>{$exp['accname']}</td>\n\t\t\t\t<td>{$mySal['details']}</td>\n\t\t\t\t<td>{$mySal['type']}</td>\n\t\t\t\t<td><a href='salded-edit.php?refno={$mySal['refno']}'>Edit</a></td>\n\t\t\t</tr>\n";
        $i++;
    }
    $viewDeducts .= "</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $viewDeducts;
}
function printGrp()
{
    # Set up table to display in
    $printGrp = "\r\n    <h3>Asset Groups</h3>\r\n    <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n    <tr><th>Group</th><th>Cost Account</th><th>Accumulated Depreciation Account</th><th>Depreciation Account</th><th colspan=2>Options</th></tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $sql = "SELECT * FROM assetgrp WHERE div = '" . USER_DIV . "' ORDER BY grpname ASC";
    $GrpRslt = db_exec($sql) or errDie("Unable to retrieve Asset Groups from database.");
    if (pg_numrows($GrpRslt) < 1) {
        return "\r\n\t\t\t\t<li> There are no Assets Groups in Cubit.</li><br>" . mkQuickLinks(ql("assetgrp-new.php", "Add Asset Group"), ql("assetgrp-view.php", "View Asset Groups"));
    }
    while ($Grp = pg_fetch_array($GrpRslt)) {
        # get ledger account name(cost)
        core_connect();
        $sql = "SELECT accname FROM accounts WHERE accid = '{$Grp['costacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acccost = pg_fetch_array($accRslt);
        # get ledger account name(accum dep)
        $sql = "SELECT accname FROM accounts WHERE accid = '{$Grp['accdacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acdacc = pg_fetch_array($accRslt);
        # get ledger account name(dep)
        $sql = "SELECT accname FROM accounts WHERE accid = '{$Grp['depacc']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $accdep = pg_fetch_array($accRslt);
        # alternate bgcolor
        $printGrp .= "<tr class='" . bg_class() . "'><td>{$Grp['grpname']}</td><td>{$acccost['accname']}</td><td>{$acdacc['accname']}</td><td>{$accdep['accname']}</td><td><a href='assetgrp-edit.php?grpid={$Grp['grpid']}'>Edit</a></td>";
        $printGrp .= "<td><a href='assetgrp-rem.php?grpid={$Grp['grpid']}'>Remove</a></td></tr>";
        $i++;
    }
    $printGrp .= "</table>\r\n    <p>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=15%>\r\n        <tr><td><br></td></tr>\r\n        <tr><th>Quick Links</th></tr>\r\n\t\t<tr class='bg-odd'><td><a href='assetgrp-new.php'>Add Asset Group</a></td></tr>\r\n\t\t<script>document.write(getQuicklinkSpecial());</script>\r\n\t</table>";
    return $printGrp;
}
function slctacc()
{
    // connect
    db_conn(PRD_DB);
    $refnum = getrefnum();
    /*refnum*/
    // Accounts (debit)
    $view = "<center>\n        <h3> Journal transaction </h3>\n        <br><br>\n        <form action='" . SELF . "' method=post name=form>\n        <input type=hidden name=key value=details>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n        <tr><th>Field</th><th>Value</th></tr>\n        <tr class='bg-odd'><td>Date</td><td><input type=text size=2 name=day maxlength=2  value='" . date("d") . "'>-<input type=text size=2 name=mon maxlength=2  value='" . date("m") . "'>-<input type=text size=4 name=year maxlength=4 value='" . date("Y") . "'></td></tr>\n        <tr class='bg-odd'><td>Reference Number</td><td><input type=text size=10 name=refnum value='" . $refnum++ . "'></td></tr>\n        <tr><td><br></td></tr>\n        <tr><td align=center>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n                <tr><td><h4>Debit</h4></td></tr>\n                <tr><th>Select Account</th></tr>\n                <tr class='bg-even'>\n                <td valign=center>\n                <select name='dtaccid'>";
    core_connect();
    $sql = "SELECT * FROM accounts WHERE div = '" . USER_DIV . "' ORDER BY accname ASC";
    $accRslt = db_exec($sql);
    if (pg_numrows($accRslt) < 1) {
        return "<li>There are No accounts in Cubit.";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        $view .= "<option value='{$acc['accid']}'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</option>";
    }
    $view .= "</select></td></tr>\n                </table>\n        </td>\n        <td align=center>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n                <tr><td><h4>Credit</h4></td></tr>\n                <tr><th>Select Account</th></tr>\n                <tr class='bg-even'>\n                <td valign=center>\n                <select name=ctaccid>";
    $sql = "SELECT * FROM accounts WHERE div = '" . USER_DIV . "' ORDER BY accname ASC";
    $accRslt = db_exec($sql);
    if (pg_numrows($accRslt) < 1) {
        return "<li>There are No accounts in Cubit.";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        $view .= "<option value='{$acc['accid']}'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</option>";
    }
    $view .= "</select>\n                </td><td><input name=details type=submit value='Enter Details >'></td></tr>\n                </table>\n        </td></tr>\n        </table><br><br><br>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n        <td align=center>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n                        <tr><td><h4>Debit</h4></td></tr>\n                        <tr><th>Account number</th></tr>\n                        <tr class='bg-even'><td valign=center><input type=text name=dtaccnum size=20></td></tr>\n                </table>\n        </td>\n        <td align=center>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n                        <tr><td><h4>Credit</h4></td></tr>\n                        <tr><th>Account number</th></tr>\n                        <tr class='bg-even'>\n                <td valign=center><input type=text name=ctaccnum size=20></td><td><input type=submit value='Enter Details >'></td></tr></table>\n        </td></tr>\n        </table>\n        <br>\n        <input type=button value='< Go Back' onClick='javascript:history.back();'>\n        </form>\n        <table border=0 cellpadding='2' cellspacing='1' width=15%>\n\t<tr><td><br></td></tr>\n\t<tr><th>Quick Links</th></tr>\n\t<tr class=datacell><td align=center><a href='trans-new.php'>Journal Transactions</td></tr>\n\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table></center>";
    return $view;
}
function slctAcc()
{
    // Layout
    $slctAcc = "<h3>Select Account</h3>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<form action='" . SELF . "' method=post>\n\t<input type=hidden name=key value=viewtran>\n\t<tr><th>Field</th><th>Value</th></tr>\n\t<tr class='bg-even'><td>Account Name</td><td><select name=accid>";
    core_connect();
    $sql = "SELECT * FROM accounts ORDER BY accname ASC";
    $accRslt = db_exec($sql) or errDie("Could not retrieve Categories Information from the Database.", SELF);
    if (pg_numrows($accRslt) < 1) {
        return "<li class=err> There are no Accounts in Cubit.";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        $branname = branname($acc['div']);
        $slctAcc .= "<option value='{$acc['accid']}'>{$acc['accname']} - {$branname}</option>";
    }
    $slctAcc .= "</select></td><td><input type=submit name=details value='View Transactions'></td></tr>\n\t<tr class='bg-odd'><td colspan=3><br></td></tr>\n\t<tr class='bg-even'><td>Account Number</td><td><input type=text name=topacc size=3 maxlength=3> / <input type=text name=accnum size=3 maxlength=3></td><td><input type=submit value='View Transactions'></td></tr>\n\t<tr class='bg-odd'><td>Select Period</td><td valign=center colspan=3>\n\t<select name=prd>";
    db_conn(YR_DB);
    $sql = "SELECT * FROM info WHERE prdname !=''";
    $prdRslt = db_exec($sql);
    if (pg_numrows($prdRslt) < 1) {
        return "<li class=err>ERROR : There are no periods set for the current year";
    }
    while ($prd = pg_fetch_array($prdRslt)) {
        if ($prd['prddb'] == PRD_DB) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $slctAcc .= "<option value='{$prd['prddb']}' {$sel}>{$prd['prdname']}</option>";
    }
    $slctAcc .= "\n\t</select></td></tr>\n\t<tr><td align=center><input type=button value='&laquo Back' onClick='javascript:history.back()'></td></tr>\n\t</form>\n\t</table>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=100>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<tr class=datacell><td align=center><a target=_blank href='../core/acc-new2.php'>Add account (New Window)</a></td></tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $slctAcc;
}
function add()
{
    core_connect();
    # Get Petty cash account
    $cashacc = gethook("accnum", "bankacc", "name", "Petty Cash");
    # Get account name for thy lame User's Sake
    $accRslt = get("core", "*", "accounts", "accid", $cashacc);
    if (pg_numrows($accRslt) < 1) {
        return "<li class='err'> Petty Cash Account not found.</li>";
    }
    $acc = pg_fetch_array($accRslt);
    # Check available funds
    $cashacc = gethook("accnum", "bankacc", "name", "Petty Cash");
    core_connect();
    $sql = "SELECT (debit - credit) as bal FROM trial_bal WHERE month='" . PRD_DB . "' AND accid = '{$cashacc}' AND div = '" . USER_DIV . "'";
    $accbRslt = db_exec($sql);
    if (pg_numrows($accbRslt) < 1) {
        return "<li class='err'> Petty Cash Account not found.</li>";
    }
    $accb = pg_fetch_array($accbRslt);
    $accb['bal'] = sprint($accb['bal']);
    # mourn if the is no money
    if ($accb['bal'] < 1) {
        return "\n\t\t\t<li class='err'> There are no Petty Cash funds available.</li>\n\t\t\t<p>\n\t\t\t<table " . TMPL_tblDflts . ">\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='petty-trans.php'>Transfer funds to petty cash account</a></td>\n\t\t\t\t</tr>\n\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t</table>";
    }
    // Layout
    $add = "\n\t\t<h3>Funds transfer to Bank</h3>\n\t\t<table " . TMPL_tblDflts . " width='300'>\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<tr>\n\t\t\t\t<th>Field</th>\n\t\t\t\t<th>Value</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Bank Account</td>\n\t\t\t\t<td valign='center'>\n\t\t\t\t\t<select name='bankid'>";
    db_connect();
    $sql = "SELECT * FROM bankacct WHERE btype != 'int' AND div = '" . USER_DIV . "'";
    $banks = db_exec($sql);
    if (pg_numrows($banks) < 1) {
        return "<li class='err'> There are no accounts held at the selected Bank.</li>\n\t\t<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct Selection'>";
    }
    while ($bacc = pg_fetch_array($banks)) {
        $add .= "<option value='{$bacc['bankid']}'>({$bacc['acctype']}) {$bacc['accname']} - {$bacc['bankname']}</option>";
    }
    if (!isset($date_day)) {
        $trans_date_setting = getCSetting("USE_TRANSACTION_DATE");
        if (isset($trans_date_setting) and $trans_date_setting == "yes") {
            $trans_date_value = getCSetting("TRANSACTION_DATE");
            $date_arr = explode("-", $trans_date_value);
            $date_year = $date_arr[0];
            $date_month = $date_arr[1];
            $date_day = $date_arr[2];
        } else {
            $date_year = date("Y");
            $date_month = date("m");
            $date_day = date("d");
        }
    }
    $add .= "\n\t\t\t\t\t</select>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Date</td>\n\t        \t<td>" . mkDateSelect("date", $date_year, $date_month, $date_day) . "</td>\n\t        </tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Received from</td>\n\t        \t<td valign='center'><input size='20' name='name' value='Petty Cash'></td>\n\t        </tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Description</td>\n\t        \t<td valign='center'><textarea cols='18' rows='2' name='descript'></textarea></td>\n\t        </tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Cheque Number</td>\n\t        \t<td valign='center'><input size='10' name='cheqnum'></td>\n\t        </tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Amount</td>\n\t        \t<td valign='center'>" . CUR . " <input type='text' size='10' name='amount'>&nbsp;&nbsp;Max : " . CUR . " {$accb['bal']}<input type='hidden' name='max' value='{$accb['bal']}'></td>\n\t        </tr>\n\t        <tr class='" . bg_class() . "'>\n\t        \t<td>Petty Cash Account</td>\n\t        \t<td><input type='hidden' name='accinv' value='{$acc['accid']}'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</td>\n\t        </tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td><input type='button' value='< Cancel' onClick='javascript:history.back();'></td>\n\t\t\t\t<td valign='center'><input type='submit' value='Confirm >'></td>\n\t\t\t</tr>\n\t\t</table>";
    # main table (layout with menu)
    $OUTPUT = "\n\t\t\t<center>\n\t        <table width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td width='65%' align='left'>{$add}</td>\n\t\t\t\t\t<td valign='top' align='center'>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='65%'>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t        </table>";
    return $OUTPUT;
}
function viewLedg($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    # Search by refnum
    if ($search == "lname") {
        $v->isOk($lname, "string", 1, 255, "Invalid Search String.");
        $lname = strtolower($lname);
        # Create the Search SQL
        $search = "SELECT * FROM in_ledgers WHERE lower(lname) LIKE '%{$lname}%' AND div = '" . USER_DIV . "' ORDER BY lname ASC";
    }
    # View all
    if ($search == "all") {
        # create the Search SQL
        $search = "SELECT * FROM in_ledgers WHERE div = '" . USER_DIV . "' ORDER BY lname ASC";
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    core_connect();
    // Layout
    $viewLedg = "\r\n\t\t<center>\r\n\t\t<h3>View High Speed Input Ledgers</h3>\r\n\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t<tr>\r\n\t\t\t\t<th>Ledger Name</th>\r\n\t\t\t\t<th>Debit</th>\r\n\t\t\t\t<th>Credit</th>\r\n\t\t\t\t<th colspan='10'>Options</th>\r\n\t\t\t</tr>";
    $ledRslt = db_exec($search) or errDie("ERROR: Unable to retrieve High Speed Input ledgers from database.", SELF);
    if (pg_numrows($ledRslt) < 1) {
        return "\r\n\t\t<li> No High Speed input ledgers found.<br><br>\r\n\t\t<table " . TMPL_tblDflts . " width='25%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<th>Quick Links</th>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='ledger-new.php'>New High Speed Input Ledger</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='../main.php'>Main Menu</td>\r\n\t\t\t</tr>\r\n\t\t</table>";
    }
    # display all transaction
    $i = 0;
    while ($led = pg_fetch_array($ledRslt)) {
        #get vars from tran as the are in db
        foreach ($led as $key => $value) {
            ${$key} = $value;
        }
        # get account names
        $deb = get("core", "accname, topacc, accnum", "accounts", "accid", $dtaccid);
        $debacc = pg_fetch_array($deb);
        $ct = get("core", "accname, topacc,accnum", "accounts", "accid", $ctaccid);
        $ctacc = pg_fetch_array($ct);
        $viewLedg .= "\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td><a href='ledger-run.php?ledgid={$ledgid}'>{$led['lname']}</a></td>\r\n\t\t\t\t<td>{$debacc['topacc']}/{$debacc['accnum']} - {$debacc['accname']}</td>\r\n\t\t\t\t<td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td>\r\n\t\t\t\t<td><a href='ledger-det.php?ledgid={$ledgid}'>View Details</a></td>\r\n\t\t\t\t<td>&nbsp;&nbsp;<a href='ledger-run.php?ledgid={$ledgid}'>Run</a>&nbsp;&nbsp;</td>\r\n\t\t\t\t<td><a href='ledger-edit.php?ledgid={$ledgid}'>Edit</a></td>\r\n\t\t\t\t<td><a href='ledger-rem.php?ledgid={$ledgid}'>Delete</a></td>\r\n\t\t\t</tr>";
        $i++;
    }
    $viewLedg .= "\r\n\t\t</table>\r\n\t\t<p>\r\n\t\t<table " . TMPL_tblDflts . " width='25%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<th>Quick Links</th>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='ledger-new.php'>New High Speed Input Ledger</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='ledger-view.php'>View High Speed Input Ledger</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='../main.php'>Main Menu</td>\r\n\t\t\t</tr>\r\n\t\t</table>";
    return $viewLedg;
}
function view()
{
    $view = "<center><table width=90%>\n        <tr><td width=80%><h3>View Purchases</h3></td>\n        <td class='bg-even'><a href='purchase-new.php'>Add New Purchase</a></td></tr>\n        </table><br>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=90%>\n        <tr><th>Item Name</th><th>Description</th><th>Quantity</th><th>Total Cost</th><th>Payment Method</th><th>Item Account</th><th>Account Used</th></tr>";
    $credit = "";
    core_connect();
    $sql = "SELECT * FROM purchases ORDER BY purchid DESC";
    $purchRslt = db_exec($sql);
    if (pg_numrows($purchRslt) < 1) {
        return "<li class=err> There are no purchased items in Cubit yet";
    } else {
        $i = 0;
        // for bgcolor
        while ($purch = pg_fetch_array($purchRslt)) {
            foreach ($purch as $key => $value) {
                ${$key} = $value;
            }
            # get used account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$usedacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $usedaccname = $acc['accname'];
            # get paid account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$paidacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $paidaccname = $acc['accname'];
            # if credit => check if it is paid if not give option to pay
            if ($paytype == "Credit") {
                $sql = "SELECT * FROM credit_purch WHERE purchid = '{$purchid}' AND amount > 0";
                $ctRslt = db_exec($sql);
                if (pg_numrows($ctRslt) > 0) {
                    # get credit info
                    $ct = pg_fetch_array($ctRslt);
                    $credit .= "<tr class='" . bg_class() . "'><td>{$itemname}</td><td>{$descript}</td><td>{$quantity}</td><td>" . CUR . " {$tlcost}</td><td>" . CUR . " {$ct['amount']}</td><td>{$paytype}</td><td>{$paidaccname}</td><td>{$usedaccname}</td>";
                    $credit .= "<td><a href='credit-purch-pay.php?purchid={$purchid}'> Pay</a></td></tr>";
                } else {
                    $view .= "<tr class='" . bg_class() . "'><td>{$itemname}</td><td>{$descript}</td><td>{$quantity}</td><td>" . CUR . " {$tlcost}</td><td>{$paytype}</td><td>{$paidaccname}</td><td>{$usedaccname}</td></tr>";
                }
            } else {
                $view .= "<tr class='" . bg_class() . "'><td>{$itemname}</td><td>{$descript}</td><td>{$quantity}</td><td>" . CUR . " {$tlcost}</td><td>{$paytype}</td><td>{$paidaccname}</td><td>{$usedaccname}</td></tr>";
            }
            $i++;
        }
    }
    # view outstanding credits if they exits
    if (strlen($credit) > 5) {
        $view .= "<tr><td colspan=7><br></td></tr>\n                <tr><td colspan=7><h4>Outstanding Credit Purchases</h4></td></tr>\n                <tr><th>Item Name</th><th>Description</th><th>Quantity</th><th>Total Cost</th><th>Outstanding amount</th><th>Payment Method</th><th>Item Account</th><th>Account Used</th></tr>\n                {$credit}\n                </table>\n\n\n\t";
    } else {
        $view .= "</table>";
    }
    $view .= "\n\n<p>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<tr><th>Quick Links</th></tr>\n\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $view;
}
function recon($id)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid Bank Recon number.");
    # display errors, if any
    if ($v->isError()) {
        $theseErrors = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $theseErrors .= "<li class=err>" . $e["msg"];
        }
        $theseErrors .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $theseErrors;
    }
    # connect to core DB
    core_connect();
    # get the Bank Recon
    $sql = "SELECT * FROM save_bank_recon WHERE id = '{$id}'";
    $recRslt = db_exec($sql) or errDie("Unable to retrieve Bank Recon from the Database", SELF);
    if (pg_numrows($recRslt) < 1) {
        return "<center><li> Invalid Bank Recon Number.";
    }
    $rec = pg_fetch_array($recRslt);
    $recon = base64_decode($rec['recon']);
    db_conn('cubit');
    $payments = "<p>\r\n\t<h3>Reconciled Payments</h3>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='80%'>\r\n\t<tr><td><b>Date</b></td><td><b>To</b></td><td><b>Description</b></td><td><b>Cheque</b></td><td><b>Amount</b></td></tr>";
    $Sl = "SELECT * FROM cashbook WHERE rid='{$id}' AND trantype='withdrawal'";
    $Ri = db_exec($Sl) or errDie("Unable to get data,");
    if (pg_num_rows($Ri) > 0) {
        while ($data = pg_fetch_array($Ri)) {
            $payments .= "<tr><td>{$data['date']}</td><td>{$data['name']}</td><td>{$data['descript']}</td><td>{$data['cheqnum']}</td><td align=right>" . CUR . " " . sprint($data['amount']) . "</td></tr>";
        }
        $payments .= "</table>";
    } else {
        $payments = "<p>There were no reconciled payments.";
    }
    $depos = "<p>\r\n\t<h3>Reconciled Deposits</h3>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='80%'>\r\n\t<tr><td><b>Date</b></td><td><b>From</b></td><td><b>Description</b></td><td><b>Cheque</b></td><td><b>Amount</b></td></tr>";
    $Sl = "SELECT * FROM cashbook WHERE rid='{$id}' AND trantype='deposit'";
    $Ri = db_exec($Sl) or errDie("Unable to get data,");
    if (pg_num_rows($Ri) > 0) {
        while ($data = pg_fetch_array($Ri)) {
            $depos .= "<tr><td>{$data['date']}</td><td>{$data['name']}</td><td>{$data['descript']}</td><td>{$data['cheqnum']}</td><td align=right>" . CUR . " " . sprint($data['amount']) . "</td></tr>";
        }
        $depos .= "</table>";
    } else {
        $depos = "<p>There were no reconciled deposits.";
    }
    $OUTPUT = $recon . "</table>" . $payments . $depos;
    require "../tmpl-print.php";
}
function prnDebtors()
{
    // Connect to database
    db_connect();
    $sql = "SELECT * FROM debtors_batch WHERE proc = 'no'";
    $batRslt = db_exec($sql);
    if (pg_numrows($batRslt) < 1) {
        return "<li class=err> There are no outstanding debtors in Cubit.";
    } else {
        // Layout
        $view = "<center><h3>Debtors Outstanding Batch Entries</h3>\r\n                <form action='debtors-batch-proc.php' method=post>\r\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=90%>\r\n                <tr><th>Customer Name</th><th>Order Date</th><th>Invoice Date</th><th>Outstanding Amount</th><th>Terms</th><th>Amount Paid</th><th>Account Paid</th></tr>";
        $i = 0;
        // for bgcolor
        $tot = 0;
        while ($bat = pg_fetch_array($batRslt)) {
            db_connect();
            # Get all the details
            $sql = "SELECT * FROM credit_invoices WHERE ordnum = '{$bat['ordnum']}'";
            $invRslt = db_exec($sql) or errDie("Unable to access database.");
            if (pg_numrows($invRslt) < 1) {
                return "<li class=err> - Invalid ord number.";
            }
            $inv = pg_fetch_array($invRslt);
            # Get debt invoice info
            $sql = "SELECT * FROM debtors WHERE ordnum ='{$bat['ordnum']}'";
            $dtRslt = db_exec($sql) or errDie("Unable to retrieve invoice details from database.");
            if (pg_numrows($dtRslt) < 1) {
                return "<li class=err>Invalid Invoice Number.";
            }
            $dt = pg_fetch_array($dtRslt);
            foreach ($inv as $keys => $values) {
                ${$keys} = $values;
            }
            # get paid account name
            core_connect();
            $sql = "SELECT accname FROM accounts WHERE accid = '{$bat['accpaid']}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            foreach ($inv as $keys => $values) {
                ${$keys} = $values;
            }
            $view .= "<tr class='bg-odd'>\r\n                                <td>{$cusname}</td>\r\n                                <td align=center>{$orddate}</td>\r\n                                <td align=center>{$invdate}</td>\r\n                                <td align=center>" . CUR . " {$dt['amount']}</td>\r\n                                <td align=center>{$dt['terms']} days</td>\r\n                                <td align=center>" . CUR . " {$bat['paidamt']}</td>\r\n                                <td align=center>{$acc['accname']}</td>\r\n                                <td align=center><input type=checkbox name='ord[]' value='{$bat['batchid']}'></td>\r\n                            </tr>";
            $tot += $bat['paidamt'];
        }
        $view .= "<tr><td><br></td></tr>\r\n                <tr class='bg-even'><td colspan=5><b>Total Amount Received</b></td><td colspan=2><b>" . CUR . " " . sprintf("%01.2f", round($tot, 2)) . "</b></td></tr>\r\n                <tr><td><br></td></tr>\r\n                <tr><td colspan=6><br></td><td colspan=2 align=right><input type=submit value='Process Selected' name=proc> <input type=submit value='Remove Selected' name=rem></td><tr></table>\r\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=100>\r\n                        <tr><th>Quick Links</th></tr>\r\n                        <tr bgcolor='#88BBFF'><td><a href='debtors-view.php'>View Debtors</a></td></tr>\r\n                        <tr bgcolor='#88BBFF'><td><a href='reporting/debtors-age.php'>Debtors - Age</a></td></tr>\r\n                        <script>document.write(getQuicklinkSpecial());</script>\r\n                        <tr bgcolor='#88BBFF'><td><a href='main.php'>Main Menu</a></td></tr>\r\n                </table>";
    }
    return $view;
}
function slctAcc()
{
    // Layout
    $slctAcc = "\n\t\t<h3>Select Account</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t<input type='hidden' name='key' value='viewtran'>\n\t\t\t<tr>\n\t\t\t\t<th>Field</th>\n\t\t\t\t<th>Value</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Account Name</td>\n\t\t\t\t<td>\n\t\t\t\t\t<select name='accid'>";
    core_connect();
    $sql = "SELECT * FROM accounts WHERE div = '" . USER_DIV . "' ORDER BY accname ASC";
    $accRslt = db_exec($sql) or errDie("Could not retrieve Categories Information from the Database.", SELF);
    if (pg_numrows($accRslt) < 1) {
        return "<li class='err'> There are no Accounts in Cubit.</li>";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        $slctAcc .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
    }
    $slctAcc .= "\n\t\t\t\t\t</select>\n\t\t\t\t</td>\n\t\t\t\t<td><input type='submit' name='details' value='View Transactions'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'><td colspan='3'><br></td></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Account Number</td>\n\t\t\t\t<td><input type='text' name='topacc' size='4' maxlength='4'> / <input type='text' name='accnum' size='3' maxlength='3'></td>\n\t\t\t\t<td><input type='submit' value='View Transactions'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Period</td>\n\t\t\t\t<td valign='center' colspan='3'>" . finMonList("prd", PRD_DB) . "</td>\n\t\t\t</tr>\n\t\t\t<tr><td></td></tr>\n\t\t</form>\n\t\t</table>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . " width='100'>\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 align='center'><a target=_blank href='../core/acc-new2.php'>Add account (New Window)</a></td></tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td align='center'><a href='index-reports.php'>Financials</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td align='center'><a href='index-reports-journal.php'>Current Year Details General Ledger Reports</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td align='center'><a href='../main.php'>Main Menu</a></td>\n\t\t\t</tr>\n\t\t</table>";
    return $slctAcc;
}
function win($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    /* --------------------------- bal ------------------------------*/
    $report = "";
    core_connect();
    $sql = "SELECT * FROM trialgrpaccids WHERE gkey = '{$gkey}'";
    $gaccRslt = db_exec($sql) or errDie("Unable to get group accounts information.");
    while ($gacc = pg_fetch_array($gaccRslt)) {
        $sql = "SELECT * FROM trial_bal WHERE accid = '{$gacc['accid']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql) or errDie("Unable to view account.");
        $acc = pg_fetch_array($accRslt);
        $report .= "<tr class='bg-even'><td>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</td>";
        if (true) {
            if ($acc['debit'] > $acc['credit']) {
                $acc['debit'] = sprint($acc['debit'] - $acc['credit']);
                $acc['credit'] = 0;
            }
            if ($acc['credit'] > $acc['debit']) {
                $acc['credit'] = sprint($acc['credit'] - $acc['debit']);
                $acc['debit'] = 0;
            }
            if ($acc['credit'] == $acc['debit']) {
                $acc['credit'] = 0;
                $acc['debit'] = 0;
            }
        }
        if (floatval($acc['debit']) == 0) {
            $report .= "<td align=right> - </td>";
        } else {
            $report .= "<td align=right>{$acc['debit']}</td>";
        }
        if (floatval($acc['credit']) == 0) {
            $report .= "<td align=right> - </td>";
        } else {
            $report .= "<td align=right>{$acc['credit']}</td>";
        }
        $report .= "</tr>";
    }
    /* -- Final Layout -- */
    $details = "<center><h3>Trial Balance Note 000/00{$gkey}</h3>\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=360>\r\n\t<tr><th>Accounts</th><th>Debit</th><th>Credit</th></tr>\r\n\t{$report}\r\n\t<tr><td><br></td></tr>\r\n\t<tr><td colspan=3 align=center><input type=button value='[X]Close' onClick='javascript:window.close();'></td></tr>\r\n\t</table>";
    return $details;
}
function view()
{
    core_connect();
    $sql = "SELECT * FROM accounts WHERE div = '" . USER_DIV . "' ORDER BY accname ASC";
    $accRslt = db_exec($sql) or errDie("Could not retrieve Categories Information from the Database.", SELF);
    if (pg_numrows($accRslt) < 1) {
        return "<li class='err'> There are no Accounts in Cubit.</li>";
    }
    $slctAcc = "\n\t\t<select name='accid'>\n\t\t\t<option value='0'>All Accounts</option>";
    while ($acc = pg_fetch_array($accRslt)) {
        $slctAcc .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
    }
    $slctAcc .= "</select>";
    $prds = finMonList("prd", PRD_DB);
    //layout
    $view = "\n\t\t<center>\n\t\t<h3>Journal Entries Report</h3>\n\t\t<table cellpadding='5' width='80%'>\n\t\t\t<tr>\n\t\t\t\t<td width='60%'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='510'>\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='viewtrans'>\n\t\t\t\t\t\t<input type='hidden' name='search' value='date'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>By Date Range</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 width='80%' align='center'>\n\t\t\t\t\t\t\t\t<table>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td>" . mkDateSelect("from") . "</td>\n\t\t\t\t\t\t\t\t\t\t<td>&nbsp;&nbsp;TO&nbsp;&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t<td>" . mkDateSelect("to") . "</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 class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Period  {$prds}</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<th colspan='2'>Select Account</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 colspan='2'>{$slctAcc}</td>\n\t\t\t\t\t\t\t<td valign='bottom'><input type='submit' value='Search &raquo;'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</form>\n\t\t\t\t\t" . TBL_BR . "\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>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='370'>\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='viewtrans'>\n\t\t\t\t\t\t<input type='hidden' name='search' value='refnum'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>By Journal number</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 width='80%' align='center'>\n\t\t\t\t\t\t\t\tFrom <input type='text' size='5' name='fromnum'> to <input type='text' size='5' name='tonum'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td rowspan='2' valign='bottom'><input type='submit' value='View All &raquo;'></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>Select Period {$prds}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</form>\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>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='370'>\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='viewtrans'>\n\t\t\t\t\t\t<input type='hidden' name='search' value='all'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>View All</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>Select Period {$prds}</td>\n\t\n\t\t\t\t\t\t\t<td rowspan='2' valign='bottom'><input type='submit' value='View All &raquo;'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</form>\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<p>\n\t\t<table " . TMPL_tblDflts . " width='25%'>\n\t\t\t" . TBL_BR . "\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='" . bg_class() . "'>\n\t\t\t\t<td align='center'><a target=_blank href='../core/acc-new2.php'>Add account (New Window)</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><a href='index-reports.php'>Financials</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><a href='index-reports-journal.php'>Current Year Details General Ledger Reports</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><a href='../main.php'>Main Menu</td>\n\t\t\t</tr>\n\t\t</table>";
    return $view;
}
function prnCreditors()
{
    // Connect to database
    core_connect();
    $sql = "SELECT * FROM credit_batch WHERE proc='no' ORDER BY batchid ASC";
    $batRslt = db_exec($sql);
    if (pg_numrows($batRslt) < 1) {
        return "<li class=err> - There are no Outstanding batched credit payments in Cubit.";
    } else {
        // Layout
        $view = "<center><h3>Outstanding Creditors Batch Entries</h3>\n                <form action='credit-batch-proc.php' method=post>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n                        <tr><th>Retailer</th><th>Item Name</th><th>Description</th><th>Quantity</th><th>Outstanding amount</th><th>Amount Paid</th><th>Account used</th><th>Account paid</th></tr>";
        $i = 0;
        // for bgcolor
        while ($bat = pg_fetch_array($batRslt)) {
            $sql = "SELECT * FROM purchases WHERE purchid='{$bat['purchid']}'";
            $ctRslt = db_exec($sql);
            if (pg_numrows($ctRslt) < 1) {
                return "<li class=err> Invalid Purchase No.";
            }
            $ct = pg_fetch_array($ctRslt);
            foreach ($ct as $key => $value) {
                ${$key} = $value;
            }
            # get credit infomation
            $sql = "SELECT * FROM credit_purch WHERE purchid = '{$purchid}' AND amount > 0";
            $ctRslt = db_exec($sql);
            $ct = pg_fetch_array($ctRslt);
            # get used account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$usedacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $usedaccname = $acc['accname'];
            # get paid account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$paidacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $paidaccname = $acc['accname'];
            $view .= "<tr class='bg-odd'>\n                                <td>{$retailer}</td>\n                                <td>{$itemname}</td>\n                                <td>{$descript}</td>\n                                <td>{$quantity}</td>\n                                <td>" . CUR . " {$ct['amount']}</td>\n                                <td>" . CUR . " {$bat['amount']}</td>\n                                <td>{$usedaccname}</td>\n                                <td>{$paidaccname}</td>\n                                <td><input type=checkbox name='bat[]' value='{$bat['batchid']}'><!--<a href='credit-batch-proc.php?batchid={$bat['batchid']}'>Process</a>--></td></tr>\n                        </tr>";
        }
    }
    $view .= "<tr><td colspan=6><br></td><td colspan=2 align=right><input type=submit value='Process Selected' name=proc> <input type=submit value='Cancel Selected' name=rem></td><tr>\n                </table>\n                <p>\n                <table border=0 cellpadding='2' cellspacing='1'>\n\t\t<tr><th>Quick Links</th></tr>\n                        <tr bgcolor='#88BBFF'><td><a href='creditors-view.php'>View Creditors</a></td></tr>\n                        <script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $view;
}
function prnCreditors()
{
    // Connect to database
    core_connect();
    $sql = "SELECT * FROM purchases WHERE paytype = 'Credit' ORDER BY purchid DESC";
    $prnCrtsRslt = db_exec($sql);
    if (pg_numrows($prnCrtsRslt) < 1) {
        return "<li class=err> There are no credit purchases yet in Cubit.";
    } else {
        // Layout
        $view = "<center><h3>Outstanding Credit Purchases</h3>\n                <form action='credit-multi-pay.php' method=post>\n                <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=90%>\n                <tr><th>Item Name</th><th>Description</th><th>Quantity</th><th>Total Cost</th><th>Outstanding amount</th><th>Terms</th><th>Item Account</th><th>Account Used</th></tr>";
        $i = 0;
        // for bgcolor
        while ($crt = pg_fetch_array($prnCrtsRslt)) {
            foreach ($crt as $key => $value) {
                ${$key} = $value;
            }
            # get credit infomation
            $sql = "SELECT * FROM credit_purch WHERE purchid = '{$purchid}' AND amount > 0";
            $ctRslt = db_exec($sql);
            $ct = pg_fetch_array($ctRslt);
            # get used account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$usedacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $usedaccname = $acc['accname'];
            # get paid account name
            $sql = "SELECT accname FROM accounts WHERE accid = '{$paidacc}'";
            $accRslt = db_exec($sql);
            $acc = pg_fetch_array($accRslt);
            $paidaccname = $acc['accname'];
            if ($ct['amount'] > 0) {
                $view .= "<tr class='" . bg_class() . "'><td>{$itemname}</td><td>{$descript}</td><td>{$quantity}</td><td>" . CUR . " {$tlcost}</td><td>" . CUR . " {$ct['amount']}</td><td>{$ct['terms']} {$ct['period']}</td><td>{$paidaccname}</td><td>{$usedaccname}</td>";
                $view .= "<td><input type=checkbox name='pay[]' value='{$purchid}'><a href='credit-purch-pay.php?purchid={$purchid}'> Pay</a></td></tr>";
            } else {
                continue;
            }
        }
    }
    $view .= "<tr><td colspan=6><br></td><td colspan=2 align=right><input type=submit value='Pay Selected' name=proc> <input type=submit value='Batch Selected' name=bat></td><tr>\n                </table>\n                <p>\n                <table border=0 cellpadding='2' cellspacing='1'>\n\t\t<tr><th>Quick Links</th></tr>\n                <tr bgcolor='#88BBFF'><td><a href='credit-batch-view.php'>View Creditors Batch</a></td></tr>\n                <script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>\n\n\n\n\n\t";
    return $view;
}
function confirm($grpid)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($grpid, "num", 1, 50, "Invalid Asset Group id.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Select Stock
    db_connect();
    $sql = "SELECT * FROM assetgrp WHERE grpid = '{$grpid}' AND div = '" . USER_DIV . "'";
    $grpRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($grpRslt) < 1) {
        return "<li> Invalid Asset Group ID.";
    } else {
        $grp = pg_fetch_array($grpRslt);
    }
    core_connect();
    $sql = "SELECT accname FROM accounts WHERE accid = '{$grp['costacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $acccost = pg_fetch_array($accRslt);
    # get ledger account name(accum dep)
    $sql = "SELECT accname FROM accounts WHERE accid = '{$grp['accdacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $acdacc = pg_fetch_array($accRslt);
    # get ledger account name(dep)
    $sql = "SELECT accname FROM accounts WHERE accid = '{$grp['depacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $accdep = pg_fetch_array($accRslt);
    $confirm = "<h3>Confirm Remove Asset Group</h3>\r\n\t<form action='" . SELF . "' method=post>\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<input type=hidden name=key value=write>\r\n\t<input type=hidden name=grpname value='{$grp['grpname']}'>\r\n\t<input type=hidden name=grpid value='{$grpid}'>\r\n\t<tr><th>Field</th><th>Value</th></tr>\r\n\t<tr class='bg-odd'><td>Asset Group</td><td>{$grp['grpname']}</td></tr>\r\n\t<tr class='bg-even'><td>Cost Account</td><td>{$acccost['accname']}</td></tr>\r\n\t<tr class='bg-odd'><td>Accumulated Depreciation Account</td><td>{$acdacc['accname']}</td></tr>\r\n\t<tr class='bg-even'><td>Depreciation Account</td><td>{$accdep['accname']}</td></tr>\r\n\t<tr><td><br></td></tr>\r\n\t<tr><td align=right></td><td valign=left><input type=submit value='Delete &raquo;'></td></tr>\r\n\t</table></form>\r\n\t<p>\r\n\t<table border=0 cellpadding='2' cellspacing='1'>\r\n\t<tr><th>Quick Links</th></tr>\r\n\t<tr class='bg-odd'><td><a href='assetgrp-new.php'>Add Asset Group</a></td></tr>\r\n\t<tr class='bg-odd'><td><a href='assetgrp-view.php'>View Asset Groups</a></td></tr>\r\n\t<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>\r\n\t</table>";
    return $confirm;
}
function edit($batchid)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($batchid, "num", 1, 20, "Invalid batch number.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Connect
    core_connect();
    # Get all the details
    $sql = "SELECT * FROM batch WHERE batchid = '{$batchid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to access database.");
    $tran = pg_fetch_array($rslt);
    # Get account to be debited
    $dtaccRs = get("core", "*", "accounts", "accid", $tran['debit']);
    if (pg_numrows($dtaccRs) < 1) {
        return "<li> Accounts to be debited does not exist.</li>";
    }
    $dtacc = pg_fetch_array($dtaccRs);
    # Get account to be debited
    $ctaccRs = get("core", "*", "accounts", "accid", $tran['credit']);
    if (pg_numrows($ctaccRs) < 1) {
        return "<li> Accounts to be debited does not exist.</li>";
    }
    $ctacc = pg_fetch_array($ctaccRs);
    # Explode date
    $date = explode("-", $tran['date']);
    // Deatils
    $edit = "\n\t\t<h3>Edit Batch Entry</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='batchid' value='{$batchid}'>\n\t\t<table " . TMPL_tblDflts . " width='500'>\n\t\t\t<tr>\n\t\t\t\t<td width='50%'><h3>Debit</h3></td>\n\t\t\t\t<td width='50%'><h3>Credit</h3></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$dtacc['topacc']}/{$dtacc['accnum']} - {$dtacc['accname']}</td>\n\t\t\t\t<td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Date</td>\n\t\t\t\t<td>" . mkDateSelect("date", $date[0], $date[1], $date[2]) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Reference No.</td>\n\t\t\t\t<td valign='center'><input type='text' size='20' name='refnum' value='{$tran['refnum']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Amount</td>\n\t\t\t\t<td valign='center'>" . CUR . "<input type='text' size='20' name='amount' value='{$tran['amount']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Transaction Details</td>\n\t\t\t\t<td valign='center'><textarea cols='20' rows='5' name='details'>{$tran['details']}</textarea></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Person Authorising</td>\n\t\t\t\t<td valign='center'><input type='hidden' size='20' name='author' value='{$tran['author']}'>{$tran['author']}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td><input type='button' value='Back' OnClick='javascript:history.back()'></td>\n\t\t\t\t<td valign='center'><input type='submit' value='Edit Entry'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<table border=0 cellpadding='2' cellspacing='1' width=15%>\n\t\t\t<tr><td><br></td></tr>\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 align='center'><a href='trans-new.php'>Journal Transactions</td>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $edit;
}
function rem($deptid)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($deptid, "num", 1, 50, "Invalid Department id.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>-" . $e["msg"] . "<br>";
        }
        return $confirm;
    }
    # Select Stock
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$deptid}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($deptRslt) < 1) {
        return "<li> Invalid Department ID.";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    core_connect();
    $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['incacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $accinc = pg_fetch_array($accRslt);
    # get debtors account name
    $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['debtacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $accdebt = pg_fetch_array($accRslt);
    # get creditors account name
    $sql = "SELECT accname FROM accounts WHERE accid = '{$dept['credacc']}' AND div = '" . USER_DIV . "'";
    $accRslt = db_exec($sql);
    $acccred = pg_fetch_array($accRslt);
    $rem = "<h3>Confirm Remove Department</h3>\r\n\t\t<form action='" . SELF . "' method=post>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t\t<input type=hidden name=key value=write>\r\n\t\t<input type=hidden name=deptid value='{$dept['deptid']}'>\r\n\t\t<input type=hidden name=deptname value='{$dept['deptname']}'>\r\n\t\t<tr><th>Field</th><th>Value</th></tr>\r\n\t\t<tr class='bg-odd'><td>Dept No</td><td>{$dept['deptno']}</td></tr>\r\n\t\t<tr class='bg-even'><td>Department</td><td>{$dept['deptname']}</td></tr>\r\n\t\t<tr class='bg-odd'><td>Income Account</td><td>{$accinc['accname']}</td></tr>\r\n\t\t<tr class='bg-even'><td>Debtors Control Account</td><td>{$accdebt['accname']}</td></tr>\r\n\t\t<tr class='bg-odd'><td>Creditors Control Account</td><td>{$acccred['accname']}</td></tr>\r\n\t\t<tr><td><br></td></tr>\r\n\t\t<tr><td colspan=2 align=right><input type=submit value='Remove &raquo;'></td></tr>\r\n\t\t</table></form>\r\n\t\t<p>\r\n\t\t<table border=0 cellpadding='2' cellspacing='1'>\r\n\t\t<tr><th>Quick Links</th></tr>\r\n\t\t<tr class='bg-odd'><td><a href='dept-view.php'>View Department</a></td></tr>\r\n\t\t<tr class='bg-odd'><td><a href='../main.php'>Main Menu</a></td></tr>\r\n\t\t</table>";
    return $rem;
}
function rem($batchid)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($batchid, "num", 1, 20, "Invalid batch number.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # connect
    core_connect();
    # Get all the details
    $sql = "SELECT * FROM batch WHERE batchid = '{$batchid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to access database.");
    $tran = pg_fetch_array($rslt);
    # get account to be debited
    $dtaccRs = get("core", "*", "accounts", "accid", $tran['debit']);
    if (pg_numrows($dtaccRs) < 1) {
        return "<li> Accounts to be debited does not exist";
    }
    $dtacc = pg_fetch_array($dtaccRs);
    # get account to be debited
    $ctaccRs = get("core", "*", "accounts", "accid", $tran['credit']);
    if (pg_numrows($ctaccRs) < 1) {
        return "<li> Accounts to be debited does not exist";
    }
    $ctacc = pg_fetch_array($ctaccRs);
    // Deatils
    $rem = "\n        <h3>Remove Batch Entry</h3>\n        <form action='" . SELF . "' method=post name=form>\n        <input type=hidden name=key value=write>\n        <input type=hidden name='batchid' value='{$batchid}'>\n        <input type=hidden name=dtaccname value='{$dtacc['accname']}'>\n        <input type=hidden name=ctaccname value='{$ctacc['accname']}'>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=500>\n        <tr><td width=50%><h3>Debit</h3></td><td width=50%><h3>Credit</h3></td></tr>\n        <tr class='bg-odd'><td>{$dtacc['topacc']}/{$dtacc['accnum']} - {$dtacc['accname']}</td><td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td></tr>\n        <tr><td><br></td></tr>\n        <tr><td><br></td></tr>\n        <tr class='bg-odd'><td>Date</td><td>{$tran['date']}</td></tr>\n        <tr class='bg-even'><td>Reference No.</td><td valign=center>{$tran['refnum']}</td></tr>\n        <tr class='bg-odd'><td>Amount</td><td valign=center>" . CUR . " {$tran['amount']}</td></tr>\n        <tr class='bg-even'><td>Transaction Details</td><td valign=center>{$tran['details']}</td></tr>\n        <tr class='bg-odd'><td>Person Authorising</td><td valign=center>{$tran['author']}</td></tr>\n        <tr><td><input type=button value=Back OnClick='javascript:history.back()'></td><td valign=center><input type=submit value='Remove Entry'></td></tr>\n        </form>\n        </table>\n\t<table border=0 cellpadding='2' cellspacing='1' width=15%>\n        <tr><td>\n        <br>\n        </td></tr>\n        <tr><th>Quick Links</th></tr>\n        <tr class=datacell><td align=center><a href='trans-new.php'>Journal Transactions</td></tr>\n        <script>document.write(getQuicklinkSpecial());</script>\n        </table>";
    return $rem;
}
function viewAllow()
{
    # connect to db
    db_connect();
    # select entries from db
    $i = 0;
    $viewAllow = "\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Reimbursement name</th>\n\t\t\t\t<th>Account</th>\n\t\t\t</tr>";
    $sql = "SELECT * FROM rbs WHERE div = '" . USER_DIV . "' ORDER BY name";
    $allowRslt = db_exec($sql) or errDie("Unable to select allowances from database.");
    if (pg_numrows($allowRslt) < 1) {
        return "<li class='err'>No reimbursements found in database.</li><br>";
    }
    while ($myAllow = pg_fetch_array($allowRslt)) {
        # get ledger account name
        core_connect();
        $sql = "SELECT accname FROM accounts WHERE accid = '{$myAllow['account']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acc = pg_fetch_array($accRslt);
        $viewAllow .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$myAllow['name']}</td>\n\t\t\t\t<td>{$acc['accname']}</td>\n\t\t\t\t<td><a href='rbs-edit.php?id={$myAllow['id']}'>Edit</a></td>\n\t\t\t</tr>\n";
        $i++;
    }
    $viewAllow .= "</table>";
    return $viewAllow;
}
function getactual()
{
    # get petty cash account
    $pcacc = gethook("accnum", "bankacc", "name", "Petty Cash");
    core_connect();
    $pcbalRs = db_exec("SELECT sum(credit) as credit, sum(debit) as debit FROM trial_bal WHERE period='" . PRD_DB . "' AND accid = '{$pcacc}'");
    $pcbal = pg_fetch_array($pcbalRs);
    $bankct = $pcbal['credit'];
    $bankdt = $pcbal['debit'];
    # get bank balances
    db_connect();
    $sql = "SELECT bankid FROM bankacct WHERE div = '" . USER_DIV . "'";
    $bankRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve bank account details from database.", SELF);
    while ($bnk = pg_fetch_array($bankRslt)) {
        $bacc = gethook("accnum", "bankacc", "accid", $bnk['bankid']);
        core_connect();
        $bbalRs = db_exec("SELECT sum(credit) as credit, sum(debit) as debit FROM trial_bal WHERE accid = '{$bacc}'");
        $bbal = pg_fetch_array($bbalRs);
        $bankct += $bbal['credit'];
        $bankdt += $bbal['debit'];
    }
    # get cash on hand
    db_conn("exten");
    $sql = "SELECT pca FROM departments  WHERE div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve departments from database.", SELF);
    while ($dept = pg_fetch_array($deptRslt)) {
        core_connect();
        $deptbalRs = db_exec("SELECT sum(credit) as credit, sum(debit) as debit FROM trial_bal WHERE accid = '{$dept['pca']}'");
        $deptbal = pg_fetch_array($deptbalRs);
        $bankct += $deptbal['credit'];
        $bankdt += $deptbal['debit'];
    }
    $ret['debit'] = sprint($bankdt);
    $ret['credit'] = sprint($bankct);
    return $ret;
}
function viewFringe()
{
    # connect to db
    db_connect();
    # select entries from db
    $i = 0;
    $OUTPUT = "\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Name</th>\n\t\t\t\t<th>Account</th>\n\t\t\t\t<th>Type</th>\n\t\t\t</tr>";
    $sql = "SELECT * FROM fringebens WHERE div = '" . USER_DIV . "' ORDER BY fringeben";
    $rslt = db_exec($sql) or errDie("Unable to select fringe benefits from database.");
    if (pg_numrows($rslt) < 1) {
        return "\n\t\t\t<li class='err'>No fringe benefits found.</li><br>" . mkQuickLinks(ql("fringeben-add.php", "Add Fringe Benefit"), ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    }
    while ($myFringe = pg_fetch_array($rslt)) {
        # get ledger account name
        core_connect();
        $sql = "SELECT accname FROM accounts WHERE accid = '{$myFringe['accid']}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        $acc = pg_fetch_array($accRslt);
        $OUTPUT .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$myFringe['fringeben']}</td>\n\t\t\t\t<td>{$acc['accname']}</td>\n\t\t\t\t<td>{$myFringe['type']}</td>\n\t\t\t</tr>\n";
        $i++;
    }
    $OUTPUT .= "\n\t\t</table>" . mkQuickLinks(ql("fringeben-add.php", "Add Fringe Benefit"), ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $OUTPUT;
}