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 confirm($_POST)
{
    # get vars
    extract($_POST);
    if (!isset($out1)) {
        $out1 = '';
    }
    if (!isset($out2)) {
        $out2 = '';
    }
    if (!isset($out3)) {
        $out3 = '';
    }
    if (!isset($out4)) {
        $out4 = '';
    }
    if (!isset($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", 0, 255, "Invalid Description.");
    $v->isOk($cheqnum, "num", 0, 30, "Invalid Cheque number.");
    $v->isOk($amt, "float", 1, 10, "Invalid amount.");
    $v->isOk($rate, "float", 1, 10, "Invalid exchange rate.");
    $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]];
        }
    }
    $amt = sprint($amt);
    $lamt = sprint($amt * $rate);
    if (sprint($tot + $out + $out1 + $out2 + $out3 + $out4 + $out5 - $amt) != 0) {
        return "<li>{$tot} - {$amt} The total amount for Invoices not equal to the amount received. Please check the details.<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>\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>";
    }
    $confirm = "\n\t\t\t<h3>New International 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='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}'>\n\t\t\t\t<input type='hidden' name='rate' value='{$rate}'>";
    # 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);
    # Supplier name
    $sql = "SELECT supno,supname,currency 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<tr class='" . bg_class() . "'>\n\t\t<td>Account</td>\n\t\t<td>{$bank['accname']} - {$bank['bankname']}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Date</td>\n\t\t<td valign='center'>{$date}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Paid To</td>\n\t\t<td valign='center'>({$sup['supno']}) {$sup['supname']}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Description</td>\n\t\t<td valign='center'>{$descript}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Cheque Number</td>\n\t\t<td valign='center'>{$cheqnum}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Amount</td>\n\t\t<td valign='center'>{$sup['currency']} {$amt} | " . CUR . " {$lamt}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Exchange rate</td>\n\t\t<td valign='center'>" . CUR . " / {$sup['currency']} {$rate}</td>\n\t</tr>";
    if ($all == 0) {
        // Layout
        $confirm .= "\n\t\t\t\t<tr><td colspan='2'><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)) {
            foreach ($invids as $key => $value) {
                if ($paidamt[$invids[$key]] < 0.01) {
                    continue;
                }
                db_connect();
                # Get all the details
                $sql = "SELECT purid as invid,intpurid as invid2,fbalance,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,fbalance,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]}.";
                    }
                }
                $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>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                $confirm .= "\n\t\t\t\t\t\t\t<td>{$sup['currency']} <input type='hidden' name='paidamt[]' size='7' value='{$paidamt[$invid]}'>{$paidamt[$invid]}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
        }
        if ($out > 0) {
            $confirm .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out} </b></td>\n\t\t\t\t\t</tr>";
        }
    }
    if ($all == 1) {
        $age30 = sage($supid, 59);
        $age60 = sage($supid, 89);
        $age90 = sage($supid, 119);
        $age120 = sage($supid, 149);
        $i = 0;
        if ($out1 > 0) {
            // Connect to database
            db_connect();
            $sql = "SELECT purid as invid,intpurid as invid2,fbalance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND fbalance>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\t\t<tr><td colspan='2'><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[]' value='{$inv['invid']}'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t<td>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out1 >= $inv['fbalance']) {
                    $val = $inv['fbalance'];
                    $out1 = $out1 - $inv['fbalance'];
                } else {
                    $val = $out1;
                    $out1 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>{$sup['currency']} {$val}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
            if ($out1 > 0) {
                $confirm .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out1} (Current) </b></td>\n\t\t\t\t\t\t</tr>";
            }
        }
        if ($out2 > 0) {
            if ($out2 > $age30) {
                return "You cannot allocate {$sup['currency']} {$out2} to 30 days, you only owe {$sup['currency']} {$age30}";
            }
            // Connect to database
            db_connect();
            $sql = "SELECT purid as invid,intpurid as invid2,fbalance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND fbalance>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<tr><td colspan='2'><br></td></tr>\n\t\t\t\t\t\t\t<tr><td colspan='2'><h3>Outstanding Purchases</h3></td></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\t<tr class='" . bg_class() . "'>\n\t\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\t<td>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out2 >= $inv['fbalance']) {
                    $val = $inv['fbalance'];
                    $out2 = $out2 - $inv['fbalance'];
                } else {
                    $val = $out2;
                    $out2 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>{$sup['currency']} {$val}</td>\n\t\t\t\t\t\t\t</tr>";
                $i++;
            }
            if ($out2 > 0) {
                $confirm .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out2} (30 days)</b></td>\n\t\t\t\t\t\t\t</tr>";
            }
        }
        if ($out3 > 0) {
            if ($out3 > $age60) {
                return "You cannot allocate {$sup['currency']} {$out3} to 60 days, you only owe {$sup['currency']} {$age60}";
            }
            // Connect to database
            db_connect();
            $sql = "SELECT purid as invid,intpurid as invid2,fbalance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND fbalance>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\t<tr><td colspan='2'><br></td></tr>\n\t\t\t\t\t\t\t\t<tr><td colspan='2'><h3>Outstanding Purchases</h3></td></tr>\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th>Purchase</th>\n\t\t\t\t\t\t\t\t\t<th>Outstanding Amount</th>\n\t\t\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t\t\t<th>Amount</th>\n\t\t\t\t\t\t\t\t</tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\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\t<td>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out3 >= $inv['fbalance']) {
                    $val = $inv['fbalance'];
                    $out3 = $out3 - $inv['fbalance'];
                } else {
                    $val = $out3;
                    $out3 = 0;
                }
                $confirm .= "\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>{$sup['currency']} {$val}</td>\n\t\t\t\t\t\t\t</tr>";
                $i++;
            }
            if ($out3 > 0) {
                $confirm .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td colspan='5'><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out3} (60 days)</b></td>\n\t\t\t\t\t\t\t</tr>";
            }
        }
        if ($out4 > 0) {
            if ($out4 > $age90) {
                return "You cannot allocate {$sup['currency']} {$out4} to 90 days, you only owe {$sup['currency']} {$age90}";
            }
            // Connect to database
            db_connect();
            $sql = "SELECT purid as invid,intpurid as invid2,fbalance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND fbalance>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<tr><td colspan='2'><br></td></tr>\n\t\t\t\t\t\t\t<tr><td colspan='2'><h3>Outstanding Purchases</h3></td></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\t<tr class='" . bg_class() . "'>\n\t\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\t<td>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t\t<td>{$inv['odate']}</td>";
                if ($out4 >= $inv['fbalance']) {
                    $val = $inv['fbalance'];
                    $out4 = $out4 - $inv['fbalance'];
                } else {
                    $val = $out4;
                    $out4 = 0;
                }
                $confirm .= "<td><input type='hidden' name='paidamt[]' size='10' value='{$val}'>{$sup['currency']} {$val}</td></tr>";
                $i++;
            }
            if ($out4 > 0) {
                $confirm .= "<tr class='" . bg_class() . "'><td colspan=5><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out4} (90 days)</b></td></tr>";
            }
        }
        if ($out5 > 0) {
            if ($out5 > $age120) {
                return "You cannot allocate {$sup['currency']} {$out5} to 120 days, you only owe {$sup['currency']} {$age120}";
            }
            // Connect to database
            db_connect();
            $sql = "SELECT purid as invid,intpurid as invid2,fbalance,pdate as odate FROM suppurch WHERE supid = '{$supid}' AND fbalance>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<tr><td colspan=2><br></td></tr>\n\t\t\t\t\t\t<tr><td colspan=2><h3>Outstanding Purchases</h3></td></tr>\n\t\t\t\t\t\t<tr><th>Purchase</th><th>Outstanding Amount</th><th>Date</th><th>Amount</th></tr>";
                }
                $invid = $inv['invid'];
                $confirm .= "<tr class='" . bg_class() . "'><td><input type=hidden size=20 name=invids[] value='{$inv['invid']}'>{$inv['invid']}</td><td>{$sup['currency']} {$inv['fbalance']}</td><td>{$inv['odate']}</td>";
                if ($out5 >= $inv['fbalance']) {
                    $val = $inv['fbalance'];
                    $out5 = $out5 - $inv['fbalance'];
                } else {
                    $val = $out5;
                    $out5 = 0;
                }
                $confirm .= "<td><input type=hidden name='paidamt[]' size=10 value='{$val}'>{$sup['currency']} {$val}</td></tr>";
                $i++;
            }
            if ($out5 > 0) {
                $confirm .= "<tr class='" . bg_class() . "'><td colspan=5><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out5} (120 days)</b></td></tr>";
            }
        }
    }
    if ($all == 2) {
        // Layout
        $confirm .= "<tr><td colspan=2><br></td></tr>\n\t\t<tr><td colspan='2'><h3>Outstanding Purchases</h3></td></tr>\n\t\t<!--<table " . TMPL_tblDflts . " width=90%>-->\n\t\t\t<tr>\n\t\t\t\t<th>Purchase</th>\n\t\t\t\t<th>Outstanding amount</th>\n\t\t\t\t<th>Date</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t</tr>";
        $i = 0;
        // for bgcolor
        if (isset($invids)) {
            foreach ($invids as $key => $value) {
                if ($paidamt[$invids[$key]] < 0.01) {
                    continue;
                }
                db_connect();
                # Get all the details
                $sql = "SELECT purid as invid,intpurid as invid2,fbalance,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,fbalance,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]}.";
                    }
                }
                $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>{$sup['currency']} {$inv['fbalance']}</td>\n\t\t\t\t\t\t\t<td>{$inv['odate']}</td>\n\t\t\t\t\t\t\t<td>{$sup['currency']} <input type='hidden' name='paidamt[]' size='7' value='{$paidamt[$invid]}'>{$paidamt[$invid]}</td>\n\t\t\t\t\t\t</tr>";
                $i++;
            }
        }
        if ($out > 0) {
            $confirm .= "<tr class='" . bg_class() . "'><td colspan='5'><b>A general transaction will debit the supplier's account with {$sup['currency']} {$out} </b></td></tr>";
        }
    }
    $confirm .= "\n\t\t<input type='hidden' name='out1' value='{$out1}'>\n\t\t<input type='hidden' name='out2' value='{$out2}'>\n\t\t<input type='hidden' name='out3' value='{$out3}'>\n\t\t<input type='hidden' name='out4' value='{$out4}'>\n\t\t<input type='hidden' name='out5' value='{$out5}'>\n\t\t<tr>\n\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'></td>\n\t\t\t<td align='right'><input type='submit' value='Write &raquo'></td>\n\t\t</tr>\n\t</form>\n\t</table>\n\t<p>\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr>\n\t\t\t<th>Quick Links</th>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td><a href='bank-pay-supp.php'>Add supplier payment</a></td>\n\t\t</tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $confirm;
}
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;
}