function viewcash($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 20, "Invalid Bank Account Number.");
    $v->isOk($fday, "num", 1, 2, "Invalid Day for the 'From' date.");
    $v->isOk($fmonth, "num", 1, 2, "Invalid month for the 'From' date..");
    $v->isOk($fyear, "num", 1, 4, "Invalid year for the 'From' date..");
    $v->isOk($lday, "num", 1, 2, "Invalid Day for the 'To' date.");
    $v->isOk($lmonth, "num", 1, 2, "Invalid month for the 'To' date..");
    $v->isOk($lyear, "num", 1, 4, "Invalid year for the 'To' date..");
    # lets mix the date
    $from = sprintf("%02.2d", $fday) . "-" . sprintf("%02.2d", $fmonth) . "-" . $fyear;
    $to = sprintf("%02.2d", $lday) . "-" . sprintf("%02.2d", $lmonth) . "-" . $lyear;
    # 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;
    }
    # Get account name for bank account
    db_connect();
    $sql = "SELECT accname,bankname FROM bankacct WHERE bankid= '{$bankid}'";
    $bankRslt = db_exec($sql);
    $bank = pg_fetch_array($bankRslt);
    // Receipts
    $OUTPUT = "<center><h3>Cash Book : {$bank['accname']}<br><br>{$from} to {$to}</h3>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<tr><td colspan=7><h4>Analysis of Receipts</h4></td></tr>\n\t<tr><th>Date</th><th>Bank Account Name</th><th>Cheque Number</th><th>Received From : </th><th>Description</th><th>Ledger Account</th><th>Amount</th></tr>";
    # date format
    $from = explode("-", $from);
    $from = $from[2] . "-" . $from[1] . "-" . $from[0];
    $to = explode("-", $to);
    $to = $to[2] . "-" . $to[1] . "-" . $to[0];
    $rtotal = 0;
    # Received total amount
    # Connect to database
    db_Connect();
    $sql = "SELECT * FROM cashbook WHERE bankid = '{$bankid}' AND date >= '{$from}' AND date <= '{$to}' AND trantype='deposit' AND banked='yes' ORDER BY date DESC";
    $accntRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve bank deposits details from database.", SELF);
    $numrows = pg_numrows($accntRslt);
    if ($numrows < 1) {
        $OUTPUT .= "<tr><td colspan=7 align=center><li class=err>There are no Payments/cheques received on the selected period.</td></tr>";
    } else {
        # display all bank Deposits
        for ($i = 0; $i < $numrows; $i++) {
            $accnt = pg_fetch_array($accntRslt, $i);
            if (strlen($accnt['accids']) > 0) {
                $acc['accname'] = "Multiple Accounts";
                $acc['accnum'] = "000";
                $acc['topacc'] = "000";
            } else {
                # get account name for the account involved
                $AccRslt = undget("core", "accname", "accounts", "accid", $accnt['accinv']);
                $acc = pg_fetch_array($AccRslt);
            }
            /*
            # get account name for account involved
            $accRslt = get("core", "accname,topacc,accnum", "accounts", "accid", $accnt['accinv']);
            $acc = pg_fetch_array($accRslt);
            */
            # get account name for bank account
            db_connect();
            $sql = "SELECT accname FROM bankacct WHERE bankid= '{$accnt['bankid']}'";
            $bnameRslt = db_exec($sql);
            $bname = pg_fetch_array($bnameRslt);
            # format date
            $accnt['date'] = explode("-", $accnt['date']);
            $accnt['date'] = $accnt['date'][2] . "-" . $accnt['date'][1] . "-" . $accnt['date'][0];
            $rtotal += $accnt['amount'];
            // add to rtotal
            $OUTPUT .= "<tr class='" . bg_class() . "'><td>{$accnt['date']}</td><td>{$bname['accname']}</td><td align=center>{$accnt['cheqnum']}</td><td align=center>{$accnt['name']}</td><td>{$accnt['descript']}</td><td>{$acc['topacc']}/{$acc['accnum']}  {$acc['accname']}</td><td>" . CUR . " {$accnt['amount']}</td></tr>";
        }
        # print the total
        $OUTPUT .= "<tr class='bg-even''><td colspan=6><b>Total Receipts</b></td><td><b>" . CUR . " " . sprintf("%01.2f", $rtotal) . "</b></td></tr>";
    }
    # Seperate the tables with two rows
    $OUTPUT .= "<tr><td colspan=7><br></td></tr><tr><td colspan=7><br></td></tr>";
    # Payments
    $OUTPUT .= "<tr><td colspan=7><h4>Analysis of Payments</h4></td></tr>\n\t<tr><th>Date</th><th>Bank Account Name</th><th>Cheque Number</th><th>Paid to: </th><th>Description</th><th>Ledger Account</th><th>Amount</th></tr>";
    $ptotal = 0;
    # payments total
    # Connect to database
    db_Connect();
    $sql = "SELECT * FROM cashbook WHERE bankid = '{$bankid}' AND date >= '{$from}' AND date <= '{$to}' AND trantype='withdrawal' AND banked='yes' ORDER BY date DESC";
    $accntRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve bank deposits details from database.", SELF);
    $numrows = pg_numrows($accntRslt);
    if ($numrows < 1) {
        $OUTPUT .= "<tr><td colspan=7 align=center><li class=err>There are no Payments made on the selected period.</td></tr>";
    } else {
        # Display all bank Deposits
        for ($i = 0; $i < $numrows; $i++) {
            $accnt = pg_fetch_array($accntRslt, $i);
            if (strlen($accnt['accids']) > 0) {
                $acc['accname'] = "Multiple Accounts";
                $acc['accnum'] = "000";
                $acc['topacc'] = "000";
            } else {
                # get account name for the account involved
                $AccRslt = undget("core", "accname", "accounts", "accid", $accnt['accinv']);
                $acc = pg_fetch_array($AccRslt);
            }
            /*
            # get account name for account involved
            $accRslt = get("core", "accname,topacc,accnum", "accounts", "accid", $accnt['accinv']);
            $acc = pg_fetch_array($accRslt);
            */
            # get account name for bank account
            db_connect();
            $sql = "SELECT accname FROM bankacct WHERE bankid= '{$accnt['bankid']}'";
            $bnameRslt = db_exec($sql);
            $bname = pg_fetch_array($bnameRslt);
            # format date
            $accnt['date'] = explode("-", $accnt['date']);
            $accnt['date'] = $accnt['date'][2] . "-" . $accnt['date'][1] . "-" . $accnt['date'][0];
            $ptotal += $accnt['amount'];
            # add to total
            $OUTPUT .= "<tr class='" . bg_class() . "'><td>{$accnt['date']}</td><td>{$bname['accname']}</td><td align=center>{$accnt['cheqnum']}</td><td align=center>{$accnt['name']}</td><td>{$accnt['descript']}</td><td>{$acc['topacc']}/{$acc['accnum']}  {$acc['accname']}</td><td>" . CUR . " {$accnt['amount']}</td></tr>";
        }
        # print the total
        $OUTPUT .= "<tr class='bg-even''><td colspan=6><b>Total Payments</b></td><td><b>" . CUR . " " . sprintf("%01.2f", $ptotal) . "</b></td></tr>";
    }
    $OUTPUT .= "<tr><td colspan=7><br></td></tr>\n\n\t<!--\n\t<tr><td align=center colspan=10>\n\t\t<form action='../xls/banked-xls.php' method=post name=form>\n\t\t<input type=hidden name=key value=viewcash>\n\t\t<input type=hidden name=bankid value='{$bankid}'>\n\t\t<input type=hidden name=fday value='{$fday}'>\n\t\t<input type=hidden name=fmonth value='{$fmonth}'>\n\t\t<input type=hidden name=fyear value='{$fyear}'>\n\t\t<input type=hidden name=lday value='{$lday}'>\n\t\t<input type=hidden name=lmonth value='{$lmonth}'>\n\t\t<input type=hidden name=lyear value='{$lyear}'>\n\t\t<input type=submit name=xls value='Export to spreadsheet'>\n\t\t</form>\n\t</td></tr>\n\t-->\n\n\t</table>\n\t<p>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $OUTPUT;
}
function viewtran($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($prd, "string", 1, 14, "Invalid Period number.");
    if (isset($details)) {
        $v->isOk($accid, "string", 1, 20, "Invalid Account number.");
        $hide = "\n\t\t<input type=hidden name=prd value='{$prd}'>\n\t\t<input type=hidden name=details value='{$details}'>\n\t\t<input type=hidden name=accid value='{$accid}'>";
    } else {
        $v->isOk($topacc, "num", 1, 20, "Invalid Account number.");
        $v->isOk($accnum, "num", 0, 20, "Invalid Account number.");
        $hide = "\n\t\t<input type=hidden name=prd value='{$prd}'>\n\t\t<input type=hidden name=topacc value='{$topacc}'>\n\t\t<input type=hidden name=accnum value='{$accnum}'>";
    }
    # 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;
    }
    if (isset($details)) {
        $accRs = undget("core", "accname, accid, topacc, accnum", "accounts", "accid", $accid);
        $acc = pg_fetch_array($accRs);
    } else {
        if (strlen($accnum) < 2) {
            // account numbers
            $accRs = undget("core", "accname, accid, topacc, accnum", "accounts", "topacc", "{$topacc}' AND accnum = '000");
            if (pg_numrows($accRs) < 1) {
                return "<li> Accounts number : {$accnum} does not exist";
            }
            $acc = pg_fetch_array($accRs);
        } else {
            // account numbers
            $accRs = undget("core", "accname, accid, topacc, accnum", "accounts", "topacc", "{$topacc}' AND accnum = '{$accnum}");
            if (pg_numrows($accRs) < 1) {
                return "<li> Accounts number : {$topacc}/{$accnum} does not exist";
            }
            $acc = pg_fetch_array($accRs);
        }
    }
    db_conn($prd);
    // Set up table to display in
    $OUTPUT = "<center>\n\t<h3>Journal Entries for Account : {$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</h3>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='80%'>\n\t<tr><th>Date</th><th>Reference</th><th>Contra Acc</th><th>Description</th><th>Debit</th><th>Credit</th><th>User</th></tr>";
    # Get Transactions
    $sql = "SELECT * FROM transect WHERE debit = '{$acc['accid']}' OR credit = '{$acc['accid']}'";
    $tranRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve Transaction details from database.", SELF);
    if (pg_numrows($tranRslt) < 1) {
        $OUTPUT .= "<tr class='bg-odd'><td colspan=10>No Transactions found</td></tr>";
        # counts
        $credtot = 0;
        $debtot = 0;
    } else {
        # counts
        $credtot = 0;
        $debtot = 0;
        # display all transactions
        while ($tran = pg_fetch_array($tranRslt)) {
            #get vars from tran as the are in db
            foreach ($tran as $key => $value) {
                ${$key} = $value;
            }
            if ($debit == $acc['accid']) {
                $cacc = $credit;
                $debitamt = "R " . $amount;
                $debtot += $amount;
                $creditamt = "";
            } else {
                $debitamt = "";
                $creditamt = "R " . $amount;
                $credtot += $amount;
                $cacc = $debit;
            }
            # format date
            $date = explode("-", $date);
            $date = $date[2] . "-" . $date[1] . "-" . $date[0];
            # get contra account name
            $caccRs = undget("core", "div, accname,topacc,accnum", "accounts", "accid", $cacc);
            $cacc = pg_fetch_array($caccRs);
            $branname = branname($cacc['div']);
            $OUTPUT .= "<tr class='bg-odd'><td>{$date}</td><td>{$refnum}</td><td>{$cacc['topacc']}/{$cacc['accnum']} - {$cacc['accname']} - {$branname}</td><td>{$details}</td><td>{$debitamt}</td><td>{$creditamt}</td><td>{$author}</td></tr>";
        }
    }
    $OUTPUT .= "<tr class='bg-even'><td colspan=4><b>Total</b></td><td><b>" . CUR . " {$debtot}</b></td><td><b>" . CUR . " {$credtot}</b></td><td></td></tr>\n\t<tr><td><br></td></tr>\n\n\t<!--\n\t<tr><td align=center colspan=10>\n\t\t\t<form action='../xls/acc-trans-xls.php' method=post name=form>\n\t\t\t<input type=hidden name=key value=viewtran>\n\t\t\t{$hide}\n\t\t\t<input type=submit name=xls value='Export to spreadsheet'>\n\t\t\t</form>\n\t</td></tr>\n\t-->\n\n\t</table>\n\t<p>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=25%>\n\t\t<tr><td><br></td></tr>\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 $OUTPUT;
}
function printdep($bankid)
{
    // Set up table to display in
    $OUTPUT = "<h3>Outstanding Payments</h3>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n        <form action='../bank/bank-bankall.php' method=post>\n        <tr><th>Bank Name</th><th>Account Name</th><th>Date</th><th>Paid to</th><th>Description</th><th>Transaction Type</th><th>Amount</th><th>Account</th></tr>";
    // Connect to database
    db_Connect();
    $sql = "SELECT * FROM cashbook WHERE bankid = '{$bankid}' AND trantype = 'withdrawal' AND banked='no' ORDER BY date DESC";
    $accntRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve bank deposits details from database.", SELF);
    $numrows = pg_numrows($accntRslt);
    if ($numrows < 1) {
        $OUTPUT = "<li class=err> There are no outstanding Bank Payment entries.</li>";
        return $OUTPUT;
    }
    # display all bank Deposits
    $tot = 0;
    for ($i = 0; $i < $numrows; $i++) {
        $accnt = pg_fetch_array($accntRslt, $i);
        if (strlen($accnt['accids']) > 0) {
            $acc['accname'] = "Multiple Accounts";
            $acc['accnum'] = "000";
            $acc['topacc'] = "000";
        } else {
            # get account name for the account involved
            $AccRslt = undget("core", "accname", "accounts", "accid", $accnt['accinv']);
            $acc = pg_fetch_array($AccRslt);
        }
        /*
        # get account name for account involved
        $accRslt = get("core", "accname", "accounts", "accid", $accnt['accinv']);
        $acc = pg_fetch_array($accRslt);
        */
        # get account name for bank account
        db_connect();
        $sql = "SELECT accname,bankname FROM bankacct WHERE bankid= '{$accnt['bankid']}'";
        $bankRslt = db_exec($sql);
        $bank = pg_fetch_array($bankRslt);
        $OUTPUT .= "<tr class='" . bg_class() . "'><td>{$bank['bankname']}</td><td align=center>{$bank['accname']}</td><td align=center>{$accnt['date']}</td><td align=center>{$accnt['name']}</td><td align=center>{$accnt['descript']}</td><td align=center>{$accnt['trantype']}</td><td align=right>" . CUR . " {$accnt['amount']}</td><td align=center>{$acc['accname']}</td>\n\t\t\t<td><a href='../bank/cheq-cancel.php?cashid={$accnt['cashid']}'>Cancel</td></tr>";
        $tot += $accnt['amount'];
    }
    $OUTPUT .= "<tr class='bg-even'><td colspan=6><b>Total Outstanding</b></td><td colspan=3><b>" . CUR . " {$tot}</b></td></tr>\n        </form></table>";
    // return OUTPUT
    return $OUTPUT;
}
function save_bal()
{
    # Get Owners Equity Sub Headings
    $oesubRslt = undget("core", "*", "bal_sheet", "type", "OESUB' AND div = '11111111");
    $sheet = "<center>\n\t<h3> Balance sheet for period : " . PRD_NAME . "<br><br>Date: " . date("d M Y") . " </h3>\n\t<table cellpadding='3' cellspacing='0' border=1 bordercolor='#000000' width=750>\n\t<tr><th colspan=3>Owners Equity</th></tr>";
    # get accounts
    $oebal = 0;
    // OE Balance
    while ($oesub = pg_fetch_array($oesubRslt)) {
        $sheet .= "<tr><td colspan=3><b>{$oesub['value']}<b></td></tr>";
        $sql = "SELECT * FROM bal_sheet WHERE type ='OEACC' AND ref = {$oesub['ref']} AND div = 11111111";
        $accRslt = db_exec($sql) or errDie("Unable to retrieve balance sheet settings from the Database.", SELF);
        # get account BAlances
        while ($acc = pg_fetch_array($accRslt)) {
            list($topacc, $accnum) = explode("/", $acc['value']);
            # Get balance
            $query = "SELECT sum(credit) as credit,sum(debit) as debit FROM trial_bal WHERE topacc = '{$topacc}' AND accnum = '{$accnum}'";
            $balRslt = db_exec($query) or errDie("Unable to retrieve Account Balances from the Database.", SELF);
            $accbal = pg_fetch_array($balRslt);
            $balance = $accbal['credit'] - $accbal['debit'];
            $oebal += $balance;
            $balacc = getaccnum($acc['value']);
            $sheet .= "<tr><td><blockquote><li>{$balacc['accname']}</td><td>" . CUR . " {$balance}</td><td><br></td></tr>";
        }
    }
    $oebal = sprint($oebal);
    # print Net Income and OE Balance on last column
    $netincome = getNetIncome();
    $oebal += $netincome;
    $sheet .= "<tr><td>&nbsp<b>Net Income</b></td><td>" . CUR . " {$netincome}</td><td><br></td></tr>";
    $sheet .= "<tr><td colspan=2><b>Total</b></td><td><b>" . CUR . " {$oebal}</b></td></tr>";
    # Get Assets Sub Headings
    $abal = 0;
    // Assets Balance
    $asssubRslt = undget("core", "*", "bal_sheet", "type", "ASSSUB' AND div = '11111111");
    $sheet .= "<tr><th colspan=3>Assets</th></tr>";
    # get accounts
    while ($asssub = pg_fetch_array($asssubRslt)) {
        $sheet .= "<tr><td colspan=3><b>{$asssub['value']}<b></td></tr>";
        $sql = "SELECT * FROM bal_sheet WHERE type ='ASSACC' AND ref = {$asssub['ref']} AND div = 11111111";
        $accRslt = db_exec($sql) or errDie("Unable to retrieve balance sheet settings from the Database.", SELF);
        # get account BAlances
        while ($acc = pg_fetch_array($accRslt)) {
            list($topacc, $accnum) = explode("/", $acc['value']);
            # Get Balance
            $query = "SELECT sum(credit) as credit,sum(debit) as debit FROM trial_bal WHERE topacc = '{$topacc}' AND accnum = '{$accnum}'";
            $balRslt = db_exec($query) or errDie("Unable to retrieve Account Balances from the Database.", SELF);
            $accbal = pg_fetch_array($balRslt);
            $balance = $accbal['debit'] - $accbal['credit'];
            // calc Balance
            $abal += $balance;
            $balacc = getaccnum($acc['value']);
            $sheet .= "<tr><td><blockquote><li>{$acc['value']} - {$balacc['accname']}</td><td>" . CUR . " {$balance}</td><td><br></td></tr>";
        }
    }
    $abal = sprint($abal);
    # print assets balance on last column
    $sheet .= "<tr><td colspan=2><b>Total</b></td><td><b>" . CUR . " {$abal}</b></td></tr>\n\t</table><br>";
    $output = base64_encode($sheet);
    core_connect();
    $sql = "INSERT INTO save_bal_sheet(gendate, output, div) VALUES('" . date("Y-m-d") . "', '{$output}', '" . USER_DIV . "')";
    $Rs = db_exec($sql) or errdie("Unable to save the Balance Sheet.");
    $sheet .= "\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=25%>\n\t<tr><th>Quick Links</th></tr>\n\t<tr class=datacell><td align=center><a target=_blank href='../core/acc-new2.php'>Add account (New Window)</a></td></tr>\n\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $sheet;
}
function viewtrans($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    # Search by date
    if ($search == "date") {
        $v->isOk($from_day, "num", 1, 2, "Invalid from Date day.");
        $v->isOk($from_month, "num", 1, 2, "Invalid from Date month.");
        $v->isOk($from_year, "num", 1, 4, "Invalid from Date Year.");
        $v->isOk($to_day, "num", 1, 2, "Invalid to Date day.");
        $v->isOk($to_month, "num", 1, 2, "Invalid to Date month.");
        $v->isOk($to_year, "num", 1, 4, "Invalid to Date Year.");
        # mix dates
        $fromdate = $from_year . "-" . $from_month . "-" . $from_day;
        $todate = $to_year . "-" . $to_month . "-" . $to_day;
        if (!checkdate($from_month, $from_day, $from_year)) {
            $v->isOk($fromdate, "num", 1, 1, "Invalid from date.");
        }
        if (!checkdate($to_month, $to_day, $to_year)) {
            $v->isOk($todate, "num", 1, 1, "Invalid to date.");
        }
        $hide = "\n\t\t\t<input type=hidden name=prd value='{$prd}'>\n\t\t\t<input type=hidden name=search value='{$search}'>\n\t\t\t<input type=hidden name=fday value='{$fday}'>\n\t\t\t<input type=hidden name=fmon value='{$fmon}'>\n\t\t\t<input type=hidden name=fyear value='{$fyear}'>\n\t\t\t<input type=hidden name=today value='{$today}'>\n\t\t\t<input type=hidden name=tomon value='{$tomon}'>\n\t\t\t<input type=hidden name=toyear value='{$toyear}'>";
        # Create the Search SQL
        $search = "SELECT * FROM transect WHERE date >= '{$fromdate}' AND date <= '{$todate}' ORDER BY refnum ASC";
    }
    # Search by refnum
    if ($search == "refnum") {
        $v->isOk($fromnum, "num", 1, 20, "Invalid 'from' ref  number.");
        $v->isOk($tonum, "num", 1, 20, "Invalid 'to' ref  number.");
        $hide = "\n\t\t\t<input type=hidden name=prd value='{$prd}'>\n\t\t\t<input type=hidden name=search value='{$search}'>\n\t\t\t<input type=hidden name=fromnum value='{$fromnum}'>\n\t\t\t<input type=hidden name=tonum value='{$tonum}'>";
        # Create the Search SQL
        $search = "SELECT * FROM transect WHERE refnum >= {$fromnum} AND refnum <= {$tonum} ORDER BY refnum ASC";
    }
    # View all
    if ($search == "all") {
        $hide = "\n\t\t\t<input type=hidden name=prd value='{$prd}'>\n\t\t\t<input type=hidden name=search value='{$search}'>";
        # Create the Search SQL
        $search = "SELECT * FROM transect ORDER BY refnum ASC";
    }
    # 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;
    }
    // Layout
    $OUTPUT = "<center>\n\t\t<h3>Journal Entries Report</h3>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='100%'>\n        <tr><th>Date</th><th>Debit</th><th>Credit</th><th>Ref No</th><th>Amount</th><th>Details</th><th>Authorised By</th></tr>";
    db_conn($prd);
    $tranRslt = db_exec($search) or errDie("ERROR: Unable to retrieve Transaction details from database.", SELF);
    if (pg_numrows($tranRslt) < 1) {
        return "<li> There are no Transactions in the selected Period.<br><br>\n\t\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=25%>\n\t\t\t\t<tr><th>Quick Links</th></tr>\n\t\t\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\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t</table>";
    }
    # display all transaction
    while ($tran = pg_fetch_array($tranRslt)) {
        # get vars from tran as the are in db
        foreach ($tran as $key => $value) {
            ${$key} = $value;
        }
        # format date
        $date = explode("-", $date);
        $date = $date[2] . "-" . $date[1] . "-" . $date[0];
        # Get account names
        $deb = undget("core", "div,accname, topacc, accnum", "accounts", "accid", $debit);
        $debacc = pg_fetch_array($deb);
        $ct = undget("core", "div,accname, topacc,accnum", "accounts", "accid", $credit);
        $ctacc = pg_fetch_array($ct);
        $dtbranname = branname($debacc['div']);
        $ctbranname = branname($ctacc['div']);
        $OUTPUT .= "<tr class='bg-odd'><td>{$date}</td><td>{$debacc['topacc']}/{$debacc['accnum']}&nbsp;&nbsp;&nbsp;{$debacc['accname']} - {$dtbranname}</td><td>{$ctacc['topacc']}/{$ctacc['accnum']}&nbsp;&nbsp;&nbsp;{$ctacc['accname']} - {$ctbranname}</td><td>{$refnum}</td><td>" . CUR . " {$amount}</td><td>{$details}</td><td>{$author}</td></tr>";
    }
    $OUTPUT .= "\n\t\t<tr><td><br></td></tr>\n\n\t\t<!--\n\t\t<tr><td align=center colspan=10>\n\t\t\t<form action='../xls/alltrans-xls.php' method=post name=form>\n\t\t\t<input type=hidden name=key value=viewtrans>\n\t\t\t{$hide}\n\t\t\t<input type=submit name=xls value='Export to spreadsheet'>\n\t\t\t</form>\n\t\t</td></tr>\n\t\t-->\n\n\t\t</table>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=25%>\n\t\t\t<tr><th>Quick Links</th></tr>\n\t\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\t<script>document.write(getQuicklinkSpecial());</script>\n        </table>";
    return $OUTPUT;
}
function write($_POST)
{
    # get stock vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 50, "Invalid transit number.");
    $v->isOk($stkid, "num", 1, 50, "Invalid stock number.");
    $v->isOk($sstkid, "num", 1, 50, "Invalid stock number.");
    $v->isOk($sdiv, "num", 1, 50, "Invalid branch number.");
    $v->isOk($whid, "num", 1, 50, "Invalid warehouse number.");
    $v->isOk($tunits, "num", 1, 50, "Invalid number of units.");
    if ($stkid == $sstkid) {
        $v->isOk($shelf, "string", 0, 10, "Invalid Shelf number.");
        $v->isOk($row, "string", 0, 10, "Invalid Row number.");
        $v->isOk($minlvl, "num", 0, 10, "Invalid minimum stock level.");
        $v->isOk($maxlvl, "num", 0, 10, "Invalid maximum stock level.");
        $v->isOk($selamt, "float", 0, 10, "Invalid selling amount.");
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>-" . $e["msg"] . "</li>";
        }
        return $confirm;
    }
    # Select Stock
    db_connect();
    $sql = "SELECT * FROM stock WHERE stkid = '{$stkid}' AND div = '" . USER_DIV . "'";
    $stkRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($stkRslt) < 1) {
        return "<li> Invalid Stock ID.</li>";
    } else {
        $stk = pg_fetch_array($stkRslt);
    }
    if ($stkid == $sstkid) {
        $sstk = $stk;
        $head = "New Stock";
        $data = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Location</td>\n\t\t\t\t<td>Shelf : <input type='hidden' name='shelf' value='{$shelf}'>{$shelf} - Row : <input type='hidden' name='row' value='{$row}'>{$row}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Level</td>\n\t\t\t\t<td>Minimum : <input type='hidden' name='minlvl' value='{$minlvl}'>{$minlvl} -  Maximum : <input type='hidden' name='maxlvl' value='{$maxlvl}'>{$maxlvl}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Selling price per unit</td>\n\t\t\t\t<td>" . CUR . " <input type='hidden' name='selamt' value='{$stk['selamt']}'>{$stk['selamt']}</td>\n\t\t\t</tr>";
    } else {
        $sql = "SELECT * FROM stock WHERE stkid = '{$sstkid}' AND div = '{$sdiv}'";
        $sstkRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
        if (pg_numrows($sstkRslt) < 1) {
            return "<li> Invalid Stock ID.</li>";
        } else {
            $sstk = pg_fetch_array($sstkRslt);
        }
        $head = "";
        $data = "";
    }
    db_conn("exten");
    # get warehouse
    $sql = "SELECT * FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
    $whRslt = db_exec($sql);
    $wh = pg_fetch_array($whRslt);
    # get warehouse
    $sql = "SELECT * FROM warehouses WHERE whid = '{$whid}' AND div = '{$sdiv}'";
    $swhRslt = db_exec($sql);
    $swh = pg_fetch_array($swhRslt);
    /* Start Stock transfering */
    db_connect();
    $csamt = $tunits * $stk['csprice'];
    if ($stkid == $sstkid) {
        # Create new stock item on the other hand
        $sql = "\n\t\t\t\tINSERT INTO stock (\n\t\t\t\t\tstkcod, stkdes, prdcls, classname, csamt, units, \n\t\t\t\t\tbuom, suom, rate, shelf, row, minlvl, \n\t\t\t\t\tmaxlvl, csprice, selamt, catid, catname, whid, \n\t\t\t\t\tblocked, type, alloc, com, serd, div\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$sstk['stkcod']}', '{$sstk['stkdes']}', '{$sstk['prdcls']}', '{$sstk['classname']}', '{$csamt}',  '{$tunits}', \n\t\t\t\t\t'{$sstk['buom']}', '{$sstk['suom']}', '{$sstk['rate']}', '{$shelf}', '{$row}', '{$minlvl}', \n\t\t\t\t\t'{$maxlvl}', '{$sstk['csprice']}', '{$sstk['selamt']}', '{$sstk['catid']}', '{$sstk['catname']}', '{$whid}', \n\t\t\t\t\t'n', '{$sstk['type']}', '0', '0', '{$sstk['serd']}', '{$sdiv}'\n\t\t\t\t)";
        $rslt = db_exec($sql) or errDie("Unable to insert stock to Cubit.", SELF);
        /*
        # Reduce on the other hand
        $sql = "UPDATE stock SET units = (units - '$tunits'), csamt = (csamt - '$csamt') WHERE stkid = '$stkid' AND div = '".USER_DIV."'";
        $rslt = db_exec($sql) or errDie("Unable to update stock in Cubit.",SELF);
        */
    } else {
        # Move units and csamt
        $sql = "UPDATE stock SET units = (units + '{$tunits}'), csamt = (csamt + '{$csamt}') WHERE stkid = '{$sstkid}' AND div = '{$sdiv}'";
        $rslt = db_exec($sql) or errDie("Unable to update stock in Cubit.", SELF);
        /*
        # Reduce on the other hand
        $sql = "UPDATE stock SET units = (units - '$tunits'), csamt = (csamt - '$csamt') WHERE stkid = '$stkid' AND div = '".USER_DIV."'";
        $rslt = db_exec($sql) or errDie("Unable to update stock in Cubit.",SELF);
        */
    }
    $serials = "";
    $sRs = undget("cubit", "*", "transerial", "tid", $id);
    if (pg_numrows($sRs) > 0) {
        $serials = "<tr><th colspan=2>Units Serial Numbers</th></tr>";
        while ($ser = pg_fetch_array($sRs)) {
            $serials .= "<tr class='" . bg_class() . "'><td colspan='2' align='center'>{$ser['serno']}</td></tr>";
            ext_uninvSer($ser['serno'], $sstkid);
        }
    }
    # Remove stock from transit
    $sql = "DELETE FROM transit WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to remove stock from transit.", SELF);
    # todays date
    $date = date("d-m-Y");
    $refnum = getrefnum($date);
    # dt(conacc) ct(stkacc)
    // writetransdiv($wh['conacc'], $wh['stkacc'], $date, $refnum, $csamt, "Stock Transfer", USER_DIV);
    // writetransdiv($swh['stkacc'], $swh['conacc'], $date, $srefnum, $csamt, "Stock Transfer", $sdiv);
    /* End Stock transfering */
    db_connect();
    # Original Branch
    $sql = "SELECT * FROM branches WHERE div = '{$stk['div']}'";
    $branRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($branRslt) < 1) {
        return "<li> Invalid Branch ID.</li>";
    } else {
        $bran = pg_fetch_array($branRslt);
    }
    # Selected Branch
    $sql = "SELECT * FROM branches WHERE div = '{$sdiv}'";
    $sbranRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($sbranRslt) < 1) {
        return "<li> Invalid Branch ID.</li>";
    } else {
        $sbran = pg_fetch_array($sbranRslt);
    }
    # Select Stock
    db_connect();
    $sql = "SELECT * FROM stock WHERE stkid = '{$stkid}' AND div = '" . USER_DIV . "'";
    $stkRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($stkRslt) < 1) {
        return "<li> Invalid Stock ID.</li>";
    } else {
        $stk = pg_fetch_array($stkRslt);
    }
    # available stock units
    $avstk = $stk['units'] - $stk['alloc'];
    # return
    $write = "\n\t\t<h3> Stock has been Transfered </h3>\n\t\t<table " . TMPL_tblDflts . " width='350'>\n\t\t\t<tr>\n\t\t\t\t<th width='40%'>Field</th>\n\t\t\t\t<th width='60%'>Value</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Branch</td>\n\t\t\t\t<td>{$bran['branname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Warehouse</td>\n\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Category</td>\n\t\t\t\t<td>{$stk['catname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Stock code</td>\n\t\t\t\t<td>{$stk['stkcod']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Stock description</td>\n\t\t\t\t<td>" . nl2br($stk['stkdes']) . "</pre></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>On Hand</td>\n\t\t\t\t<td>{$stk['units']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Allocated</td>\n\t\t\t\t<td>{$stk['alloc']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Available</td>\n\t\t\t\t<td>{$avstk}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>On Order</td>\n\t\t\t\t<td>{$stk['ordered']}</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t{$serials}\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Transfered to {$head}</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>To Branch</td>\n\t\t\t\t<td>{$sbran['branname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>To Store </td>\n\t\t\t\t<td>{$swh['whname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Stock code</td>\n\t\t\t\t<td>{$sstk['stkcod']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Stock description</td>\n\t\t\t\t<td>" . nl2br($sstk['stkdes']) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Number of units transfered</td>\n\t\t\t\t<td>{$tunits}</td>\n\t\t\t</tr>\n\t\t\t{$data}\n\t\t</table>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . " width='15%'>\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><a href='stock-transit-view.php'>View Stock in transit</a></td>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $write;
}
function rem($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 50, "Invalid transit 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;
    }
    # Select Stock
    db_connect();
    $sql = "SELECT * FROM transit WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $tranRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($tranRslt) < 1) {
        return "<li> Invalid transit number.";
    } else {
        $tran = pg_fetch_array($tranRslt);
    }
    $sRs = undget("cubit", "*", "transerial", "tid", $id);
    if (pg_numrows($sRs) > 0) {
        while ($ser = pg_fetch_array($sRs)) {
            ext_uninvSer($ser['serno'], $tran['stkid']);
        }
    }
    /* start replcing stock */
    # Put stock back
    $sql = "UPDATE stock SET units = (units + '{$tran['tunits']}'), csamt = (csamt + '{$tran['cstamt']}') WHERE stkid = '{$tran['stkid']}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to update stock in Cubit.", SELF);
    # Remove stock from transit
    $sql = "DELETE FROM transit WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to remove stock from transit.", SELF);
    # todays date
    $date = date("d-m-Y");
    $refnum = getrefnum($date);
    # dt(conacc) ct(stkacc)
    # writetrans($wh['conacc'], $wh['stkacc'], $date, $refnum, $csamt, "Stock Transfer", USER_DIV);
    /* End replcing stock */
    // Layout
    $write = "\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='50%'>\n\t<tr><th>Stock tranfer cancelled</th></tr>\n\t<tr class=datacell><td>Stock tranfer has been cancelled.</td></tr>\n\t</table>\n\t<p>\n\t<table border=0 cellpadding='2' cellspacing='1'>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<tr class='bg-odd'><td><a href='stock-transit-view.php'>View Stock in transit</a></td></tr>\n\t\t<tr bgcolor='#88BBFF'><td><a href='stock-view.php'>View Stock</a></td></tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $write;
}
function viewsaved($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 30, "Invalid Bank Account.");
    $v->isOk($fday, "num", 1, 2, "Invalid Day for the 'From' date.");
    $v->isOk($fmonth, "num", 1, 2, "Invalid month for the 'From' date..");
    $v->isOk($fyear, "num", 1, 4, "Invalid year for the 'From' date..");
    $v->isOk($lday, "num", 1, 2, "Invalid Day for the 'To' date.");
    $v->isOk($lmonth, "num", 1, 2, "Invalid month for the 'To' date..");
    $v->isOk($lyear, "num", 1, 4, "Invalid year for the 'To' date..");
    # lets mix the date
    $from = sprintf("%02.2d", $fyear) . "-" . sprintf("%02.2d", $fmonth) . "-" . $fday;
    $to = sprintf("%02.2d", $lyear) . "-" . sprintf("%02.2d", $lmonth) . "-" . $lday;
    # 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;
    }
    # get bank details
    $bankRslt = undget("cubit", "accname,bankname", "bankacct", "bankid", $bankid);
    $bank = pg_fetch_array($bankRslt);
    // Query server
    core_connect();
    $sql = "SELECT * FROM save_bank_recon WHERE bankid='{$bankid}' AND gendate >= '{$from}' AND gendate <= '{$to}'";
    $Rslt = db_exec($sql) or errDie("ERROR: Unable to view saved Trial balances", SELF);
    // Die with custom error if failed
    if (pg_numrows($Rslt) < 1) {
        $OUTPUT = "<li> There are no saved Bank Reconciliations.";
    } else {
        // Layout
        $OUTPUT = "<h3>View Saved Bank Reconciliations</h3>\n\t\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=300>\n\t\t\t<tr><th>Bank Recon No.</th><th>Date</th></tr>";
        // display all statements
        for ($i = 0; $recon = pg_fetch_array($Rslt); $i++) {
            # date format
            $date = explode("-", $recon['gendate']);
            $date = $date[2] . "-" . $date[1] . "-" . $date[0];
            $OUTPUT .= "<tr class='" . bg_class() . "'><td>{$recon['id']}</td><td>{$date}</td><td><a target='_blank' href='bank-recon-print.php?id={$recon['id']}'>Print</a></td></tr>";
        }
        $OUTPUT .= "</table>";
    }
    $OUTPUT .= "\n\t\t<p>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t\t\t<tr><th>Quick Links</th></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    // call template to display the info and die
    return $OUTPUT;
}
예제 #9
0
 function ext_serStk($serno)
 {
     $TABS = array("1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", '8' => '8', "9" => "9", "0" => "0", 'a' => '0', "b" => '0', "c" => '0', 'd' => '1', "e" => '1', "f" => '1', 'g' => '2', "h" => '2', "i" => '2', 'j' => '3', "k" => '3', "l" => '3', "m" => '4', "n" => '4', "o" => '4', "p" => '5', "q" => '5', "r" => '5', "s" => '6', "t" => '6', "u" => '6', "v" => '7', "w" => '7', "x" => '7', "y" => '8', "z" => '8');
     $tab = $TABS[strtolower($serno[0])];
     db_connect();
     $rs = undget("cubit", "stkid", "serial{$tab}", "serno", $serno);
     $ser = pg_fetch_array($rs);
     db_connect();
     $sql = "SELECT * FROM stock WHERE stkid = '{$ser['stkid']}' AND div = '" . USER_DIV . "'";
     $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
     $stk = pg_fetch_array($stkRslt);
     return $stk;
 }
