Ejemplo n.º 1
0
function barcode_encode_ean($ean, $encoding = "EAN-13")
{
    $digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
    $mirror = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010");
    $guards = array("9a1a", "1a1a1", "a1a");
    $ean = trim($ean);
    if (eregi("[^0-9]", $ean)) {
        return array("text" => "Invalid EAN-Code");
    }
    $encoding = strtoupper($encoding);
    if ($encoding == "ISBN") {
        if (!preg_match("^978", $ean)) {
            $ean = "978" . $ean;
        }
    }
    if (preg_match("^978", $ean)) {
        $encoding = "ISBN";
    }
    if (strlen($ean) < 12 || strlen($ean) > 13) {
        return array("text" => "Invalid {$encoding} Code (must have 12/13 numbers)");
    }
    $ean = substr($ean, 0, 12);
    $eansum = barcode_gen_ean_sum($ean);
    $ean .= $eansum;
    $line = $guards[0];
    for ($i = 1; $i < 13; $i++) {
        $str = $digits[$ean[$i]];
        if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) {
            $line .= strrev($str);
        } else {
            $line .= $str;
        }
        if ($i == 6) {
            $line .= $guards[1];
        }
    }
    $line .= $guards[2];
    /* create text */
    $pos = 0;
    $text = "";
    for ($a = 0; $a < 13; $a++) {
        if ($a > 0) {
            $text .= " ";
        }
        $text .= "{$pos}:12:{$ean[$a]}";
        if ($a == 0) {
            $pos += 12;
        } else {
            if ($a == 6) {
                $pos += 12;
            } else {
                $pos += 7;
            }
        }
    }
    return array("encoding" => $encoding, "bars" => $line, "text" => $text);
}
Ejemplo n.º 2
0
function barcode_encode_ean($ean, $encoding = "EAN-13")
{
    $digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
    $mirror = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010");
    $guards = array("9a1a", "1a1a1", "a1a");
    $ean = trim($ean);
    if (eregi("[^0-9]", $ean)) {
        return array("text" => "Codigo EAN incorrecto");
    }
    $encoding = strtoupper($encoding);
    if ($encoding == "ISBN") {
        !ereg("^978", $ean) ? $ean = "978" . $ean : null;
    }
    ereg("^978", $ean) ? $encoding = "ISBN" : null;
    if (strlen($ean) < 12 || strlen($ean) > 13) {
        return array("text" => "Codigo {$encoding} no valido (debe ser de 12 o 13 digitos)");
    }
    $ean = substr($ean, 0, 12);
    $eansum = barcode_gen_ean_sum($ean);
    $ean = $ean . $eansum;
    $line = $guards[0];
    for ($i = 1; $i < 13; $i++) {
        $str = $digits[$ean[$i]];
        $i < 7 && $mirror[$ean[0]][$i - 1] == 1 ? $line .= strrev($str) : ($line .= $str);
        $i == 6 ? $line .= $guards[1] : null;
    }
    $line .= $guards[2];
    $pos = 0;
    $text = "";
    for ($a = 0; $a < 13; $a++) {
        if ($a > 0) {
            $text .= " ";
        }
        $text .= "{$pos}:12:{$ean[$a]}";
        if ($a == 0) {
            $pos += 12;
        } else {
            if ($a == 6) {
                $pos += 12;
            } else {
                $pos += 7;
            }
        }
    }
    if (showNumbersInBarCode == "true") {
        return array("encoding" => $encoding, "bars" => $line, "text" => $text);
    } else {
        return array("encoding" => $encoding, "bars" => $line, "text" => "");
    }
}
Ejemplo n.º 3
0
function via_table($ean)
{
    $bars['a'][0] = '0001101';
    $bars['a'][1] = '0011001';
    $bars['a'][2] = '0010011';
    $bars['a'][3] = '0111101';
    $bars['a'][4] = '0100011';
    $bars['a'][5] = '0110001';
    $bars['a'][6] = '0101111';
    $bars['a'][7] = '0111011';
    $bars['a'][8] = '0110111';
    $bars['a'][9] = '0001011';
    $bars['b'][0] = '0100111';
    $bars['b'][1] = '0110011';
    $bars['b'][2] = '0011011';
    $bars['b'][3] = '0100001';
    $bars['b'][4] = '0011101';
    $bars['b'][5] = '0111001';
    $bars['b'][6] = '0000101';
    $bars['b'][7] = '0010001';
    $bars['b'][8] = '0001001';
    $bars['b'][9] = '0010111';
    $bars['c'][0] = '1110010';
    $bars['c'][1] = '1100110';
    $bars['c'][2] = '1101100';
    $bars['c'][3] = '1000010';
    $bars['c'][4] = '1011100';
    $bars['c'][5] = '1001110';
    $bars['c'][6] = '1010000';
    $bars['c'][7] = '1000100';
    $bars['c'][8] = '1001000';
    $bars['c'][9] = '1110100';
    $start_end = '101';
    $mid = '01010';
    $seq[0] = 'aaaaaa';
    $seq[1] = 'aababb';
    $seq[2] = 'aabbab';
    $seq[3] = 'aabbba';
    $seq[4] = 'abaabb';
    $seq[5] = 'abbaab';
    $seq[6] = 'abbbaa';
    $seq[7] = 'ababab';
    $seq[8] = 'ababba';
    $seq[9] = 'abbaba';
    $ean = trim($ean);
    if (eregi("[^0-9]", $ean)) {
        return "Invalid EAN-Code";
    }
    if (strlen($ean) < 12 || strlen($ean) > 13) {
        return "Invalid Code (must have 12/13 numbers)";
    }
    $ean = substr($ean, 0, 12);
    $eansum = barcode_gen_ean_sum($ean);
    $ean .= $eansum;
    $group1 = '';
    $txt1 = '';
    for ($i = 1; $i < 7; $i++) {
        $group1 .= $bars[$seq[intval($ean[0])][$i - 1]][intval($ean[$i])];
        $txt1 .= $ean[$i];
    }
    $group2 = '';
    $txt2 = '';
    for ($i = 7; $i < 13; $i++) {
        $group2 .= $bars['c'][intval($ean[$i])];
        $txt2 .= $ean[$i];
    }
    $width = 2;
    $height = $width * 60;
    $height1 = $width * 66;
    $height2 = $width * 6;
    $height3 = $width * 11;
    $width20 = $width * 20;
    $tbl = to_table_bars($start_end, $width, $height1, "rowspan=2");
    $tbl .= to_table_bars($group1, $width, $height, "");
    $tbl .= to_table_bars($mid, $width, $height1, "rowspan=2");
    $tbl .= to_table_bars($group2, $width, $height, "");
    $tbl .= to_table_bars($start_end, $width, $height1, "rowspan=2");
    $tbl = "<tr><td><div style='display:block;height:{$height}px;width:{$width20}px;'></div></td>" . $tbl . "</tr>";
    $t = "<td><div style='display:block;height:{$height2}px;width:{$width}px;'></div></td>";
    $tbl .= "<tr><td rowspan=2 style='text-align:center;font-family:Arial;font-size:{$height3}px;'>" . $ean[0] . "</td>";
    $tbl .= " <td colspan=42 rowspan=2 style='text-align:center;font-family:Arial;font-size:{$height3}px;'>{$txt1}</td>";
    $tbl .= " ";
    $tbl .= "<td colspan=42 rowspan=2 style='text-align:center;font-family:Arial;font-size:{$height3}px;'>{$txt2}</td>";
    $tbl .= " </tr>";
    $tbl .= "<tr>{$t}{$t}{$t} {$t}{$t}{$t}{$t}{$t} {$t}{$t}{$t}</tr>";
    #return $res;
    $tbl = "<table cellspacing=0 cellpadding=0>" . $tbl . "</table>";
    return $tbl;
}
 /**
  * Encode EAN
  *
  * @param	string	$ean		Code
  * @param	string	$encoding	Encoding
  * @return	array				array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
  */
 function generate_barcode($id_prod)
 {
     require_once DOL_DOCUMENT_ROOT . "/core/lib/barcode.lib.php";
     global $conf, $langs, $db;
     $loop = true;
     $encoding = "EAN-13";
     //CODIGO QUE MUESTRA SI BARCODE_TYPE VIENE NULL
     $sqlBar = "select fk_barcode_type from " . MAIN_DB_PREFIX . "product";
     $sqlBar .= " WHERE rowid = " . $id_prod;
     $result = $this->db->query($sqlBar);
     if ($this->db->num_rows($result)) {
         $bt = $this->db->fetch_object($result);
     }
     //
     while ($loop) {
         $ean = rand(0, 999999) . rand(0, 999999);
         $ean = substr($ean, 0, 12);
         $eansum = barcode_gen_ean_sum($ean);
         $ean .= $eansum;
         $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "product";
         $sql .= " WHERE barcode=" . $ean;
         $db->begin();
         $resql = $db->query($sql);
         if ($resql) {
             if ($db->num_rows($resql) == 0) {
                 $loop = false;
                 //Se indica validación para actualizar fk_barcode_type cuando venga nulo, ya que en los reportes
                 //si no viene el campo no imprime reporte alguno y marca error en la libreria de TCPDF
                 if (empty($bt->fk_barcode_type)) {
                     $bartype = ", fk_barcode_type = 2";
                 } else {
                     $bartype = "";
                 }
                 $sql = "UPDATE " . MAIN_DB_PREFIX . "product";
                 $sql .= " SET barcode = " . $ean;
                 $sql .= $bartype . " WHERE rowid=" . $id_prod;
                 $res = $db->query($sql);
                 $db->commit();
             }
         }
     }
     return $res;
 }