function slct($err = "")
{
    $defwhid = getSetting("DEF_WH");
    $warehouses = qryWarehouse();
    $whs = db_mksel($warehouses, "whid", $defwhid, "#whid", "(#whno) #whname");
    $stockcats = qryStockCat();
    $cats = db_mksel($stockcats, "catid", false, "#catid", "(#catcod) #cat");
    $stockclass = qryStockClass();
    $class = db_mksel($stockclass, "clasid", false, "#clasid", "#classname");
    $view = "\n\t\t\t\t<h3>Stock Sales Report</h3>\n\t\t\t\t{$err}\n\t\t\t\t<table cellpadding='5'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t<form action='" . SELF . "' method=post name=form>\n\t\t\t\t\t\t\t\t<input type=hidden name=key value=view>\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>Store</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align=center colspan=2>{$whs}</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>By Category</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center'>{$cats}</td>\n\t\t\t\t\t\t\t\t\t<td valign='bottom'><input type=submit name=cat value='View'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>By Classification</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center'>{$class}</td>\n\t\t\t\t\t\t\t\t\t<td valign='bottom'><input type='submit' name='class' value='View'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th colspan='2'>All Categories and Classifications</th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td align='center' colspan='2'><input type='submit' name='all' value='View All'></td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t\t<p>\n\t\t\t\t<table border=0 cellpadding='2' cellspacing='1' width='15%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Quick Links</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><a href='stock-add.php'>Add Stock</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
    return $view;
}
function enter()
{
    extract($_POST);
    $qry = new dbSelect("config", "trh", grp(m("where", "readonly='f'")));
    $qry->run();
    while ($row = $qry->fetch_array()) {
        if (!isset(${$row["name"]})) {
            ${$row["name"]} = array("desc" => $row["description"], "value" => $row["value"], "type" => $row["type"]);
        }
    }
    $display = array("General Settings" => array("INTERVAL", "MANAGEUSER"), "SMTP Settings" => array("SMTP_SERVER", "SMTP_USER", "SMTP_PASS", "SMTP_FROM"), "POP3 Settings" => array("POP3_SERVER", "POP3_USER", "POP3_PASS"));
    $cat = false;
    $OUT = "\n\t<h3>Transactioning Configuration</h3>\n\t<form method='post' action='" . SELF . "'>\n\t<input type='hidden' name='key' value='write' />\n\t<table " . TMPL_tblDflts . " width='400'>";
    $pc = false;
    foreach ($display as $cat => $cnames) {
        if ($cat != $pc) {
            if ($pc != false) {
                $OUT .= TBL_BR;
            }
            $OUT .= "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>{$cat}</th>\n\t\t\t</tr>";
        }
        $i = 0;
        foreach ($cnames as $vname) {
            if (!isset($vname)) {
                $vname = "";
            }
            $OUT .= "\n\t\t\t<input type='hidden' name='{$vname}[desc]' value='" . ${$vname}["desc"] . "' />\n\t\t\t<input type='hidden' name='{$vname}[type]' value='" . ${$vname}["type"] . "' />\n\t\t\t<tr bgcolor='" . bgcolor($i) . "'>\n\t\t\t\t<td>" . ${$vname}["desc"] . "</td>\n\t\t\t\t<td>";
            switch (${$vname}["type"]) {
                case "yn":
                    $OUT .= "\n\t\t\t\t\t<select name='{$vname}[value]'>\n\t\t\t\t\t\t<option value='y' " . (${$vname}["value"] != "n") . ">Yes</option>\n\t\t\t\t\t\t<option value='n' " . (${$vname}["value"] == "n") . ">Yes</option>\n\t\t\t\t\t</select>";
                    break;
                case "passwd":
                    $OUT .= "\n\t\t\t\t\t<input type='password' name='{$vname}[value]' value='" . ${$vname}["value"] . "' />";
                    break;
                case "ulist":
                    $uq = qryUsers();
                    $OUT .= db_mksel($uq, "{${'vname'}}[value]", ${'vname'}["value"], "#userid", "#username");
                    break;
                case "str":
                default:
                    $OUT .= "\n\t\t\t\t\t<input type='text' name='{$vname}[value]' value='" . ${$vname}["value"] . "' />";
                    break;
            }
            $OUT .= "\n\t\t\t\t</td>\n\t\t\t</tr>";
        }
    }
    $OUT .= "\n\t<tr>\n\t\t<td colspan='2' align='right'><input type='submit' value='Save' /></td>\n\t</tr>\n\t</table>\n\t</form>";
    return $OUT;
}
function enter($err = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["price"] = "0.00";
    $fields["cust_id"] = 0;
    $fields["vatcode"] = "2";
    $fields["vatinc"] = "inc";
    if (!isset($date_year)) {
        explodeDate(DATE_STD, $date_year, $date_month, $date_day);
    }
    extract($fields, EXTR_SKIP);
    db_connect();
    // 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']}'";
    $asgrp_rslt = db_exec($sql) or errDie("Unable to retrieve asset group.");
    $asgrp_data = pg_fetch_array($asgrp_rslt);
    // Retrieve customer
    $sql = "SELECT * FROM cubit.customers ORDER BY surname ASC";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
    $cust_sel = "<select name='cust_id' style='width: 100%'>";
    $cust_sel .= "<option " . fsel($cust_id == "-1") . " value='-1'>Cash Sale</option>";
    //$cust_sel.= "<option ".fsel($cust_id == "-2")." value='-2'>Ledger Account Sale</option>";
    $cust_sel .= "<optgroup label='Customer Sale'>";
    while ($cust_data = pg_fetch_array($cust_rslt)) {
        $sel = fsel($cust_id == $cust_data["cusnum"]);
        $cust_sel .= "\r\n\t\t<option {$sel} value='{$cust_data['cusnum']}'>\r\n\t\t\t{$cust_data['cusname']} {$cust_data['surname']}\r\n\t\t</option>";
    }
    $cust_sel .= "</optgroup>";
    $cust_sel .= "</select>";
    $cds = qryVatcode();
    $sel_vatcode = db_mksel($cds, "vatcode", $vatcode, "#id", "#code");
    if ($asset_data["nonserial"] == "1") {
        if (!isset($qty)) {
            $qty = $asset_data["serial2"];
        }
        $qtyinput = "\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Sell Units</td>\r\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='qty' value='{$qty}' /></td>\r\n\t\t\t\t\t\t</tr>";
    } else {
        $qtyinput = "<input type='hidden' name='qty' value='1' />";
    }
    $OUTPUT = "\r\n\t\t\t\t<center>\r\n\t\t\t\t<h3>Asset Sale</h3>\r\n\t\t\t\t{$err}\r\n\t\t\t\t<form method='POST' action='" . SELF . "'>\r\n\t\t\t\t\t<input type='hidden' name='key' value='confirm' />\r\n\t\t\t\t\t<input type='hidden' name='asset_id' value='{$asset_id}' />\r\n\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<th colspan='2'>Details</th>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Group</td>\r\n\t\t\t\t\t\t<td>{$asgrp_data['grpname']}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Serial Number</td>\r\n\t\t\t\t\t\t<td>{$asset_data['serial']}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>2nd Serial Number/Qty</td>\r\n\t\t\t\t\t\t<td>{$asset_data['serial2']}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Description</td>\r\n\t\t\t\t\t\t<td>{$asset_data['des']}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Customer</td>\r\n\t\t\t\t\t\t<td>{$cust_sel}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Date</td>\r\n\t\t\t\t\t\t<td>" . mkDateSelect("date", $date_year, $date_month, $date_day) . "</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Vat</td>\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t<input type='radio' name='vatinc' value='inc' " . fcheck($vatinc != "exc") . " /> Including\r\n\t\t\t\t\t\t\t<input type='radio' name='vatinc' value='exc' " . fcheck($vatinc == "exc") . " /> Excluding\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Vatcode</td>\r\n\t\t\t\t\t\t<td>{$sel_vatcode}</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t{$qtyinput}\r\n\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t<td>Selling Price/Asset Unit</td>\r\n\t\t\t\t\t\t<td nowrap='t'>\r\n\t\t\t\t\t\t\t" . CUR . " <input type='text' size='10' name='price' value='{$price}'>\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<td colspan='2' align='right'>\r\n\t\t\t\t\t\t\t<input type='submit' value='Confirm &raquo' />\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t</table>\r\n\t\t\t\t</form>\r\n\t\t\t\t</center>";
    return $OUTPUT;
}
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 enter($errors = "")
{
    global $_POST;
    extract($_POST);
    require "locale_codes.php";
    // Retrieve list of locales from the locales directory
    db_conn("cubit");
    $sql = "SELECT locale FROM users WHERE username='******'";
    $localeRslt = db_exec($sql) or errDie("Unable to retrieve user locale settings from Cubit.");
    $locale_user = pg_fetch_result($localeRslt, 0);
    define("LOCALE_DIR", "./locale");
    $h_dir = opendir(LOCALE_DIR);
    $ar_dir = array();
    while (false !== ($dir = readdir($h_dir))) {
        $ar_dir[] = $dir;
    }
    $locale_sel = "<select name='locale' style='width: 180px'>";
    foreach ($ar_dir as $locale_code) {
        if (is_dir(LOCALE_DIR . "/" . $locale_code) && preg_match("/[a-z]{2,2}_[A-Z]{2,2}/", $locale_code)) {
            if ($locale_code == $locale_user) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            $ar_locale = explode("_", $locale_code);
            // Retrieve the name of the langauge
            foreach ($ar_languages as $lang_name => $lang_code) {
                if ($ar_locale[0] == $lang_code) {
                    $language = $lang_name;
                }
            }
            // Retrieve the name of the country
            foreach ($ar_countries as $country_name => $country_code) {
                if ($ar_locale[1] == $country_code) {
                    $country = $country_name;
                }
            }
            $locale_sel .= "<option value='{$locale_code}' {$selected}>{$language} ({$country})</option>";
        }
    }
    $locale_sel .= "</select>";
    // Admin Settings
    db_conn("cubit");
    $sql = "SELECT admin FROM users WHERE username='******'";
    $admRslt = db_exec($sql) or errDie("Unable to retrieve user information from Cubit.");
    $adm = pg_fetch_result($admRslt, 0);
    if ($adm == 1) {
        // Retrieve a list of all of the current company's usernames
        db_conn("cubit");
        $sql = "SELECT * FROM users ORDER BY username ASC";
        $usersRslt = db_exec($sql) or errDie("Unable to retrieve a list of usernames from Cubit.");
        $usernames = "<select name='username' style='width: 180px'>";
        while ($usrData = pg_fetch_array($usersRslt)) {
            if ($usrData["username"] == USER_NAME) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            $usernames .= "<option value='{$usrData['userid']}' {$selected}>{$usrData['username']}</option>";
        }
        $usernames .= "</select>";
        // Create a dropdown from the list of locales
        //$ar_dir = scandir(LOCALE_DIR);
        $ar_dir = array();
        $h_localdir = opendir(LOCALE_DIR);
        while (false !== ($file = readdir($h_localdir))) {
            $ar_dir[] = $file;
        }
        $dlocale_sel = "<select name='dlocale' style='width: 180px'>";
        $defloc = getCSetting("LOCALE_DEFAULT");
        foreach ($ar_dir as $dlocale_code) {
            if (is_dir(LOCALE_DIR . "/" . $dlocale_code) && preg_match("/[a-z]{2,3}_[A-Z]{2,3}/", $dlocale_code)) {
                $ar_dlocale = explode("_", $dlocale_code);
                // Retrieve the name of the langauge
                foreach ($ar_languages as $lang_name => $lang_code) {
                    if ($ar_dlocale[0] == $lang_code) {
                        $dlanguage = $lang_name;
                    }
                }
                // Retrieve the name of the country
                foreach ($ar_countries as $country_name => $country_code) {
                    if ($ar_dlocale[1] == $country_code) {
                        $dcountry = $country_name;
                    }
                }
                if ($defloc == $dlocale_code) {
                    $selected = "selected";
                } else {
                    $selected = "";
                }
                $dlocale_sel .= "<option value='{$dlocale_code}' {$selected}>{$dlanguage} ({$dcountry})</option>";
            }
        }
        $dlocale_sel .= "</select>";
        /* timezone setting */
        $timezone = getCSetting("LOCALE_TIMEZONE");
        $tzs = qryTimezone(false, "timezone, continent AS optgroup");
        $tzlist = db_mksel($tzs, "timezone", $timezone, "#timezone", "#timezone");
        $adm_settings = "\n\t\t<tr class='" . bg_class() . "'>\n\t\t  <td>Username</td>\n\t\t  <td>{$usernames}</td>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t  <td>" . COMP_NNAME . "'s default locale</td>\n\t\t  <td>{$dlocale_sel}</td>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t    <td>Timezone</td>\n\t\t    <td>{$tzlist}</td>\n\t\t</tr>";
    }
    /* locale enabled ? */
    db_conn("cubit");
    $sql = "SELECT locale_enable FROM users WHERE username='******'";
    $rslt = db_exec($sql) or errDie("Unable to retrieve locale setting.");
    $locale_enable = pg_fetch_result($rslt, 0);
    if ($locale_enable != "disabled") {
        $locen_che = "checked";
    } else {
        $locen_che = "";
    }
    // Layout
    $OUTPUT = "\n\t<h3>Locale Settings</h3>\n\t{$errors}\n\t<form method='post' action='" . SELF . "'>\n\t<input type='hidden' name='key' value='confirm'>\n\t<table " . TMPL_tblDflts . ">\n\t<tr>\n\t\t<th colspan=2>Settings</th>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td>Locale Enabled</td>\n\t\t<td><input type='checkbox' name='enable' value='enabled' {$locen_che}></td>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t<td>User's Locale</td>\n\t\t<td>{$locale_sel}</td>\n\t</tr>\n\t{$adm_settings}\n\t<tr>\n\t\t<td colspan=2 align=right><input type=submit value='Confirm &raquo'></td>\n\t</tr>\n\t</table>\n\t</form>" . mkQuickLinks(ql("locale-translate.php", "Translate Cubit"), ql("locale-settings.php", "Locale Settings"));
    return $OUTPUT;
}
function enter($err = "")
{
    extract($_REQUEST);
    $fields = grp(m("deptid", 0), m("accno", false), m("surname", ""), m("title", ""), m("location", ""), m("fcid", ""), m("category", 0), m("class", 0), m("init", ""), m("sales_rep", 0), m("paddr1", ""), m("addr1", ""), m("del_addr1", ""), m("comments", ""), m("vatnum", ""), m("contname", ""), m("bustel", ""), m("tel", ""), m("cellno", ""), m("fax", ""), m("email", ""), m("url", ""), m("pricelist", 0), m("traddisc", 0), m("setdisc", 0), m("chrgint", 0), m("overdue", 0), m("intrate", 0), m("o_year", date("Y")), m("o_month", date("m")), m("o_day", date("d")), m("credterm", 0), m("credlimit", ""), m("lead_source", 0), m("bankname", ""), m("branname", ""), m("brancode", ""), m("bankaccname", ""), m("bankaccno", ""), m("bankacctype", ""), m("team_id", 0), m("registration", ""));
    if (isset($cusnum)) {
        if ($cusnum == "-S") {
            return "<li class='err'>Invalid Customer</li><br><input type='button' value='[X] Close' onClick=\"window.close();\">";
        }
        $qry = new dbSelect("customers", "cubit", grp(m("where", "cusnum='{$cusnum}'")));
        $qry->run();
        if ($qry->num_rows() <= 0) {
            $OUT = "<li class='err'>Customer not found.</li>";
            return $OUT;
        }
        $c = $qry->fetch_array();
        $qry->free();
        /* split the date into the fields */
        list($c["o_year"], $c["o_month"], $c["o_day"]) = explode("-", $c["odate"]);
        foreach ($fields as $k => $v) {
            if (isset($c[$k])) {
                $fields[$k] = $c[$k];
            }
        }
        $cusid = "<input type='hidden' name='cusnum' value='{$cusnum}' />";
    } else {
        $cusid = "";
    }
    extract($fields, EXTR_SKIP);
    /* get next available account number */
    if ($accno === false) {
        $lastid = pglib_lastid("cubit.customers", "cusnum");
        $sql = "SELECT accno FROM cubit.customers WHERE cusnum = '{$lastid}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        if (pg_numrows($accRslt) < 1) {
            do {
                $lastid--;
                # get last account number
                $sql = "SELECT accno FROM cubit.customers WHERE cusnum = '{$lastid}' AND div = '" . USER_DIV . "'";
                $accRslt = db_exec($sql);
                if (pg_numrows($accRslt) < 1) {
                    $accno = "";
                    $naccno = "";
                } else {
                    $acc = pg_fetch_array($accRslt);
                    $accno = $acc['accno'];
                }
            } while (strlen($accno) < 1 && $lastid > 1);
        } else {
            $acc = pg_fetch_array($accRslt);
            $accno = $acc['accno'];
        }
        if (strlen($accno) > 0) {
            $num = preg_replace("/[^\\d]+/", "", $accno);
            $num++;
            $chars = preg_replace("/[\\d]/", "", $accno);
            $naccno = $chars . $num;
        } else {
            $naccno = 1;
        }
        $accno = $naccno;
    }
    /* customer categories */
    $qry = qryCategory();
    $cats = db_mksel($qry, "category", $category, "#catid", "#category");
    /* customer class */
    $qry = qryClass();
    $classlist = db_mksel($qry, "class", $class, "#clasid", "#classname");
    /* pricelists */
    $qry = qryPricelist();
    $pricelists = db_mksel($qry, "pricelist", $pricelist, "#listid", "#listname");
    /* customer departments */
    $qry = qryDepartment();
    $depts = db_mksel($qry, "deptid", $deptid, "#deptid", "#deptname");
    /* customer title */
    $get_titles = "SELECT title FROM titles ORDER BY title";
    $run_titles = db_exec($get_titles) or errDie("Unable to get title information.");
    if (pg_numrows($run_titles) < 1) {
        $titles = array("Mr" => "Mr", "Mrs" => "Mrs", "Miss" => "Miss");
    } else {
        $titles = array();
        while ($tarr = pg_fetch_array($run_titles)) {
            $titles[$tarr['title']] = $tarr['title'];
        }
    }
    $titles = extlib_cpsel("title", $titles, $title);
    /* credit terms */
    $qry = new dbSelect("ct", "exten", grp(m("where", "div='" . USER_DIV . "'")));
    $qry->run();
    while ($cd = $qry->fetch_array()) {
        $days[$cd['days']] = $cd['days'];
    }
    $credterms = extlib_cpsel("credterm", $days, $credterm);
    // unset so we can use same array
    unset($days);
    /* overdue periods */
    $qry = new dbSelect("od", "exten", grp(m("where", "div='" . USER_DIV . "'")));
    $qry->run();
    while ($cd = $qry->fetch_array()) {
        $days[$cd['days']] = $cd['days'];
    }
    $overdues = extlib_cpsel("overdue", $days, $overdue);
    /* customer is local/international */
    $locs = grp(m("loc", "Local"), m("int", "International"));
    $locsel = extlib_cpsel("location", $locs, $location);
    /* currency */
    $qry = qryCurrency();
    $currsel = db_mksel($qry, "fcid", $fcid, "#fcid", "#descrip");
    /* lead sources */
    $select_source = extlib_cpsel("lead_source", crm_get_leadsrc(-1), $lead_source);
    /* something from crm */
    if (isset($_GET["crm"])) {
        $ex = "<input type='hidden' name='crm' value='' />";
    } else {
        $ex = "";
    }
    /* sales rep selection */
    $qry = qrySalesPerson();
    $sales_reps = db_mksel($qry, "sales_rep", $sales_rep, "#salespid", "#salesp", "0:None");
    if (!isset($re)) {
        $re = "not";
    } else {
        $re = remval($re);
    }
    if (isset($cusnum)) {
        $bran = "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Branches</td>\n\t\t\t<td><input type='button' onClick=\"window.open('cust-branch-add.php?cusnum={$cusnum}','','width=380,height=300,status=1')\" value='Add Branch'><input type=button onClick=\"window.open('cust-branch-view.php?cusnum={$cusnum}','','width=500,height=400,status=1')\" value='View Branch'></td>\n\t\t</tr>";
    } else {
        $bran = "";
    }
    // Retrieve teams
    $sql = "SELECT * FROM crm.teams ORDER BY name ASC";
    $team_rslt = db_exec($sql) or errDie("Unable to retrieve teams.");
    $team_sel = "<select name='team_id'>";
    $team_sel .= "<option value='0'>[None]</option>";
    while ($team_data = pg_fetch_array($team_rslt)) {
        if ($team_id == $team_data["id"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $team_sel .= "<option value='{$team_data['id']}' {$sel}>{$team_data['name']}</option>";
    }
    $team_sel .= "</select>";
    // Layout
    $OUT = "\n\t<form action='" . SELF . "' method='post'>\n\t\t{$err}\n\t\t<input type='hidden' name='key' value='confirm' />\n\t\t<input type='hidden' name='re' value='{$re}' />\n\t\t{$ex}\n\t\t{$cusid}\n\t\t" . onthespot_passon() . "\n\t<table cellpadding='0' cellspacing='0'>\n\t\t<tr>\n\t\t\t<th colspan='2'>Add Customer : Customer Details</th>\n\t\t</tr>\n\t\t<tr valign='top'>\n\t\t\t<td>\n\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Department</td>\n\t\t\t\t\t<td>{$depts}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Acc No</td>\n\t\t\t\t\t<td><input type='text' size='20' name='accno' value='{$accno}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Company/Name</td>\n\t\t\t\t\t<td><input type='text' size='20' name='surname' value='{$surname}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Registration/ID</td>\n\t\t\t\t\t<td><input type='text' size='20' name='registration' value='{$registration}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Title {$titles}</td>\n\t\t\t\t\t<td>Initials <input type='text' size='15' name='init' value='{$init}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Type</td>\n\t\t\t\t\t<td>{$locsel}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Currency</td>\n\t\t\t\t\t<td>{$currsel}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("Categories are used to group customers. For example: PTA,JHB,CT") . ">\n\t\t\t\t\t<td>" . REQ . "Category</td>\n\t\t\t\t\t<td>{$cats}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("Classifications are used to group customers. For example: Wholesale,Retail") . ">\n\t\t\t\t\t<td>" . REQ . "Classification</td>\n\t\t\t\t\t<td>{$classlist}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Link to Sales rep</td>\n\t\t\t\t\t<td>{$sales_reps}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>" . REQ . "Postal Address</td>\n\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='paddr1'>{$paddr1}</textarea></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>\n\t\t\t\t\t\t" . REQ . "Physical Address<br>\n\t\t\t\t\t\t<font size='-2'>\n\t\t\t\t\t\t\t<input style='width: 11px; height: 11px;' type='checkbox' name='addr_same' " . (isset($addr_same) ? "checked='t'" : "") . " />\n\t\t\t\t\t\t\tSame As Postal Address\n\t\t\t\t\t\t</font>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='addr1'>{$addr1}</textarea></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Delivery Address</td>\n\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='del_addr1'>{$del_addr1}</textarea></td>\n\t\t\t\t</tr>\n\t\t\t\t{$bran}\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Team Permissions</td>\n\t\t\t\t\t<td>{$team_sel}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Comments</td>\n\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='comments'>{$comments}</textarea></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td>\n\t\t\t<td>\n\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "VAT Number</td>\n\t\t\t\t\t<td><input type='text' size='21' name='vatnum' value='{$vatnum}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Business Tel.</td>\n\t\t\t\t\t<td><input type='text' size='21' name='bustel' value='{$bustel}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Contact Name</td>\n\t\t\t\t\t<td><input type='text' size='21' name='contname' value='{$contname}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Home Tel.</td>\n\t\t\t\t\t<td><input type='text' size='21' name='tel' value='{$tel}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Cell No.</td>\n\t\t\t\t\t<td><input type='text' size='21' name='cellno' value='{$cellno}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Fax No.</td>\n\t\t\t\t\t<td><input type='text' size='21' name='fax' value='{$fax}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>E-mail</td>\n\t\t\t\t\t<td><input type='text' size='21' name='email' value='{$email}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Web Address</td>\n\t\t\t\t\t<td>http://<input type='text' size='30' name='url' value='{$url}' /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("When invoicing prices comes from the pricelist. Add more at stock settings.") . ">\n\t\t\t\t\t<td>" . REQ . "Price List</td>\n\t\t\t\t\t<td>{$pricelists}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td " . ass("This is the default discount on invoices, but can be changed per invoice") . ">Trade Discount &nbsp;<input type='text' size='6' name='traddisc' value='{$traddisc}' />%</td>\n\t\t\t\t\t<td>Settlement Discount <input type='text' size='7' name='setdisc' value='{$setdisc}' />%</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Charge Interest : Yes <input type='radio' name='chrgint' value='yes' " . ($chrgint == "yes" ? "checked='t'" : "") . " /> No<input type='radio' name='chrgint' value='no' " . ($chrgint != "yes" ? "checked='t'" : "") . " /></td>\n\t\t\t\t\t<td " . ass("Depending on interest settings, invoices older than this will get interest.") . ">Overdue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$overdues}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("Depending on interest settings, this is the interest this client will be charged.") . ">\n\t\t\t\t\t<td>Interest Rate</td>\n\t\t\t\t\t<td><input type='text' size='7' name='intrate' value='{$intrate}' />%</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Open Date</td>\n\t\t\t\t\t<td>" . mkDateSelect("o", $o_year, $o_month, $o_day) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Credit Term &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$credterms}</td>\n\t\t\t\t\t<td>Credit Limit: 0<input type='hidden' name='credlimit' value='0'/></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Lead Source</td>\n\t\t\t\t\t<td>{$select_source}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr><Td><br></td></tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<th colspan='2'> Bank Details</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Bank </td>\n\t\t\t\t\t<td><input type='text' size='20' name='bankname' value='{$bankname}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Branch</td>\n\t\t\t\t\t<td><input type='text' size='20' name='branname' value='{$branname}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Branch Code</td>\n\t\t\t\t\t<td><input type='text' size='20' name='brancode' value='{$brancode}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Name</td>\n\t\t\t\t\t<td><input type='text' size='20' name='bankaccname' value='{$bankaccname}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Number</td>\n\t\t\t\t\t<td><input type='text' size='20' name='bankaccno' value='{$bankaccno}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account Type</td>\n\t\t\t\t\t<td><input type='text' size='20' name='bankacctype' value='{$bankacctype}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2' align='right'><input type='submit' value='Confirm &raquo;' /></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</form>\n\t\t\t</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td align='center'>\n\t\t\t\t</table>" . mkQuickLinks(ql("customers-view.php", "View Customers"));
    return $OUT;
}
function enter($err = "")
{
    extract($_REQUEST);
    $fields = grp(m("deptid", 0), m("accno", false), m("surname", ""), m("title", ""), m("location", ""), m("fcid", ""), m("category", 0), m("class", 0), m("init", ""), m("sales_rep", 0), m("paddr1", ""), m("addr1", ""), m("del_addr1", ""), m("comments", ""), m("vatnum", ""), m("contname", ""), m("bustel", ""), m("tel", ""), m("cellno", ""), m("fax", ""), m("email", ""), m("url", ""), m("pricelist", 0), m("traddisc", 0), m("setdisc", 0), m("setdays", 1), m("chrgint", 0), m("overdue", 0), m("intrate", 0), m("o_year", date("Y")), m("o_month", date("m")), m("o_day", date("d")), m("credterm", 0), m("credlimit", ""), m("lead_source", 0), m("bankname", ""), m("branname", ""), m("brancode", ""), m("bankaccname", ""), m("bankaccno", ""), m("bankacctype", ""), m("team_id", 0), m("registration", ""), m("bankacct", ""), m("bankid", 0), m("blocked", "no"));
    if (isset($cusnum)) {
        if ($cusnum == "-S") {
            return "<li class='err'>Invalid Customer</li><br><input type='button' value='[X] Close' onClick=\"window.close();\">";
        }
        $qry = new dbSelect("customers", "cubit", grp(m("where", "cusnum='{$cusnum}'")));
        $qry->run();
        if ($qry->num_rows() <= 0) {
            $OUT = "<li class='err'>Customer not found.</li>";
            return $OUT;
        }
        $c = $qry->fetch_array();
        $qry->free();
        /* split the date into the fields */
        list($c["o_year"], $c["o_month"], $c["o_day"]) = explode("-", $c["odate"]);
        foreach ($fields as $k => $v) {
            if (isset($c[$k])) {
                $fields[$k] = $c[$k];
            }
        }
        $cusid = "<input type='hidden' name='cusnum' value='{$cusnum}' />";
    } else {
        $cusid = "";
    }
    extract($fields, EXTR_SKIP);
    /* get next available account number */
    if ($accno === false) {
        $lastid = pglib_lastid("cubit.customers", "cusnum");
        $sql = "SELECT accno FROM cubit.customers WHERE cusnum = '{$lastid}' AND div = '" . USER_DIV . "'";
        $accRslt = db_exec($sql);
        if (pg_numrows($accRslt) < 1) {
            do {
                $lastid--;
                # get last account number
                $sql = "SELECT accno FROM cubit.customers WHERE cusnum = '{$lastid}' AND div = '" . USER_DIV . "'";
                $accRslt = db_exec($sql);
                if (pg_numrows($accRslt) < 1) {
                    $accno = "";
                    $naccno = "";
                } else {
                    $acc = pg_fetch_array($accRslt);
                    $accno = $acc['accno'];
                }
            } while (strlen($accno) < 1 && $lastid > 1);
        } else {
            $acc = pg_fetch_array($accRslt);
            $accno = $acc['accno'];
        }
        if (strlen($accno) > 0) {
            $num = preg_replace("/[^\\d]+/", "", $accno);
            $num++;
            $chars = preg_replace("/[\\d]/", "", $accno);
            $naccno = $chars . $num;
        } else {
            $naccno = 1;
        }
        $accno = $naccno;
    }
    /* customer categories */
    $qry = qryCategory();
    $cats = db_mksel($qry, "category", $category, "#catid", "#category");
    /* customer class */
    $qry = qryClass();
    $classlist = db_mksel($qry, "class", $class, "#clasid", "#classname");
    /* pricelists */
    $qry = qryPricelist();
    $pricelists = db_mksel($qry, "pricelist", $pricelist, "#listid", "#listname");
    /* customer departments */
    $qry = qryDepartment();
    $depts = db_mksel($qry, "deptid", $deptid, "#deptid", "#deptname");
    /* customer title */
    $get_titles = "SELECT title FROM titles ORDER BY title";
    $run_titles = db_exec($get_titles) or errDie("Unable to get title information.");
    if (pg_numrows($run_titles) < 1) {
        $titles = array("Mr" => "Mr", "Mrs" => "Mrs", "Miss" => "Miss");
    } else {
        $titles = array();
        while ($tarr = pg_fetch_array($run_titles)) {
            $titles[$tarr['title']] = $tarr['title'];
        }
    }
    $titles = extlib_cpsel("title", $titles, $title);
    /* credit terms */
    $qry = new dbSelect("ct", "exten", grp(m("where", "div='" . USER_DIV . "'")));
    $qry->run();
    while ($cd = $qry->fetch_array()) {
        $days[$cd['days']] = $cd['days'];
    }
    $cred_days = array("0" => "0", "30" => "30", "60" => "60");
    $credterms = extlib_cpsel("credterm", $cred_days, $credterm);
    // unset so we can use same array
    unset($days);
    /* overdue periods */
    //	$qry = new dbSelect("od", "exten", grp(
    //		m("where", "div='".USER_DIV."'")
    //	));
    //	$qry->run();
    //	while ($cd = $qry->fetch_array()) {
    //		$days[$cd['days']] = $cd['days'];
    //	}
    //	$overdues = extlib_cpsel("overdue", $days, $overdue);
    /* customer is local/international */
    $locs = grp(m("loc", "Local"), m("int", "International"));
    $locsel = extlib_cpsel("location", $locs, $location);
    /* currency */
    $qry = qryCurrency();
    $currsel = db_mksel($qry, "fcid", $fcid, "#fcid", "#descrip");
    /* lead sources */
    $select_source = extlib_cpsel("lead_source", crm_get_leadsrc(-1), $lead_source);
    /* something from crm */
    if (isset($_GET["crm"])) {
        $ex = "<input type='hidden' name='crm' value='' />";
    } else {
        $ex = "";
    }
    /* sales rep selection */
    $qry = qrySalesPerson();
    $sales_reps = db_mksel($qry, "sales_rep", $sales_rep, "#salespid", "#salesp", "0:None");
    if (!isset($re)) {
        $re = "not";
    } else {
        $re = remval($re);
    }
    if (isset($cusnum)) {
        $bran = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Branches</td>\n\t\t\t\t<td><input type='button' onClick=\"window.open('cust-branch-add.php?cusnum={$cusnum}','','width=380,height=300,status=1')\" value='Add Branch'><input type=button onClick=\"window.open('cust-branch-view.php?cusnum={$cusnum}','','width=500,height=400,status=1')\" value='View Branch'></td>\n\t\t\t</tr>";
    } else {
        $bran = "";
    }
    // Retrieve teams
    $sql = "SELECT * FROM crm.teams ORDER BY name ASC";
    $team_rslt = db_exec($sql) or errDie("Unable to retrieve teams.");
    $team_sel = "<select name='team_id'>";
    $team_sel .= "<option value='0'>[None]</option>";
    while ($team_data = pg_fetch_array($team_rslt)) {
        if ($team_id == $team_data["id"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $team_sel .= "<option value='{$team_data['id']}' {$sel}>{$team_data['name']}</option>";
    }
    $team_sel .= "</select>";
    $get_accts = "SELECT * FROM bankacct ORDER BY accname";
    $run_accts = db_exec($get_accts) or errDie("Unable to get bank account information.");
    if (pg_numrows($run_accts) < 1) {
        $bankacct_drop = "";
    } else {
        $bankacct_drop = "<select name='bankid'>";
        while ($barr = pg_fetch_array($run_accts)) {
            if ($bankid == $barr['bankid']) {
                $bankacct_drop .= "<option value='{$barr['bankid']}' selected>({$barr['acctype']}) {$barr['accname']}</option>";
            } else {
                $bankacct_drop .= "<option value='{$barr['bankid']}'>({$barr['acctype']}) {$barr['accname']}</option>";
            }
        }
        $bankacct_drop .= "</select>";
    }
    $setdayssel1 = "";
    $setdayssel2 = "";
    $setdayssel3 = "";
    $setdayssel4 = "";
    $setdayssel5 = "";
    if (isset($setdays) and $setdays == "0") {
        $setdayssel1 = "selected";
    }
    if (isset($setdays) and $setdays == "1") {
        $setdayssel2 = "selected";
    }
    if (isset($setdays) and $setdays == "7") {
        $setdayssel3 = "selected";
    }
    if (isset($setdays) and $setdays == "15") {
        $setdayssel4 = "selected";
    }
    if (isset($setdays) and $setdays == "25") {
        $setdayssel5 = "selected";
    }
    //			<option $setdayssel1 value='0'>Last Day Of The Month</option>
    $setdays_drop = "\n\t\t<select name='setdays'>\n\t\t\t<option {$setdayssel2} value='1'>1st Day Of The Month</option>\n\t\t\t<option {$setdayssel3} value='7'>7th Of The Month</option>\n\t\t\t<option {$setdayssel4} value='15'>15th Of The Month</option>\n\t\t\t<option {$setdayssel5} value='25'>25th Of The Month</option>\n\t\t</select>";
    //Overdue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$overdues
    $display_piclist = "";
    $display_iframe = "";
    #check if this cust has any pics ...
    if (isset($cusnum) and strlen($cusnum) > 0) {
        #editing customer ... show frame if we have pics
        $get_pics = "SELECT * FROM display_images WHERE type = 'customer' AND ident_id = '{$cusnum}' LIMIT 1";
        $run_pics = db_exec($get_pics) or errDie("Unable to get customer images information.");
        if (pg_numrows($run_pics) < 1) {
            #no pics for this customer
            $display_iframe = "";
        } else {
            #compile listing for customer
            $get_piclist = "SELECT * FROM display_images WHERE type = 'customer' AND ident_id = '{$cusnum}'";
            $run_piclist = db_exec($get_piclist) or errDie("Unable to get customer images information.");
            if (pg_numrows($run_piclist) < 1) {
                #wth .. pic went missing somewhere ...
                #so nothing
            } else {
                $display_piclist = "\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<th>Picture Name</th>\n\t\t\t\t\t\t\t\t\t<th>View</th>\n\t\t\t\t\t\t\t\t\t<th>Remove</th>\n\t\t\t\t\t\t\t\t</tr>";
                while ($arr = pg_fetch_array($run_piclist)) {
                    $display_piclist .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td>{$arr['image_name']}</td>\n\t\t\t\t\t\t\t\t\t<td><a target='iframe1' href='view_image.php?picid={$arr['id']}'>View</a></td>\n\t\t\t\t\t\t\t\t\t<td><input type='checkbox' name='rempicid[{$arr['id']}]' value='yes'></td>\n\t\t\t\t\t\t\t\t</tr>";
                    #at least 1 picture for this customer
                    $display_iframe = "<tr><td colspan='2'><iframe name='iframe1' width='200' height='260' scrolling='false' marginwidth='0' marginheight='0' frameborder='0' src='view_image.php?picid={$arr['id']}'></iframe></td></tr>";
                }
                $display_piclist .= "\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
            }
        }
    }
    $show_back_button = "";
    $show_next_button = "";
    $showbuttons = "";
    if (isset($cusnum) and strlen($cusnum) > 0 and $cusnum != 0) {
        db_connect();
        #get 1 lower cusnum
        $get_prev = "SELECT cusnum FROM customers WHERE cusnum < '{$cusnum}' ORDER BY cusnum DESC LIMIT 1";
        $run_prev = db_exec($get_prev) or errDie("Unable to get previous customer information.");
        if (pg_numrows($run_prev) > 0) {
            $back_cusnum = pg_fetch_result($run_prev, 0, 0);
            $show_back_button = "<input type='button' onClick=\"document.location='customers-new.php?cusnum={$back_cusnum}';\" value='View Previous Customer'>";
        }
        $get_next = "SELECT cusnum FROM customers WHERE cusnum > '{$cusnum}' ORDER BY cusnum ASC LIMIT 1";
        $run_next = db_exec($get_next) or errDie("Unable to get next customer information.");
        if (pg_numrows($run_next) > 0) {
            $next_cusnum = pg_fetch_result($run_next, 0, 0);
            $show_next_button = "<input type='button' onClick=\"document.location='customers-new.php?cusnum={$next_cusnum}';\" value='View Next Customer'>";
        }
        $showbuttons = "{$show_back_button} {$show_next_button} <br><br>";
        $showcontact = "<input type='button' onClick=\"popupSized('conper-add.php?type=cust&id={$cusnum}','Add Additional Contact','880','580')\" value='Add Additional Contact'>";
    }
    //http://127.0.0.1/cubit3.33/conper-add.php?type=cust&id=2
    // Layout
    $OUT = "\n\t\t{$showbuttons}\n\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t{$err}\n\t\t\t<input type='hidden' name='key' value='confirm' />\n\t\t\t<input type='hidden' name='re' value='{$re}' />\n\t\t\t<input type='hidden' name='blocked' value='{$blocked}' />\n\t\t\t{$ex}\n\t\t\t{$cusid}\n\t\t\t" . onthespot_passon() . "\n\t\t<table cellpadding='0' cellspacing='0'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Add Customer : Customer Details</th>\n\t\t\t</tr>\n\t\t\t<tr valign='top'>\n\t\t\t\t<td>\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Department</td>\n\t\t\t\t\t\t<td>{$depts}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Acc No</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='accno' value='{$accno}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Company/Name</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='surname' value='{$surname}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Registration/ID</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='registration' value='{$registration}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Title {$titles}</td>\n\t\t\t\t\t\t<td>Initials <input type='text' size='15' name='init' value='{$init}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Type</td>\n\t\t\t\t\t\t<td>{$locsel}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Currency</td>\n\t\t\t\t\t\t<td>{$currsel}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "' " . ass("Categories are used to group customers. For example: PTA,JHB,CT") . ">\n\t\t\t\t\t\t<td>" . REQ . "Category</td>\n\t\t\t\t\t\t<td>{$cats}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "' " . ass("Classifications are used to group customers. For example: Wholesale,Retail") . ">\n\t\t\t\t\t\t<td>" . REQ . "Classification</td>\n\t\t\t\t\t\t<td>{$classlist}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Link to Sales rep</td>\n\t\t\t\t\t\t<td>{$sales_reps}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td valign='top'>" . REQ . " Postal Address</td>\n\t\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='paddr1'>{$paddr1}</textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td valign='top'>\n\t\t\t\t\t\t\t" . REQ . " Physical Address<br>\n\t\t\t\t\t\t\t<font size='-2'>\n\t\t\t\t\t\t\t\t<input style='width: 11px; height: 11px;' type='checkbox' name='addr_same' " . (isset($addr_same) ? "checked='t'" : "") . " />\n\t\t\t\t\t\t\t\tSame As Postal Address\n\t\t\t\t\t\t\t</font>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='addr1'>{$addr1}</textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td valign='top'>Delivery Address</td>\n\t\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='del_addr1'>{$del_addr1}</textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t{$bran}\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Team Permissions</td>\n\t\t\t\t\t\t<td>{$team_sel}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td valign='top'>Comments</td>\n\t\t\t\t\t\t<td valign='center'><textarea rows='4' cols='19' name='comments'>{$comments}</textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<th colspan='2'> Bank Details</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 </td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='bankname' value='{$bankname}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Branch</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='branname' value='{$branname}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Branch Code</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='brancode' value='{$brancode}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Account Name</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='bankaccname' value='{$bankaccname}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Account Number</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='bankaccno' value='{$bankaccno}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Account Type</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='bankacctype' value='{$bankacctype}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Bank Account To Use</td>\n\t\t\t\t\t\t<td>{$bankacct_drop}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td>\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "VAT Number</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='vatnum' value='{$vatnum}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Business Tel.</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='bustel' value='{$bustel}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Contact Name</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='text' size='21' name='contname' value='{$contname}' />\n\t\t\t\t\t\t\t{$showcontact}\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Home Tel.</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='tel' value='{$tel}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Cell No.</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='cellno' value='{$cellno}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Fax No.</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='fax' value='{$fax}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>E-mail</td>\n\t\t\t\t\t\t<td><input type='text' size='21' name='email' value='{$email}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Web Address</td>\n\t\t\t\t\t\t<td>http://<input type='text' size='30' name='url' value='{$url}' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "' " . ass("When invoicing prices comes from the pricelist. Add more at stock settings.") . ">\n\t\t\t\t\t\t<td>" . REQ . "Price List</td>\n\t\t\t\t\t\t<td>{$pricelists}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td " . ass("This is the default discount on invoices, but can be changed per invoice") . ">Trade Discount &nbsp;<input type='text' size='6' name='traddisc' value='{$traddisc}' />%</td>\n\t\t\t\t\t\t<td>Settlement Discount <input type='text' size='7' name='setdisc' value='{$setdisc}' />%</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>" . REQ . "Charge Interest : Yes <input type='radio' name='chrgint' value='yes' " . ($chrgint == "yes" ? "checked='t'" : "") . " /> No<input type='radio' name='chrgint' value='no' " . ($chrgint != "yes" ? "checked='t'" : "") . " /></td>\n\t\t\t\t\t\t<td>Statement Day {$setdays_drop}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td " . ass("Depending on interest settings, invoices older than this will get interest.") . ">Interest Rate <input type='text' size='7' name='intrate' value='{$intrate}' />%</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Account Open Date</td>\n\t\t\t\t\t\t<td>" . mkDateSelect("o", $o_year, $o_month, $o_day) . "</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Default Credit Term &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$credterms}</td>\n\t\t\t\t\t\t<td>Credit Limit <input type='text' size='7' name='credlimit' value='{$credlimit}'/></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Lead Source</td>\n\t\t\t\t\t\t<td>{$select_source}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t{$display_iframe}\n\t\t\t\t\t{$display_piclist}\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2' align='right'><input type='submit' value='Confirm &raquo;' /></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t\t</form>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t</table>" . mkQuickLinks(ql("customers-view.php", "View Customers"));
    return $OUT;
}
function selectcus($err = "")
{
    extract($_POST);
    require_lib("validate");
    $v = new validate();
    $v->isOk($bankid, "num", 1, 30, "Select Bank Account.");
    $v->isOk($date_day, "num", 1, 2, "Invalid Date day.");
    $v->isOk($date_month, "num", 1, 2, "Invalid Date month.");
    $v->isOk($date_year, "num", 1, 4, "Invalid Date Year.");
    $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($amt, "float", 1, 40, "Invalid amount.");
    if ($all == 0) {
        $all_desc = "Auto Allocation";
    } else {
        if ($all == 1) {
            $all_desc = "Allocate to Age Analysis";
        } else {
            if ($all == 2) {
                $all_desc = "Allocate to Each Invoice";
            } else {
                $v->addError("", "Invalid allocation method selected.");
            }
        }
    }
    if ($v->isError()) {
        return method($v->genErrors());
    }
    $date = mkdate($date_year, $date_month, $date_day);
    if (!isset($cusids)) {
        $cusids = array();
        $amts = array();
    }
    /* remove selected allocations */
    if (isset($rem)) {
        foreach ($rem as $k => $dummy) {
            if (isset($cusids[$k])) {
                unset($cusids[$k]);
                unset($amts[$k]);
            }
        }
    }
    /* add the newly entered amount to the array */
    if (isset($btn_new)) {
        if ($new_amt != "0.00" && !empty($new_amt) && $v->isOk($new_amt, "float", 1, 1, "") && $v->isOk($new_cus, "num", 1, 10, "")) {
            $cusids[] = $new_cus;
            $amts[] = $new_amt;
            $new_cus = false;
            $new_amt = "0.00";
        } else {
            $err = "<li class='err'>Invalid value entered/selected</li>";
        }
    }
    if (sprint(array_sum($amts)) > sprint($amt)) {
        $err = "<li class='err'>Sum of debtor amounts exceeds receipt amount</li>";
    }
    if (isset($_POST["btn_back"])) {
        return method();
    }
    if (empty($err) && (isset($btn_new) || isset($btn_update))) {
        if (sprint(array_sum($amts)) == sprint($amt)) {
            $_POST["amts"] = $amts;
            $_POST["cusids"] = $cusids;
            return confirm();
        }
    }
    if (!isset($new_cus)) {
        $new_cus = "";
    }
    if (!isset($new_amt)) {
        $new_amt = "";
    }
    db_connect();
    #get bank account info
    $get_bank = "SELECT * FROM bankacct WHERE bankid = '{$bankid}' LIMIT 1";
    $run_bank = db_exec($get_bank) or errDie("Unable to get banking information.");
    if (pg_numrows($run_bank) < 1) {
        $bank = array();
        $bank['accname'] = "";
        $bank['bankname'] = "";
    } else {
        $bank = pg_fetch_array($run_bank);
    }
    $qry = qryCustomer();
    $sel_cust = db_mksel($qry, "new_cus", $new_cus, "#cusnum", "#surname, #cusname");
    $OUT = "\n\t\t<h3>New Bank Receipt</h3>\n\t\t<form method='POST' action='" . SELF . "'>\n\t\t\t<input type='hidden' name='key' value='selectcus'>\n\t\t\t<input type='hidden' name='accnum' value='' />\n\t\t\t<input type='hidden' name='bankid' value='{$bankid}' />\n\t\t\t<input type='hidden' name='date' value='{$date}' />\n\t\t\t<input type='hidden' name='all' value='{$all}' />\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='descript' value='{$descript}' />\n\t\t\t<input type='hidden' name='reference' value='{$reference}' />\n\t\t\t<input type='hidden' name='cheqnum' value='{$cheqnum}' />\n\t\t\t<input type='hidden' name='amt' value='{$amt}' />\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Receipt 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>Description</td>\n\t\t\t\t<td valign='center'>" . nl2br($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>Allocation</td>\n\t\t\t\t<td valign='center'>{$all_desc}</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>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Amount Unallocated</td>\n\t\t\t\t<td valign='center'>" . CUR . " " . sprint($amt - array_sum($amts)) . "</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<br />\n\t\t{$err}\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td colspan='3' align='right'>\n\t\t\t\t\t<input type='submit' name='btn_back' value='&laquo; Back' />\n\t\t\t\t\t| <input type='submit' name='btn_update' value='Update' />\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th>Customer</th>\n\t\t\t\t<th>Amount</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$sel_cust}</td>\n\t\t\t\t<td><input type='text' size='6' name='new_amt' value='{$new_amt}' />\n\t\t\t\t<td><input type='submit' name='btn_new' value='Add &raquo;' /></td>\n\t\t\t</tr>";
    foreach ($cusids as $k => $cusid) {
        $ci = qryCustomer($cusid);
        $OUT .= "\n\t\t\t<input type='hidden' name='cusids[{$k}]' value='{$cusid}' />\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$ci['surname']}, {$ci['cusname']}</td>\n\t\t\t\t<td><input type='text' size='6' name='amts[{$k}]' value='{$amts[$k]}' />\n\t\t\t\t<td><input type='checkbox' name='rem[{$k}]' /></td>\n\t\t\t</tr>";
    }
    $OUT .= "\n\t\t\t<tr>\n\t\t\t\t<td colspan='3' align='right'>\n\t\t\t\t\t<input type='submit' name='btn_back' value='&laquo; Back' />\n\t\t\t\t\t| <input type='submit' name='btn_update' value='Update' />\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>" . mkQuickLinks(ql("trans-new.php", "Journal Transactions"), ql("../customers-view.php", "View Customers (New Window)", true));
    return $OUT;
}
/**
 * @ignore
 */
function stock($tran, $cacc, $refnum, $day, $mon, $year)
{
    $stk = qryStock();
    $stks = db_mksel($stk, "stkid", false, "#stkid", "(#stkcod) #stkdes");
    if ($tran == "dt") {
        $dtct = "inc";
    } else {
        $dtct = "dec";
    }
    $OUT = "\n\t<h3>You Selected a Stock Control account</h3>\n\t<h4>Select Stock Item</h4>\n\t<form action='" . relpath("stock-balance.php") . "' method='post'>\n\t<input type='hidden' name='entry' value='{$dtct}' />\n\t<input type='hidden' name='caccid' value='{$cacc}' />\n\t<table " . TMPL_tblDflts . " width='300'>\n\t<tr>\n\t\t<th>Field</th>\n\t\t<th>Value</th>\n\t</tr>\n\t<tr class='" . bg_class() . "'>\n\t\t<td valign='top'>Select Stock Item</td>\n\t\t<td>{$stks}</td>\n\t</tr>\n\t<tr>\n\t\t<td align='center'><input type='button' value='&laquo Back' onClick='javascript:history.back()' /></td>\n\t\t<td align='center'><input type='submit' value='Continue &raquo;' /></td>\n\t</tr>\n\t</table>\n\t</form>";
    return $OUT;
}