diff --git a/htdocs/comm/action/card.php b/htdocs/comm/action/card.php index f572379d42e..066001f6aa7 100644 --- a/htdocs/comm/action/card.php +++ b/htdocs/comm/action/card.php @@ -7,7 +7,7 @@ * Copyright (C) 2013 Florian Henry * Copyright (C) 2014 Cedric GROSS * Copyright (C) 2015 Alexandre Spangaro - * Copyright (C) 2018-2019 Frédéric France + * Copyright (C) 2018-2021 Frédéric France * Copyright (C) 2019 Ferran Marcet * * This program is free software; you can redistribute it and/or modify @@ -1180,11 +1180,12 @@ if ($action == 'create') { $projectid = GETPOST('projectid', 'int'); - print ''.$langs->trans("Project").''; + print ''.$langs->trans("Project").''; print img_picto('', 'project', 'class="paddingrightonly"'); print $formproject->select_projects((!empty($societe->id) ? $societe->id : -1), $projectid, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500 widthcentpercentminusxx'); - print ' '; + print ' '; + print ''; $urloption = '?action=create&donotclearsession=1'; $url = dol_buildpath('comm/action/card.php', 2).$urloption; diff --git a/htdocs/core/lib/barcode.lib.php b/htdocs/core/lib/barcode.lib.php index cd8764a8a87..54bbc0a7666 100644 --- a/htdocs/core/lib/barcode.lib.php +++ b/htdocs/core/lib/barcode.lib.php @@ -100,7 +100,7 @@ function barcode_print($code, $encoding = "ANY", $scale = 2, $mode = "png") } /** - * Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only EAN-13/ISBN is possible + * Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only EAN-13/ISBN or UPC is possible * * You can use the following encodings (when you have genbarcode): * ANY choose best-fit (default) @@ -125,7 +125,13 @@ function barcode_encode($code, $encoding) { global $genbarcode_loc; - if ((preg_match("/^ean$/i", $encoding)) + if ((preg_match("/^upc$/i", $encoding)) + && (preg_match("/^[0-9]{11,12}$/", $code)) + ) { + /* use built-in UPC-Encoder */ + dol_syslog("barcode.lib.php::barcode_encode Use barcode_encode_upc"); + $bars = barcode_encode_upc($code, $encoding); + } elseif ((preg_match("/^ean$/i", $encoding)) || (($encoding) && (preg_match("/^isbn$/i", $encoding)) && ((strlen($code) == 9 || strlen($code) == 10) || @@ -182,39 +188,19 @@ function barcode_gen_ean_sum($ean) return (10 - ((3 * $esum + $osum) % 10)) % 10; } + /** - * Encode EAN + * Generate EAN bars * - * @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, 'error': error message if error) + * @param string $ean EAN to encode + * @return string Encoded EAN */ -function barcode_encode_ean($ean, $encoding = "EAN-13") +function barcode_gen_ean_bars($ean) { $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"); + $guards = array("9a1a", "1a1a1", "a1a7"); - $ean = trim($ean); - if (preg_match("/[^0-9]/i", $ean)) { - return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)"); - } - $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("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (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]]; @@ -229,6 +215,40 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") } $line .= $guards[2]; + return $line; +} + +/** + * 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, 'error': error message if error) + */ +function barcode_encode_ean($ean, $encoding = "EAN-13") +{ + $ean = trim($ean); + if (preg_match("/[^0-9]/i", $ean)) { + return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)"); + } + $encoding = strtoupper($encoding); + if ($encoding == "ISBN") { + if (!preg_match("/^978/", $ean)) { + $ean = "978".$ean; + } + } + if (preg_match("/^97[89]/", $ean)) { + $encoding = "ISBN"; + } + if (strlen($ean) < 12 || strlen($ean) > 13) { + return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)"); + } + + $ean = substr($ean, 0, 12); + $eansum = barcode_gen_ean_sum($ean); + $ean .= $eansum; + $bars = barcode_gen_ean_bars($ean); + /* create text */ $pos = 0; $text = ""; @@ -249,7 +269,57 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") return array( "error" => '', "encoding" => $encoding, - "bars" => $line, + "bars" => $bars, + "text" => $text + ); +} + +/** + * Encode UPC + * + * @param string $upc Code + * @param string $encoding Encoding + * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info, 'error': error message if error) + */ +function barcode_encode_upc($upc, $encoding = "UPC") +{ + $upc = trim($upc); + if (preg_match("/[^0-9]/i", $upc)) { + return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (not a numeric)"); + } + $encoding = strtoupper($encoding); + if (strlen($upc) < 11 || strlen($upc) > 12) { + return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (must have 11/12 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (must have 11/12 numbers)"); + } + + $upc = substr("0".$upc, 0, 12); + $eansum = barcode_gen_ean_sum($upc); + $upc .= $eansum; + $bars = barcode_gen_ean_bars($upc); + + /* create text */ + $pos = 0; + $text = ""; + for ($a = 1; $a < 13; $a++) { + if ($a > 1) { + $text .= " "; + } + $text .= "$pos:12:{$upc[$a]}"; + if ($a == 1) { + $pos += 15; + } elseif ($a == 6) { + $pos += 17; + } elseif ($a == 11) { + $pos += 15; + } else { + $pos += 7; + } + } + + return array( + "error" => '', + "encoding" => $encoding, + "bars" => $bars, "text" => $text ); } diff --git a/htdocs/core/modules/barcode/doc/phpbarcode.modules.php b/htdocs/core/modules/barcode/doc/phpbarcode.modules.php index 27e90e94ba1..56c2a12fee6 100644 --- a/htdocs/core/modules/barcode/doc/phpbarcode.modules.php +++ b/htdocs/core/modules/barcode/doc/phpbarcode.modules.php @@ -102,15 +102,15 @@ class modPhpbarcode extends ModeleBarCode if ($encoding == 'ISBN') { $supported = 1; } + if ($encoding == 'UPC') { + $supported = 1; + } // Formats that hangs on Windows (when genbarcode.exe for Windows is called, so they are not // activated on Windows) if (file_exists($genbarcode_loc) && empty($_SERVER["WINDIR"])) { if ($encoding == 'EAN8') { $supported = 1; } - if ($encoding == 'UPC') { - $supported = 1; - } if ($encoding == 'C39') { $supported = 1; }