require "../core-settings.php";
# decide what to do
if (isset($_GET['accid'])) {
    $_GET['prd'] = PRD_DB;
    $_GET['details'] = "";
    $OUTPUT = viewtran($_GET);
} elseif (isset($_POST["key"])) {
    switch ($_POST["key"]) {
        case "viewtran":
            $OUTPUT = viewtran($_POST);
            break;
        default:
            $OUTPUT = slctAcc($_POST);
    }
} else {
    $OUTPUT = slctAcc($_POST);
}
# Get templete
require "../template.php";
# Select Category
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)) {
function write()
{
    global $_GET;
    extract($_GET);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($empnum, "num", 1, 9, "Invalid employee selected.");
    $v->isOk($expacc_pension, "string", 1, 3, "Invalid pension account.");
    $v->isOk($expacc_provident, "string", 1, 3, "Invalid provident account.");
    $v->isOk($expacc_medical, "string", 1, 3, "Invalid medical account.");
    $v->isOk($expacc_ret, "string", 1, 3, "Invalid retirement annuity account.");
    $v->isOk($expacc_uif, "string", 1, 3, "Invalid uif account.");
    $v->isOk($expacc_salwages, "string", 1, 3, "Invalid salaries and wages account.");
    $v->isOk($expacc_sdl, "string", 1, 3, "Invalid sdl account.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return slctAcc($confirm);
    }
    $fields = array("pension", "provident", "uif", "medical", "ret", "salwages", "sdl");
    if (isset($dedaccs) && is_array($dedaccs)) {
        foreach ($dedaccs as $key => $value) {
            if (!$v->isOk($key . $value, "string", 2, 20, "")) {
                continue;
            }
            db_conn("cubit");
            $sql = "UPDATE empdeduct SET accid='{$value}' WHERE id='{$key}' AND empnum='{$empnum}'";
            $rslt = db_exec($sql);
        }
    }
    if (isset($allowaccs) && is_array($allowaccs)) {
        foreach ($allowaccs as $key => $value) {
            if (!$v->isOk($key . $value, "string", 2, 20, "")) {
                continue;
            }
            db_conn("cubit");
            $sql = "UPDATE empallow SET accid='{$value}' WHERE id='{$key}' AND empnum='{$empnum}'";
            $rslt = db_exec($sql);
        }
    }
    $fields_sql = array();
    foreach ($fields as $fn) {
        $fields_sql[] = "expacc_{$fn}='" . ${"expacc_{$fn}"} . "'";
    }
    db_conn("cubit");
    $sql = "UPDATE employees\n\t\t\tSET " . implode(",", $fields_sql) . "\n\t\t\tWHERE empnum='{$empnum}'";
    $rslt = db_exec($sql) or errDie("Error updating employee expense accounts.");
    # status report
    $write = "\n\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t<tr>\n\t\t\t\t<th>Employee Expense account links Successfully Updated</th>\n\t\t\t</tr>\n\t\t<table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $write;
}
if (isset($_POST["key"])) {
    switch ($_POST["key"]) {
        case "slctacc":
            $OUTPUT = slctAcc($_POST);
            break;
        case "confirm":
            $OUTPUT = confirm($_POST);
            break;
        case "write":
            $OUTPUT = write($_POST);
            break;
        default:
            $OUTPUT = slctAcc();
    }
} else {
    $OUTPUT = slctAcc();
}
# get template
require "template.php";
# Select Account
function slctAcc()
{
    core_connect();
    $accnts = mkAccSelect("accid", 1, ACCTYPE_B);
    // 	$sql = "SELECT * FROM accounts WHERE acctype ='B' AND div = '".USER_DIV."'";
    // 	$accRslt = db_exec($sql);
    // 	if(pg_numrows($accRslt) < 1){
    // 		return "<li> ERROR : There are no accounts in the category selected.";
    // 	}
    // 	$accnts = "<select name='accid'>";
    // 	while($acc = pg_fetch_array($accRslt)){
function viewtran($_POST)
{
    # get vars
    extract($_POST);
    # 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\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t\t<input type='hidden' name='details' value='{$details}'>\n\t\t\t<input type='hidden' name='accid' value='{$accid}'>";
    } else {
        $v->isOk($topacc, "num", 1, 4, "Invalid Account number.");
        $hide = "\n\t\t\t<input type='hidden' name='prd' value='{$prd}'>\n\t\t\t<input type='hidden' name='topacc' value='{$topacc}'>";
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return $confirm . slctAcc();
    }
    if (isset($details)) {
        $acc = qryAccounts($accid);
    } else {
        if (strlen($topacc) > 2) {
            $acc = qryAccountsNum($topacc, "000");
        } else {
            return "<li> Accounts number : {$topacc}/000 does not exist.</li>";
        }
    }
    db_conn($prd);
    // Set up table to display in
    $OUTPUT = "\n\t\t<center>\n\t\t<h3>Journal Entries for Account : {$acc['topacc']}/{$acc['accnum']} - {$acc['accname']} and Sub Accounts</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Date</th>\n\t\t\t\t<th>Reference</th>\n\t\t\t\t<th>Contra Acc</th>\n\t\t\t\t<th>Description</th>\n\t\t\t\t<th>Debit</th>\n\t\t\t\t<th>Credit</th>\n\t\t\t\t<th>User</th>\n\t\t\t</tr>";
    # get all sub accounts
    core_connect();
    $sql = "SELECT * FROM accounts WHERE topacc = '{$acc['topacc']}' AND div = '" . USER_DIV . "' ORDER BY accnum ASC";
    $subRs = db_exec($sql);
    # all totals
    $allcredtot = 0;
    $alldebtot = 0;
    while ($subacc = pg_fetch_array($subRs)) {
        $OUTPUT .= "\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='10'><h2>Account : {$subacc['topacc']}/{$subacc['accnum']} - {$subacc['accname']}</h2></td>\n\t\t\t</tr>";
        # get Transactions
        db_conn($prd);
        $sql = "SELECT * FROM transect WHERE debit = '{$subacc['accid']}' AND div = '" . USER_DIV . "' OR credit = '{$subacc['accid']}' AND div = '" . USER_DIV . "'";
        $tranRslt = db_exec($sql) or errDie("ERROR: Unable to retrieve Transaction details from database.", SELF);
        if (pg_numrows($tranRslt) < 1) {
            # counts
            $credtot = 0;
            $debtot = 0;
            $OUTPUT .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='10'>No Transactions found</td>\n\t\t\t\t</tr>";
            $debtot = sprint($debtot);
            $credtot = sprint($credtot);
            $OUTPUT .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='4'><b>Total</b></td>\n\t\t\t\t\t<td><b>" . CUR . " {$debtot}</b></td>\n\t\t\t\t\t<td><b>" . CUR . " {$credtot}</b></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
        } else {
            # counts
            $credtot = 0;
            $debtot = 0;
            # display all transactions
            while ($tran = pg_fetch_array($tranRslt)) {
                extract($tran);
                $amount = sprint($amount);
                if ($debit == $subacc['accid']) {
                    $cacc['accname'] = $caccname;
                    $cacc['accnum'] = $caccnum;
                    $cacc['topacc'] = $ctopacc;
                    $debitamt = "R " . $amount;
                    $debtot += $amount;
                    $alldebtot += $amount;
                    $creditamt = "";
                } else {
                    $debitamt = "";
                    $creditamt = "R " . $amount;
                    $credtot += $amount;
                    $allcredtot += $amount;
                    $cacc['accname'] = $daccname;
                    $cacc['accnum'] = $daccnum;
                    $cacc['topacc'] = $dtopacc;
                }
                /*
                # get contra account name
                $caccRs = get("core","accname,topacc,accnum","accounts","accid",$cacc);
                $cacc = pg_fetch_array($caccRs);
                */
                $OUTPUT .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$date}</td>\n\t\t\t\t\t\t<td>{$custom_refnum}</td>\n\t\t\t\t\t\t<td>{$cacc['topacc']}/{$cacc['accnum']} - {$cacc['accname']}</td>\n\t\t\t\t\t\t<td>{$details}</td>\n\t\t\t\t\t\t<td align='right'>{$debitamt}</td>\n\t\t\t\t\t\t<td align='right'>{$creditamt}</td>\n\t\t\t\t\t\t<td>{$author}</td>\n\t\t\t\t\t</tr>";
            }
            if ($acc["acctype"] == "I") {
                $accbal = $credtot - $debtot;
            } else {
                if ($acc["acctype"] == "E") {
                    $accbal = $debtot - $credtot;
                } else {
                    if ($acc["acctype"] == "B") {
                        $accbal = $debtot - $credtot;
                    } else {
                        return "<li class='err'>Account number is beyond limits.</li>";
                    }
                }
            }
            $debtot = sprint($debtot);
            $credtot = sprint($credtot);
            $accbal = sprint($accbal);
            $OUTPUT .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='4'><b>Total</b></td>\n\t\t\t\t\t<td><b>" . CUR . " {$debtot}</b></td>\n\t\t\t\t\t<td><b>" . CUR . " {$credtot}</b></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='4'><b>Balance</b></td>\n\t\t\t\t\t<td colspan='2'><b>" . CUR . " {$accbal}</b></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
        }
    }
    $alldebtot = sprint($alldebtot);
    $allcredtot = sprint($allcredtot);
    $OUTPUT .= "\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='4'><b>Total</b></td>\n\t\t\t\t<td><b>" . CUR . " {$alldebtot}</b></td>\n\t\t\t\t<td><b>" . CUR . " {$allcredtot}</b></td>\n\t\t\t\t<td></td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center' colspan='10'>\n\t\t\t\t\t<form action='../xls/accsub-trans-xls.php' method='POST' name='form'>\n\t\t\t\t\t\t<input type='hidden' name='key' value='viewtran'>\n\t\t\t\t\t\t{$hide}\n\t\t\t\t\t\t<input type='submit' name='xls' value='Export to spreadsheet'>\n\t\t\t\t\t</form>\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<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'><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</td>\n\t\t\t</tr>\n\t\t</table>";
    return $OUTPUT;
}