function write($_POST)
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    foreach ($run 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.[{$key}]");
            }
        } 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;
    }
    pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
    foreach ($run 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);
        if (isset($vataccid[$value])) {
            $vatRs = get("core", "accname, topacc, accnum", "accounts", "accid", $vataccid[$value]);
            $vatacc[$key] = pg_fetch_array($vatRs);
        }
        if (isset($chrgvat[$value])) {
            if ($vatinc[$value] == 'yes') {
                # Calculate amount
                $amt[$key] = sprint($amount[$key] - $vatamt[$value]);
                $totamt[$key] = sprint($amount[$key]);
            } else {
                # Calculate amount
                $amt[$key] = sprint($amount[$key]);
                $totamt[$key] = sprint($amount[$key] + $vatamt[$value]);
            }
            $datea = explode("-", $date[$key]);
            $cdate = "{$datea['2']}-{$datea['1']}-{$datea['0']}";
            # Check VAt Deductable account
            if ($vatdedacc[$value] == $dtaccid[$key]) {
                db_connect();
                $Sl = "SELECT * FROM vatcodes WHERE id='{$vatcodes[$key]}'";
                $Ri = db_exec($Sl);
                if (pg_num_rows($Ri) < 1) {
                    return "Please select the vatcode";
                }
                $vd = pg_fetch_array($Ri);
                vatr($vd['id'], $cdate, "INPUT", $vd['code'], $refnum[$key], "{$descript[$key]} VAT", -$totamt[$key], -$vatamt[$value]);
                writetrans($vataccid[$value], $ctaccid[$key], $date[$key], $refnum[$key], $vatamt[$value], $descript[$key] . "  VAT");
                writetrans($dtaccid[$key], $ctaccid[$key], $date[$key], $refnum[$key], $amt[$key], $descript[$key]);
                isBankRec($dtaccid[$key], 'deposit', $date[$key], $ctacc[$key]['accname'], $descript[$key], 0, $amt[$key], $ctaccid[$key]);
                $amounts = "|{$amt[$key]}|{$vatamt[$value]}";
                $accids = "|{$dtaccid[$key]}|{$vataccid[$value]}";
                $vats = "|0|0";
                $chrgvats = "|nov|nov";
                isBankmRec($ctaccid[$key], 'withdrawal', $date[$key], $dtacc[$key]['accname'], $descript[$key], 0, $totamt[$key], $dtaccid[$key], $amounts, $accids, $vats, $chrgvats);
                //isBankRec($ctaccid[$key], 'withdrawal', $date[$key], $dtacc[$key]['accname'], $descript[$key], 0, $amt[$key], $dtaccid[$key]);
            } elseif ($vatdedacc[$value] == $ctaccid[$key]) {
                db_connect();
                $Sl = "SELECT * FROM vatcodes WHERE id='{$vatcodes[$key]}'";
                $Ri = db_exec($Sl);
                if (pg_num_rows($Ri) < 1) {
                    return "Please select the vatcode.";
                }
                $vd = pg_fetch_array($Ri);
                vatr($vd['id'], $cdate, "OUTPUT", $vd['code'], $refnum[$key], "{$descript[$key]} VAT", $totamt[$key], $vatamt[$value]);
                writetrans($dtaccid[$key], $vataccid[$value], $date[$key], $refnum[$key], $vatamt[$value], $descript[$key] . "  VAT");
                writetrans($dtaccid[$key], $ctaccid[$key], $date[$key], $refnum[$key], $amt[$key], $descript[$key]);
                isBankRec($ctaccid[$key], 'withdrawal', $date[$key], $dtacc[$key]['accname'], $descript[$key], 0, $amt[$key], $dtaccid[$key]);
                $amounts = "|{$amt[$key]}|{$vatamt[$value]}";
                $accids = "|{$ctaccid[$key]}|{$vataccid[$value]}";
                $vats = "|0|0";
                $chrgvats = "|nov|nov";
                isBankmRec($dtaccid[$key], 'deposit', $date[$key], $ctacc[$key]['accname'], $descript[$key], 0, $totamt[$key], $ctaccid[$key], $amounts, $accids, $vats, $chrgvats);
                // isBankRec($dtaccid[$key], 'deposit', $date[$key], $ctacc[$key]['accname'], $descript[$key], 0, $amt[$key], $ctaccid[$key]);
            }
        } else {
            $totamt[$key] = sprint($amount[$key]);
            $vatamt[$value] = sprint(0);
            # Write normal transaction
            writetrans($dtaccid[$key], $ctaccid[$key], $date[$key], $refnum[$key], $totamt[$key], $descript[$key]);
            isBankRec($dtaccid[$key], 'deposit', $date[$key], $ctacc[$key]['accname'], $descript[$key], 0, $totamt[$key], $ctaccid[$key]);
            isBankRec($ctaccid[$key], 'withdrawal', $date[$key], $dtacc[$key]['accname'], $descript[$key], 0, $totamt[$key], $dtaccid[$key]);
        }
        # write transaction
        # writetrans($dtaccid[$key],$ctaccid[$key], $date[$key], $refnum[$key], $amount[$key], $descript[$key]);
    }
    pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
    # Start layout
    $write = "\n\t\t<center>\n\t\t<h3>Recurring Transaction 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>VAT Amount</th>\n\t\t\t\t<th>Total Transaction Amount</th>\n\t\t\t\t<th>Description</th>\n\t\t\t</tr>";
    foreach ($run 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 align='right'>" . CUR . " {$amount[$key]}</td>\n\t\t\t\t<td align='right'>" . CUR . " {$vatamt[$value]}</td>\n\t\t\t\t<td align='right'>" . CUR . " {$totamt[$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<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>";
    return $write;
}
function write($_POST)
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($ledgid, "num", 1, 20, "Invalid Input Ledger Number.");
    $v->isOk($ctaccid, "num", 1, 50, "Invalid Account to be Credited.");
    $v->isOk($dtaccid, "num", 1, 50, "Invalid Account to be Debited.");
    foreach ($amount as $key => $value) {
        $v->isOk($refnum[$key], "num", 1, 10, "Invalid Reference number.[" . ($key + 1) . "]");
        $v->isOk($amount[$key], "float", 1, 20, "Invalid Amount.[" . ($key + 1) . "]");
        $v->isOk($descript[$key], "string", 0, 255, "Invalid Details.[" . ($key + 1) . "]");
        $v->isOk($date[$key], "date", 1, 14, "Invalid date.[" . ($key + 1) . "]");
    }
    # 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;
    }
    # Get ledger settings
    core_connect();
    $sql = "SELECT * FROM in_ledgers WHERE ledgid='{$ledgid}' AND div = '" . USER_DIV . "'";
    $ledRslt = db_exec($sql);
    if (pg_numrows($ledRslt) < 1) {
        return "<li>Invalid Input Ledger Number.</li>";
    }
    $led = pg_fetch_array($ledRslt);
    # Accounts details
    $dtaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $dtaccid);
    $dtacc = pg_fetch_array($dtaccRs);
    $ctaccRs = get("core", "accname, topacc, accnum", "accounts", "accid", $ctaccid);
    $ctacc = pg_fetch_array($ctaccRs);
    # vat account option
    if ($led['chrgvat'] == 'yes') {
        # The percantage
        $VATP = TAX_VAT;
        # get vat account
        $vataccRs = get("core", "*", "accounts", "accid", $led['vataccid']);
        $vatacc = pg_fetch_array($vataccRs);
        $vatopt = "\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td> Vat Account: </td>\r\n\t\t\t\t<td>{$vatacc['topacc']}/{$vatacc['accnum']} - {$vatacc['accname']}</td>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td>VAT </td>\r\n\t\t\t\t<td>{$VATP} %</td>\r\n\t\t\t</tr>";
    } else {
        $vatopt = "";
    }
    # write the transaction with the aid of the writetrans functions
    $tot = 0;
    foreach ($amount as $key => $value) {
        # Calculate vat
        if ($led['chrgvat'] == 'yes') {
            # The percantage
            $VATP = TAX_VAT;
            if ($led['vatinc'] == 'yes') {
                # Calc VAT
                $VAT = sprint($amount[$key] - $amount[$key] / (100 + $VATP) * 100);
                # Calculate amount
                $amt = $amount[$key] - $VAT;
                # Check VAt Deductable account
                if ($led['vatdedacc'] == $led['dtaccid']) {
                    writetrans($led['vataccid'], $ctaccid, $date[$key], $refnum[$key], $VAT, $descript[$key] . "  Vat");
                    writetrans($dtaccid, $ctaccid, $date[$key], $refnum[$key], $amt, $descript[$key]);
                    isBankRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amt, $ctaccid);
                    $amounts = "|{$amt}|{$VAT}";
                    $accids = "|{$dtaccid}|{$led['vataccid']}";
                    $vats = "|0|0";
                    $chrgvats = "|nov|nov";
                    isBankmRec($ctaccid, 'withdrawal', $date[$key], $dtacc['accname'], $descript[$key], 0, $amount[$key], $dtaccid, $amounts, $accids, $vats, $chrgvats);
                } elseif ($led['vatdedacc'] == $led['ctaccid']) {
                    writetrans($dtaccid, $led['vataccid'], $date[$key], $refnum[$key], $VAT, $descript[$key] . "  Vat");
                    writetrans($dtaccid, $ctaccid, $date[$key], $refnum[$key], $amt, $descript[$key]);
                    isBankRec($ctaccid, 'withdrawal', $date[$key], $dtacc['accname'], $descript[$key], 0, $amt, $dtaccid);
                    $amounts = "|{$amt}|{$VAT}";
                    $accids = "|{$ctaccid}|{$led['vataccid']}";
                    $vats = "|0|0";
                    $chrgvats = "|nov|nov";
                    isBankmRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amount[$key], $ctaccid, $amounts, $accids, $vats, $chrgvats);
                    // isBankRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amt, $ctaccid);
                }
            } elseif ($led['vatinc'] == 'no') {
                # Calc VAT
                $VAT = sprint($VATP / 100 * $amount[$key]);
                # Calculate amount
                $amt = $amount[$key];
                # Check VAt Deductable account
                if ($led['vatdedacc'] == $led['dtaccid']) {
                    writetrans($led['vataccid'], $ctaccid, $date[$key], $refnum[$key], $VAT, $descript[$key] . "  Vat");
                    writetrans($dtaccid, $ctaccid, $date[$key], $refnum[$key], $amt, $descript[$key]);
                    isBankRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amt, $ctaccid);
                    $amounts = "|{$amt}|{$VAT}";
                    $accids = "|{$dtaccid}|{$led['vataccid']}";
                    $vats = "|0|0";
                    $chrgvats = "|nov|nov";
                    isBankmRec($ctaccid, 'withdrawal', $date[$key], $dtacc['accname'], $descript[$key], 0, $amount[$key], $dtaccid, $amounts, $accids, $vats, $chrgvats);
                } elseif ($led['vatdedacc'] == $led['ctaccid']) {
                    writetrans($dtaccid, $led['vataccid'], $date[$key], $refnum[$key], $VAT, $descript[$key] . "  Vat");
                    writetrans($dtaccid, $ctaccid, $date[$key], $refnum[$key], $amt, $descript[$key]);
                    isBankRec($ctaccid, 'withdrawal', $date[$key], $dtacc['accname'], $descript[$key], 0, $amt, $dtaccid);
                    $amounts = "|{$amt}|{$VAT}";
                    $accids = "|{$ctaccid}|{$led['vataccid']}";
                    $vats = "|0|0";
                    $chrgvats = "|nov|nov";
                    isBankmRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amount[$key], $ctaccid, $amounts, $accids, $vats, $chrgvats);
                    // isBankRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amt, $ctaccid);
                }
            }
            $tot += $amount[$key];
        } else {
            # Write normal transactions
            writetrans($dtaccid, $ctaccid, $date[$key], $refnum[$key], $amount[$key], $descript[$key]);
            isBankRec($dtaccid, 'deposit', $date[$key], $ctacc['accname'], $descript[$key], 0, $amount[$key], $ctaccid);
            isBankRec($ctaccid, 'withdrawal', $date[$key], $dtacc['accname'], $descript[$key], 0, $amount[$key], $dtaccid);
            $tot += $amount[$key];
        }
    }
    # Hmmmm ?
    $ret = $key + 1;
    $tot = sprint($tot);
    // Start layout
    $write = "\r\n\t\t<center>\r\n\t\t<h3>Journal Transactions have been recorded</h3>\r\n\t\t<table " . TMPL_tblDflts . " width='500'>\r\n\t\t\t<tr>\r\n\t\t\t\t<td width='50%'><h3>Debit</h3></td>\r\n\t\t\t\t<td width='50%'><h3>Credit</h3></td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td>{$dtacc['topacc']}/{$dtacc['accnum']} - {$dtacc['accname']}</td>\r\n\t\t\t\t<td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr><td><br></td></tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<th colspan='2'>Details</td>\r\n\t\t\t</tr>\r\n\t\t\t{$vatopt}\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td>Number of transactions</td>\r\n\t\t\t\t<td>{$ret}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td><b>Total amount</b></td>\r\n\t\t\t\t<td><b>" . CUR . " {$tot}</b></td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t\t<p>\r\n\t\t<table " . TMPL_tblDflts . " width='25%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<th>Quick Links</th>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='ledger-view.php'>View High Speed Input Ledgers</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='ledger-new.php'>New High Speed Input Ledgers</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td align='center'><a href='../main.php'>Main Menu</td>\r\n\t\t\t</tr>\r\n\t\t</table>";
    return $write;
}