function display()
{
    extract($_REQUEST);
    $fields = array();
    $fields["from_year"] = date("Y");
    $fields["from_month"] = date("m");
    $fields["from_day"] = "01";
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("d");
    extract($fields, EXTR_SKIP);
    $from_date = "{$from_year}-{$from_month}-{$from_day}";
    $to_date = "{$to_year}-{$to_month}-{$to_day}";
    if (isset($id)) {
        $where = "AND doc_id='{$id}'";
    } else {
        $where = "";
    }
    $sql = "SELECT *,extract('epoch' FROM timestamp) AS e_time FROM cubit.document_movement\n\tWHERE (timestamp BETWEEN '{$from_date} 00:00:00' AND '{$to_date} 23:59:59') {$where} ORDER BY timestamp DESC";
    $dm_rslt = db_exec($sql) or errDie("Unable to retrieve document movement");
    $dm_out = "";
    while ($dm_data = pg_fetch_array($dm_rslt)) {
        $bgcolor = bgcolorg();
        $dm_out .= "<tr class='" . bg_class() . "'>\n\t\t\t<td>" . date("d-m-Y G:i:s", $dm_data["e_time"]) . "</td>\n\t\t\t<td>{$dm_data['doc_id']}</td>\n\t\t\t<td>{$dm_data['project']}</td>\n\t\t\t<td>{$dm_data['area']}</td>\n\t\t\t<td>{$dm_data['discipline']}</td>\n\t\t\t<td>{$dm_data['doc_type']}</td>\n\t\t\t<td>{$dm_data['revision']}</td>\n\t\t\t<td>{$dm_data['drawing_num']}</td>\n\t\t\t<td>{$dm_data['sheet_num']}</td>\n\t\t\t<td>{$dm_data['title']}</td>\n\t\t\t<td>{$dm_data['location']}</td>\n\t\t\t<td>{$dm_data['contract']}</td>\n\t\t\t<td>{$dm_data['contractor']}</td>\n\t\t\t<td>{$dm_data['code']}</td>\n\t\t\t<td>{$dm_data['issue_for']}</td>\n\t\t\t<td>{$dm_data['qs']}</td>\n\t\t\t<td>{$dm_data['status']}</td>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td colspan='20'>{$dm_data['movement_description']}</td>\n\t\t</tr>";
    }
    if (empty($dm_out)) {
        $dm_out = "<tr class='" . bg_class() . "'>\n\t\t\t<td colspan='20'><li>No results found.</li></td>\n\t\t</tr>";
    }
    $OUTPUT = "<center>\n\t<h3>Document Movement Report</h3>\n\t<form method='post' action='" . SELF . "'>\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr>\n\t\t\t<th colspan='4'>Date Range</th>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>" . mkDateSelect("from", $from_year, $from_month, $from_day) . "</td>\n\t\t\t<td><b>To</b></td>\n\t\t\t<td>" . mkDateSelect("to", $to_year, $to_month, $to_day) . "</td>\n\t\t\t<td><input type='submit' value='Select' /></td>\n\t\t</tr>\n\t</table>\n\t</form>\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr>\n\t\t\t<th>Time</th>\n\t\t\t<th>Record Number</th>\n\t\t\t<th>Project</th>\n\t\t\t<th>Area</th>\n\t\t\t<th>Discipline</th>\n\t\t\t<th>Document Type</th>\n\t\t\t<th>Revision</th>\n\t\t\t<th>Drawing Number</th>\n\t\t\t<th>Sheet Number</th>\n\t\t\t<th>Title</th>\n\t\t\t<th>Location</th>\n\t\t\t<th>Contract</th>\n\t\t\t<th>Contractor</th>\n\t\t\t<th>Code</th>\n\t\t\t<th>Issue For</th>\n\t\t\t<th>QS</th>\n\t\t\t<th>Status</th>\n\t\t</tr>\n\t\t{$dm_out}\n\t</table>";
    return $OUTPUT;
}
function slctAcc()
{
    global $PRDMON;
    $fprd = finMonList("fprd", $PRDMON[1]);
    $tprd = finMonList("tprd", PRD_DB);
    // Layout
    $slctAcc = "\n\t\t<h3>Select Options</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 colspan='2'>Period Range</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>From : {$fprd}</td>\n\t\t\t\t<td>To : {$tprd}</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></td>\n\t\t\t\t<td align='right'><input type='submit' value='View All &raquo;'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<p>\n\t\t<table " . bgcolorg() . " width='100'>\n\t\t\t<tr><th>Quick Links</th></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>\n\t\t\t</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 display()
{
    extract($_REQUEST);
    $reports = array("acid_test", "customers_turnover", "daily_sales_in_receivables", "inventory_turnover", "days_cost_of_sales_in_inventory", "accounts_receivable_turnover", "accounts_payable_turnover", "days_cost_of_sales_in_accounts_payable", "assets_turnover", "debt_ratio", "depreciation_expense_gross_depreciable_property", "current_ratio", "working_captial", "accounts_receivable_working_capital", "inventory_working_capital", "long_term_liabilities_working_capital", "sales_working_capital", "gross_profit_turnover", "net_profit_turnover", "return_on_assets", "return_on_investment", "repairs_and_maintenance_gross_depreciation_of_property", "owners_equity", "debt_equity", "financial_leverage", "interest_net_income_excluding_interest", "operating_cycle_days");
    $not_percentage = array("daily_sales_in_receivables", "days_cost_of_sales_in_inventory", "days_cost_of_sales_in_accounts_payable", "working_capital", "operating_cycle_days");
    $i = 1;
    $reports_out = "";
    foreach ($reports as $name) {
        if ($i == 1) {
            $reports_out .= "<tr><td width='20%' valign='top'>";
        } else {
            $reports_out .= "<td width='20%' valign='top'>";
        }
        if (function_exists($name)) {
            list($description, $calculation) = call_user_func($name);
        } else {
            $description = "Not implemented.";
        }
        $calculation = sprint($calculation);
        if (!in_array($name, $not_percentage)) {
            $calculation .= "%";
        }
        $reports_out .= "\n\t\t<table " . TMPL_tblDflts . " width='100%' style='border: 1px solid #fff'>\n\t\t\t<tr><th>" . nice_name($name) . "</th></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>\n\t\t\t\t\t<span style='font-size: 0.9em; letter-spacing: -0.065em'>\n\t\t\t\t\t\t{$description}\n\t\t\t\t\t</span>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr bgcolor='#ffffff'>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<span style='font-size: 1.2em; color: #f00; font-weight:bold'>\n\t\t\t\t\t\t{$calculation}\n\t\t\t\t\t</span>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</table>";
        if ($i == 5) {
            $reports_out .= "</td></tr>";
            $i = 0;
        } else {
            $reports_out .= "</td>";
        }
        $i++;
    }
    if (!isset($heart)) {
        $sql = "SELECT value FROM cubit.settings WHERE constant='HEART'";
        $heart_rslt = db_exec($sql) or errDie("Unable to retrieve heart display value.");
        $heart = pg_fetch_result($heart_rslt, 0);
    } else {
        $sql = "UPDATE cubit.settings SET value='{$heart}' WHERE constant='HEART'";
        db_exec($sql) or errDie("Unable to update heart status");
    }
    if ($heart) {
        $heart_yes = "checked";
        $heart_no = "";
    } else {
        $heart_yes = "";
        $heart_no = "checked";
    }
    $bgcolor = bgcolorg();
    $ql = mkQuickLinks(ql("ratio_settings.php", "Link Accounts to Ratios"));
    $OUTPUT = "\n\t<table cellpadding='5' cellspacing='2'>{$reports_out}</table>\n\t<center>\n\t<form method='post' action='" . SELF . "' name='form'>\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr><th colspan='2'>Display Heart on Main Menu</th></tr>\n\t\t<tr>\n\t\t\t<td class='" . bg_class() . "' align='center'>\n\t\t\t\tYes <input type='radio' name='heart' value='1'\n\t\t\t\tonchange='javascript:document.form.submit()' {$heart_yes} />\n\t\t\t</td>\n\t\t\t<td class='" . bg_class() . "' align='center'>\n\t\t\t\tNo <input type='radio' name='heart' value='0'\n\t\t\t\tonchange='javascript:document.form.submit()' {$heart_no} />\n\t\t\t</td>\n\t\t</tr>\n\t\t<tr><td colspan='2' align='center'>{$ql}</td></tr>\n\t</table>\n\t</form>\n\t</center>";
    return $OUTPUT;
}
function display()
{
    extract($_REQUEST);
    // Retrieve account types
    $sql = "SELECT id, rname FROM cubit.ratio_account_types ORDER BY rname ASC";
    $type_rslt = db_exec($sql) or errDie("Unable to retrieve ratios.");
    $ratios_out = "";
    while ($type_data = pg_fetch_array($type_rslt)) {
        // Retrieve accounts
        $sql = "SELECT accounts.accid, topacc, accnum, accname\n\t\t\t\tFROM cubit.ratio_account_owners\n\t\t\t\t\tLEFT JOIN core.accounts\n\t\t\t\t\t\tON ratio_account_owners.accid=accounts.accid\n\t\t\t\tWHERE type_id='{$type_data['id']}'\n\t\t\t\tORDER BY id ASC";
        $acc_rslt = db_exec($sql) or errDie("Unable to retrieve accounts");
        $rows = pg_num_rows($acc_rslt) + 1;
        $bgcolor = bgcolorg();
        $results = pg_num_rows($acc_rslt);
        $ratios_out .= "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td rowspan='{$rows}'><b>{$type_data['rname']}</b></td>";
        $i = 0;
        $tr = "";
        while ($acc_data = pg_fetch_array($acc_rslt)) {
            // Should a new row be created
            if ($i) {
                $tr = "<tr class='" . bg_class() . "'>";
            } else {
                $tr = "";
            }
            $ratios_out .= "\n\t\t\t{$tr}\n\t\t\t\t<td>({$acc_data['topacc']}/{$acc_data['accnum']}) {$acc_data['accname']}</td>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<input type='checkbox' name='rem[{$type_data['id']}]'\n\t\t\t\t\tvalue='{$acc_data['accid']}'\n\t\t\t\t\tonchange='javascript:document.form.submit()' />\n\t\t\t\t</td>\n\t\t\t</tr>";
            $i++;
        }
        // Retrieve accounts
        $sql = "SELECT accid, topacc, accnum, accname FROM core.accounts\n\t\t\t\tORDER BY topacc, accnum ASC";
        $acc_rslt = db_exec($sql) or errDie("Unable to retrieve accounts.");
        $acc_sel = "<select name='account[{$type_data['id']}]'\n\t\t\t\t\tonchange='javascript:document.form.submit()'>";
        $acc_sel .= "<option value='0'>[None]</option>";
        while ($acc_data = pg_fetch_array($acc_rslt)) {
            $acc_sel .= "\n\t\t\t<option value='{$acc_data['accid']}'>\n\t\t\t\t({$acc_data['topacc']}/{$acc_data['accnum']}) {$acc_data['accname']}\n\t\t\t</option>";
        }
        $acc_sel .= "</select>";
        // New row
        if ($results) {
            $tr = "<tr class='" . bg_class() . "'>";
        } else {
            $tr = "";
        }
        $ratios_out .= "\n\t\t\t{$tr}\n\t\t\t<td>{$acc_sel}</td>\n\t\t\t<td>&nbsp;</td>\n\t\t</tr>";
    }
    if (empty($ratios_out)) {
        $ratios_out = "<tr class='" . bg_class() . "'>\n\t\t\t<td colspan='3'><li>No results found.</li></td>\n\t\t</tr>";
    }
    $OUTPUT = "\n\t<center>\n\t<h3>Link Accounts to Ratios</h3>\n\t<form method='post' action='" . SELF . "' name='form'>\n\t<input type='hidden' name='key' value='update' />\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr>\n\t\t\t<th>Ratio</th>\n\t\t\t<th>Accounts</th>\n\t\t\t<th>Remove</th>\n\t\t</tr>\n\t\t{$ratios_out}\n\t</table>\n\t</form>";
    return $OUTPUT;
}
function printSalesp()
{
    $OUT = "\n    <h3>Sales People</h3>\n    <table " . TMPL_tblDflts . ">\n    <tr>\n    \t<th>Number</th>\n    \t<th>Sales Person</th>\n    \t<th>Commission</th>\n    </tr>";
    $sp = qrySalesPerson();
    while ($salesp = $sp->fetch_array()) {
        $bgColor = bgcolorg();
        if ($salesp["com"] != 0) {
            $com_disp = "{$salesp['com']} %";
        } else {
            $com_disp = "Using Commission Set Under Stock Settings";
        }
        $OUT .= "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>{$salesp['salespno']}</td>\n\t\t\t<td>{$salesp['salesp']}</td>\n\t\t\t<td>{$com_disp}</td>\n\t\t\t<td><a href='salesp-edit.php?salespid={$salesp['salespid']}'>Edit</a></td>\n\t\t\t<td><a href='salesp-rem.php?salespid={$salesp['salespid']}'>Remove</a></td>\n\t\t</tr>";
    }
    $OUT .= "\n\t</table>\n\t<br />";
    $OUT .= mkQuickLinks(ql("salesp-add.php", "Add Sales Person"));
    return $OUT;
}
function writerem($_POST)
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($bank)) {
        foreach ($bank as $key => $value) {
            $v->isOk($bank[$key], "num", 1, 50, "Invalid Batch ID.");
        }
    } else {
        return "<li> - No Batch Entries Seleted. Please select at least one batch entry.</li>";
    }
    # display errors, if any
    if ($v->isError()) {
        $write = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $write .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $write .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $write;
    }
    db_conn('core');
    foreach ($bank as $key => $value) {
        # Get all the details
        $sql = "SELECT * FROM batch WHERE batchid = '{$value}' AND div = '" . USER_DIV . "'";
        $rslt = db_exec($sql) or errDie("Unable to access database.");
        $tran = pg_fetch_array($rslt);
        // Accounts details
        $dtaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $tran['debit']);
        $dtacc[$key] = pg_fetch_array($dtaccRs);
        $ctaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $tran['credit']);
        $ctacc[$key] = pg_fetch_array($ctaccRs);
        $date[$key] = $tran['date'];
        $refnum[$key] = $tran['refnum'];
        $amount[$key] = $tran['amount'];
        $descript[$key] = $tran['details'];
        # Remove the entries one by one
        core_Connect();
        $query = "DELETE FROM batch WHERE batchid = '{$bank[$key]}'";
        $Ex = db_exec($query) or errDie("Unable to delete batch file entries.", SELF);
    }
    // Start layout
    $write = "\n\t\t\t\t<center>\n\t\t\t\t<h3>Journal transactions entries have been removed from batch file</h3>\n\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t<th>Ref num</th>\n\t\t\t\t\t\t<th>Debit</th>\n\t\t\t\t\t\t<th>Credit</th>\n\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t<th>Description</th>\n\t\t\t\t\t</tr>";
    foreach ($bank as $key => $value) {
        $write .= "\n    \t\t\t\t<tr bgcolor=" . bgcolorg() . ">\n    \t\t\t\t\t<td>{$date[$key]}</td>\n    \t\t\t\t\t<td>{$refnum[$key]}</td>\n                        <td valign='center'>" . $dtacc[$key]['topacc'] . "/" . $dtacc[$key]['accnum'] . " " . $dtacc[$key]['accname'] . "</td>\n                        <td valign='center'>" . $ctacc[$key]['topacc'] . "/" . $ctacc[$key]['accnum'] . " " . $ctacc[$key]['accname'] . "</td>\n                        <td>" . CUR . " {$amount[$key]}</td>\n                        <td>{$descript[$key]}</td>\n\t\t\t\t\t</tr>";
    }
    $write .= "\n\t\t\t\t\t\t</table>\n\t\t\t        <br>\n\t\t\t        <table " . TMPL_tblDflts . " width='25%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='datacell'>\n\t\t\t\t\t\t\t<td align='center'><a href='trans-batch.php'>Add Journal Transactions to batch</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='datacell'>\n\t\t\t\t\t\t\t<td align='center'><a href='batch-view.php'>View batch Entries</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t        </table>";
    return $write;
}
function confirm()
{
    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" => "pension", "Provident" => "provident", "UIF" => "uif", "Medical Aid" => "medical", "Retirement Annuity" => "ret", "Salaries & Wages" => "salwages", "SDL" => "sdl");
    foreach ($fields as $fdesc => $fn) {
        if (isb(${"expacc_{$fn}"})) {
            $v->addError("", "Account is blocked for: {$fdesc}");
        }
        $rslt = get("core", "accname", "accounts", "accid", ${"expacc_{$fn}"});
        ${"name_{$fn}"} = pg_fetch_result($rslt, 0, 0);
    }
    $emp_ded = "";
    if (isset($dedaccs) && is_array($dedaccs)) {
        foreach ($dedaccs as $key => $value) {
            if (!$v->isOk($key . $value, "string", 2, 20, "")) {
                continue;
            }
            // salded
            db_conn("cubit");
            $sql = "SELECT salded.deduction FROM salded, empdeduct\n\t\t\t\t\tWHERE salded.id=empdeduct.dedid AND empdeduct.id='{$key}'";
            $rslt = db_exec($sql);
            $salded = pg_fetch_array($rslt);
            if (isb($value)) {
                $v->addError("", "Account is blocked for: {$salded['deduction']}");
            }
            // accounts
            $name_ded = pg_fetch_result(get("core", "accname", "accounts", "accid", $value), 0, 0);
            $emp_ded .= "\n\t\t\t<input type='hidden' name='dedaccs[{$key}]' value='{$value}'>\n\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t<td>{$salded['deduction']}</td>\n\t\t\t\t<td>{$name_ded}</td>\n\t\t\t</tr>";
        }
    }
    $emp_allow = "";
    if (isset($allowaccs) && is_array($allowaccs)) {
        foreach ($allowaccs as $key => $value) {
            if (!$v->isOk($key . $value, "string", 2, 20, "")) {
                continue;
            }
            // salded
            db_conn("cubit");
            $sql = "SELECT allowances.allowance FROM allowances, empallow\n\t\t\t\t\tWHERE allowances.id=empallow.allowid AND empallow.id='{$key}'";
            $rslt = db_exec($sql);
            $allowinfo = pg_fetch_array($rslt);
            if (isb($value)) {
                $v->addError("", "Account is blocked for: {$allowinfo['allowance']}");
            }
            // accounts
            $name_allow = pg_fetch_result(get("core", "accname", "accounts", "accid", $value), 0, 0);
            $emp_allow .= "\n\t\t\t<input type='hidden' name='allowaccs[{$key}]' value='{$value}'>\n\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t<td>{$allowinfo['allowance']}</td>\n\t\t\t\t<td>{$name_allow}</td>\n\t\t\t</tr>";
        }
    }
    // display account block errors if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return slctAcc($confirm);
    }
    $confirm = "\n\t\t<h3>Company Contributions to Employee Deductions Expense Accounts</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t<form action='" . SELF . "' method='GET'>\n\t\t\t<input type='hidden' name='key' value='write'>\n\t\t\t<input type='hidden' name='empnum' value='{$empnum}'>";
    foreach ($fields as $fn) {
        $confirm .= "<input type='hidden' name='expacc_{$fn}' value='" . ${"expacc_{$fn}"} . "'>";
    }
    $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>Salaries and Wages</td>\n\t\t\t\t<td>{$name_salwages}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Pension Fund</td>\n\t\t\t\t<td>{$name_pension}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Provident Fund</td>\n\t\t\t\t<td>{$name_provident}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Retirement Annuity Fund</td>\n\t\t\t\t<td>{$name_ret}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Medical Aid</td>\n\t\t\t\t<td>{$name_medical}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>UIF</td>\n\t\t\t\t<td>{$name_uif}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Skills Development Levy</td>\n\t\t\t\t<td>{$name_sdl}</td>\n\t\t\t</tr>\n\t\t\t{$emp_ded}\n\t\t\t<tr><th colspan='2'>Allowances</th></tr>\n\t\t\t{$emp_allow}\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'></td>\n\t\t\t\t<td align='right'><input type='submit' value='Add Link &raquo'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $confirm;
}
function view($supid)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($supid, "num", 1, 50, "Invalid Supplier id.");
    # 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
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$supid}' AND div = '" . USER_DIV . "'";
    $suppRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
    if (pg_numrows($suppRslt) < 1) {
        return "<li> Invalid supplier ID.</li>";
    } else {
        $supp = pg_fetch_array($suppRslt);
        # get vars
        foreach ($supp as $key => $value) {
            ${$key} = $value;
        }
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$deptid}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $deptname = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
        $deptname = $dept['deptname'];
    }
    db_conn("cubit");
    $sql = "SELECT * FROM supp_groups WHERE id='{$groupid}'";
    $grpRslt = db_exec($sql);
    $group = pg_fetch_array($grpRslt);
    db_conn('cubit');
    $Sl = "SELECT id FROM cons WHERE supp_id='{$supid}'";
    $Ry = db_exec($Sl) or errDie("Unable to get contact from db.");
    $i = 0;
    $conpers = "";
    if (pg_num_rows($Ry) > 0) {
        $cdata = pg_fetch_array($Ry);
        $Sl = "SELECT * FROM conpers WHERE con='{$cdata['id']}' ORDER BY name";
        $Ry = db_exec($Sl) or errDie("Unable to get contacts from db.");
        if (pg_num_rows($Ry) > 0) {
            $conpers = "\r\n\t\t\t\t<h3>Contact Persons</h3>\r\n\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<th>Name</th>\r\n\t\t\t\t\t\t<th>Position</th>\r\n\t\t\t\t\t\t<th>Tel</th>\r\n\t\t\t\t\t\t<th>Cell</th>\r\n\t\t\t\t\t\t<th>Fax</th>\r\n\t\t\t\t\t\t<th>Email</th>\r\n\t\t\t\t\t\t<th>Notes</th>\r\n\t\t\t\t\t\t<th colspan='2'>Options</th>\r\n\t\t\t\t\t</tr>";
            while ($cp = pg_fetch_array($Ry)) {
                $i++;
                $bgcolor = $i % 2 ? bgcolorg() : bgcolorg();
                $conpers .= "\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>{$cp['name']}</td>\r\n\t\t\t\t\t\t<td>{$cp['pos']}</td>\r\n\t\t\t\t\t\t<td>{$cp['tell']}</td>\r\n\t\t\t\t\t\t<td>{$cp['cell']}</td>\r\n\t\t\t\t\t\t<td>{$cp['fax']}</td>\r\n\t\t\t\t\t\t<td>{$cp['email']}</td>\r\n\t\t\t\t\t\t<td>{$cp['notes']}</td>\r\n\t\t\t\t\t\t<td><a href='conper-edit.php?id={$cp['id']}&type=edit'>Edit</a></td>\r\n\t\t\t\t\t\t<td><a href='conper-rem.php?id={$cp['id']}'>Delete</a></td>\r\n\t\t\t\t\t</tr>";
            }
            $conpers .= "</table>";
        }
    }
    // Retrieve team name
    $sql = "SELECT name FROM crm.teams WHERE id='{$team_id}'";
    $team_rslt = db_exec($sql) or errDie("Unable to retrieve teams.");
    $team_name = pg_fetch_result($team_rslt, 0);
    # Layout
    $confirm = "\r\n\t\t<h3>Supplier Details</h3>\r\n\t\t<table cellpadding=0 cellspacing=0>\r\n\t\t\t<tr valign='top'>\r\n\t\t\t\t<td>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'>Supplier Details</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Department</td>\r\n\t\t\t\t\t\t\t<td>{$deptname}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Supplier No</td>\r\n\t\t\t\t\t\t\t<td>{$supno}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Name</td>\r\n\t\t\t\t\t\t\t<td>{$supname}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Group</td>\r\n\t\t\t\t\t\t\t<td>{$group['groupname']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Branch</td>\r\n\t\t\t\t\t\t\t<td>{$branch}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>VAT Number</td>\r\n\t\t\t\t\t\t\t<td>{$vatnum}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Address</td>\r\n\t\t\t\t\t\t\t<td><pre>{$supaddr}</pre></td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Postal Address</td>\r\n\t\t\t\t\t\t\t<td><pre>{$suppostaddr}</pre></td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Contact Name</td>\r\n\t\t\t\t\t\t\t<td>{$contname}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Tel No.</td>\r\n\t\t\t\t\t\t\t<td>{$tel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Fax No.</td>\r\n\t\t\t\t\t\t\t<td>{$fax}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Cell No.</td>\r\n\t\t\t\t\t\t\t<td>{$cell}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>E-mail</td>\r\n\t\t\t\t\t\t\t<td>{$email}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Web Address</td>\r\n\t\t\t\t\t\t\t<td>http://{$url}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Status BEE</td>\r\n\t\t\t\t\t\t\t<td>{$bee_status}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Team Permissions</td>\r\n\t\t\t\t\t\t\t<td>{$team_name}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Comments</td>\r\n\t\t\t\t\t\t\t<td>{$comments}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t\t<td>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<th colspan='2'> Bank Details</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Bank </td>\r\n\t\t\t\t\t\t\t<td>{$bankname}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Branch</td>\r\n\t\t\t\t\t\t\t<td>{$branname}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Branch Code</td>\r\n\t\t\t\t\t\t\t<td>{$brancode}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Account Number</td>\r\n\t\t\t\t\t\t\t<td>{$bankaccno}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Reference Number</td>\r\n\t\t\t\t\t\t\t<td>{$reference}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Settlement Discount %</td>\r\n\t\t\t\t\t\t\t<td>{$setdisc} %</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr><td><br></td></tr>\r\n\t\t\t\t\t\t<tr><td>";
    // Documents
    //		$sdoc_db = new dbSelect("supplier_docs", "crm", m("where", "supid='$supid'"));
    //		$sdoc_db->run();
    $docs_out = "";
    //		while ($sdoc_data = $sdoc_db->fetch_array()) {
    db_conn('crm');
    $get_docs = "SELECT * FROM supplier_docs WHERE supid = '{$supid}'";
    $run_docs = db_exec($get_docs) or errDie("Unable to get supplier document information.");
    while ($sdoc_data = pg_fetch_array($run_docs)) {
        if (strlen($sdoc_data['filename']) > 0) {
            $showdoc = "{$sdoc_data['filename']}";
        } elseif (strlen($sdoc_data['real_filename']) > 0) {
            $showdoc = "{$sdoc_data['real_filename']}";
        } else {
            $showdoc = "File" . $sdoc_data["id"];
        }
        $docs_out .= "\r\n\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t<td><a href='supp_doc_get.php?id={$sdoc_data['id']}'>{$showdoc}</a></td>\r\n\t\t\t\t\t<td>" . getFileSize($sdoc_data["size"]) . "</td>\r\n\t\t\t\t</tr>";
    }
    $confirm .= "\r\n\t\t\t\t</tr>\r\n\t\t\t</td>\r\n\t\t\t<tr>\r\n\t\t\t\t<td colspan='2'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'>Documents</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th>Filename</th>\r\n\t\t\t\t\t\t\t<th>Size</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t{$docs_out}\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>";
    $confirm .= "\r\n\t\t\t\t</table>\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<td colspan='2' align='right'>\r\n\t\t\t\t\t\t\t<table border=0 cellpadding='2' cellspacing='1'>\r\n\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t\t<th>Quick Links</th>\r\n\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t\t\t<td><a href='supp-view.php'>View Suppliers</a></td>\r\n\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\r\n\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t</table>\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t</table>\r\n\t\t\t\t</form>\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t{$conpers}";
    return $confirm;
}
function write($_POST)
{
    # Get vars
    extract($_POST);
    if (isset($back)) {
        return slctacc($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    foreach ($amount as $key => $value) {
        $v->isOk($ctaccid[$key], "num", 1, 50, "Invalid Account to be Credited.[{$key}]");
        $v->isOk($dtaccid[$key], "num", 1, 50, "Invalid Account to be Debited.[{$key}]");
        $v->isOk($refnum[$key], "num", 1, 10, "Invalid Reference number.[{$key}]");
        $v->isOk($amount[$key], "float", 1, 20, "Invalid Amount.[{$key}]");
        $v->isOk($descript[$key], "string", 0, 255, "Invalid Details.[{$key}]");
        $datea = explode("-", $date[$key]);
        if (count($datea) == 3) {
            if (!checkdate($datea[1], $datea[0], $datea[2])) {
                $v->isOk($date[$key], "num", 1, 1, "Invalid date.");
            }
        } else {
            $v->isOk($date[$key], "num", 1, 1, "Invalid date.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $write = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $write .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $write .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $write;
    }
    foreach ($amount as $key => $value) {
        // Accounts details
        $dtaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $dtaccid[$key]);
        $dtacc[$key] = pg_fetch_array($dtaccRs);
        $ctaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $ctaccid[$key]);
        $ctacc[$key] = pg_fetch_array($ctaccRs);
        # begin sql transaction
        pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
        // Insert the records into the transaction table
        core_connect();
        # format date of loss
        $date[$key] = explode("-", $date[$key]);
        $date[$key] = $date[$key][2] . "-" . $date[$key][1] . "-" . $date[$key][0];
        # Insert into batch
        $sql = "\n\t\t\tINSERT INTO batch (\n\t\t\t\tdate, debit, credit, refnum, amount, \n\t\t\t\tauthor, details, proc, div\n\t\t\t) VALUES (\n\t\t\t\t'{$date[$key]}', '{$dtaccid[$key]}', '{$ctaccid[$key]}', '{$refnum[$key]}', '{$amount[$key]}', \n\t\t\t\t'" . USER_NAME . "', '{$descript[$key]}', 'no', '" . USER_DIV . "'\n\t\t\t)";
        $transRslt = db_exec($sql) or errDie("Unable to insert Transaction  details to database", SELF);
        # commit sql transaction
        pglib_transaction("COMMIT") or errDie("Unable to finish a database transaction.", SELF);
    }
    // Start layout
    $write = "\n\t\t<center>\n\t\t<h3>Journal transactions have been recorded to a batch file</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>Ref num</th>\n\t\t\t\t<th>Debit</th>\n\t\t\t\t<th>Credit</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t\t<th>Description</th>\n\t\t\t</tr>";
    foreach ($amount as $key => $value) {
        $write .= "\n\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t<td>{$date[$key]}</td>\n\t\t\t\t<td>{$refnum[$key]}</td>\n\t\t\t\t<td valign='center'>" . $dtacc[$key]['topacc'] . "/" . $dtacc[$key]['accnum'] . " " . $dtacc[$key]['accname'] . "</td>\n\t\t\t\t<td valign='center'>" . $ctacc[$key]['topacc'] . "/" . $ctacc[$key]['accnum'] . " " . $ctacc[$key]['accname'] . "</td>\n\t\t\t\t<td nowrap>" . CUR . " {$amount[$key]}</td>\n\t\t\t\t<td>{$descript[$key]}</td>\n\t\t\t</tr>";
    }
    $write .= "\n\t\t</table>\n\t\t<br>\n\t\t<table " . TMPL_tblDflts . " width='25%'>\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='../reporting/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='trans-new.php'>Journal Transaction</td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td align='center'><a href='batch-view.php'>View batch file</td>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $write;
}
function translate($offset = 1)
{
    global $_POST;
    extract($_POST);
    require_lib("validate");
    $v = new validate();
    $v->isOk($language, "string", 1, 2, "Invalid language code.");
    $v->isOk($country, "string", 2, 2, "Invalid country code.");
    $v->isOk($encoding, "string", 1, 255, "Invalid character encoding.");
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>{$e['msg']}</li>";
        }
        return enter($confirm);
    }
    $fields = array();
    $fields["untrans"] = "";
    $fields["search"] = "";
    $fields["replace"] = "";
    $fields["sr_inf"] = "";
    extract($fields, EXTR_SKIP);
    if (!isset($untrans)) {
        $untrans = "";
    }
    $locale = $language . "_" . $country;
    $sz_msgdir = "locale/{$locale}/LC_MESSAGES";
    $sz_msgpath = "locale/{$locale}/LC_MESSAGES/messages.po";
    // Create the directory if it does not already exist
    if (!cfs::is_dir($sz_msgdir)) {
        cfs::mkdir($sz_msgdir);
    }
    // Open the pot file
    if (!cfs::is_file($sz_msgpath) || cfs::filesize($sz_msgpath) == 0) {
        $ar_messages = cfs::get_contents("messages.po");
        cfs::put_contents($sz_msgpath, $ar_messages);
    }
    // Retrieve the translation text
    $tl = "";
    $ar_pot_file = cfs::file($sz_msgpath);
    if (!empty($untrans)) {
        $ar_pot_file = removeTranslated($ar_pot_file);
    }
    if (isset($srchrep)) {
        $sr_ar = searchReplace($ar_pot_file, $search, $replace, $locale);
        $ar_pot_file = $sr_ar["file"];
        $sr_inf = "Replaced: <b>{$sr_ar['count']}</b>";
    }
    // Retrieve the amount of msgid's for the offset
    $n_count = 0;
    for ($i = 0; $i < count($ar_pot_file); $i++) {
        if (!isset($ar_pot_file[$i])) {
            continue;
        }
        if (preg_match("/(^msgid \")([^\"]*)(\")/", $ar_pot_file[$i], $ar_matches) && $ar_matches[2] != "") {
            $n_count++;
        }
    }
    // Calculate the page numbers
    $current_page = intval($offset / OFFSET_SIZE) + 1;
    $total_pages = intval($n_count / OFFSET_SIZE) + 1;
    // Calculate the starting value of the next and previous buttons
    $n_next = $offset + OFFSET_SIZE;
    $n_prev = $offset - OFFSET_SIZE;
    // Decide which buttons to display
    if ($n_next > $n_count) {
        $sz_next = "<input type=submit value='" . ct("Translate") . "'>";
    } else {
        $sz_next = "<input type=submit name='next' value='" . ct("Next &raquo") . "'>";
    }
    if ($n_prev < 0) {
        $sz_prev = "";
    } else {
        $sz_prev = "<input type=submit name='prev' value='" . ct("&laquo Previous") . "'>";
    }
    // POT file is empty
    if (!count($ar_pot_file)) {
        $OUTPUT = "<li class='err'>Unable to load translation text.</li>" . mkQuickLinks(ql("locale-translate.php", "Translate Cubit"), ql("locale-settings.php", "Locale Settings"));
        return $OUTPUT;
    }
    // Start reading each line of the translation within the offset
    $n_count2 = 0;
    foreach ($ar_pot_file as $i => $value) {
        if (preg_match("/(^msgid \")([^\"]*)(\")/", $ar_pot_file[$i], $ar_msgid) && $ar_msgid[2] != "") {
            $n_count2++;
            if ($n_count2 >= $offset && $n_count2 < $n_next) {
                if (preg_match("/(^msgstr \")([^\"]*)(\")/", $ar_pot_file[$i + 1], $ar_msgstr)) {
                    $tl .= "<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><b>" . htmlspecialchars($ar_msgid[2]) . "</b></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td align=center>\n\t\t\t\t\t\t<input type=text style='width: 495px;\n\t\t\t\t\t\tbackground:'" . bgcolorg() . "' name='tl[" . ($i + 1) . "]'\n\t\t\t\t\t\tvalue='{$ar_msgstr['2']}'>\n\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
                }
            }
        }
    }
    require "locale_codes.php";
    // Retrieve the name of the language
    if (isset($ar_languages) && is_array($ar_languages)) {
        $lang_out = "";
        foreach ($ar_languages as $lang_name => $lang_code) {
            if ($lang_code == $language) {
                $lang_out = $lang_name;
            }
        }
    } else {
        $OUTPUT = "<li class='err'>Unable to load language codes.</li>" . mkQuickLinks(ql("locale-translate.php", "Translate Cubit"), ql("locale-settings.php", "Locale Settings"));
        return $OUTPUT;
    }
    // Retrieve the name of the country
    if (isset($ar_countries) && is_array($ar_countries)) {
        $country_out = "";
        foreach ($ar_countries as $country_name => $country_code) {
            if ($country_code == $country) {
                $country_out = $country_name;
            }
        }
    } else {
        $OUTPUT = "<li class='err'>Unable to load country codes.</li>" . mkQuickLinks(ql("locale-translate.php", "Translate Cubit"), ql("locale-settings.php", "Locale Settings"));
        return $OUTPUT;
    }
    $OUTPUT = "<center>\n\t<h3>" . ct("Translate Cubit") . "</h3>\n\t<form method=post action='" . SELF . "' name='form'>\n\t<input type=hidden name=key value='write'>\n\t<input type=hidden name=loffset value='{$offset}'>\n\t<input type=hidden name=language value='{$language}'>\n\t<input type=hidden name=country value='{$country}'>\n\t<input type=hidden name=encoding value='{$encoding}'>\n\t<input type='hidden' name='total_pages' value='{$total_pages}' />\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=500>\n      <tr>\n        <th colspan=2>" . ct("Translation Info") . "</th>\n      </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td>" . ct("Language") . "</td>\n\t    <td>{$lang_out}</td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td>" . ct("Country") . "</td>\n\t    <td>{$country_out}</td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td>" . ct("Character Encoding") . "</td>\n\t    <td>\n\t      {$encoding}\n\t    </td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td>" . ct("Page") . "</td>\n\t    <td>\n\t    \t<input type='text' name='page_txt' size='3' value='{$current_page}'\n\t    \tstyle='text-align: center'> of {$total_pages}\n\t    \t<input type='submit' name='page_btn' value='Goto' />\n\t    </td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t  \t<td colspan='2' align='center'>\n\t  \t\t<input type='checkbox' name='untrans' value='checked' {$untrans}\n\t  \t\tonchange='javascript:document.form.submit();' />\n\t  \t\tDisplay Only Untranslated Sentences\n\t  \t</td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t  \t<td colspan='2'>\n\t  \t\t<table " . TMPL_tblDflts . " width='100%'>\n\t  \t\t\t<tr>\n\t  \t\t\t\t<th colspan='5'>Search and Replace</th>\n\t  \t\t\t</tr>\n\t  \t\t\t<tr class='" . bg_class() . "'>\n\t  \t\t\t\t<td>Search</td>\n\t  \t\t\t\t<td>\n\t  \t\t\t\t\t<input type='text' name='search' value='{$search}'\n\t  \t\t\t\t\tstyle='width: 100%' />\n\t  \t\t\t\t</td>\n\t  \t\t\t\t<td>Replace</td>\n\t  \t\t\t\t<td>\n\t  \t\t\t\t\t<input type='text' name='replace' value='{$replace}'\n\t  \t\t\t\t\tstyle='width: 100%' />\n\t  \t\t\t\t</td>\n\t  \t\t\t\t<td>\n\t  \t\t\t\t\t<input type='submit' name='srchrep' value='Search & Replace'\n\t  \t\t\t\t\tstyle='width: 100%' />\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 colspan='5' align='center'>{$sr_inf}</td>\n\t  \t\t\t</tr>\n\t  \t\t</table>\n\t  \t</td>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td colspan=2>\n\t      Some of the translation sentences includes special words and characters\n\t      such as `" . htmlspecialchars("&laquo") . "', `" . htmlspecialchars("&raquo") . "', `<', `<<', ect... Please include these words\n\t      and characters in your translation sentences as well.<p>\n\t      After clicking <i>" . ct("Next") . "</i> or <i>" . ct("Previous") . "</i> the current state of the translation is automatically saved.\n\t  </tr>\n\t</table>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=500>\n\t  <tr>\n\t    <th>" . ct("Translate") . "</th>\n\t  </tr>\n\t  <tr class='" . bg_class() . "'>\n\t    <td>\n\t      <table border=0 cellpadding=0 cellspacing=0 width=500>\n\t        <tr class='" . bg_class() . "'>\n\t          <td width=50% align=left>{$sz_prev}</td>\n\t          <td width=50% align=right>{$sz_next}</td>\n\t        </tr>\n\t      </table>\n\t    </td>\n\t  </tr>\n\t  {$tl}\n\t  <tr class='" . bg_class() . "'>\n\t    <td>\n\t      <table border=0 cellpadding=0 cellspacing=0 width=500>\n\t        <tr class='" . bg_class() . "'>\n\t          <td width=50% align=left>{$sz_prev}</td>\n\t          <td width=50% align=right>{$sz_next}</td>\n\t        </tr>\n\t      </table>\n\t    </td>\n\t  </tr>\n\t</table>\n\t</form>\n\t</center>" . mkQuickLinks(ql("locale-translate.php", "Translate Cubit"), ql("locale-settings.php", "Locale Settings"));
    return $OUTPUT;
}
function confirmUser($_POST)
{
    # Get vars
    extract($_POST);
    # validate input
    require "libs/validate.lib.php";
    $v = new validate();
    $v->isOk($username, "string", 1, 20, "Invalid user name.");
    $username2 = str_replace(" ", "", $username);
    if (strlen($username) > strlen($username2)) {
        $v->isOk($username, "num", 0, 0, "Error : user name must not contain spaces.");
    }
    $v->isOk($div, "num", 1, 20, "Invalid Branch.");
    $v->isOk($password, "string", 1, 20, "Invalid password.");
    if (isset($f1)) {
        $v->isOk($password2, "string", 1, 20, "Invalid password 2.");
        $v->pwMatch($password, $password2, "Passwords do not match.");
    }
    $v->isOk($tool, "string", 1, 3, "Invalid tooltips selection.");
    $v->isOk($ispos, "string", 1, 3, "Invalid POS user selection.");
    # display errors, if any
    if ($v->isError()) {
        $theseErrors = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $theseErrors .= "-" . $e["msg"] . "<br>";
        }
        $theseErrors = "\n\t\t<tr>\n\t\t\t<td class='err' colspan='2'>{$theseErrors}</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td colspan='2'><br></td>\n\t\t</tr>";
        return enterUser($username, $theseErrors);
        exit;
    }
    if (!isset($dept_sel)) {
        $dept_sel = "0";
    }
    # Get branch name
    db_connect();
    $sql = "SELECT branname FROM branches WHERE div = '{$div}'";
    $branRslt = db_exec($sql);
    $bran = pg_fetch_array($branRslt);
    if (isset($f1)) {
        $ex = "<input type='hidden' name='f2' value=''>";
        # exit if user exists
        $sql = "SELECT username FROM users WHERE username = '******'";
        $usrRslt = db_exec($sql) or errDie("Unable to check cubit for existing username.");
        if (pg_numrows($usrRslt) > 0) {
            return "\n\t\t\t\t<li class='err'>User, {$username}, already exists in cubit.</li>\n\t\t\t\t<br>\n\t\t\t\t" . mkQuickLinks(ql("admin-usradd.php", "Add New User"));
        }
    } else {
        $ex = "";
    }
    require "locale_codes.php";
    $ar_locale = explode("_", $locale);
    // Retrieve the name of the langauge
    foreach ($ar_languages as $lang_name => $lang_code) {
        if ($ar_locale[0] == $lang_code) {
            $language = $lang_name;
        }
    }
    // Retrieve the name of the country
    foreach ($ar_countries as $country_name => $country_code) {
        if ($ar_locale[1] == $country_code) {
            $country = $country_name;
        }
    }
    if ($empnum) {
        $sql = "SELECT sname, fnames, enum FROM cubit.employees WHERE empnum='{$empnum}'";
        $emp_rslt = db_exec($sql) or errDie("Unable to retrieve employee.");
        $emp_data = pg_fetch_array($emp_rslt);
        $employee = "{$emp_data['sname']} {$emp_data['fnames']} - {$emp_data['enum']}";
    } else {
        $employee = "[None]";
    }
    if (isset($payroll_group) and is_array($payroll_group)) {
        $sendpayroll = "";
        foreach ($payroll_group as $each) {
            $sendpayroll .= "<input type='hidden' name='payroll_group[]' value='{$each}'>";
        }
    }
    $confirmUser = "******" . TMPL_tblDflts . ">\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='write'>\n\t\t\t<input type='hidden' name='username' value='{$username}'>\n\t\t\t<input type='hidden' name='div' value='{$div}'>\n\t\t\t<input type='hidden' name='password' value='{$password}'>\n\t\t\t<input type='hidden' name='locale' value='{$locale}'>\n\t\t\t<input type='hidden' name='tool' value='{$tool}'>\n\t\t\t<input type='hidden' name='ispos' value='{$ispos}'>\n\t\t\t<input type='hidden' name='empnum' value='{$empnum}' />\n\t\t\t<input type='hidden' name='old_dept' value='{$dept_sel}' />\n\t\t\t{$sendpayroll}\n\t\t\t{$ex}\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>Username</td>\n\t\t\t\t<td>{$username}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Password</td>\n\t\t\t\t<td>*</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Locale</td>\n\t\t\t\t<td>{$language} ({$country})</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Show Tooltips</td>\n\t\t\t\t<td>{$tool}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>POS User</td>\n\t\t\t\t<td>{$ispos}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Employee</td>\n\t\t\t\t<td>{$employee}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td><br></td>\n\t\t\t</tr>\n\t\t</table>";
    if ($ispos == 'No') {
        // add the department selection
        $confirmUser .= "\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th>User Settings</th>\n\t\t\t\t</tr>";
        // create the administrator setting
        $rslt = db_exec("SELECT admin FROM users WHERE username='******' ");
        if (pg_num_rows($rslt) == 0 || pg_result($rslt, 0, 0) == 0) {
            $Ch = "";
        } else {
            $Ch = "checked";
        }
        $confirmUser .= "\n\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t<td><input {$Ch} type='checkbox' name='admin' value='1'> ADMINISTRATOR</td>\n\t\t\t</tr>";
        $confirmUser .= "\n\t\t\t</table>\n\t\t\t<br>";
        // add the department selection
        $confirmUser .= "\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<td align='right' colspan='3'><input type='submit' name='doneBtn' value='Done &raquo'></td>\n\t\t\t\t</tr>\n\t\t\t\t" . TBL_BR . "\n\t\t\t</table>\n\t\t\t<br>";
        $get_depts = "SELECT * FROM depts ORDER BY dept";
        $run_depts = db_exec($get_depts) or errDie("Unable to get department information.");
        if (pg_numrows($run_depts) < 1) {
            return "<li class='err'>No Department Information Found.</li>";
        } else {
            $department_drop = "<select name='dept_sel' onChange='document.form.submit()'>";
            $department_drop .= "<option value='0'>Select Department</option>";
            while ($darr = pg_fetch_array($run_depts)) {
                if ($dept_sel == $darr['deptid']) {
                    $department_drop .= "<option value='{$darr['deptid']}' selected>{$darr['dept']}</option>";
                } else {
                    $department_drop .= "<option value='{$darr['deptid']}'>{$darr['dept']}</option>";
                }
            }
            $department_drop .= "</select>";
        }
        $confirmUser .= "\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Select Department</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$department_drop}</td>\n\t\t\t\t</tr>\n\t\t\t\t" . TBL_BR . "\n\t\t\t</table>";
        $confirmUser .= "\n\t\t\t<table " . TMPL_tblDflts . " width='65%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='4'>Select user Permissions</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td valign='top' colspan='2'>\n\t\t\t\t\t\t<table width='100%' cellpadding='1' cellspacing='1'>";
        db_connect();
        $sql = "SELECT * FROM depts WHERE deptid = '{$dept_sel}'";
        $rslt = db_exec($sql);
        $i = 0;
        while ($dep = pg_fetch_array($rslt)) {
            $confirmUser .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'><input type='submit' name='deps[{$dep['deptid']}]' value='Add'><input type='submit' name='depsrem[{$dep['deptid']}]' value='Remove'> {$dep['dept']}</td>\n\t\t\t\t</tr>";
            $sql = "SELECT * FROM deptscripts WHERE dept='{$dep['deptid']}' ORDER BY script";
            $srslt = db_exec($sql);
            $i++;
            // Remove checked = yes on the $confirmUser line in this while loop
            while ($scr = pg_fetch_array($srslt)) {
                $Tp['script'] = $scr['scriptname'];
                $Sql = "SELECT script FROM userscripts WHERE username='******' and script='{$scr['script']}' LIMIT 1";
                $Ex = db_exec($Sql);
                if (pg_numrows($Ex) > 0) {
                    $Ch = "checked";
                } else {
                    $Ch = "";
                }
                $Tp['script'] = strtoupper($Tp['script']);
                $confirmUser .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t<table>\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<td>.....</td>\n\t\t\t\t\t\t\t\t\t<td><input type='checkbox' name='perm[]' {$Ch} value='{$scr['script']}'></td>\n\t\t\t\t\t\t\t\t\t<td>{$Tp['script']}</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
            }
            $confirmUser .= "<tr class='" . bg_class() . "'><td colspan=2><br></td></tr>";
            if ($i == "9") {
                $confirmUser .= "\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td valign='top'>\n\t\t\t\t\t\t<table width='100%' cellpadding='1' cellspacing='1'>";
            }
        }
    }
    $confirmUser .= "\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right' colspan='3'><input type='submit' name='doneBtn' value='Done &raquo'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Quick Links</th>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $confirmUser;
}
function write($_POST)
{
    # print "<pre>"; var_dump($_POST);exit;
    # Get vars
    extract($_POST);
    if (isset($back)) {
        unset($_POST["back"]);
        return details($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($accid, "num", 1, 50, "Invalid Account to be Debited.");
    $v->isOk($tran, "string", 1, 3, "Invalid type of transaction.");
    foreach ($amount as $key => $value) {
        if ($value > 0) {
            $v->isOk($caccid[$key], "num", 1, 50, "Invalid Contra account.");
            $v->isOk($refnum[$key], "num", 1, 10, "Invalid Reference number.");
            $v->isOk($amount[$key], "float", 1, 20, "Invalid Amount.");
            $v->isOk($descript[$key], "string", 0, 255, "Invalid Details.");
            $v->isOk($date[$key], "date", 1, 14, "Invalid Date.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $write = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $write .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $write .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $write;
    }
    # get account
    $accRs = get("core", "*", "accounts", "accid", $accid);
    $macc = pg_fetch_array($accRs);
    # them transactions
    foreach ($amount as $key => $value) {
        if ($value > 0) {
            # how to write
            if ($tran == 'dt') {
                # write transaction
                writetrans($accid, $caccid[$key], $date[$key], $refnum[$key], $amount[$key], $descript[$key]);
            } else {
                # write transaction
                writetrans($caccid[$key], $accid, $date[$key], $refnum[$key], $amount[$key], $descript[$key]);
            }
        }
    }
    // Start layout
    $write = "\n\t\t<center>\n\t\t<h3>Journal transactions have been recorded</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>Ref num</th>\n\t\t\t\t<th>Debit</th>\n\t\t\t\t<th>Credit</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t\t<th>Description</th>\n\t\t\t</tr>";
    foreach ($amount as $key => $value) {
        if ($amount <= 0) {
            continue;
        }
        # Accounts details
        $caccRs = get("core", "*", "accounts", "accid", $caccid[$key]);
        $cacc = pg_fetch_array($caccRs);
        if (pg_numrows($caccRs) < 1) {
            return "<li>There are No accounts in Cubit.{$caccid[$key]}</li>";
        }
        # how to view
        if ($tran == 'dt') {
            $accts = "<td>{$macc['topacc']}/{$macc['accnum']} - {$macc['accname']}</td><td>{$cacc['topacc']}/{$cacc['accnum']} - {$cacc['accname']}</td>";
        } else {
            $accts = "<td>{$cacc['topacc']}/{$cacc['accnum']} - {$cacc['accname']}</td><td>{$macc['topacc']}/{$macc['accnum']} - {$macc['accname']}</td>";
        }
        $write .= "\n\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t<td>{$date[$key]}</td>\n\t\t\t\t<td>{$refnum[$key]}</td>\n\t\t\t\t{$accts}\n\t\t\t\t<td>" . CUR . " {$amount[$key]}</td>\n\t\t\t\t<td>{$descript[$key]}</td>\n\t\t\t</tr>";
    }
    $write .= "\n\t\t</table>\n\t\t<br>\n\t\t<table " . TMPL_tblDflts . " width='25%'>\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='../reporting/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='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 $write;
}
function confirm($_POST)
{
    # get vars
    extract($_POST);
    if (isset($back)) {
        header("Location: bank-pay-supp.php?supid={$supid}&paidamt[]={$amt}&descript={$descript}&reference={$reference}");
        //		return method($supid);
    }
    if (!isset($out1)) {
        $out1 = '';
    }
    if (!isset($out2)) {
        $out2 = '';
    }
    if (!isset($out3)) {
        $out3 = '';
    }
    if (!isset($out4)) {
        $out4 = '';
    }
    if (!isset($out5)) {
        $out5 = '';
    }
    $OUT1 = $out1;
    $OUT2 = $out2;
    $OUT3 = $out3;
    $OUT4 = $out4;
    $OUT5 = $out5;
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($all, "num", 1, 1, "Invalid allocation.");
    $v->isOk($bankid, "num", 1, 30, "Invalid Bank Account.");
    $v->isOk($date, "date", 1, 14, "Invalid Date.");
    $v->isOk($descript, "string", 1, 255, "Invalid Description.");
    $v->isOk($reference, "string", 0, 50, "Invalid Reference Name/Number.");
    $v->isOk($cheqnum, "num", 0, 30, "Invalid Cheque number.");
    $v->isOk($amt, "float", 1, 10, "Invalid amount.");
    $v->isOk($out, "float", 1, 10, "Invalid out amount.");
    $v->isOk($out1, "float", 0, 10, "Invalid paid amount(currant).");
    $v->isOk($out2, "float", 0, 10, "Invalid paid amount(30).");
    $v->isOk($out3, "float", 0, 10, "Invalid paid amount(60).");
    $v->isOk($out4, "float", 0, 10, "Invalid paid amount(90).");
    $v->isOk($out5, "float", 0, 10, "Invalid paid amount(120).");
    $v->isOk($supid, "num", 1, 10, "Invalid Supplier number.");
    if (isset($invids)) {
        foreach ($invids as $key => $value) {
            if ($paidamt[$invids[$key]] < 0.01) {
                continue;
            }
            $v->isOk($invids[$key], "num", 1, 50, "Invalid Invoice No. [{$key}]");
            $v->isOk($paidamt[$invids[$key]], "float", 1, 20, "Invalid amount to be paid. [{$key}]");
        }
    }
    # 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;
    }
    $out1 += 0;
    $out2 += 0;
    $out3 += 0;
    $out4 += 0;
    $out5 += 0;
    # check invoice payments
    $tot = 0;
    if (isset($invids)) {
        foreach ($invids as $key => $value) {
            if ($paidamt[$invids[$key]] < 0.01) {
                continue;
            }
            $tot += $paidamt[$invids[$key]];
        }
    }
    if (sprint($tot + $out + $out1 + $out2 + $out3 + $out4 + $out5) != sprint($amt)) {
        return "<li class='err'>{$tot} - {$amt} The total amount is not equal to the amount paid. Please check the details.</li>" . alloc($_POST);
    }
    vsprint($out);
    $confirm = "\n\t\t\t<h3>New Bank Payment</h3>\n\t\t\t<h4>Confirm entry (Please check the details)</h4>\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='write'>\n\t\t\t\t<input type='hidden' name='bankid' value='{$bankid}'>\n\t\t\t\t<input type='hidden' name='date' value='{$date}'>\n\t\t\t\t<input type='hidden' name='supid' value='{$supid}'>\n\t\t\t\t<input type='hidden' name='descript' value='{$descript}'>\n\t\t\t\t<input type='hidden' name='reference' value='{$reference}'>\n\t\t\t\t<input type='hidden' name='cheqnum' value='{$cheqnum}'>\n\t\t\t\t<input type='hidden' name='all' value='{$all}'>\n\t\t\t\t<input type='hidden' name='out' value='{$out}'>\n\t\t\t\t<input type='hidden' name='amt' value='{$amt}'>";
    # Get bank account name
    db_connect();
    $sql = "SELECT accname,bankname FROM bankacct WHERE bankid = '{$bankid}' AND div = '" . USER_DIV . "'";
    $bankRslt = db_exec($sql);
    $bank = pg_fetch_array($bankRslt);
    if (pg_num_rows($bankRslt) < 1) {
        $bank['accname'] = "Cash";
        $bank['bankname'] = "";
    }
    # Supplier name
    $sql = "SELECT supno,supname FROM suppliers WHERE supid = '{$supid}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql);
    $sup = pg_fetch_array($supRslt);
    $confirm .= "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Payment Details</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Account</td>\n\t\t\t\t<td>{$bank['accname']} - {$bank['bankname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Date</td>\n\t\t\t\t<td valign='center'>{$date}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Paid To</td>\n\t\t\t\t<td valign='center'>({$sup['supno']}) {$sup['supname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Description</td>\n\t\t\t\t<td valign='center'>{$descript}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Reference</td>\n\t\t\t\t<td valign='center'>{$reference}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Cheque Number</td>\n\t\t\t\t<td valign='center'>{$cheqnum}</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 . " {$amt}</td>\n\t\t\t</tr>";
    if ($all == 0) {
        // Layout
        $confirm .= "\n\t\t\t\t" . TBL_BR . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t</tr>\n\t\t\t\t<!--<table " . TMPL_tblDflts . " width='90%'>-->\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t<th>Outstanding amount</th>\n\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t</tr>";
        $i = 0;
        // for bgcolor
        if (isset($invids)) {
            foreach ($invids as $key => $value) {
                if ($paidamt[$invids[$key]] < 0.01) {
                    continue;
                }
                db_conn("cubit");
                # Get all the details
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE purid='{$invids[$key]}' AND supid = '{$supid}' AND div = '" . USER_DIV . "'";
                $invRslt = db_exec($sql) or errDie("Unable to access database.");
                if (pg_numrows($invRslt) < 1) {
                    $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE intpurid='{$invids[$key]}' AND div = '" . USER_DIV . "'";
                    $invRslt = db_exec($sql) or errDie("Unable to access database.");
                    if (pg_numrows($invRslt) < 1) {
                        return "<li class='err'> - Invalid ord number {$invids[$key]}.</li>";
                    }
                }
                $inv = pg_fetch_array($invRslt);
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='hidden' name='paidamt[]' size='7' value='{$paidamt[$invid]}'>{$paidamt[$invid]}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
        }
        // 0.01 because of high precisions like 0.0000000001234 still matching
        if ($out >= 0.01) {
            $confirm .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t</td>\n\t\t\t</tr>";
        }
    }
    if ($all == 1) {
        $age30 = sage($supid, 59);
        $age60 = sage($supid, 89);
        $age90 = sage($supid, 119);
        $age120 = sage($supid, 149);
        $bgColor = bgcolorg();
        $i = 0;
        if ($out1 > 0) {
            // Connect to database
            db_conn("cubit");
            $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND balance>0 AND pdate >='" . extlib_ago(29) . "' AND pdate <='" . extlib_ago(-1) . "'  AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
            $prnInvRslt = db_exec($sql);
            while ($inv = pg_fetch_array($prnInvRslt) and $out1 > 0) {
                if ($inv['invid'] == 0) {
                    continue;
                }
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                if ($i == 0) {
                    $confirm .= "\n\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out1 >= $inv['balance']) {
                    $val = $inv['balance'];
                    $out1 = $out1 - $inv['balance'];
                } else {
                    $val = $out1;
                    $out1 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out1 >= 0.01) {
                $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
            }
        }
        if ($out2 > 0) {
            if ($out2 > $age30) {
                $_POST['OUT1'] = $OUT1;
                $_POST['OUT2'] = $OUT2;
                $_POST['OUT3'] = $OUT3;
                $_POST['OUT4'] = $OUT4;
                $_POST['OUT5'] = $OUT5;
                $out2 = sprint($out2);
                return "<li class='err'>You cannot allocate " . CUR . " {$out2} to 30 days, you only owe " . CUR . " {$age30}</li>" . alloc($_POST);
            }
            // Connect to database
            db_conn("cubit");
            $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND balance>0 AND pdate >='" . extlib_ago(59) . "' AND pdate <='" . extlib_ago(29) . "'  AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
            $prnInvRslt = db_exec($sql);
            while ($inv = pg_fetch_array($prnInvRslt) and $out2 > 0) {
                if ($inv['invid'] == 0) {
                    continue;
                }
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                if ($i == 0) {
                    $confirm .= "\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out2 >= $inv['balance']) {
                    $val = $inv['balance'];
                    $out2 = $out2 - $inv['balance'];
                } else {
                    $val = $out2;
                    $out2 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out2 >= 0.01) {
                $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
            }
        }
        if ($out3 > 0) {
            if ($out3 > $age60) {
                $_POST['OUT1'] = $OUT1;
                $_POST['OUT2'] = $OUT2;
                $_POST['OUT3'] = $OUT3;
                $_POST['OUT4'] = $OUT4;
                $_POST['OUT5'] = $OUT5;
                $out3 = sprint($out3);
                return "<li class='err'>You cannot allocate " . CUR . " {$out3} to 60 days, you only owe " . CUR . " {$age60} </lI>" . alloc($_POST);
            }
            // Connect to database
            db_conn("cubit");
            $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND balance>0 AND pdate >='" . extlib_ago(89) . "' AND pdate <='" . extlib_ago(59) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
            $prnInvRslt = db_exec($sql);
            while ($inv = pg_fetch_array($prnInvRslt) and $out3 > 0) {
                if ($inv['invid'] == 0) {
                    continue;
                }
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                if ($i == 0) {
                    $confirm .= "\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out3 >= $inv['balance']) {
                    $val = $inv['balance'];
                    $out3 = $out3 - $inv['balance'];
                } else {
                    $val = $out3;
                    $out3 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out3 >= 0.01) {
                $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
            }
        }
        if ($out4 > 0) {
            if ($out4 > $age90) {
                $_POST['OUT1'] = $OUT1;
                $_POST['OUT2'] = $OUT2;
                $_POST['OUT3'] = $OUT3;
                $_POST['OUT4'] = $OUT4;
                $_POST['OUT5'] = $OUT5;
                $out4 = sprint($out4);
                return "<li class='err'>You cannot allocate " . CUR . " {$out4} to 90 days, you only owe " . CUR . " {$age90}</li>" . alloc($_POST);
            }
            // Connect to database
            db_conn("cubit");
            $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND balance>0 AND pdate >='" . extlib_ago(119) . "' AND pdate <='" . extlib_ago(89) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
            $prnInvRslt = db_exec($sql);
            while ($inv = pg_fetch_array($prnInvRslt) and $out4 > 0) {
                if ($inv['invid'] == 0) {
                    continue;
                }
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                if ($i == 0) {
                    $confirm .= "\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out4 >= $inv['balance']) {
                    $val = $inv['balance'];
                    $out4 = $out4 - $inv['balance'];
                } else {
                    $val = $out4;
                    $out4 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out4 >= 0.01) {
                $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
            }
        }
        if ($out5 > 0) {
            if ($out5 > $age120) {
                $_POST['OUT1'] = $OUT1;
                $_POST['OUT2'] = $OUT2;
                $_POST['OUT3'] = $OUT3;
                $_POST['OUT4'] = $OUT4;
                $_POST['OUT5'] = $OUT5;
                $out5 = sprint($out5);
                return "<li class='err'>You cannot allocate " . CUR . " {$out5} to 120 days, you only owe " . CUR . " {$age120}</li>" . alloc($_POST);
            }
            // Connect to database
            db_conn("cubit");
            $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND balance>0 AND pdate >='" . extlib_ago(149) . "' AND pdate <='" . extlib_ago(119) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
            $prnInvRslt = db_exec($sql);
            while ($inv = pg_fetch_array($prnInvRslt) and $out5 > 0) {
                if ($inv['invid'] == 0) {
                    continue;
                }
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                if ($i == 0) {
                    $confirm .= "\n\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out5 >= $inv['balance']) {
                    $val = $inv['balance'];
                    $out5 = $out5 - $inv['balance'];
                } else {
                    $val = $out5;
                    $out5 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out5 >= 0.01) {
                $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
            }
        }
    }
    if ($all == 2) {
        // Layout
        $confirm .= "\n\t\t\t\t" . TBL_BR . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t</tr>\n\t\t\t\t<!--<table " . TMPL_tblDflts . " width='90%'>-->\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t<th>Outstanding amount</th>\n\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t<th>Amount</th>\n\t\t\t\t</tr>";
        $i = 0;
        // for bgcolor
        if (isset($invids)) {
            foreach ($invids as $key => $value) {
                if ($paidamt[$invids[$key]] < 0.01) {
                    continue;
                }
                db_conn("cubit");
                # Get all the details
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE purid='{$invids[$key]}' AND div = '" . USER_DIV . "'";
                $invRslt = db_exec($sql) or errDie("Unable to access database.");
                if (pg_numrows($invRslt) < 1) {
                    $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE intpurid='{$invids[$key]}' AND div = '" . USER_DIV . "'";
                    $invRslt = db_exec($sql) or errDie("Unable to access database.");
                    if (pg_numrows($invRslt) < 1) {
                        return "<li class='err'> - Invalid ord number {$invids[$key]}.</li>";
                    }
                }
                $inv = pg_fetch_array($invRslt);
                if ($inv['invid2'] > 0) {
                    $inv['invid'] = $inv['invid2'];
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='hidden' name='paidamt[]' size='7' value='{$paidamt[$invid]}'>{$paidamt[$invid]}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
        }
        // 0.01 because of high precisions like 0.0000000001234 still matching
        if ($out >= 0.01) {
            $confirm .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t</td>\n\t\t\t</tr>";
        }
    }
    vsprint($out1);
    vsprint($out2);
    vsprint($out3);
    vsprint($out4);
    vsprint($out5);
    vsprint($OUT1);
    vsprint($OUT2);
    vsprint($OUT3);
    vsprint($OUT4);
    vsprint($OUT5);
    $confirm .= "\n\t\t\t\t<input type='hidden' name='out1' value='{$out1}'>\n\t\t\t\t<input type='hidden' name='out2' value='{$out2}'>\n\t\t\t\t<input type='hidden' name='out3' value='{$out3}'>\n\t\t\t\t<input type='hidden' name='out4' value='{$out4}'>\n\t\t\t\t<input type='hidden' name='out5' value='{$out5}'>\n\t\t\t\t<input type='hidden' name='OUT1' value='{$OUT1}'>\n\t\t\t\t<input type='hidden' name='OUT2' value='{$OUT2}'>\n\t\t\t\t<input type='hidden' name='OUT3' value='{$OUT3}'>\n\t\t\t\t<input type='hidden' name='OUT4' value='{$OUT4}'>\n\t\t\t\t<input type='hidden' name='OUT5' value='{$OUT5}'>\n\t\t\t\t<input type='hidden' name='date_day' value='{$date_day}'>\n\t\t\t\t<input type='hidden' name='date_month' value='{$date_month}'>\n\t\t\t\t<input type='hidden' name='date_year' value='{$date_year}'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td><input type='submit' name='back' value='&laquo; Correction'></td>\n\t\t\t\t\t<td align='right'><input type='submit' value='Write &raquo'></td>\n\t\t\t\t</tr>\n\t\t\t</form>\n\t\t\t</table>\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='bank-pay-supp.php'>Add supplier payment</a></td>\n\t\t\t\t</tr>\n\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t</table>";
    return $confirm;
}
function slip($_POST, $pure = false)
{
    # get vars
    extract($_POST);
    $empnum += 0;
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($from_day)) {
        $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.");
        }
    } else {
        if (isset($mon)) {
            $v->isOk($mon, "num", 1, 2, "Invalid month selected.");
        }
    }
    $v->isOk($empnum, "num", 1, 14, "Invalid employee selected.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    #check what we have permission to
    $get_perm = "SELECT payroll_groups FROM users WHERE username = '******'USER_NAME']}' LIMIT 1";
    $run_perm = db_exec($get_perm) or errDie("Unable to get payroll groups permission information.");
    if (pg_numrows($run_perm) > 0) {
        $parr = pg_fetch_array($run_perm);
        if (strlen($parr['payroll_groups']) > 0) {
            $pay_grps = explode(",", $parr['payroll_groups']);
        } else {
            $pay_grps = array();
        }
    } else {
        $pay_grps = array();
    }
    if (isset($emp_group) and is_array($emp_group)) {
        $emp_groups = array();
        $emps = array();
        foreach ($emp_group as $each) {
            if (!in_array($each, $pay_grps)) {
                continue;
            }
            $emp_groups[] = $each;
            $get_emp = "SELECT empnum FROM employees WHERE emp_group = '{$each}'";
            $run_emp = db_exec($get_emp) or errDie("Unable to get employees information.");
            if (pg_numrows($run_emp) > 0) {
                while ($earr = pg_fetch_array($run_emp)) {
                    $emps[] = $earr['empnum'];
                }
            }
        }
    } else {
        #check for which groups we have perm
        $get_check = "SELECT payroll_groups FROM users WHERE username = '******'USER_NAME']}' LIMIT 1";
        $run_check = db_exec($get_check) or errDie("Unable to get employees group permissions.");
        if (pg_numrows($run_check) > 0) {
            $earr = pg_fetch_array($run_check);
            if (strlen($earr['payroll_groups']) > 0) {
                $eperms = explode(",", $earr['payroll_groups']);
                $egsearch = " AND (emp_group = '" . implode("' OR emp_group = '", $eperms) . "')";
            } else {
                $egsearch = "AND FALSE";
            }
        }
        $emp_groups[] = array(0 => '0');
        $get_emp = "SELECT empnum FROM employees WHERE true {$egsearch}";
        $run_emp = db_exec($get_emp) or errDie("Unable to get employees information.");
        while ($earr = pg_fetch_array($run_emp)) {
            $emps[] = $earr['empnum'];
        }
    }
    if (!isset($emps)) {
        $emps = array(0);
    }
    if (in_array('0', $emp_groups)) {
        $show_all = TRUE;
    } else {
        $show_all = FALSE;
    }
    $totgross = 0;
    $totcomm = 0;
    $totins = 0;
    $totuif = 0;
    $totpaye = 0;
    $totded = 0;
    $totsal = 0;
    if (!isset($salyear) or strlen($salyear) < 1) {
        $salyear = EMP_YEAR;
    }
    /* get employee details */
    db_connect();
    if (isset($from_day)) {
        $retfunc = "slctDate";
        if ($empnum != "0") {
            #if not all then use selected employee
            $empw = "empnum='{$empnum}' AND ";
        } else {
            #else use all payslips ... but only with emps in selected group
            if (!$show_all) {
                $empw = "";
                foreach ($emps as $each) {
                    $empw .= "empnum='{$each}' OR ";
                }
                $empw .= "empnum='{$each}'";
            }
        }
        if (substr($empw, -4) == "AND ") {
            $empw = substr($empw, 0, -4);
        }
        if (!isset($empw)) {
            $empw = "true";
        }
        $sql = "SELECT 'salp' AS paytype, * FROM salpaid\n\t\t\t\tWHERE ({$empw}) AND saldate >= '{$fromdate}' AND saldate <= '{$todate}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tUNION\n\t\t\t\tSELECT 'salr' AS paytype, * FROM salr\n\t\t\t\tWHERE ({$empw}) AND saldate >= '{$fromdate}' AND saldate <= '{$todate}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tORDER BY true_ids ASC";
    } else {
        if (isset($empnum)) {
            $retfunc = "slctEmployee";
            $sql = "SELECT 'salp' AS paytype, * FROM salpaid\n\t\t\t\tWHERE month='{$mon}' AND empnum='{$empnum}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tUNION\n\t\t\t\tSELECT 'salr' AS paytype, * FROM salr\n\t\t\t\tWHERE month='{$mon}' AND empnum='{$empnum}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tORDER BY true_ids ASC";
        } else {
            invalid_use();
        }
    }
    $pRslt = db_exec($sql) or errDie("Unable to select employee payments from database.");
    if (pg_numrows($pRslt) < 1) {
        return "<li class='err'> - Employee salaries matching the search criteria not found.</li>" . $retfunc();
    }
    $slip = "";
    if (pg_numrows($pRslt) > 0) {
        $empdata = array();
        $empcounter = array();
        while ($pay = pg_fetch_array($pRslt)) {
            $en = $pay["empnum"];
            $mwid = "{$pay['month']}:{$pay['week']}";
            if (!isset($empdata[$en])) {
                $empdata[$en] = array();
            }
            if (!isset($empdata[$en][$mwid])) {
                $empdata[$en][$mwid] = array("gross" => 0, "comm" => 0, "loanins" => 0, "uif" => 0, "paye" => 0, "salary" => 0, "saldate" => "", "payslip" => 0);
            }
            $ed =& $empdata[$en][$mwid];
            $gross = $pay['salary'] - $pay['totallow'] - $pay['comm'] + $pay['totded'] + $pay['uif'] + $pay['paye'] + $pay['loanins'];
            $ed["saldate"] = $pay["saldate"];
            if ($pay["paytype"] == "salp") {
                $ed["gross"] += $gross;
                $ed["comm"] += $pay["comm"];
                $ed["loanins"] += $pay["loanins"];
                $ed["uif"] += $pay["uif"];
                $ed["paye"] += $pay["paye"];
                // 				$ed["totded"] += $pay["totded"];
                $ed["salary"] += $pay["salary"];
                $ed["payslip"] = $pay["id"];
                $totgross += $gross;
                $totcomm += $pay['comm'];
                $totins += $pay['loanins'];
                $totuif += $pay['uif'];
                $totpaye += $pay['paye'];
                $totded += $pay['totded'];
                $totsal += $pay['salary'];
            } else {
                $ed["gross"] -= $gross;
                $ed["comm"] -= $pay["comm"];
                $ed["loanins"] -= $pay["loanins"];
                $ed["uif"] -= $pay["uif"];
                $ed["paye"] -= $pay["paye"];
                // 				$ed["totded"] -= $pay["totded"];
                $ed["salary"] -= $pay["salary"];
                $ed["payslip"] = "{$pay['id']}&rev=true";
                $totgross -= $gross;
                $totcomm -= $pay['comm'];
                $totins -= $pay['loanins'];
                $totuif -= $pay['uif'];
                $totpaye -= $pay['paye'];
                $totded -= $pay['totded'];
                $totsal -= $pay['salary'];
            }
            $get_deds = "SELECT distinct(type) FROM emp_ded WHERE payslip = '{$pay['id']}'";
            $run_deds = db_exec($get_deds) or errDie("Unable to get salary deduction information.");
            if (pg_numrows($run_deds) > 0) {
                // 				$deductions = "";
                // 				$ed["totded"] = array()
                $colspan = 0;
                while ($darr = pg_fetch_array($run_deds)) {
                    $darr['type'] += 0;
                    if ($darr['type'] > 0) {
                        print "adding a deduction<br>";
                        $get_amt = "SELECT amount, description FROM emp_ded WHERE payslip = '{$pay['id']}' AND type = '{$darr['type']}' LIMIT 1";
                        $run_amt = db_exec($get_amt) or errDie("Unable to get employee deduction amount.");
                        $deduction_heading .= "<th>" . pg_fetch_result($run_amt, 0, 1) . "</th>";
                        $ed["totded"][] = "<td nowrap>" . CUR . " " . sprint(pg_fetch_result($run_amt, 0, 0)) . "</td>";
                        // 						$deductions .= "<td nowrap>".CUR." ".sprint($darr['amount'])."</td>";
                        $colspan++;
                    }
                }
            } else {
                // 				$deductions = "";
                $colspan = 1;
            }
        }
        print "----------<br>";
        print "<pre>";
        var_dump($ed);
        print "</pre>";
        print "<br>>>>>>>>>>>>>>>>>>>>><br>";
        // print "<pre>";
        // var_dump ($
        foreach ($empdata as $empnum => $months) {
            foreach ($months as $monthweek => $sal) {
                list($month, $week) = explode(":", $monthweek);
                if (($emp = qryEmployee($empnum, "fnames, sname, basic_sal, payprd")) === false) {
                    $emp = qryLEmployee($empnum, "fnames, sname, basic_sal, payprd");
                }
                // not a date range but a single employee, store the name
                if (!isset($from_day)) {
                    $empname = "{$emp['fnames']} {$emp['sname']}";
                }
                /* create month week description */
                $mw_desc = getMonthName($month);
                // weekly
                if ($emp["payprd"] == "w") {
                    $mw_desc .= ", Week {$week}";
                    // fortnightly
                } else {
                    if ($emp["payprd"] == "f") {
                        if ($week == 1) {
                            $week = "1-2";
                        } else {
                            if ($week == 2) {
                                $week = "3-4";
                            } else {
                                $week = "5";
                            }
                        }
                        $mw_desc .= ", Week {$week}";
                    }
                }
                print "<pre>";
                var_dump($sal["totded"]);
                print "</pre>";
                $bgColor = bgcolorg();
                $slip .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$emp['fnames']} {$emp['sname']}</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["gross"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["comm"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["loanins"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["uif"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["paye"]) . "</td>\n\t\t\t\t\t\t" . implode("", $sal["totded"]) . "\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["salary"]) . "</td>\n\t\t\t\t\t\t<td nowrap>{$mw_desc}</td>\n\t\t\t\t\t\t<td nowrap>{$sal['saldate']}</td>";
                if (!$pure) {
                    $slip .= "\n\t\t\t\t\t\t<td><a href='payslip-view.php?empnum={$empnum}&id={$sal['payslip']}'>View</a></td>\n\t\t\t\t\t\t<td><a target='_blank' href='payslip-print.php?id={$sal['payslip']}'>Print</a></td>";
                }
                $slip .= "</tr>";
            }
        }
        # Format the totals
        $totgross = sprint($totgross);
        $totcomm = sprint($totcomm);
        $totins = sprint($totins);
        $totuif = sprint($totuif);
        $totpaye = sprint($totpaye);
        $totded = sprint($totded);
        $totsal = sprint($totsal);
        $slip .= "\n\t\t\t<tr class='bg-even'>\n\t\t\t\t<td><b>Total</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totgross}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totcomm}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totins}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totuif}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totpaye}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totded}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totsal}</b></td>\n\t\t\t\t<td colspan='4'></td>\n\t\t\t</tr>";
    } else {
        return "<li> - There are no salary payments for the selected month</li>";
    }
    if (isset($from_day)) {
        $title = "<h3>Salaries Paid {$fromdate} TO {$todate}</h3>";
    } else {
        $title = "<h3>Salaries for {$empname}</h3>";
    }
    $slip = "\n\t\t<center>\n\t\t{$title}\n\t\t<table " . TMPL_tblDflts . " width='70%'>\n\t\t\t<tr>\n\t\t\t\t<th>Employee</th>\n\t\t\t\t<th>Gross Salary</th>\n\t\t\t\t<th>Commission</th>\n\t\t\t\t<th>Low or interest free loan</th>\n\t\t\t\t<th>UIF</th>\n\t\t\t\t<th>PAYE</th>\n\t\t\t\t<th colspan='{$colspan}'>Deductions</th>\n\t\t\t\t<th>Nett Income</th>\n\t\t\t\t<th>Month/Week</th>\n\t\t\t\t<th>Payment Date</th>\n\t\t\t\t" . (!$pure ? "<th colspan='2'>Options</th>" : "") . "\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th colspan='6'></th>\n\t\t\t\t{$deduction_heading}\n\t\t\t\t<th colspan='5'></th>\n\t\t\t</tr>\n\t\t\t{$slip}\n\t\t\t" . TBL_BR;
    if (!$pure) {
        $slip .= "\n\t\t\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t\t" . array2form($_REQUEST) . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><input name=key type=submit value='Export to Spreadsheet'></td>\n\t\t\t\t</tr>\n\t\t\t</form>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee")) . "\n\t\t\t</td></tR>";
    }
    $slip .= "\n\t\t</table>\n\t\t</center>";
    return $slip;
}
function write($_POST)
{
    # Get vars
    extract($_POST);
    if (isset($back)) {
        unset($_POST["back"]);
        return slctacc($_POST);
    }
    if (isset($cback)) {
        return custconfirm($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    foreach ($amount as $key => $value) {
        if ($value > 0) {
            continue;
        }
        $v->isOk($ctaccid[$key], "num", 1, 50, "Invalid Account to be Credited.[{$key}]");
        $v->isOk($dtaccid[$key], "num", 1, 50, "Invalid Account to be Debited.[{$key}]");
        $v->isOk($refnum[$key], "num", 1, 10, "Invalid Reference number.[{$key}]");
        $v->isOk($amount[$key], "float", 1, 20, "Invalid Amount.[{$key}]");
        $v->isOk($descript[$key], "string", 0, 255, "Invalid Details.[{$key}]");
        $datea = explode("-", $date[$key]);
        if (count($datea) == 3) {
            if (!checkdate($datea[1], $datea[0], $datea[2])) {
                $v->isOk("dadasdas", "num", 1, 1, "Invalid date.");
            }
        } else {
            $v->isOk("asdasd", "num", 1, 1, "Invalid date.");
        }
        $date[$key] = $datea[2] . "-" . $datea[1] . "-" . $datea[0];
    }
    # display errors, if any
    if ($v->isError()) {
        $write = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $write .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $write .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $write;
    }
    foreach ($amount as $key => $value) {
        if ($value <= 0) {
            continue;
        }
        // Accounts details
        $dtaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $dtaccid[$key]);
        $dtacc[$key] = pg_fetch_array($dtaccRs);
        $ctaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $ctaccid[$key]);
        $ctacc[$key] = pg_fetch_array($ctaccRs);
        $td = $date[$key];
        if (isDebtors($dtaccid[$key])) {
            # Select customer
            db_connect();
            $sql = "SELECT * FROM customers WHERE cusnum = '{$dcusnum[$key]}' AND div = '" . USER_DIV . "'";
            $custRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
            if (pg_numrows($custRslt) < 1) {
                return "<li> Invalid Customer ID.</li>";
            } else {
                $cust = pg_fetch_array($custRslt);
            }
            # Get department
            db_conn("exten");
            $sql = "SELECT * FROM departments WHERE deptid = '{$cust['deptid']}' AND div = '" . USER_DIV . "'";
            $deptRslt = db_exec($sql);
            if (pg_numrows($deptRslt) < 1) {
                return "<i class='err'>Department Not Found</i>";
            } else {
                $dept = pg_fetch_array($deptRslt);
            }
            db_connect();
            # Begin updates
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            $sdate = date("Y-m-d");
            # record the payment on the statement
            $sql = "\n\t\t\t\t\tINSERT INTO stmnt (\n\t\t\t\t\t\tcusnum, invid, amount, date, type, st, div, allocation_date\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$cust['cusnum']}', '0', '{$amount[$key]}', '{$td}', '{$descript[$key]}', 'n', '" . USER_DIV . "', '{$td}'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            $sql = "\n\t\t\t\t\tINSERT INTO open_stmnt (\n\t\t\t\t\t\tcusnum, invid, amount, balance, date, \n\t\t\t\t\t\ttype, st, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$cust['cusnum']}', '0', '{$amount[$key]}', '{$amount[$key]}', '{$td}', \n\t\t\t\t\t\t'{$descript[$key]}', 'n', '" . USER_DIV . "'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            # update the customer (make balance more)
            $sql = "UPDATE customers SET balance = (balance + '{$amount[$key]}') WHERE cusnum = '{$cust['cusnum']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to update customer in Cubit.", SELF);
            # Commit updates
            pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
            # Make ledge record
            custledger($cust['cusnum'], $ctaccid[$key], $td, $refnum[$key], $descript[$key], $amount[$key], "d");
            custDT($amount[$key], $cust['cusnum'], $td);
            $dtaccid[$key] = $dept['debtacc'];
            $descript[$key] = $descript[$key] . " - Customer {$cust['surname']}";
        } elseif (isCreditors($dtaccid[$key])) {
            # Select supplier
            db_connect();
            $sql = "SELECT * FROM suppliers WHERE supid = '{$dsupid[$key]}' AND div = '" . USER_DIV . "'";
            $suppRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
            if (pg_numrows($suppRslt) < 1) {
                return "<li> Invalid Supplier ID.</li>";
            } else {
                $supp = pg_fetch_array($suppRslt);
            }
            # Get department
            db_conn("exten");
            $sql = "SELECT * FROM departments WHERE deptid = '{$supp['deptid']}' AND div = '" . USER_DIV . "'";
            $deptRslt = db_exec($sql);
            if (pg_numrows($deptRslt) < 1) {
                return "<i class='err'>Department Not Found</i>";
            } else {
                $dept = pg_fetch_array($deptRslt);
            }
            db_connect();
            # Begin updates
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            $edate = date("Y-m-d");
            # record the payment on the statement
            $sql = "\n\t\t\t\t\tINSERT INTO sup_stmnt (\n\t\t\t\t\t\tsupid, edate, ref, cacc, descript, amount, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$supp['supid']}', '{$td}', '0', '{$ctaccid[$key]}', '{$descript[$key]}', '-{$amount[$key]}', '" . USER_DIV . "'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            # update the supplier (make balance more)
            $sql = "UPDATE suppliers SET balance = (balance - '{$amount[$key]}') WHERE supid = '{$supp['supid']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to update supplier in Cubit.", SELF);
            # Commit updates
            pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
            # Ledger Records
            suppledger($supp['supid'], $ctaccid[$key], $td, $refnum[$key], $descript[$key], $amount[$key], 'd');
            suppDT($amount[$key], $supp['supid'], $td);
            $dtaccid[$key] = $dept['credacc'];
            $descript[$key] = $descript[$key] . " - Supplier {$supp['supname']}";
        } elseif (isStock($dtaccid[$key])) {
            # Select Stock
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$dstkids[$key]}' 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);
            }
            # Get warehouse name
            db_conn("exten");
            $sql = "SELECT * FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # Update Stock
            db_connect();
            $sql = "UPDATE stock SET csamt = (csamt + '{$amount[$key]}') WHERE stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to insert stock to Cubit.", SELF);
            $sdate = date("Y-m-d");
            # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details
            stockrec($stk['stkid'], $stk['stkcod'], $stk['stkdes'], 'dt', $td, 0, $amount[$key], "Stock Debit Transaction");
            db_connect();
            $dtaccid[$key] = $wh['stkacc'];
        }
        if (isDebtors($ctaccid[$key])) {
            # Select customer
            db_connect();
            $sql = "SELECT * FROM customers WHERE cusnum = '{$ccusnum[$key]}' AND div = '" . USER_DIV . "'";
            $custRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
            if (pg_numrows($custRslt) < 1) {
                return "<li> Invalid Customer ID.</li>";
            } else {
                $cust = pg_fetch_array($custRslt);
            }
            # Get department
            db_conn("exten");
            $sql = "SELECT * FROM departments WHERE deptid = '{$cust['deptid']}' AND div = '" . USER_DIV . "'";
            $deptRslt = db_exec($sql);
            if (pg_numrows($deptRslt) < 1) {
                return "<i class='err'>Department Not Found</i>";
            } else {
                $dept = pg_fetch_array($deptRslt);
            }
            db_connect();
            # Begin updates
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            $sdate = date("Y-m-d");
            # record the payment on the statement
            $sql = "\n\t\t\t\t\tINSERT INTO stmnt (\n\t\t\t\t\t\tcusnum, invid, amount, date, type, st, div, allocation_date\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$cust['cusnum']}', '0', '-{$amount[$key]}', '{$td}', '{$descript[$key]}', 'n', '" . USER_DIV . "', '{$td}'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            $sql = "\n\t\t\t\t\tINSERT INTO open_stmnt (\n\t\t\t\t\t\tcusnum, invid, amount, balance, date, \n\t\t\t\t\t\ttype, st, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$cust['cusnum']}', '0', '-{$amount[$key]}', '-{$amount[$key]}', '{$td}', \n\t\t\t\t\t\t'{$descript[$key]}', 'n', '" . USER_DIV . "'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            # update the customer (make balance more)
            $sql = "UPDATE customers SET balance = (balance - '{$amount[$key]}') WHERE cusnum = '{$cust['cusnum']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to update customer in Cubit.", SELF);
            # Commit updates
            pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
            # Make ledge record
            custledger($cust['cusnum'], $dtaccid[$key], $sdate, $refnum[$key], $descript[$key], $amount[$key], "c");
            custCT($amount[$key], $cust['cusnum'], $td);
            $ctaccid[$key] = $dept['debtacc'];
            $descript[$key] = $descript[$key] . " - Customer {$cust['surname']}";
        } elseif (isCreditors($ctaccid[$key])) {
            # Select supplier
            db_connect();
            $sql = "SELECT * FROM suppliers WHERE supid = '{$csupid[$key]}' AND div = '" . USER_DIV . "'";
            $suppRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
            if (pg_numrows($suppRslt) < 1) {
                return "<li> Invalid Supplier ID.</li>";
            } else {
                $supp = pg_fetch_array($suppRslt);
            }
            # Get department
            db_conn("exten");
            $sql = "SELECT * FROM departments WHERE deptid = '{$supp['deptid']}' AND div = '" . USER_DIV . "'";
            $deptRslt = db_exec($sql);
            if (pg_numrows($deptRslt) < 1) {
                return "<i class='err'>Department Not Found</i>";
            } else {
                $dept = pg_fetch_array($deptRslt);
            }
            db_connect();
            # Begin updates
            pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
            $edate = date("Y-m-d");
            # record the payment on the statement
            $sql = "\n\t\t\t\t\tINSERT INTO sup_stmnt (\n\t\t\t\t\t\tsupid, edate, ref, cacc, descript, amount, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$supp['supid']}', '{$td}', '0', '{$dtaccid[$key]}', '{$descript[$key]}', '{$amount[$key]}', '" . USER_DIV . "'\n\t\t\t\t\t)";
            $stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.", SELF);
            # update the supplier (make balance more)
            $sql = "UPDATE suppliers SET balance = (balance + '{$amount[$key]}') WHERE supid = '{$supp['supid']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to update supplier in Cubit.", SELF);
            # Commit updates
            pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
            # Ledger Records
            suppledger($supp['supid'], $dtaccid[$key], $edate, $refnum[$key], $descript[$key], $amount[$key], 'c');
            suppCT($amount[$key], $supp['supid'], $td);
            $ctaccid[$key] = $dept['credacc'];
            $descript[$key] = $descript[$key] . " - Supplier {$supp['supname']}";
        } elseif (isStock($ctaccid[$key])) {
            # Select Stock
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$cstkids[$key]}' 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);
            }
            # Get warehouse name
            db_conn("exten");
            $sql = "SELECT * FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # Update Stock
            db_connect();
            $sql = "UPDATE stock SET csamt = (csamt + '{$amount[$key]}') WHERE stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
            $rslt = db_exec($sql) or errDie("Unable to insert stock to Cubit.", SELF);
            $sdate = date("Y-m-d");
            # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details
            stockrec($stk['stkid'], $stk['stkcod'], $stk['stkdes'], 'ct', $td, 0, $amount[$key], "Stock Credit Transaction");
            db_connect();
            $ctaccid[$key] = $wh['stkacc'];
        }
        # write transaction
        writetrans($dtaccid[$key], $ctaccid[$key], $date[$key], $refnum[$key], $amount[$key], $descript[$key]);
    }
    // Layout
    $write = "\n\t\t<center>\n\t\t<h3>Journal transactions have been recorded</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>Ref num</th>\n\t\t\t\t<th>Debit</th>\n\t\t\t\t<th>Credit</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t\t<th>Description</th>\n\t\t\t</tr>";
    foreach ($amount as $key => $value) {
        if ($value <= 0) {
            continue;
        }
        if (isDebtors($dtaccid[$key])) {
            $dcusRs = get("cubit", "*", "customers", "cusnum", $dcusnum[$key]);
            $dcus = pg_fetch_array($dcusRs);
            $dt = "<td valign='center'>{$dcus['accno']} - {$dcus['cusname']} {$dcus['surname']}</td>";
        } elseif (isCreditors($dtaccid[$key])) {
            $dsupRs = get("cubit", "*", "suppliers", "supid", $dsupid[$key]);
            $dsup = pg_fetch_array($dsupRs);
            $dt = "<td valign='center'>{$dsup['supno']} - {$dsup['supname']}</td>";
        } elseif (isStock($dtaccid[$key])) {
            $dstkRs = get("cubit", "*", "stock", "stkid", $dstkids[$key]);
            $dstk = pg_fetch_array($dstkRs);
            $dt = "<td valign='center'>{$dstk['stkcod']} - {$dstk['stkdes']}</td>";
        } else {
            $dt = "<td valign='center'>" . $dtacc[$key]['accname'] . "</td>";
        }
        if (isDebtors($ctaccid[$key])) {
            $ccusRs = get("cubit", "*", "customers", "cusnum", $ccusnum[$key]);
            $ccus = pg_fetch_array($ccusRs);
            $ct = "<td valign='center'>{$ccus['accno']} - {$ccus['cusname']} {$ccus['surname']}</td>";
        } elseif (isCreditors($ctaccid[$key])) {
            $csupRs = get("cubit", "*", "suppliers", "supid", $csupid[$key]);
            $csup = pg_fetch_array($csupRs);
            $ct = "<td valign='center'>{$csup['supno']} - {$csup['supname']}</td>";
        } elseif (isStock($ctaccid[$key])) {
            $cstkRs = get("cubit", "*", "stock", "stkid", $cstkids[$key]);
            $cstk = pg_fetch_array($cstkRs);
            $ct = "<td valign='center'>{$cstk['stkcod']} - {$cstk['stkdes']}</td>";
        } else {
            $ct = "<td valign='center'>" . $ctacc[$key]['accname'] . "</td>";
        }
        $write .= "\n\t\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t\t<td>{$date[$key]}</td>\n\t\t\t\t\t<td>{$refnum[$key]}</td>\n\t\t\t\t\t{$dt}\n\t\t\t\t\t{$ct}\n\t\t\t\t\t<td>" . CUR . " {$amount[$key]}</td>\n\t\t\t\t\t<td>{$descript[$key]}</td>\n\t\t\t\t</tr>";
    }
    $write .= "\n\t\t</table>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . " width='25%'>\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='../reporting/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='multi-trans.php'>Journal Transactions</td>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $write;
}
function write($_POST)
{
    # Get vars
    extract($_POST);
    if (isset($back)) {
        return slctacc($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $date = $date_year . "-" . $date_month . "-" . $date_day;
    if (!checkdate($date_month, $date_day, $date_year)) {
        $v->isOk($date, "num", 1, 1, "Invalid date.");
    }
    foreach ($accid as $key => $value) {
        $dtamount[$key] += 0;
        $ctamount[$key] += 0;
        if ($dtamount[$key] > 0 || $ctamount[$key] > 0) {
            $v->isOk($accid[$key], "num", 1, 50, "Invalid Account to be Debited.[{$key}]");
            $v->isOk($dtamount[$key], "float", 1, 20, "Invalid Debit Amount.[{$key}]");
            $v->isOk($ctamount[$key], "float", 1, 20, "Invalid Credit Amount.[{$key}]");
            $v->isOk($descript[$key], "string", 0, 255, "Invalid Details.[{$key}]");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $write = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $write .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $write .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $write;
    }
    foreach ($accid as $key => $value) {
        # begin sql transaction
        pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
        if ($dtamount[$key] > 0) {
            writetrans($accid[$key], $account, $date, $refnum, $dtamount[$key], $descript[$key]);
        } elseif ($ctamount[$key] > 0) {
            writetrans($account, $accid[$key], $date, $refnum, $ctamount[$key], $descript[$key]);
        }
        pglib_transaction("COMMIT") or errDie("Unable to finish a database transaction.", SELF);
    }
    // Start layout
    $write = "\n\t\t<center>\n\t\t<h3>Journal transactions have been recorded.</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Account</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</tr>";
    foreach ($accid as $key => $value) {
        if ($dtamount[$key] > 0 || $ctamount[$key] > 0) {
            core_connect();
            $accRss = get("core", "*", "accounts", "accid", $accid[$key]);
            if (pg_numrows($accRss) < 1) {
                return "<li> Accounts to be debited does not exist.</li>";
            }
            $acc_each[$key] = pg_fetch_array($accRss);
            $write .= "\n\t\t\t\t<tr bgcolor=" . bgcolorg() . ">\n\t\t\t\t\t<td valign='center'>" . $acc_each[$key]['accname'] . "</td>\n\t\t\t\t\t<td>{$descript[$key]}</td>\n\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($dtamount[$key]) . "</td>\n\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($ctamount[$key]) . "</td>\n\t\t\t\t</tr>";
        }
    }
    $write .= "\n\t\t</table>\n\t\t<br>\n\t\t<table " . TMPL_tblDflts . " width='25%'>\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='../reporting/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='trans-new.php'>Journal Transaction</td>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td align='center'><a href='batch-view.php'>View batch file</td>\n\t\t\t</tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $write;
}
function confirm($_POST)
{
    # get vars
    extract($_POST);
    if (isset($back)) {
        return method($supid);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($all, "num", 1, 1, "Invalid allocation.");
    $v->isOk($rec_amount, "num", 1, 10, "Invalid supplier payment amount.");
    for ($t = 0; $t < $rec_amount; $t++) {
        if (!isset($out1[$t])) {
            $out1[$t] = '';
        }
        if (!isset($out2[$t])) {
            $out2[$t] = '';
        }
        if (!isset($out3[$t])) {
            $out3[$t] = '';
        }
        if (!isset($out4[$t])) {
            $out4[$t] = '';
        }
        if (!isset($out5[$t])) {
            $out5[$t] = '';
        }
        // 	$OUT1=$out1;
        // 	$OUT2=$out2;
        // 	$OUT3=$out3;
        // 	$OUT4=$out4;
        // 	$OUT5=$out5;
        $v->isOk($bankid[$t], "num", 1, 30, "Invalid Bank Account.");
        $v->isOk($date[$t], "date", 1, 14, "Invalid Date.");
        $v->isOk($descript[$t], "string", 0, 255, "Invalid Description.");
        $v->isOk($reference[$t], "string", 0, 50, "Invalid Reference Name/Number.");
        $v->isOk($cheqnum[$t], "num", 0, 30, "Invalid Cheque number.");
        $v->isOk($amt[$t], "float", 1, 10, "Invalid amount.");
        $v->isOk($out[$t], "float", 1, 10, "Invalid out amount.");
        $v->isOk($out1[$t], "float", 0, 10, "Invalid paid amount(currant).");
        $v->isOk($out2[$t], "float", 0, 10, "Invalid paid amount(30).");
        $v->isOk($out3[$t], "float", 0, 10, "Invalid paid amount(60).");
        $v->isOk($out4[$t], "float", 0, 10, "Invalid paid amount(90).");
        $v->isOk($out5[$t], "float", 0, 10, "Invalid paid amount(120).");
        $v->isOk($supid[$t], "num", 1, 10, "Invalid Supplier number.");
        if (isset($invids[$t])) {
            foreach ($invids[$t] as $key => $value) {
                if ($paidamt[$t][$invids[$t][$key]] < 0.01) {
                    continue;
                }
                $v->isOk($invids[$t][$key], "num", 1, 50, "Invalid Invoice No. [{$key}]");
                $v->isOk($paidamt[$t][$invids[$t][$key]], "float", 1, 20, "Invalid amount to be paid. [{$key}]");
            }
        }
        $out1[$t] += 0;
        $out2[$t] += 0;
        $out3[$t] += 0;
        $out4[$t] += 0;
        $out5[$t] += 0;
    }
    # 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;
    }
    for ($t = 0; $t < $rec_amount; $t++) {
        # check invoice payments
        $tot[$t] = 0;
        if (isset($invids[$t])) {
            foreach ($invids[$t] as $key => $value) {
                if ($paidamt[$t][$invids[$t][$key]] < 0.01) {
                    continue;
                }
                $tot[$t] += $paidamt[$t][$invids[$t][$key]];
            }
        }
        if (sprint($tot[$t] + $out[$t] + $out1[$t] + $out2[$t] + $out3[$t] + $out4[$t] + $out5[$t] - $amt[$t]) != 0) {
            //				return "<li class='err'>$tot[$t] - $amt[$t] The total amount is not equal to the amount paid. Please check the details.</li>".alloc($_POST);
        }
        vsprint($out[$t]);
        $passon .= "\n\t\t\t\t<input type='hidden' name='bankid[{$t}]' value='{$bankid[$t]}'>\n\t\t\t\t<input type='hidden' name='date[{$t}]' value='{$date[$t]}'>\n\t\t\t\t<input type='hidden' name='supid[{$t}]' value='{$supid[$t]}'>\n\t\t\t\t<input type='hidden' name='descript[{$t}]' value='{$descript[$t]}'>\n\t\t\t\t<input type='hidden' name='reference[{$t}]' value='{$reference[$t]}'>\n\t\t\t\t<input type='hidden' name='cheqnum[{$t}]' value='{$cheqnum[$t]}'>\n\t\t\t\t<input type='hidden' name='out[{$t}]' value='{$out[$t]}'>\n\t\t\t\t<input type='hidden' name='amt[{$t}]' value='{$amt[$t]}'>";
        $passon2 = "";
    }
    $confirm = "\n\t\t\t<h3>New Bank Payment</h3>\n\t\t\t<h4>Confirm entry (Please check the details)</h4>\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='write'>\n\t\t\t\t<input type='hidden' name='all' value='{$all}'>\n\t\t\t\t<input type='hidden' name='rec_amount' value='{$rec_amount}'>\n\t\t\t\t{$passon}";
    for ($t = 0; $t < $rec_amount; $t++) {
        # Get bank account name
        db_connect();
        $sql = "SELECT accname,bankname FROM bankacct WHERE bankid = '{$bankid[$t]}' AND div = '" . USER_DIV . "'";
        $bankRslt = db_exec($sql);
        $bank = pg_fetch_array($bankRslt);
        if (pg_num_rows($bankRslt) < 1) {
            $bank[$t]['accname'] = "Cash";
            $bank[$t]['bankname'] = "";
        }
        # Supplier name
        $sql = "SELECT supno,supname FROM suppliers WHERE supid = '{$supid[$t]}' AND div = '" . USER_DIV . "'";
        $supRslt = db_exec($sql);
        $sup = pg_fetch_array($supRslt);
        $bank1 = $bank[$t]['accname'];
        $bank2 = $bank[$t]['bankname'];
        $confirm .= "\n\t\t\t" . TBL_BR . "\n\t\t\t" . TBL_BR . "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'><h3>Supplier</h3></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th>Supplier</th>\n\t\t\t\t<th>Account</th>\n\t\t\t\t<th>Date</th>\n\t\t\t\t<th>Description</th>\n\t\t\t\t<th>Reference</th>\n\t\t\t\t<th>Cheque Number</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='center'>({$sup['supno']}) {$sup['supname']}</td>\n\t\t\t\t<td>{$bank1} - {$bank2}</td>\n\t\t\t\t<td valign='center'>{$date[$t]}</td>\n\t\t\t\t<td valign='center'>{$descript[$t]}</td>\n\t\t\t\t<td valign='center'>{$reference[$t]}</td>\n\t\t\t\t<td valign='center'>{$cheqnum[$t]}</td>\n\t\t\t\t<td valign='center'>" . CUR . " {$amt[$t]}</td>\n\t\t\t</tr>";
        if ($all == 0) {
            // Layout
            $confirm .= "\n\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t</tr>\n\t\t\t\t<!--<table " . TMPL_tblDflts . " width='90%'>-->\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t<th>Outstanding amount</th>\n\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t</tr>";
            $i = 0;
            // for bgcolor
            if (isset($invids[$t])) {
                foreach ($invids[$t] as $key => $value) {
                    if ($paidamt[$t][$invids[$t][$key]] < 0.01) {
                        continue;
                    }
                    db_conn("cubit");
                    # Get all the details
                    $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE purid='{$invids[$t]}[{$key}]' AND div = '" . USER_DIV . "'";
                    $invRslt = db_exec($sql) or errDie("Unable to access database.");
                    if (pg_numrows($invRslt) < 1) {
                        $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE intpurid='{$invids[$t]}[{$key}]' AND div = '" . USER_DIV . "'";
                        $invRslt = db_exec($sql) or errDie("Unable to access database.");
                        if (pg_numrows($invRslt) < 1) {
                            return "<li class=err> - Invalid ord number {$invids[$t]}[{$key}].";
                        }
                    }
                    $inv = pg_fetch_array($invRslt);
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='hidden' name='paidamt[{$t}][]' size='7' value='{$paidamt[$t]}[{$invid}]'>{$paidamt[$t]}[{$invid}]</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out[$t] >= 0.01) {
                $confirm .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\twith " . CUR . " " . sprint($out[$t]) . " </b>\n\t\t\t\t</td>\n\t\t\t</tr>";
            }
        }
        if ($all == 1) {
            $age30[$t] = sage($supid[$t], 59);
            $age60[$t] = sage($supid[$t], 89);
            $age90[$t] = sage($supid[$t], 119);
            $age120[$t] = sage($supid[$t], 149);
            $bgColor = bgcolorg();
            $i = 0;
            if ($out1[$t] > 0) {
                // Connect to database
                db_conn("cubit");
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid[$t]}' AND balance>0 AND pdate >='" . extlib_ago(29) . "' AND pdate <='" . extlib_ago(-1) . "'  AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
                $prnInvRslt = db_exec($sql);
                while ($inv = pg_fetch_array($prnInvRslt) and $out1[$t] > 0) {
                    if ($inv['invid'] == 0) {
                        continue;
                    }
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    if ($i == 0) {
                        $confirm .= "\n\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t</tr>";
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    if ($out1[$t] >= $inv['balance']) {
                        $val = $inv['balance'];
                        $out1[$t] = $out1[$t] - $inv['balance'];
                    } else {
                        $val = $out1[$t];
                        $out1[$t] = 0;
                    }
                    $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[{$t}][]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
                // 0.01 because of high precisions like 0.0000000001234 still matching
                if ($out1[$t] >= 0.01) {
                    $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out[$t]) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
                }
            }
            if ($out2[$t] > 0) {
                if ($out2[$t] > $age30[$t]) {
                    $_POST["out1[{$t}]"] = $out1[$t];
                    $_POST["out2[{$t}]"] = $out2[$t];
                    $_POST["out3[{$t}]"] = $out3[$t];
                    $_POST["out4[{$t}]"] = $out4[$t];
                    $_POST["out5[{$t}]"] = $out5[$t];
                    return "<li class='err'>You cannot allocate " . CUR . " {$out2[$t]} to 30 days, you only owe " . CUR . " {$age30[$t]}</li>" . alloc($_POST);
                }
                // Connect to database
                db_conn("cubit");
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid[$t]}' AND balance>0 AND pdate >='" . extlib_ago(59) . "' AND pdate <='" . extlib_ago(29) . "'  AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
                $prnInvRslt = db_exec($sql);
                while ($inv = pg_fetch_array($prnInvRslt) and $out2[$t] > 0) {
                    if ($inv['invid'] == 0) {
                        continue;
                    }
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    if ($i == 0) {
                        $confirm .= "\n\t\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    if ($out2[$t] >= $inv['balance']) {
                        $val = $inv['balance'];
                        $out2[$t] = $out2[$t] - $inv['balance'];
                    } else {
                        $val = $out2[$t];
                        $out2[$t] = 0;
                    }
                    $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[{$t}][]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
                // 0.01 because of high precisions like 0.0000000001234 still matching
                if ($out2[$t] >= 0.01) {
                    $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out[$t]) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
                }
            }
            if ($out3[$t] > 0) {
                if ($out3[$t] > $age60[$t]) {
                    $_POST["out1[{$t}]"] = $out1[$t];
                    $_POST["out2[{$t}]"] = $out2[$t];
                    $_POST["out3[{$t}]"] = $out3[$t];
                    $_POST["out4[{$t}]"] = $out4[$t];
                    $_POST["out5[{$t}]"] = $out5[$t];
                    return "<li class='err'>You cannot allocate " . CUR . " {$out3[$t]} to 60 days, you only owe " . CUR . " {$age60[$t]} </li>" . alloc($_POST);
                }
                // Connect to database
                db_conn("cubit");
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid[$t]}' AND balance>0 AND pdate >='" . extlib_ago(89) . "' AND pdate <='" . extlib_ago(59) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
                $prnInvRslt = db_exec($sql);
                while ($inv = pg_fetch_array($prnInvRslt) and $out3[$t] > 0) {
                    if ($inv['invid'] == 0) {
                        continue;
                    }
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    if ($i == 0) {
                        $confirm .= "\n\t\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    if ($out3[$t] >= $inv['balance']) {
                        $val = $inv['balance'];
                        $out3[$t] = $out3[$t] - $inv['balance'];
                    } else {
                        $val = $out3[$t];
                        $out3[$t] = 0;
                    }
                    $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
                // 0.01 because of high precisions like 0.0000000001234 still matching
                if ($out3[$t] >= 0.01) {
                    $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
                }
            }
            if ($out4[$t] > 0) {
                if ($out4[$t] > $age90[$t]) {
                    $_POST["out1[{$t}]"] = $out1[$t];
                    $_POST["out2[{$t}]"] = $out2[$t];
                    $_POST["out3[{$t}]"] = $out3[$t];
                    $_POST["out4[{$t}]"] = $out4[$t];
                    $_POST["out5[{$t}]"] = $out5[$t];
                    return "<li class='err'>You cannot allocate " . CUR . " {$out4[$t]} to 90 days, you only owe " . CUR . " {$age90[$t]}</li>" . alloc($_POST);
                }
                // Connect to database
                db_conn("cubit");
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid[$t]}' AND balance>0 AND pdate >='" . extlib_ago(119) . "' AND pdate <='" . extlib_ago(89) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
                $prnInvRslt = db_exec($sql);
                while ($inv = pg_fetch_array($prnInvRslt) and $out4[$t] > 0) {
                    if ($inv['invid'] == 0) {
                        continue;
                    }
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    if ($i == 0) {
                        $confirm .= "\n\t\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    if ($out4[$t] >= $inv['balance']) {
                        $val = $inv['balance'];
                        $out4[$t] = $out4[$t] - $inv['balance'];
                    } else {
                        $val = $out4[$t];
                        $out4[$t] = 0;
                    }
                    $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
                // 0.01 because of high precisions like 0.0000000001234 still matching
                if ($out4[$t] >= 0.01) {
                    $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
                }
            }
            if ($out5[$t] > 0) {
                if ($out5[$t] > $age120[$t]) {
                    $_POST["out1[{$t}]"] = $out1[$t];
                    $_POST["out2[{$t}]"] = $out2[$t];
                    $_POST["out3[{$t}]"] = $out3[$t];
                    $_POST["out4[{$t}]"] = $out4[$t];
                    $_POST["out5[{$t}]"] = $out5[$t];
                    return "<li class='err'>You cannot allocate " . CUR . " {$out5[$t]} to 120 days, you only owe " . CUR . " {$age120[$t]}</li>" . alloc($_POST);
                }
                // Connect to database
                db_conn("cubit");
                $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE supid = '{$supid[$t]}' AND balance>0 AND pdate >='" . extlib_ago(149) . "' AND pdate <='" . extlib_ago(119) . "' AND div = '" . USER_DIV . "' ORDER BY pdate ASC";
                $prnInvRslt = db_exec($sql);
                while ($inv = pg_fetch_array($prnInvRslt) and $out5[$t] > 0) {
                    if ($inv['invid'] == 0) {
                        continue;
                    }
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    if ($i == 0) {
                        $confirm .= "\n\t\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t</tr>";
                    }
                    $invid = $inv['invid'];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    if ($out5[$t] >= $inv['balance']) {
                        $val = $inv['balance'];
                        $out5[$t] = $out5[$t] - $inv['balance'];
                    } else {
                        $val = $out5[$t];
                        $out5[$t] = 0;
                    }
                    $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>" . CUR . " {$val}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
                // 0.01 because of high precisions like 0.0000000001234 still matching
                if ($out5[$t] >= 0.01) {
                    $confirm .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
                }
            }
        }
        if ($all == 2) {
            // Layout
            $confirm .= "\n\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><h3>Outstanding Purchases</h3></td>\n\t\t\t\t</tr>\n\t\t\t\t<!--<table " . TMPL_tblDflts . " width='90%'>-->\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t<th>Outstanding amount</th>\n\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t<th>Amount</th>\n\t\t\t\t</tr>";
            $i = 0;
            // for bgcolor
            if (isset($invids[$t])) {
                foreach ($invids[$t] as $key => $value) {
                    if ($paidamt[$t][$invids[$t][$key]] < 0.01) {
                        continue;
                    }
                    $ii = $invids[$t][$key];
                    $pp = $paidamt[$t][$key];
                    db_conn("cubit");
                    # Get all the details
                    $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE purid='{$ii}' AND div = '" . USER_DIV . "'";
                    $invRslt = db_exec($sql) or errDie("Unable to access database.");
                    if (pg_numrows($invRslt) < 1) {
                        $sql = "SELECT purid as invid,intpurid as invid2,balance,pdate as odate FROM suppurch WHERE intpurid='{$ii}' AND div = '" . USER_DIV . "'";
                        $invRslt = db_exec($sql) or errDie("Unable to access database.");
                        if (pg_numrows($invRslt) < 1) {
                            return "<li class='err'> - Invalid ord number {$ii}.</li>";
                        }
                    }
                    $inv = pg_fetch_array($invRslt);
                    if ($inv['invid2'] > 0) {
                        $inv['invid'] = $inv['invid2'];
                    }
                    $invid = $inv['invid'];
                    $ppp = $paidamt[$t][$invid];
                    $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' size='20' name='invids[{$t}][]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$inv['balance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                    $confirm .= "\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='hidden' name='paidamt[{$t}][]' size='7' value='{$ppp}'>{$ppp}</td>\n\t\t\t\t\t\t</tr>";
                    $i++;
                }
            }
            // 0.01 because of high precisions like 0.0000000001234 still matching
            if ($out[$t] >= 0.01) {
                $confirm .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account\n\t\t\t\t\twith " . CUR . " " . sprint($out) . " </b>\n\t\t\t\t</td>\n\t\t\t</tr>";
            }
        }
        vsprint($out1[$t]);
        vsprint($out2[$t]);
        vsprint($out3[$t]);
        vsprint($out4[$t]);
        vsprint($out5[$t]);
        // 	vsprint($OUT1);
        // 	vsprint($OUT2);
        // 	vsprint($OUT3);
        // 	vsprint($OUT4);
        // 	vsprint($OUT5);
        $passon2 .= "\n\t\t<input type='hidden' name='out1[{$t}]' value='{$out1[$t]}'>\n\t\t<input type='hidden' name='out2[{$t}]' value='{$out2[$t]}'>\n\t\t<input type='hidden' name='out3[{$t}]' value='{$out3[$t]}'>\n\t\t<input type='hidden' name='out4[{$t}]' value='{$out4[$t]}'>\n\t\t<input type='hidden' name='out5[{$t}]' value='{$out5[$t]}'>\n\t\t<input type='hidden' name='date_day[{$t}]' value='{$date_day[$t]}'>\n\t\t<input type='hidden' name='date_month[{$t}]' value='{$date_month[$t]}'>\n\t\t<input type='hidden' name='date_year[{$t}]' value='{$date_year[$t]}'>";
    }
    /*
    				<input type='hidden' name='OUT1' value='$OUT1'>
    				<input type='hidden' name='OUT2' value='$OUT2'>
    				<input type='hidden' name='OUT3' value='$OUT3'>
    				<input type='hidden' name='OUT4' value='$OUT4'>
    				<input type='hidden' name='OUT5' value='$OUT5'>
    */
    $confirm .= "\n\t\t\t\t{$passon2}\n\t\t\t\t" . TBL_BR . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td><input type='submit' name='back' value='&laquo; Correction'></td>\n\t\t\t\t\t<td align='right' colspan='3'><input type='submit' value='Write &raquo'></td>\n\t\t\t\t</tr>\n\t\t\t</form>\n\t\t\t</table>\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='bank-pay-supp.php'>Add supplier payment</a></td>\n\t\t\t\t</tr>\n\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t</table>";
    return $confirm;
}