Exemplo n.º 1
0
function write($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($type, "string", 1, 255, "Invalid Transaction type switch.");
    $v->isOk($typename, "string", 1, 255, "Invalid Transaction type.");
    $edate = $v->chkrDate($edate, "Invalid date.");
    $v->isOk($amount, "float", 1, 13, "Invalid Amount.");
    $v->isOk($descrip, "string", 0, 255, "Invalid description.");
    if (isset($ccids)) {
        foreach ($ccids as $key => $value) {
            $v->isOk($ccperc[$key], "float", 1, 20, "Invalid Cost center percentage.");
        }
    } else {
        return enter($_POST, "<li class=err> There are no Cost centers found.");
    }
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return enter($_POST, $confirm);
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    $type = strtolower($type);
    $edate = ext_rdate($edate);
    $edarr = explode("-", $edate);
    $prd = $edarr[1];
    ## start transaction
    pglib_transaction("BEGIN") or errDie("Unable to start transaction.");
    $ccenters = "";
    foreach ($ccids as $key => $value) {
        db_connect();
        $sql = "SELECT * FROM costcenters_links WHERE id = '{$ccids[$key]}'";
        $ccRslt = db_exec($sql) or errDie("Unable to retrieve Cost centers from database.");
        $cc = pg_fetch_array($ccRslt);
        $ccamts[$key] = sprint($amount * ($ccperc[$key] / 100));
        #we need to connect to the actual period db
        db_conn($prd);
        if ($type == "dtct") {
            $sql = "\n\t\t\t\t\tINSERT INTO cctran \n\t\t\t\t\t\t(ccid, trantype, typename, edate, description, amount, username, div, project) \n\t\t\t\t\tVALUES \n\t\t\t\t\t\t('{$ccids[$key]}', 'dt', '{$typename}', '{$edate}', '{$descrip}', '{$ccamts[$key]}', '" . USER_NAME . "', '" . USER_DIV . "', '{$ccidpro[$key]}')";
            $insRslt = db_exec($sql) or errDie("Unable to retrieve insert Cost center amounts into database.");
            $sql = "\n\t\t\t\t\tINSERT INTO cctran \n\t\t\t\t\t\t(ccid, trantype, typename, edate, description, amount, username, div, project) \n\t\t\t\t\tVALUES \n\t\t\t\t\t\t('{$ccids[$key]}', 'ct', '{$typename}', '{$edate}', '{$descrip}', '{$ccamts[$key]}', '" . USER_NAME . "', '" . USER_DIV . "', '{$ccidpro[$key]}')";
            $insRslt = db_exec($sql) or errDie("Unable to retrieve insert Cost center amounts into database.");
        } else {
            $sql = "\n\t\t\t\t\tINSERT INTO cctran \n\t\t\t\t\t\t(ccid, trantype, typename, edate, description, amount, username, div, project) \n\t\t\t\t\tVALUES \n\t\t\t\t\t\t('{$ccids[$key]}', '{$type}', '{$typename}', '{$edate}', '{$descrip}', '{$ccamts[$key]}', '" . USER_NAME . "', '" . USER_DIV . "', '{$ccidpro[$key]}')";
            $insRslt = db_exec($sql) or errDie("Unable to retrieve insert Cost center amounts into database.");
        }
    }
    db_connect();
    #now remove the temp entry
    $rem_sql = "DELETE FROM cc_popup_data WHERE id = '{$writeid}'";
    $run_rem = db_exec($rem_sql) or errDie("Unable to remove temporary cost center information.");
    pglib_transaction("COMMIT") or errDie("Unable to complete transaction.");
    // Layout
    $write = "\n\t\t<center>\n\t\t<table " . TMPL_tblDflts . " width='300'>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><b>( i )</b> Amount has been allocated to Cost Centers. <b>( i )</b></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<p>\n\t\t<input type=button value=' [X] Close ' onClick='javascript:window.close();'>\n\t\t</center>";
    return $write;
}
function write($_POST)
{
    db_connect();
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 30, "Invalid Bank Account.");
    $v->isOk($fbankid, "num", 1, 30, "Invalid Foreign Bank Account.");
    $date = $v->chkrDate($date, "Invalid date.");
    $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($rate, "float", 1, 10, "Invalid exchange rate.");
    $v->isOk($ttype, "string", 1, 4, "Invalid Transfer type option.");
    # 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;
    }
    $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>";
    }
    # Get bank account name
    db_connect();
    $sql = "SELECT * FROM bankacct WHERE bankid = '{$bankid}' AND div = '" . USER_DIV . "'";
    $bankRslt = db_exec($sql);
    $bank = pg_fetch_array($bankRslt);
    $sql = "SELECT * FROM bankacct WHERE bankid = '{$fbankid}' AND div = '" . USER_DIV . "'";
    $fbankRslt = db_exec($sql);
    $fbank = pg_fetch_array($fbankRslt);
    bank_xrate_update($fbank['fcid'], $rate);
    # date format
    $date = explode("-", $date);
    $date = $date[2] . "-" . $date[1] . "-" . $date[0];
    #refnum
    $refnum = getrefnum();
    /* -- Start Hooks -- */
    # Get hook account number
    core_connect();
    $sql = "SELECT * FROM bankacc WHERE accid = '{$bankid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to retrieve bank account link from Cubit", SELF);
    # Check if link exists
    if (pg_numrows($rslt) < 1) {
        return "<li class='err'> ERROR : The bank account that you selected doesn't appear to have an account linked to it.</li>";
    }
    $banklnk = pg_fetch_array($rslt);
    # Get hook account number
    core_connect();
    $sql = "SELECT * FROM bankacc WHERE accid = '{$fbankid}' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to retrieve bank account link from Cubit", SELF);
    # Check if link exists
    if (pg_numrows($rslt) < 1) {
        return "<li class='err'> ERROR : The bank account that you selected doesn't appear to have an account linked to it.</li>";
    }
    $fbanklnk = pg_fetch_array($rslt);
    /* -- End Hooks -- */
    $cheqnum = 0 + $cheqnum;
    if ($ttype == 'loc') {
        $famount = sprint($amount / $rate);
        # Record the payment record
        db_connect();
        $sql = "\n\t\t\tINSERT INTO cashbook (\n\t\t\t\tbankid, trantype, date, name, descript, cheqnum, amount, \n\t\t\t\tfamount, vat, chrgvat, banked, accinv, reference, div\n\t\t\t) VALUES (\n\t\t\t\t'{$bankid}', 'withdrawal', '{$date}', '{$fbank['accname']} {$fbank['bankname']}', '{$descript}', '{$cheqnum}', '{$amount}', \n\t\t\t\t'{$famount}', '0', 'no', 'no', '{$fbanklnk['accnum']}', '{$reference}', '" . USER_DIV . "'\n\t\t\t)";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        $lcashid = pglib_lastid("cashbook", "cashid");
        $sql = "\n\t\t\tINSERT INTO cashbook (\n\t\t\t\tbankid, trantype, date, name, descript, cheqnum, amount, \n\t\t\t\tfamount, vat, chrgvat, banked, accinv, lcashid, reference, div\n\t\t\t) VALUES (\n\t\t\t\t'{$fbankid}', 'deposit', '{$date}', '{$bank['accname']} {$bank['bankname']}', '{$descript}', '{$cheqnum}', '{$amount}', \n\t\t\t\t'{$famount}', '0', 'no', 'no', '{$banklnk['accnum']}',  '{$lcashid}', '{$reference}', '" . USER_DIV . "'\n\t\t\t)";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        $lcashid2 = pglib_lastid("cashbook", "cashid");
        #restore link
        $sql = "UPDATE cashbook SET lcashid = '{$lcashid2}' WHERE cashid = '{$lcashid}'";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        # Update the bankacct table (make fbalance less) [used for cashbook fc value]
        $sql = "UPDATE bankacct SET balance = (balance + '{$amount}'::numeric(13,2)), fbalance = (fbalance + '{$famount}'::numeric(13,2)) WHERE bankid = '{$fbankid}' AND div = '" . USER_DIV . "'";
        $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF);
        # DT(account involved), CT(bank)
        writetrans($fbanklnk['accnum'], $banklnk['accnum'], $date, $refnum, $amount, $descript);
    } else {
        $lamount = sprint($amount * $rate);
        # Record the payment record
        db_connect();
        $sql = "\n\t\t\tINSERT INTO cashbook (\n\t\t\t\tbankid, trantype, date, name, descript, cheqnum, amount, \n\t\t\t\tfamount, vat, chrgvat, banked, accinv, reference, div\n\t\t\t) VALUES (\n\t\t\t\t'{$bankid}', 'deposit', '{$date}', '{$fbank['accname']} {$fbank['bankname']}', '{$descript}', '{$cheqnum}', '{$lamount}', \n\t\t\t\t'{$amount}' , '0', 'no', 'no', '{$fbanklnk['accnum']}', '{$reference}', '" . USER_DIV . "'\n\t\t\t)";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        $lcashid = pglib_lastid("cashbook", "cashid");
        $sql = "\n\t\t\tINSERT INTO cashbook (\n\t\t\t\tbankid, trantype, date, name, descript, cheqnum, amount, \n\t\t\t\tfamount, vat, chrgvat, banked, accinv, lcashid, reference, div\n\t\t\t) VALUES (\n\t\t\t\t'{$fbankid}', 'withdrawal', '{$date}', '{$bank['accname']} {$bank['bankname']}', '{$descript}', '{$cheqnum}', '{$lamount}', \n\t\t\t\t'{$amount}', '0', 'no', 'no', '{$banklnk['accnum']}', '{$lcashid}', '{$reference}', '" . USER_DIV . "'\n\t\t\t)";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        $lcashid2 = pglib_lastid("cashbook", "cashid");
        #restore link
        $sql = "UPDATE cashbook SET lcashid = '{$lcashid2}' WHERE cashid = '{$lcashid}'";
        $Rslt = db_exec($sql) or errDie("Unable to add bank payment to database.", SELF);
        # Update the bankacct table (make fbalance less) [used for cashbook fc value]
        $sql = "UPDATE bankacct SET balance = (balance - '{$lamount}'::numeric(13,2)), fbalance = (fbalance - '{$amount}'::numeric(13,2)) WHERE bankid = '{$fbankid}' AND div = '" . USER_DIV . "'";
        $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF);
        # DT(account involved), CT(bank)
        writetrans($banklnk['accnum'], $fbanklnk['accnum'], $date, $refnum, $lamount, $descript);
    }
    # Status report
    $write = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>Bank Payment</th>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td>Bank Transfer added to 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 Bank 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 Bank 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\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</table>";
    return $OUTPUT;
}