function viewtran($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($fprd, "string", 1, 14, "Invalid Starting Period number.");
    $v->isOk($tprd, "string", 1, 14, "Invalid Ending Period 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;
    }
    # dates drop downs
    $months = array("1" => "January", "2" => "February", "3" => "March", "4" => "April", "5" => "May", "6" => "June", "7" => "July", "8" => "August", "9" => "September", "10" => "October", "11" => "November", "12" => "December");
    if ($tprd < $fprd) {
        return "<li class=err> Invalid Period range : {$months[$fprd]} to {$months[$tprd]}";
    }
    // Set up table to display in
    $OUTPUT = "<center>\n\t<h3>Journal Entries : {$months[$fprd]} - {$months[$tprd]}</h3>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='100%'>\n\t<tr><th>Date</th><th>Debit</th><th>Credit</th><th>Ref No</th><th>Amount</th><th>Details</th><th>User</th></tr>";
    # counts
    $credtot = 0;
    $debtot = 0;
    # Get Transactions
    for ($i = $fprd; $i <= $tprd; $i++) {
        db_conn($i);
        $sql = "SELECT * FROM transect";
        $tranRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve Transaction details from database.", SELF);
        if (pg_numrows($tranRslt) < 1) {
            continue;
        } else {
            # display all transactions
            while ($tran = pg_fetch_array($tranRslt)) {
                #get vars from tran as the are in db
                foreach ($tran as $key => $value) {
                    ${$key} = $value;
                }
                # format date
                $date = explode("-", $date);
                $date = $date[2] . "-" . $date[1] . "-" . $date[0];
                // get account names
                $deb = undget("core", "div, accname, topacc, accnum", "accounts", "accid", $debit);
                $debacc = pg_fetch_array($deb);
                $ct = undget("core", "div, accname, topacc,accnum", "accounts", "accid", $credit);
                $ctacc = pg_fetch_array($ct);
                $dtbranname = branname($debacc['div']);
                $ctbranname = branname($ctacc['div']);
                $OUTPUT .= "<tr class='bg-odd'><td>{$date}</td><td>{$debacc['topacc']}/{$debacc['accnum']} - {$debacc['accname']} - {$dtbranname}</td><td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']} - {$ctbranname}</td><td>{$refnum}</td><td>" . CUR . " {$amount}</td><td>{$details}</td><td>{$author}</td></tr>";
            }
        }
    }
    $OUTPUT .= "\n\t<tr><td><br></td></tr>\n\n\t<!--\n\t<tr><td align=center colspan=10>\n\t\t<form action='../xls/alltrans-prd-xls.php' method=post name=form>\n\t\t<input type=hidden name=key value=viewtran>\n\t\t<input type=hidden name=fprd value='{$fprd}'>\n\t\t<input type=hidden name=tprd value='{$tprd}'>\n\t\t<input type=submit name=xls value='Export to spreadsheet'>\n\t\t</form>\n\t</td></tr>\n\t-->\n\n\t</table>\n\t<p>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=25%>\n\t\t<tr><td><br></td></tr>\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 $OUTPUT;
}
/**
 * @ignore
 */
function getAccn($topacc, $accnum)
{
    # Get account
    $accRs = undget("core", "*", "accounts", "topacc", "{$topacc}' AND accnum = '{$accnum}' AND div = '" . USER_DIV);
    if (pg_numrows($accRs) < 1) {
        return false;
    }
    $acc = pg_fetch_array($accRs);
    return $acc;
}