function write($_POST)
{
    $Out = "";
    #get & send vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
        $Out .= "<input type=hidden name=\${$key} value='{$value}'>";
    }
    $blength = strlen($barcodes);
    $start = 0;
    while ($start < $blength) {
        $value = substr($barcodes, $start, $clength);
        $start = $start + $clength;
        if (strlen($value) == $clength) {
            $bars[] = $value;
        }
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    foreach ($bars as $each) {
        $v->isOk($each, "num", 1, $clength, "Invalid bar code.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = "";
        $Errors = $v->getErrors();
        foreach ($Errors as $e) {
            $errors .= "<li class=err>" . $e["msg"] . "</li>";
        }
        $errors .= "<input type=hidden name=errors value='{$errors}'>";
        return read_bars($_POST, $errors);
    }
    #we can only add as many barcodes as there is stock, so find the max and reduce the array if it exceeds the max
    pglib_transaction("begin");
    $Sl = "SELECT stkcod,stkdes,units,alloc FROM stock WHERE stkid='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to view stock");
    if (pg_numrows($Rs) < 1) {
        return "Invalid Stock id.";
    }
    $St = pg_fetch_array($Rs);
    $Av = $St['units'] - $St['alloc'];
    $i = 0;
    $Sl = "SELECT code FROM ss0 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss1 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss2 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss3 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss4 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss5 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss6 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss7 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss8 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    $Sl = "SELECT code FROM ss9 WHERE stock='{$id}' AND div = '" . USER_DIV . "'";
    $Rs = db_exec($Sl) or errDie("Unable to retrieve barcodes from db");
    while ($Tp = pg_fetch_array($Rs)) {
        $i++;
    }
    pglib_transaction("commit");
    $Wob = $Av - $i;
    if ($Wob > 0) {
        #doesnt exceed, do nothing
        foreach ($bars as $each) {
            $bars2[] = $each;
        }
    } else {
        $bars2 = array();
        for ($count = 0; $count <= $Wob; $count++) {
            $bars2[] = $bars[$count];
        }
    }
    db_conn("cubit");
    foreach ($bars2 as $each) {
        switch (substr($each, strlen($each) - 1, 1)) {
            case "0":
                $tab = "ss0";
                break;
            case "1":
                $tab = "ss1";
                break;
            case "2":
                $tab = "ss2";
                break;
            case "3":
                $tab = "ss3";
                break;
            case "4":
                $tab = "ss4";
                break;
            case "5":
                $tab = "ss5";
                break;
            case "6":
                $tab = "ss6";
                break;
            case "7":
                $tab = "ss7";
                break;
            case "8":
                $tab = "ss8";
                break;
            case "9":
                $tab = "ss9";
                break;
            default:
                return order($_POST, "The code you selected is invalid");
        }
        if (barext_ex($tab, 'code', $each) or strlen($each) == 0) {
            return read_bars($_POST, "<li class='err'>The code you selected already exits in the system.</li>");
        } else {
            $getcheck = "SELECT * FROM " . $tab . " WHERE code = '{$me}' AND active = 'no'";
            $runcheck = db_exec($getcheck) or errDie("Unable to get serial number check");
            if (pg_numrows($runcheck) < 1) {
                $Sl = "INSERT INTO " . $tab . " (code,stock,div) VALUES ('{$each}','{$id}','" . USER_DIV . "')";
                $Rs = db_exec($Sl) or errDie("Unable to update database.", SELF);
            } else {
                $arr = pg_fetch_array($runcheck);
                $Sl = "UPDATE " . $tab . " SET active = 'yes' WHERE code = '{$arr['code']}' AND stock = '{$arr['stock']}' AND div = '{$arr['div']}'";
                $Rs = db_exec($Sl) or errDie("Unable to update database.", SELF);
            }
        }
    }
    #print and reload parent window
    return "All Possible RFID Barcodes Have Been Imported<br><br\n\t\t<input type='button' onClick='javascript:window.close();' value='Close'>\n\t\t<script>\n\t\t\topener.document.formName.submit()\n\t\t</script>";
}
Example #2
0
function write($_POST)
{
    $Out = "";
    #get & send vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
        $Out .= "<input type='hidden' name=\${$key} value='{$value}'>";
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 100, "Invalid stock item id.");
    if (isset($me)) {
        $v->isOk($me, "string", 0, 100, "Invalid bar code.");
    }
    $v->isOk($sharecode, "string", 0, 100, "Invalid shared bar code.");
    # display errors, if any
    if ($v->isError()) {
        return order($_POST, $v->genErrors());
    }
    $cols = grp(m("bar", $sharecode));
    $wh = "stkid='{$id}'";
    $qry = new dbUpdate("stock", "cubit", $cols, $wh);
    $qry->run(DB_UPDATE);
    if (isset($me) && strlen($me) > 0) {
        db_conn("cubit");
        switch (substr($me, strlen($me) - 1, 1)) {
            case "0":
                $tab = "ss0";
                break;
            case "1":
                $tab = "ss1";
                break;
            case "2":
                $tab = "ss2";
                break;
            case "3":
                $tab = "ss3";
                break;
            case "4":
                $tab = "ss4";
                break;
            case "5":
                $tab = "ss5";
                break;
            case "6":
                $tab = "ss6";
                break;
            case "7":
                $tab = "ss7";
                break;
            case "8":
                $tab = "ss8";
                break;
            case "9":
                $tab = "ss9";
                break;
            default:
                return order($_POST, "The code you selected is invalid");
        }
        if (barext_ex($tab, 'code', $me) or strlen($me) == 0) {
            return order($_POST, "The code you selected aready exits in the system.");
        } else {
            $getcheck = "SELECT * FROM " . $tab . " WHERE code = '{$me}' AND active = 'no'";
            $runcheck = db_exec($getcheck) or errDie("Unable to get serial number check");
            if (pg_numrows($runcheck) < 1) {
                $Sl = "INSERT INTO " . $tab . " (code,stock,div) VALUES ('{$me}','{$id}','" . USER_DIV . "')";
                $Rs = db_exec($Sl) or errDie("Unable to update database.", SELF);
            } else {
                $arr = pg_fetch_array($runcheck);
                $Sl = "UPDATE " . $tab . " SET active = 'yes' WHERE code = '{$arr['code']}' AND stock = '{$arr['stock']}' AND div = '{$arr['div']}'";
                $Rs = db_exec($Sl) or errDie("Unable to update database.", SELF);
            }
        }
    }
    return order($_POST);
}