function confirm($_POST)
{
    // Sanity Checking
    # Get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # Redirect if must chrgvat
    if ($chrgvat == 'yes' && !isset($vataccid)) {
        return slctVatacc($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($ctaccid, "num", 1, 50, "Invalid Account to be Credited.");
    $v->isOk($dtaccid, "num", 1, 50, "Invalid Account to be Debited.");
    $v->isOk($refnum, "num", 1, 10, "Invalid Reference number.");
    $v->isOk($amount, "float", 1, 20, "Invalid Amount.");
    $v->isOk($details, "string", 0, 255, "Invalid Details.");
    $v->isOk($author, "string", 1, 30, "Invalid Authorising person name.");
    $v->isOk($chrgvat, "string", 1, 4, "Invalid charge vat option.");
    if ($chrgvat == 'yes') {
        $v->isOk($vataccid, "num", 1, 50, "Invalid VAT Account number.");
        $v->isOk($vatdedacc, "num", 1, 50, "Invalid VAT Deductable Account number.");
        $v->isOk($vatinc, "string", 1, 3, "Invalid vat inclusive selection.");
    }
    $datea = explode("-", $date);
    if (count($datea) == 3) {
        if (!checkdate($datea[1], $datea[0], $datea[2])) {
            $v->isOk($date, "num", 1, 1, "Invalid date.");
        }
    } else {
        $v->isOk($date, "num", 1, 1, "Invalid date.");
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    $amount = sprint($amount);
    if ($chrgvat == 'yes') {
        $vataccRs = get("core", "*", "accounts", "accid", $vataccid);
        $vatacc = pg_fetch_array($vataccRs);
        $vatin = ucwords($vatinc);
        $VATP = TAX_VAT;
        # if vat must be charged
        if ($vatinc == "no") {
            $vatamt = sprint($VATP / 100 * $amount);
            $totamt = sprint($amount + $vatamt);
        } else {
            $vatamt = sprint($amount / ($VATP + 100) * $VATP);
            $totamt = sprint($amount);
        }
        $vataccnum = "<tr class='bg-odd'><td>VAT Amount</td><td><input type=hidden name=vatinc value='{$vatinc}'><input type=hidden name=vatamt value='{$vatamt}'>{$vatamt}</td></tr>\n\t\t\t<tr class='bg-even'><td>Total Transaction Amount</td><td>{$totamt}</td></tr>\n\t\t\t<tr class='bg-odd'><td>VAT Account</td><td><input type=hidden name=vataccid value='{$vataccid}'><input type=hidden name=vatdedacc value='{$vatdedacc}'>{$vatacc['topacc']}/{$vatacc['accnum']} - {$vatacc['accname']}</td></tr>";
    } else {
        $vataccnum = "";
    }
    $dtaccRs = get("core", "*", "accounts", "accid", $dtaccid);
    $dtacc = pg_fetch_array($dtaccRs);
    $ctaccRs = get("core", "*", "accounts", "accid", $ctaccid);
    $ctacc = pg_fetch_array($ctaccRs);
    $confirm = "<h3>Record Journal transaction</h3>\n        <h4>Confirm entry</h4>\n        <form action='" . SELF . "' method=post>\n        <input type=hidden name=key value=write>\n        <input type=hidden name='dtaccid' value='{$dtaccid}'>\n        <input type=hidden name='ctaccid' value='{$ctaccid}'>\n        <input type=hidden name=dtaccname value='{$dtacc['accname']}'>\n        <input type=hidden name=ctaccname value='{$ctacc['accname']}'>\n        <input type=hidden name=date value='{$date}'>\n        <input type=hidden name=refnum value='{$refnum}'>\n        <input type=hidden name=amount value='{$amount}'>\n\t\t<input type=hidden name=chrgvat value='{$chrgvat}'>\n        <input type=hidden name=details value='{$details}'>\n        <input type=hidden name=author value='{$author}'>\n        <table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n        <tr><td width=50%><h3>Debit</h3></td><td width=50%><h3>Credit</h3></td></tr>\n        <tr class='bg-even'><td>{$dtacc['topacc']}/{$dtacc['accnum']} - {$dtacc['accname']}</td><td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td></tr>\n        <tr><td><br></td></tr>\n        <tr><td><br></td></tr>\n        <tr class='bg-odd'><td>Date</td><td>{$date}</td></tr>\n        <tr class='bg-even'><td>Reference number</td><td>{$refnum}</td></tr>\n\t\t<tr><td><br></td></tr>\n        <tr class='bg-odd'><td>Amount</td><td>{$amount}</td></tr>\n\t\t<tr class='bg-even'><td>Charge VAT </td><td>{$chrgvat}</td></tr>\n\t\t{$vataccnum}\n\t\t<tr><td><br></td></tr>\n\t\t<tr class='bg-even'><td>Details</td><td>{$details}</td></tr>\n        <tr class='bg-odd'><td>Authorising Person</td><td>{$author}</td></tr>\n\t\t<tr><td><br></td></tr>\n        <tr><td align=right><input type=button value='&laquo Back' onClick='javascript:history.back()'></td><td align=right><input type=submit value='Confirm Transaction &raquo'></td></tr>\n        </form></table>\n        <table border=0 cellpadding='2' cellspacing='1' width=15%>\n        <tr><td>\n        <br>\n        </td></tr>\n        <tr><th>Quick Links</th></tr>\n        <tr class=datacell><td align=center><a href='trans-new.php'>Journal Transactions</td></tr>\n        <script>document.write(getQuicklinkSpecial());</script>\n        <tr class=datacell><td align=center><a href='../main.php'>Main Menu</td></tr>\n        </table>";
    return $confirm;
}
function write($_POST)
{
    extract($_POST);
    $vatcode += 0;
    if (isset($back)) {
        unset($_POST["back"]);
        if ($chrgvat == "yes") {
            return slctVatacc($_POST);
        } elseif (isset($details)) {
            return details($_POST);
        } else {
            return details2($_POST);
        }
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($ctaccid, "num", 1, 50, "Invalid Account to be Credited.");
    $v->isOk($dtaccid, "num", 1, 50, "Invalid Account to be Debited.");
    $v->isOk($refnum, "num", 1, 10, "Invalid Reference number.");
    $v->isOk($amount, "float", 1, 20, "Invalid Amount.");
    $v->isOk($details, "string", 0, 255, "Invalid Details.");
    $v->isOk($author, "string", 1, 30, "Invalid Authorising person name.");
    $v->isOk($chrgvat, "string", 1, 4, "Invalid charge vat option.");
    if ($chrgvat == 'yes') {
        $v->isOk($vataccid, "num", 1, 50, "Invalid VAT Account number.");
        $v->isOk($vatdedacc, "num", 1, 50, "Invalid VAT Deductable Account number.");
        $v->isOk($vatamt, "float", 1, 11, "Invalid VAT Amount.");
        $v->isOk($vatinc, "string", 1, 3, "Invalid VAT inclusive selection.");
    }
    if ($v->isError()) {
        $err = $v->genErrors();
        return confirm($_POST, $err);
    }
    pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
    # Accounts details
    $dtaccRs = get("core", "*", "accounts", "accid", $dtaccid);
    $dtacc = pg_fetch_array($dtaccRs);
    $ctaccRs = get("core", "*", "accounts", "accid", $ctaccid);
    $ctacc = pg_fetch_array($ctaccRs);
    if ($chrgvat == 'yes') {
        if ($vatinc == 'yes') {
            # Calculate amount
            $amt = sprint($amount - $vatamt);
            $totamt = sprint($amount);
        } else {
            # Calculate amount
            $amt = sprint($amount);
            $totamt = sprint($amount + $vatamt);
        }
        $datea = explode("-", $date);
        //		$cdate="$datea[2]-$datea[1]-$datea[0]";
        $cdate = $date;
        # Check VAt Deductable account
        if ($vatdedacc == $dtaccid) {
            db_connect();
            $Sl = "SELECT * FROM vatcodes WHERE id='{$vatcode}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) < 1) {
                return "Please select the vatcode";
            }
            $vd = pg_fetch_array($Ri);
            ## PROCESS THIS VAT
            //vatr($vd['id'],$cdate,"INPUT",$vd['code'],$refnum,"$details VAT",-$totamt,-$vatamt);
            recordtrans('journal', '0', '0', $cdate, $refnum, -$totamt, -$vatamt, "{$details} VAT" . "|{$vd['id']}|INPUT|{$vd['code']}", '0');
            ## PROCESS THIS WRITETRANS
            recordtrans('journal', $vataccid, $ctaccid, $date, $refnum, $vatamt, '0', $details . "  VAT");
            //writetrans($vataccid, $ctaccid, $date, $refnum, $vatamt, $details."  VAT");
            ## PROCESS THIS WRITETRANS
            recordtrans('journal', $dtaccid, $ctaccid, $date, $refnum, $amt, '0', $details);
            //writetrans($dtaccid, $ctaccid, $date, $refnum, $amt, $details);
        } elseif ($vatdedacc == $ctaccid) {
            db_connect();
            $Sl = "SELECT * FROM vatcodes WHERE id='{$vatcode}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) < 1) {
                return "Please select the vatcode";
            }
            $vd = pg_fetch_array($Ri);
            ## PROCESS THIS VAT
            //vatr($vd['id'],$cdate,"OUTPUT",$vd['code'],$refnum,"$details.  VAT",$totamt,$vatamt);
            recordtrans('journal', '0', '0', $cdate, $refnum, $totamt, $vatamt, "{$details} VAT" . "|{$vd['id']}|OUTPUT|{$vd['code']}");
            ## PROCESS THIS WRITETRANS
            recordtrans('journal', $dtaccid, $vataccid, $date, $refnum, $vatamt, '0', $details . "  VAT");
            //writetrans($dtaccid, $vataccid, $date, $refnum, $vatamt, $details."  VAT");
            ## PROCESS THIS WRITETRANS
            recordtrans('journal', $dtaccid, $ctaccid, $date, $refnum, $amt, $details);
            //writetrans($dtaccid, $ctaccid, $date, $refnum, $amt, $details);
        }
    } else {
        $totamt = sprint($amount);
        # Write normal transaction
        recordtrans('journal', $dtaccid, $ctaccid, $date, $refnum, $totamt, '0', $details);
        //writetrans($dtaccid,$ctaccid, $date, $refnum, $totamt, $details);
    }
    if ($chrgvat == 'yes') {
        $vataccRs = get("core", "*", "accounts", "accid", $vataccid);
        $vatacc = pg_fetch_array($vataccRs);
        $vataccnum = "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Amount</td>\n\t\t\t\t\t\t\t<td>{$vatamt}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Total Transaction Amount</td>\n\t\t\t\t\t\t\t<td><b>{$totamt}</b></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Account</td>\n\t\t\t\t\t\t\t<td>{$vatacc['topacc']}/{$vatacc['accnum']} - {$vatacc['accname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t";
        $amt = $totamt - $vatamt;
    } else {
        $vataccnum = "";
        $amt = $amount;
    }
    if (cc_TranTypeAcc($dtaccid, $ctaccid) != false) {
        $cc_trantype = cc_TranTypeAcc($dtaccid, $ctaccid);
        $cc = "<script> CostCenter('{$cc_trantype}', 'Journal Entry', '{$date}', '{$details}', '{$amt}', '../'); </script>";
    } else {
        $cc = "";
    }
    pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF);
    // Start layout
    $write = "\n\t\t\t\t<center>\n\t\t\t\t{$cc}\n\t\t\t\t<script>\n\t\t\t\t\tparent.window.reload;\n\t\t\t\t</script>\n\t\t\t\t<h3>Journal transaction has been recorded</h3>\n\t\t\t\t<table " . TMPL_tblDflts . " width='500'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td width='50%'><h3>Debit</h3></td>\n\t\t\t\t\t\t<td width='50%'><h3>Credit</h3></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$dtacc['topacc']}/{$dtacc['accnum']} - {$dtacc['accname']}</td>\n\t\t\t\t\t\t<td>{$ctacc['topacc']}/{$ctacc['accnum']} - {$ctacc['accname']}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Amount</td>\n\t\t\t\t\t\t<td><b>{$amount}</b></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t{$vataccnum}\n\t\t\t\t</table>" . mkQuickLinks(ql("../reporting/index-reports.php", "Financials"), ql("../core/trans-new.php", "Journal Transactions"));
    return $write;
}