function add()
{
    extract($_REQUEST);
    $qry = new dbSelect("batch_cashbook", "cubit", grp(m("where", wgrp(m("cashid", "{$id}")))));
    $qry->run();
    if ($qry->num_rows() <= 0) {
        invalid_use("Invalid batch cashbook entry.");
    }
    $bcb = $qry->fetch_array();
    if ($bcb["chrgvat"] == "exc") {
        $bcb["amount"] -= $bcb["vat"];
    }
    extract($bcb, EXTR_SKIP);
    core_connect();
    $accs = qryAccounts();
    if ($accs->num_rows() <= 0) {
        $glacc = "There are no Income accounts in Cubit.";
    }
    $glacc = "<select name='accinv'>";
    while ($acc = $accs->fetch_array()) {
        if (isDisabled($acc['accid'])) {
            continue;
        }
        if ($accinv == $acc['accid']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $glacc .= "<option value='{$acc['accid']}' {$sel}>{$acc['accname']}</option>";
    }
    $glacc .= "</select>";
    $OUT = "\n\t\t\t\t<h3>Edit Bank Receipt</h3>\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t\t\t<input type='hidden' name='key' value='confirm'>\n\t\t\t\t\t<input type='hidden' name='id' value='{$id}'>\n\t\t\t\t\t<input type='hidden' name='vat' value='{$bcb['vat']}' />\n\t\t\t\t\t<input type='hidden' name='orig_vatcode' value='{$bcb['vatcode']}' />\n\t\t\t\t\t<input type='hidden' name='orig_chrgvat' value='{$bcb['chrgvat']}' />\n\t\t\t\t\t<input type='hidden' name='orig_amount' value='{$bcb['amount']}' />\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Field</th>\n\t\t\t\t\t\t<th>Value</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Bank Account</td>\n\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t<select name='bankid'>";
    db_connect();
    $qry->setTable("bankacct", "cubit");
    $qry->setOpt(grp(m("where", "btype!='int' AND div='" . USER_DIV . "'")));
    $qry->run();
    if ($qry->num_rows() <= 0) {
        return "<li class='err'> There are no accounts held at the selected Bank.\n\t\t\t<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct Selection'>";
    }
    while ($acc = $qry->fetch_array()) {
        if (isset($bankid) && $bankid == $acc['bankid']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $OUT .= "<option value='{$acc['bankid']}' {$sel}>{$acc['accname']} - {$acc['bankname']} ({$acc['acctype']})</option>";
    }
    if (!isset($name)) {
        $name = "";
        $descript = "";
        $cheqnum = "";
        $amount = "";
        $chrgvat = "";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t\t<select name='vatcode'>\n\t\t\t\t<option value='0'>Select</option>";
    $vacs = qryVatcode();
    $Vatcodes = db_mksel($vacs, "vatcode", $vatcode, "#id", "#code", "0:Select");
    list($o_year, $o_month, $o_day) = explode('-', $date);
    $sel1 = "";
    $sel2 = "";
    $sel3 = "";
    if ($chrgvat == "inc") {
        $sel1 = "checked=yes";
    } else {
        if ($chrgvat == "exc") {
            $sel2 = "checked=yes";
        } else {
            if ($chrgvat == "nov") {
                $sel3 = "checked=yes";
            } else {
                $sel1 = "checked=yes";
            }
        }
    }
    $OUT .= "\n\t\t</select>\n\t\t</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Date</td>\n\t\t<td>\n\t\t\t" . mkDateSelect("o", $o_year, $o_month, $o_day) . "\n\t\t</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Received from</td>\n\t\t<td valign='center'><input size='20' name='name' value='{$name}'></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Description</td>\n\t\t<td valign='center'><textarea col='18' rows='3' name='descript'>{$descript}</textarea></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Reference</td>\n\t\t<td valign='center'><input size='20' name='reference' value='{$reference}'></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Cheque Number</td>\n\t\t<td valign='center'><input size='20' name='cheqnum' value='{$cheqnum}'></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Amount</td>\n\t\t<td valign='center'>" . CUR . " <input type='text' size='10' name='amount' value='" . sprint($amount) . "'></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>VAT </td>\n\t\t<td>\n\t\t\t<input type='radio' name='chrgvat' value='inc' {$sel1}>Inclusive &nbsp;&nbsp;\n\t\t\t<input type='radio' name='chrgvat' value='exc' {$sel2}>Exclusive &nbsp;&nbsp;\n\t\t\t<input type='radio' name='chrgvat' value='nov' {$sel3}>No VAT\n\t\t</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>VAT Code</td>\n\t\t<td>{$Vatcodes}</td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td valign='top'>Select Contra Account</td>\n\t\t<td>{$glacc}</td>\n\t</tr>\n\t<tr>\n\t\t<td>&nbsp;</td>\n\t\t<td valign='center' align='right'><input type='submit' value='Confirm &raquo;'></td>\n\t</tr>\n\t</table>";
    # main table (layout with menu)
    $OUT .= mkQuickLinks();
    return $OUT;
}
function confirm($_POST)
{
    extract($_POST);
    if (isset($back)) {
        return view();
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 20, "Invalid Bank ID.");
    foreach ($amount as $key => $value) {
        //	if($amount[$key] > 0){
        # check all vars
        $v->isOk($to[$key], "string", 0, 255, "Invalid receipient/depositor.");
        $v->isOk($trantype[$key], "string", 1, 20, "Invalid transaction type.");
        $v->isOk($descript[$key], "string", 0, 255, "Invalid description.");
        $v->isOk($ref[$key], "string", 0, 255, "Invalid reference <b>[{$key}]</b>.");
        $v->isOk($cheqnum[$key], "num", 0, 20, "Invalid cheque number <b>[{$key}]</b>.");
        $v->isOk($amount[$key], "float", 0, 8, "Invalid amount <b>[{$key}]</b>.");
        $v->isOk($chrgvat[$key], "string", 1, 4, "Invalid VAT option.");
        $v->isOk($accinv[$key], "num", 1, 20, "Invalid account involved <b>[{$key}]</b>.");
        if (empty($amount[$key])) {
            unset($amount[$key]);
        }
        # put date together and check
        $date[$key] = mkdate($o_year[$key], $o_month[$key], $o_day[$key]);
        $v->isOk($date[$key], "date", 1, 1, "Invalid date <b>[{$key}]</b>.");
        //	}
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return enter($_POST, $confirm);
        $confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get bank acc details
    $bankRslt = get("cubit", "*", "bankacct", "bankid", $bankid);
    $bank = pg_fetch_array($bankRslt);
    # Layout
    $confirm = "\n\t\t<center>\n\t\t<h3>Type in Bank Statement</h3>\n\t\t<h4>Confirm entry</h4>\n\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t<input type='hidden' name='key' value='write'>\n\t\t\t<input type='hidden' name='bankid' value='{$bankid}'>\n\t\t\t<input type='hidden' name='number' value='{$number}'>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center' colspan='4'>Bank Account : <b>({$bank['accnum']}) {$bank['accname']}</b></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th>Date</th>\n\t\t\t\t<th>Paid to/Received from</th>\n\t\t\t\t<th>Transaction type</th>\n\t\t\t\t<th>Transaction 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>Transaction Amount</th>\n\t\t\t\t<th>VAT</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Contra Account</th>\n\t\t\t</tr>";
    # Display the trans
    $trans = "";
    foreach ($amount as $key => $value) {
        if ($amount[$key] > 0) {
            # get account name
            $accRslt = get("core", "accname,topacc,accnum", "accounts", "accid", $accinv[$key]);
            $acc = pg_fetch_array($accRslt);
            $vd = qryVatcode($vatcode[$key]);
            if ($vd["zero"] == "Yes") {
                $chrgvat[$key] = "no";
            }
            $vatp = $vd["vat_amount"];
            $totamt = $amount[$key];
            if ($chrgvat[$key] == "exc") {
                $vat = sprint($vatp / 100 * $amount[$key]);
                $totamt += $vat;
                $vatin = CUR . " <input type='text' size='6' name='vat[{$key}]' value='{$vat}' />";
            } elseif ($chrgvat[$key] == "inc") {
                $vat = sprint($amount[$key] / (100 + $vatp) * $vatp);
                $vatin = CUR . " <input type='text' size='6' name='vat[{$key}]' value='{$vat}' />";
            } else {
                $vat = "No VAT";
                $vatd = "{$vat}";
                $vatin = "No VAT";
            }
            # alternate bgcolor
            $bgColor = bgcolorc($key);
            vsprint($totamt);
            $trans .= "\n\t\t\t\t<input type='hidden' size='2' name='date[{$key}]' value='{$date[$key]}' />\n\t\t\t\t<input type='hidden' name='to[{$key}]' value='{$to[$key]}' />\n\t\t\t\t<input type='hidden' name='trantype[{$key}]' value='{$trantype[$key]}' />\n\t\t\t\t<input type='hidden' name='descript[{$key}]' value='{$descript[$key]}' />\n\t\t\t\t<input type='hidden' name='ref[{$key}]' value='{$ref[$key]}' />\n\t\t\t\t<input type='hidden' name='cheqnum[{$key}]' value='{$cheqnum[$key]}' />\n\t\t\t\t<input type='hidden' name='amount[{$key}]' value='{$amount[$key]}' />\n\t\t\t\t<input type='hidden' name='chrgvat[{$key}]' value='{$chrgvat[$key]}' />\n\t\t\t\t<input type='hidden' name='vatcode[{$key}]' value='{$vatcode[$key]}' />\n\t\t\t\t<input type='hidden' name='accinv[{$key}]' value='{$accinv[$key]}' />\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td align='center'>{$date[$key]}</td>\n\t\t\t\t\t<td align='center'>{$to[$key]}</td>\n\t\t\t\t\t<td align='center'>{$trantype[$key]}</td>\n\t\t\t\t\t<td align='center'>{$descript[$key]}</td>\n\t\t\t\t\t<td align='center'>{$ref[$key]}</td>\n\t\t\t\t\t<td align='center'>{$cheqnum[$key]}</td>\n\t\t\t\t\t<td align='center' nowrap='t'>" . CUR . " {$totamt}</td></td>\n\t\t\t\t\t<td align='center' nowrap='t'>{$vatin}</td>\n\t\t\t\t\t<td align='center'>{$vd['code']}</td>\n\t\t\t\t\t<td align='center'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</td></td>\n\t\t\t\t</tr>";
        }
    }
    if (strlen($trans) < 5) {
        $err = "<li class='err'> - Please enter full transaction details";
        return enter($_POST, $err);
    }
    $confirm .= "\n\t\t\t{$trans}\n\t\t\t<tr>\n\t\t\t\t<td></td>\n\t\t\t\t<td align='right'><input type='submit' name='batch' value='Add to Batch &raquo'></td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td><input type='submit' name='back' value='&laquo; Correction'></td>\n\t\t\t\t<td align='right' colspan='4'><input type='submit' value='Write &raquo'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>" . mkQuickLinks();
    return $confirm;
}
function invoice()
{
    if (isset($_REQUEST["btn_back"])) {
        return enter();
    }
    extract($_REQUEST);
    // Retrieve asset
    $sql = "SELECT * FROM cubit.assets WHERE id='{$asset_id}'";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
    $asset_data = pg_fetch_array($asset_rslt);
    // Retrieve asset group
    $sql = "SELECT * FROM cubit.assetgrp WHERE grpid='{$asset_data['grpid']}'";
    $grp_rslt = db_exec($sql) or errDie("Unable to retrieve asset group.");
    $grp_data = pg_fetch_array($grp_rslt);
    // Retrieve customer
    $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cust_id}'";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
    $cust_data = pg_fetch_array($cust_rslt);
    if ($cust_id > 0) {
        $ctyp = "s";
        $tval = $cust_id;
    } else {
        if ($cust_id == "-1") {
            $ctyp = "c";
            $tval = "2";
            $cust_data["surname"] = "Cash Sale";
        } else {
            if ($cust_id == "-2") {
                $ctyp = "ac";
                $tval = "";
                $cust_data["surname"] = "Cash Sale";
            } else {
                return enter("<li class='err'>Invalid sale option selected.</li>");
            }
        }
    }
    $acc = "0";
    $vatchrg = $vatinc == "exc" ? "no" : "yes";
    $vcd = qryVatcode($vatcode);
    $va = vatcalca($price * $qty, $vatchrg, "no", 0, $vcd["vat_amount"]);
    pglib_transaction("BEGIN");
    if ($cust_data["surname"] == "Cash Sale") {
        $cust_data["paddr1"] = "";
        $cust_data["vatnum"] = "";
    }
    $sql = "INSERT INTO cubit.nons_invoices(cusname, cusaddr, cusvatno, chrgvat,\r\n\t\t\t\tsdate, odate, subtot, balance, vat, total, done, username, prd,\r\n\t\t\t\tinvnum, typ, ctyp, tval, div, accid, salespn)\r\n\t\t\tVALUES ('{$cust_data['surname']}', '{$cust_data['paddr1']}',\r\n\t\t\t\t'{$cust_data['vatnum']}', '{$vatchrg}', CURRENT_DATE, '{$date}', '{$va['subtotal']}', 0,\r\n\t\t\t\t'{$va['vat']}', '{$va['total']}', 'n', '" . USER_NAME . "', '" . extractMonth($date) . "', 0, \r\n\t\t\t\t'inv', '{$ctyp}', '{$tval}', '" . USER_DIV . "', '{$acc}', 'General')";
    db_exec($sql) or errDie("Unable to create invoice");
    $ni_id = lastinvid();
    $asset_saleacc = gethook("accnum", "salesacc", "name", "saleofassets");
    $price_all = $price * $qty;
    $sql = "INSERT INTO cubit.nons_inv_items (invid, qty, description, div,\r\n\t\t\t\tamt, unitcost, accid, rqty, vatex, cunitcost, asset_id)\r\n\t\t\tVALUES ('{$ni_id}', '{$qty}', '{$asset_data['des']}', '" . USER_DIV . "',\r\n\t\t\t\t'{$price_all}', '{$price}', '{$asset_saleacc}', '0', \r\n\t\t\t\t'{$vatcode}', '0', '{$asset_id}')";
    db_exec($sql) or errDie("Unable to create invoice.");
    pglib_transaction("COMMIT");
    header("Location: asset-invoice-print.php?invid={$ni_id}&printpage=t");
    exit;
}
function report()
{
    extract($_POST);
    $date = $date_year . "-" . $date_month . "-" . $date_day;
    $amount += 0;
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($user, "string", 1, 50, "Invalid user.");
    $v->isOk($amount, "float", 1, 10, "Invalid amount.");
    $v->isOk($disp_sales, "string", 1, 3, "Invalid display sales selection.");
    if (!checkdate($date_month, $date_day, $date_year)) {
        $v->isOk($date, "num", 1, 1, "Invalid order date.");
    }
    $met = remval($met);
    # display errors, if any
    if ($v->isError()) {
        $err = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $err .= "<li class='err'>{$e['msg']}</li>";
        }
        return $confirm;
    }
    if ($user != "0") {
        $whe = " AND by='{$user}'";
        $whe_sales = "AND username='******'";
    } else {
        $whe = "";
        $whe_sales = "";
    }
    if ($met != "all") {
        $whe .= " AND method='{$met}'";
    } else {
        $whe .= "";
    }
    // Display the sales
    if ($disp_sales == "yes") {
        if (substr($date_month, 0, 1) == "0") {
            $date_month = substr($date_month, 1, 1);
        }
        // Retrieve the sales if neccessary
        //		db_conn((int)$date_month); // connect to the disired period
        db_connect();
        //		$sql = "SELECT * FROM \"$date_month\".pinvoices WHERE odate = '$date' $whe_sales ORDER BY invid ASC";
        $sql = "SELECT 'inv' AS type, invid, invnum, '0' AS noteid, '0' AS notenum, iprd AS prd\n\t\t\t\t\tFROM prd_pinvoices WHERE odate = '{$date}' {$whe_sales}\n\t\t\t\tUNION\n\t\t\t\tSELECT 'note' AS type, n.invid, n.invnum, n.noteid, n.notenum,p.prd::text AS prd\n\t\t\t\t\tFROM \"{$date_month}\".inv_notes n, cubit.payrec p \n\t\t\t\t\tWHERE n.noteid=p.note AND n.invnum=p.inv AND odate='{$date}' {$whe_sales}\n\t\t\t\tORDER BY invid ASC";
        $pinv_rslt = db_exec($sql) or errDie("Unable to retrieve the pos sales from Cubit.");
        $i = 0;
        $sales_out = "<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='6'>Sales</th>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th>Stock Code</th>\n\t\t\t\t<th width='10%'>Unit Price</th>\n\t\t\t\t<th width='5%'>Qty</th>\n\t\t\t\t<th width='10%'>Discount</th>\n\t\t\t\t<th width='5%'>VAT Code</th>\n\t\t\t\t<th width='10%'>Total</th>\n\t\t\t</tr>";
        $totals = array();
        while ($pinv_data = pg_fetch_array($pinv_rslt)) {
            //			db_conn((int)$date_month);
            db_conn($pinv_data['prd']);
            if ($pinv_data["type"] == "inv") {
                $sql = "SELECT * FROM pinv_items WHERE invid='{$pinv_data['invid']}'";
            } else {
                $sql = "SELECT * FROM inv_note_items WHERE noteid='{$pinv_data['noteid']}'";
            }
            $item_rslt = db_exec($sql) or errDie("Unable to retrieve pos sales items from Cubit.");
            $totals["unitcost"] = 0;
            $totals["disc"] = 0;
            $totals["amt"] = 0;
            if ($pinv_data["type"] == "inv") {
                $sales_out .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='6' align='left'>Invoice No: {$pinv_data['invnum']}</th>\n\t\t\t\t</tr>";
            } else {
                $sales_out .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='6' align='left'>Credit Node No: {$pinv_data['notenum']} (Invoice: {$pinv_data['invnum']})</th>\n\t\t\t\t</tr>";
            }
            while ($item_data = pg_fetch_array($item_rslt)) {
                db_conn("cubit");
                if ($item_data["stkid"] == "0") {
                    $stock_data["stkcod"] = $item_data["description"];
                } else {
                    $sql = "SELECT * FROM stock WHERE stkid='{$item_data['stkid']}'";
                    $stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock information from Cubit.");
                    $stock_data = pg_fetch_array($stock_rslt);
                }
                if ($pinv_data["type"] == "note") {
                    $item_data["unitcost"] = $item_data["amt"] / $item_data["qty"];
                }
                $vatcode_info = qryVatcode($item_data["vatcode"]);
                $sales_out .= "\n\t\t\t\t\t<tr bgcolor='" . bgcolor($i) . "'>\n\t\t\t\t\t\t<td>{$stock_data['stkcod']}</td>\n\t\t\t\t\t\t<td align='right'>" . sprint($item_data["unitcost"]) . "</td>\n\t\t\t\t\t\t<td align='center'>{$item_data['qty']}</td>\n\t\t\t\t\t\t<td align='right'>" . sprint($item_data["disc"]) . "</td>\n\t\t\t\t\t\t<td align='center'>{$vatcode_info['code']}</td>\n\t\t\t\t\t\t<td align='right'>" . sprint($item_data["amt"]) . "</td>\n\t\t\t\t\t</tr>";
                // Add to the totals
                $totals["unitcost"] += $item_data["unitcost"];
                $totals["disc"] += $item_data["disc"];
                $totals["amt"] += $item_data["amt"];
            }
            $sales_out .= "\n\t\t\t\t<tr bgcolor='" . bgcolor($i) . "'>\n\t\t\t\t\t<td>&nbsp</td>\n\t\t\t\t\t<td align='right'><b>" . sprint($totals["unitcost"]) . "</b></td>\n\t\t\t\t\t<td>&nbsp</td>\n\t\t\t\t\t<td align='right'><b>" . sprint($totals["disc"]) . "</b></td>\n\t\t\t\t\t<td>&nbsp</td>\n\t\t\t\t\t<td align='right'><b>" . sprint($totals["amt"]) . "</b></td>\n\t\t\t\t</tr>";
        }
        /* credit notes */
        // Retrieve the sales if neccessary
        db_conn((int) $date_month);
        // connect to the disired period
        $sql = "SELECT n.* FROM \"{$date_month}\".inv_notes n, cubit.payrec p \n\t\t\t\tWHERE n.noteid=p.note AND n.invnum=p.inv AND odate='{$date}' {$whe_sales}\n\t\t\t\tORDER BY noteid ASC";
        /*$pinv_rslt = db_exec($sql) or errDie("Unable to retrieve the pos sales from Cubit.");
        		
        		while ($pinv_data = pg_fetch_array($pinv_rslt)) {
        			db_conn((int)$date_month);
        			$sql = "SELECT * FROM inv_note_items WHERE noteid='$pinv_data[noteid]'";
        			$item_rslt = db_exec($sql) or errDie("Unable to retrieve pos sales items from Cubit.");
        
        			$totals["unitcost"] = 0;
        			$totals["disc"] = 0;
        			$totals["amt"] = 0;
        
        			$sales_out .= "
        						<tr>
        							<th colspan='6' align='left'>Credit Node No: $pinv_data[notenum] (Invoice: $pinv_data[invnum])</th>
        						</tr>";
        
        			while ($item_data = pg_fetch_array($item_rslt)) {
        				db_conn("cubit");
        				$sql = "SELECT * FROM stock WHERE stkid='$item_data[stkid]'";
        				$stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock information from Cubit.");
        				$stock_data = pg_fetch_array($stock_rslt);
        				
        				$item_data["unitcost"] = $item_data["amt"] / $item_data["qty"];
        
        				$sales_out .= "
        					<tr bgcolor='".bgcolor($i)."'>
        						<td>$stock_data[stkcod]</td>
        						<td align='right'>".sprint($item_data["unitcost"])."</td>
        						<td align='center'>$item_data[qty]</td>
        						<td align='right'>".sprint($item_data["disc"])."</td>
        						<td align='center'>$item_data[vatcode]</td>
        						<td align='right'>".sprint($item_data["amt"])."</td>
        					</tr>";
        
        				// Add to the totals
        				$totals["unitcost"] += $item_data["unitcost"];
        				$totals["disc"] += $item_data["disc"];
        				$totals["amt"] += $item_data["amt"];
        			}
        			$sales_out .= "
        				<tr bgcolor='".bgcolor($i)."'>
        					<td>&nbsp</td>
        					<td align='right'><b>".sprint($totals["unitcost"])."</b></td>
        					<td>&nbsp</td>
        					<td align='right'><b>".sprint($totals["disc"])."</b></td>
        					<td>&nbsp</td>
        					<td align='right'><b>".sprint($totals["amt"])."</b></td>
        				</tr>";
        		}*/
        $sales_out .= "</table>";
    } else {
        $sales_out = "";
    }
    db_conn("cubit");
    $sql = "SELECT * FROM payrec WHERE date='{$date}' {$whe}";
    $rslt = db_exec($sql) or errDie("Unable to retrieve pos report from Cubit.");
    $cash = $cheque = $credit_card = $credit = $sales = 0;
    while ($rec_data = pg_fetch_array($rslt)) {
        switch (strtolower($rec_data["method"])) {
            case "cash":
                $cash += $rec_data["amount"];
                break;
            case "cheque":
                $cheque += $rec_data["amount"];
                break;
            case "credit card":
                $credit_card += $rec_data["amount"];
                break;
            case "credit":
                $credit += $rec_data["amount"];
                break;
        }
        $sales += $rec_data["amount"];
    }
    db_conn('cubit');
    $Sl = "SELECT sum(amount) FROM payrec WHERE date='{$date}' {$whe}";
    $Ry = db_exec($Sl) or errDie("Unable to get pos rec.");
    $data = pg_fetch_array($Ry);
    $amount = sprint($amount);
    $expected = sprint($amount + $sales);
    if (!$user) {
        $user_out = "All Users";
    } else {
        $user_out = $user;
    }
    $Report = "\n\t\t<h3>POS Report: {$date} - {$user_out}</h3>\n\t\t{$sales_out}\n\t\t<table " . TMPL_tblDflts . " style='width: 100%'>\n\t\t\t<tr>\n\t\t\t\t<th colspan=2>Report</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Starting Amount</td>\n\t\t\t\t<td align='right' width='10%'>" . CUR . " {$amount}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Cash</td>\n\t\t\t\t<td align='right' width='10%'>" . sprint($cash) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Cheque</td>\n\t\t\t\t<td align='right' width='10%'>" . sprint($cheque) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Credit Card</td>\n\t\t\t\t<td align='right' width='10%'>" . sprint($credit_card) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Credit</td>\n\t\t\t\t<td align='right' width='10%'>" . sprint($credit) . "</td>\n\t\t\t</td>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Expected Amount</td>\n\t\t\t\t<td align='right' width='10%'><b>" . CUR . " {$expected}</b></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<p>\n\t\t<center>\n\t\t<form method='post' action='" . SELF . "'>\n\t\t\t<input type='hidden' name='key' value='report'>\n\t\t\t<input type='hidden' name='met' value='{$met}'>\n\t\t\t<input type='hidden' name='date_day' value='{$date_day}'>\n\t\t\t<input type='hidden' name='date_month' value='{$date_month}'>\n\t\t\t<input type='hidden' name='date_year' value='{$date_year}'>\n\t\t\t<input type='hidden' name='user' value='{$user}'>\n\t\t\t<input type='hidden' name='amount' value='{$amount}'>\n\t\t\t<input type='hidden' name='disp_sales' value='{$disp_sales}' \\>\n\t\t\t<input type='submit' name='key' value='Print'>\n\t\t</form>\n\t\t</center>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr><th>Quick Links</th></tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='reporting/index-reports.php'>Financials</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='reporting/index-reports-other.php'>Other Reports</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t</tr>\n\t\t</table>";
    return $Report;
}
/**
 * returns vat amount from vatcode number
 *
 * @param int $id vatcode id
 * @return int
 */
function vatamountID($id)
{
    if (($vc = qryVatcode($id)) === false) {
        return TAX_VAT;
    } else {
        return $vc["vat_amount"];
    }
}