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)
{
    # Processes
    db_connect();
    # Get vars
    extract($_POST);
    if (isset($back)) {
        return add($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 30, "Invalid Bank Account.");
    $v->isOk($date, "date", 1, 10, "Invalid Date Entry.");
    $v->isOk($descript, "string", 0, 255, "Invalid Description.");
    $v->isOk($reference, "string", 0, 50, "Invalid Reference Name/Number.");
    $v->isOk($cheqnum, "num", 0, 30, "Invalid Cheque number.");
    $v->isOk($amount, "float", 1, 10, "Invalid amount.");
    $v->isOk($supid, "num", 1, 20, "Invalid Supplier account.");
    # 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;
    }
    # CHECK IF THIS DATE IS IN THE BLOCKED RANGE
    $blocked_date_from = getCSetting("BLOCKED_FROM");
    $blocked_date_to = getCSetting("BLOCKED_TO");
    if (strtotime($date) >= strtotime($blocked_date_from) and strtotime($date) <= strtotime($blocked_date_to) and !user_is_admin(USER_ID)) {
        return "<li class='err'>Period Range Is Blocked. Only an administrator can process entries within this period.</li>";
    }
    # Date format
    $date = explode("-", $date);
    $date = $date[2] . "-" . $date[1] . "-" . $date[0];
    $cheqnum = 0 + $cheqnum;
    # 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);
    # Get account name
    $supRslt = get("cubit", "*", "suppliers", "supid", $supid);
    $sup = pg_fetch_array($supRslt);
    $bankaccid = getbankaccid($bankid);
    db_conn("exten");
    # Get debtors control account
    $sql = "SELECT credacc FROM departments WHERE deptid ='{$sup['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    $dept = pg_fetch_array($deptRslt);
    pglib_transaction("BEGIN");
    db_connect();
    $Sl = "INSERT INTO sup_stmnt(supid, amount, edate, descript,ref,cacc, div) VALUES('{$supid}','{$amount}','{$date}', '{$descript}','{$cheqnum}','{$bankaccid}', '" . USER_DIV . "')";
    $Rs = db_exec($Sl) or errDie("Unable to insert statement record in Cubit.", SELF);
    # Update the supplier (make balance less)
    $sql = "UPDATE suppliers SET balance = (balance + '{$amount}') WHERE supid = '{$supid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF);
    suppCT($amount, $supid, $date);
    # record the payment record
    db_connect();
    $sql = "INSERT INTO cashbook(bankid, trantype, date, name, descript, cheqnum, amount, banked, accinv, suprec, reference, div) VALUES ('{$bankid}', 'deposit', '{$date}', '{$sup['supno']} - {$sup['supname']}', '{$descript}', '{$cheqnum}', '{$amount}', 'no', '{$dept['credacc']}', '{$supid}', '{$reference}', '" . USER_DIV . "')";
    $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
    $refnum = getrefnum();
    # DT(account involved), CT(bank)
    writetrans($bankaccid, $dept['credacc'], $date, $refnum, $amount, $descript);
    suppledger($supid, $bankaccid, $date, 0, $descript, $amount, 'c');
    pglib_transaction("COMMIT");
    # Status report
    $write = "\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Bank Receipt</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='datacell'>\n\t\t\t\t\t\t<td>Bank Receipt from supplier : {$sup['supname']} added to cash book.</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
    # main table (layout with menu)
    $OUTPUT = "\n\t\t\t\t<center>\n\t\t\t\t<table width='90%'>\n\t\t\t\t\t<tr valign='top'>\n\t\t\t\t\t\t<td width='50%'>{$write}</td>\n\t\t\t\t\t\t<td align='center'>\n\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><a href='bank-pay-add.php'>Add Bank Payment</a></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><a href='bank-recpt-add.php'>Add Bank Receipt</a></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><a href='cashbook-view.php'>View Cash Book</a></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>\n\t\t\t\t</table>";
    return $OUTPUT;
}
function write($_POST)
{
    # Processes
    db_connect();
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($cashacc, "num", 1, 30, "Invalid Petty Cash Account.");
    $v->isOk($date, "date", 1, 10, "Invalid Date Entry.");
    $v->isOk($descript, "string", 0, 255, "Invalid Description.");
    $v->isOk($amount, "float", 1, 10, "Invalid amount.");
    $v->isOk($supid, "num", 1, 20, "Invalid Supplier account.");
    # 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;
    }
    # Date format
    $date = explode("-", $date);
    $date = $date[2] . "-" . $date[1] . "-" . $date[0];
    # Get account name
    $supRslt = get("cubit", "*", "suppliers", "supid", $supid);
    $sup = pg_fetch_array($supRslt);
    db_conn("exten");
    # Get debtors control account
    $sql = "SELECT credacc FROM departments WHERE deptid ='{$sup['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    $dept = pg_fetch_array($deptRslt);
    pglib_transaction("BEGIN");
    db_connect();
    $Sl = "\n\t\tINSERT INTO sup_stmnt (\n\t\t\tsupid, amount, edate, descript,ref,cacc, div\n\t\t) VALUES (\n\t\t\t'{$supid}', '{$amount}', '{$date}', 'Receipt', '0', '{$cashacc}', '" . USER_DIV . "'\n\t\t)";
    $Rs = db_exec($Sl) or errDie("Unable to insert statement record in Cubit.", SELF);
    # Update the supplier (make balance less)
    $sql = "UPDATE suppliers SET balance = (balance + '{$amount}') WHERE supid = '{$supid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF);
    suppledger($supid, $cashacc, $date, 0, "Cash Receipt", $amount, "c");
    suppCT($amount, $supid);
    # Record tranfer for patty cash report
    $sql = "\n\t\tINSERT INTO pettyrec (\n\t\t\tdate, type, det, amount, \n\t\t\tname, div\n\t\t) VALUES (\n\t\t\t'{$date}', 'Transfer', '{$descript}', '{$amount}', \n\t\t\t'Received from supplier: {$sup['supname']}', '" . USER_DIV . "'\n\t\t)";
    $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
    $refnum = getrefnum();
    writetrans($cashacc, $dept['credacc'], $date, $refnum, $amount, $descript);
    pglib_transaction("COMMIT");
    # Status report
    $write = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>Petty Cash Receipt</th>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td>Petty Cash Receipt from supplier : {$sup['supname']} added to petty cash book.</td>\n\t\t\t</tr>\n\t\t</table>";
    # main table (layout with menu)
    $OUTPUT = "\n\t\t<center>\n\t\t<table width='90%'>\n\t\t\t<tr valign='top'>\n\t\t\t\t<td width='50%'>{$write}</td>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\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='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='bank-pay-add.php'>Add Petty Cash Payment</a></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><a href='bank-recpt-add.php'>Add Petty Cash Receipt</a></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><a href='cashbook-view.php'>View Cash Book</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</center>";
    return $OUTPUT;
}