function report_options()
{
    $rslt = qrySalesPerson(false, "salesp");
    if ($rslt->num_rows() < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='sp'>";
        $salesps .= "<option value='0'>All</option>";
        while ($salesp = $rslt->fetch_array()) {
            $salesps .= "<option value='{$salesp['salesp']}'>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    $repops = "\n\t\t<h3>Commission Report</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='report'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='3'>Select Sales Rep</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Sales Rep</td>\n\t\t\t\t<td colspan='2'>{$salesps}</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<th colspan='3'>Select Report Date Range</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>" . mkDateSelect("from", date("Y"), date("m"), "01") . "</td>\n\t\t\t\t<td>To</td>\n\t\t\t\t<td>" . mkDateSelect("to") . "</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td valign='bottom' align='right' colspan='3'><input type='submit' value='Search &raquo;'></td>\n\t\t\t</tr>\n\t\t</form>\n\t\t</table>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . " width='15%'>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<th>Quick Links</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='toms/salesp-add.php'>Add Sales Person</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='sales-reports.php'>Sales 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 $repops;
}
function printSalesp()
{
    $OUT = "\n    <h3>Sales People</h3>\n    <table " . TMPL_tblDflts . ">\n    <tr>\n    \t<th>Number</th>\n    \t<th>Sales Person</th>\n    \t<th>Commission</th>\n    </tr>";
    $sp = qrySalesPerson();
    while ($salesp = $sp->fetch_array()) {
        $bgColor = bgcolorg();
        if ($salesp["com"] != 0) {
            $com_disp = "{$salesp['com']} %";
        } else {
            $com_disp = "Using Commission Set Under Stock Settings";
        }
        $OUT .= "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>{$salesp['salespno']}</td>\n\t\t\t<td>{$salesp['salesp']}</td>\n\t\t\t<td>{$com_disp}</td>\n\t\t\t<td><a href='salesp-edit.php?salespid={$salesp['salespid']}'>Edit</a></td>\n\t\t\t<td><a href='salesp-rem.php?salespid={$salesp['salespid']}'>Remove</a></td>\n\t\t</tr>";
    }
    $OUT .= "\n\t</table>\n\t<br />";
    $OUT .= mkQuickLinks(ql("salesp-add.php", "Add Sales Person"));
    return $OUT;
}
function approve()
{
    extract($_REQUEST);
    if (!isset($approve) || $approve != "i" && $approve != "a") {
        invalid_use("<li class='err'>Invalid action.<li>");
    }
    /* order info */
    $qry = new dbSelect("recvpurch", "trh", grp(m("where", wgrp(m("id", $id)))));
    $qry->run();
    if ($qry->num_rows() <= 0) {
        invalid_use("<li class='err'>Invalid Sales Order Id (TRHAPP).</li>");
    }
    $soi = $qry->fetch_array();
    /* customer info */
    $ci = qryCustomer($soi["custid"]);
    /* sales person name */
    if (empty($ci["sales_rep"])) {
        $speoples = qrySalesPerson();
        if ($speoples->num_rows() <= 0) {
            $salespn = "General";
        } else {
            $speoples->fetch_array();
            $salespn = $speoples->d["salesp"];
            $speoples->free();
        }
    } else {
        $m = qrySalesPerson($ci["sales_rep"]);
        $salespn = $m["salesp"];
    }
    /* currency info */
    $curinfo = qryCurrency($ci["fcid"]);
    $cols = grp(m("deptid", "{$ci['deptid']}"), m("cusnum", "{$ci['cusnum']}"), m("cordno", ""), m("ordno", ""), m("chrgvat", $soi["vatinc"] == "yes" ? "inc" : "no"), m("terms", "{$ci['credterm']}"), m("salespn", $salespn), m("odate", "{$soi['pdate']}"), m("accepted", "n"), m("comm", ""), m("done", "y"), m("username", USER_NAME), m("deptname", "{$ci['deptname']}"), m("cusacc", "{$ci['accno']}"), m("cusname", "{$ci['cusname']}"), m("surname", "{$ci['surname']}"), m("cusaddr", "{$ci['addr1']}"), m("cusordno", "{$soi['purnum']}"), m("cusvatno", "{$ci['vatnum']}"), m("prd", "0"), m("div", USER_DIV), m("disc", "0.00"), m("discp", "0.00"), m("delchrg", "{$soi['shipchrg']}"), m("subtot", "{$soi['subtot']}"), m("traddisc", "0.00"), m("balance", "{$soi['balance']}"), m("vat", "{$soi['vat']}"), m("total", "{$soi['total']}"), m("jobid", "0"), m("jobnum", "0"), m("dir", ""), m("location", ""), m("fcid", "{$ci['fcid']}"), m("currency", "{$curinfo['symbol']}"), m("xrate", "{$curinfo['rate']}"), m("fbalance", "0.00"), m("fsubtot", "0.00"), m("discount", "0.00"), m("delivery", "{$soi['shipchrg']}"), m("delvat", "{$soi['delvat']}"), m("display_costs", "yes"), m("proforma", "no"), m("pinvnum", "0"));
    $upd = new dbUpdate("sorders", "cubit", $cols);
    $upd->run(DB_INSERT);
    $sordid = $upd->lastvalue("sordid");
    /* items */
    $qry->reset();
    $qry->setTable("recvpurch_items");
    $qry->setOpt(grp(m("where", wgrp(m("recvpurch_id", $id)))));
    $qry->run();
    $upd->setTable("sorders_items");
    while ($row = $qry->fetch_array()) {
        if (empty($row["sup_stkcod"])) {
            invalid_use("Supplier stock codes not setup for customer. Order ignored.");
        }
        $stkinfo = qryStockC($row["sup_stkcod"]);
        $cols = grp(m("sordid", "{$sordid}"), m("whid", "{$stkinfo['whid']}"), m("stkid", "{$stkinfo['stkid']}"), m("qty", "{$row['qty']}"), m("div", USER_DIV), m("amt", "{$row['amt']}"), m("discp", "0.00"), m("disc", "0.00"), m("unitcost", "{$row['unitcost']}"), m("hidden", ""), m("funitcost", "0.00"), m("famt", "0.00"), m("pinv", "0.00"), m("vatcode", "{$stkinfo['vatcode']}"), m("description", "{$stkinfo['stkdes']}"), m("account", "0"));
        $upd->setOpt($cols);
        $upd->run(DB_INSERT);
    }
    /* set approve status */
    $upd->reset();
    $upd->setTable("recvpurch", "trh");
    $upd->setOpt(grp(m("approved", "{$approve}")), "id='{$id}'");
    $upd->run(DB_UPDATE);
    /* get customer trh config */
    $keyinfo = trhKeyCust($soi["custid"]);
    $email = $keyinfo["email"];
    /* send trh response message */
    $purinfo = array("purid" => $soi["purid"], "status" => $approve);
    $ret = send_trhmsg("cust", $soi["custid"], $email, "rsppur", $purinfo);
    if ($approve == "i") {
        header("Location: ../sorder-accept.php?sordid={$sordid}");
        exit;
    } else {
        $OUT = listorders("<li class='err'>Successfully approved sales order.</li>");
    }
    return $OUT;
}
function validate($AR)
{
    extract($AR);
    require_lib("validate");
    $v = new validate();
    $odate = mkdate($o_year, $o_month, $o_day);
    $v->isOk($deptid, "num", 1, 255, "Invalid Department.");
    $v->isOk($accno, "string", 1, 20, "Invalid Account number.");
    $v->isOk($surname, "string", 0, 255, "Invalid surname/company.");
    $v->isOk($title, "string", 0, 10, "Invalid title.");
    $v->isOk($init, "string", 0, 10, "Invalid initials.");
    $v->isOk($location, "string", 1, 3, "Invalid Type.");
    $v->isOk($fcid, "num", 1, 30, "Invalid Currency.");
    $v->isOk($category, "num", 1, 255, "Invalid Category.");
    $v->isOk($class, "num", 1, 255, "Invalid Classification.");
    $v->isOk($paddr1, "string", 1, 255, "Invalid customer postal address.");
    if (!isset($addr_same)) {
        $v->isOk($addr1, "string", 1, 255, "Invalid customer physical address.");
    }
    $v->isOk($del_addr1, "string", 0, 255, "Invalid customer delivery address.");
    $v->isOk($comments, "string", 0, 255, "Invalid characters in comment.");
    $v->isOk($vatnum, "string", 1, 255, "Invalid customer vat number.");
    $v->isOk($registration, "string", 1, 255, "Invalid registration/id number.");
    $v->isOk($contname, "string", 0, 255, "Invalid contact name.");
    $v->isOk($bustel, "string", 1, 20, "Invalid Bussines telephone.");
    $v->isOk($tel, "string", 0, 20, "Invalid Home telephone.");
    $v->isOk($cellno, "string", 0, 20, "Invalid Cell number.");
    $v->isOk($fax, "string", 0, 20, "Invalid Fax number.");
    $v->isOk($email, "email", 0, 255, "Invalid email address.");
    $v->isOk($url, "url", 0, 255, "Invalid web address.");
    $v->isOk($traddisc, "float", 0, 20, "Invalid trade discount.");
    $v->isOk($setdisc, "float", 0, 20, "Invalid settlement discount.");
    $v->isOk($pricelist, "num", 1, 20, "Invalid price list.");
    $v->isOk($chrgint, "string", 1, 4, "Invalid Charge interest option.");
    $v->isOk($overdue, "float", 0, 20, "Invalid overdue.");
    $v->isOk($intrate, "float", 1, 20, "Invalid interest rate.");
    $v->isOk($credterm, "num", 0, 20, "Invalid Credit term.");
    $v->isOk($odate, "date", 1, 14, "Invalid account open date.");
    $v->isOk($credlimit, "float", 0, 11, "Invalid credit limit.");
    /* CRM CODE */
    $v->isOk($lead_source, "num", 0, 9, "Invalid lead source selected.");
    $v->isOk($bankname, "string", 0, 20, "Invalid Bank Name.");
    $v->isOk($branname, "string", 0, 20, "Invalid Branch Name.");
    $v->isOk($brancode, "string", 0, 20, "Invalid Branch Code.");
    $v->isOk($bankaccname, "string", 0, 50, "Invalid Bank Account Name.");
    $v->isOk($bankaccno, "string", 0, 50, "Invalid Bank Account Number.");
    $v->isOk($bankacctype, "string", 0, 50, "Invalid Bank Account Type.");
    if (is_string($sales_rep) and strlen($sales_rep) > 0) {
        $qry = qrySalesPerson($sales_rep);
        if ($qry === false) {
            $v->addError(0, "The selected sales rep does not exist in Cubit.");
        }
    } else {
        $v->addError(0, "Invalid sales rep selection.");
    }
    if ($v->isError()) {
        return $v->genErrors();
    } else {
        return false;
    }
}