Ejemplo n.º 1
0
/**
 * locates a supplier by name, if not found creates and returns id
 *
 * @param string $name company name
 * @return int
 */
function locateSupplier($name)
{
    $qry = new dbSelect("suppliers", "cubit", grp(m("cols", "supid"), m("where", "lower(supname)=lower('{$name}')")));
    $qry->run();
    if ($qry->num_rows() > 0) {
        $id = $qry->fetch_result();
    } else {
        // insert into new supps
        $id = -1;
    }
    $qry->free();
    return $id;
}
Ejemplo n.º 2
0
function checknum()
{
    extract($_REQUEST);
    require_lib("validate");
    $v = new Validate();
    $v->isOk($topacc, "num", 4, 4, "Invalid Main Part.");
    $v->isOk($accnum, "num", 3, 3, "Invalid Sub Part.");
    /* is account number valid */
    if ($v->isError()) {
        $e = $v->getErrors();
        if (count($e) == 2) {
            $err = "Invalid account number.";
        } else {
            $err = $e[0]["msg"];
        }
    } else {
        /* does account number exist */
        $qry = new dbSelect("accounts", "core", grp(m("cols", "accname"), m("where", "topacc='{$topacc}' AND accnum='{$accnum}'"), m("limit", "1")));
        $qry->run();
        if (!isset($rslt)) {
            $rslt = array();
        }
        if ($qry->num_rows($rslt) > 0) {
            $accname = $qry->fetch_result();
            $err = "Account number in use: {$accname}.";
        } else {
            if ($accnum != "000") {
                $qry->setOpt(grp(m("where", "topacc='{$topacc}'")));
                $qry->run();
                if ($qry->num_rows() <= 0) {
                    $err = "Main Account doesn't exist.";
                }
            }
        }
    }
    if (!isset($err)) {
        $err = "<strong>Account number valid.</strong>";
    } else {
        $err = "<li class='err'>{$err}</li>";
    }
    return $err;
}
/**
 * returns stkid for suppliers stock code.
 *
 * returns false if no stkcod for supplier.
 *
 * @param int $suppid
 * @param int $stkcod
 * @return string
 */
function suppStkid($suppid, $stkcod)
{
    $qry = new dbSelect("suppstock", "cubit", grp(m("cols", "stkid"), m("limit", 1), m("where", "suppid='{$suppid}' AND stkcod='{$stkcod}'")));
    $qry->run();
    return $qry->fetch_result();
}
function import($frm)
{
    /* @var $frm cForm */
    if ($frm->validate("import")) {
        return view($frm);
    }
    /* get field indexes */
    $stkcod = false;
    $price = false;
    foreach ($_REQUEST["fld"] as $fi => $ft) {
        if ($ft != "ignore") {
            ${$ft} = $fi;
        }
    }
    /* import file if all field types specified */
    if ($stkcod === false || $price === false) {
        $frm->setmsg("<li class='err'>Not all field types satisfied</li>");
    } else {
        $qry = new dbSelect("spricelist", "exten", grp(m("cols", "listid"), m("where", "suppid='{$_REQUEST['supid']}'")));
        $qry->run();
        if ($qry->num_rows() <= 0) {
            $suppinfo = qrySupplier($_REQUEST["supid"]);
            $cols = grp(m("suppid", $_REQUEST["supid"]), m("listname", $suppinfo["supname"]), m("div", USER_DIV));
            $upd = new dbUpdate("spricelist", "exten", $cols);
            $upd->run(DB_INSERT);
            $listid = $upd->lastid("listid");
        } else {
            $listid = $qry->fetch_result();
        }
        $upd = new dbDelete("splist_prices", "exten", "listid='{$listid}'");
        $upd->run();
        $upd = new dbUpdate("splist_prices", "exten");
        $invalid_fields = array();
        $nosuch_fields = array();
        $file = ucfs::file("supplist");
        foreach ($file as $rd) {
            $ri = explode(",", $rd);
            $ri[$stkcod] = trim($ri[$stkcod]);
            $ri[$price] = trim($ri[$price]);
            if (cForm::validateValue($ri[$stkcod], "string", 1, 250) || cForm::validateValue($ri[$price], "float", 1, 40)) {
                $invalid_fields[] = $ri[$stkcod];
                continue;
            }
            $stkid = suppStkid($_REQUEST["supid"], $ri[$stkcod]);
            if ($stkid === false) {
                $stkinfo = array("stkid" => "0", "catid" => "0", "prdcls" => "0");
            } else {
                $stkinfo = qryStock($stkid, "stkid, catid, prdcls");
            }
            if (!isset($_REQUEST["vatinc"])) {
                $ri[$price] += $ri[$price] * TAX_VAT / 100;
            }
            $cols = grp(m("listid", $listid), m("stkid", $stkinfo["stkid"]), m("catid", $stkinfo["catid"]), m("clasid", $stkinfo["prdcls"]), m("price", $ri[$price]), m("div", USER_DIV), m("supstkcod", $ri[$stkcod]));
            $upd->setCols($cols);
            $upd->run();
        }
        if (count($invalid_fields) > 0) {
            $msg = "<br />The following items weren't imported because they contain\n\t\t\t\tinvalid values for either the stock code or the price:<br />";
            foreach ($invalid_fields as $v) {
                $msg .= "&nbsp;&nbsp;&nbsp;&nbsp;- {$v}<br />";
            }
        } else {
            $msg = "";
        }
        $frm->setmsg("<li class='err'>Successfully imported new pricelist.{$msg}</li>");
    }
    return view($frm);
}
/**
 * returns next available reference number
 *
 * @return int
 */
function getrefnum()
{
    $rn = new dbSelect("max_refnum", "core", grp(m("cols", "max(refnum)")));
    $rn->run();
    return $rn->fetch_result() + 1;
}