From 7b59a3ac2b1a65e5a7bceb5c6a22916d22299a74 Mon Sep 17 00:00:00 2001 From: Martin Neubauer Date: Fri, 12 Mar 2021 13:11:17 +0100 Subject: [PATCH 1/7] Factor out generating EAN bars --- htdocs/core/lib/barcode.lib.php | 50 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/htdocs/core/lib/barcode.lib.php b/htdocs/core/lib/barcode.lib.php index cd8764a8a87..75b932aff32 100644 --- a/htdocs/core/lib/barcode.lib.php +++ b/htdocs/core/lib/barcode.lib.php @@ -182,6 +182,36 @@ function barcode_gen_ean_sum($ean) return (10 - ((3 * $esum + $osum) % 10)) % 10; } + +/** + * Generate EAN bars + * + * @param string $ean EAN to encode + * @return string Encoded EAN + */ +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"); + + $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]; + + return $line; +} + /** * Encode EAN * @@ -191,10 +221,6 @@ function barcode_gen_ean_sum($ean) */ 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 (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)"); @@ -215,19 +241,7 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") $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]; + $bars = barcode_gen_ean_bars($ean); /* create text */ $pos = 0; @@ -249,7 +263,7 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") return array( "error" => '', "encoding" => $encoding, - "bars" => $line, + "bars" => $bars, "text" => $text ); } From 5aaeb6742c61f505bbc70c85fe37d507a1832041 Mon Sep 17 00:00:00 2001 From: Martin Neubauer Date: Fri, 12 Mar 2021 13:54:27 +0100 Subject: [PATCH 2/7] Add UPC support to built-in barcode generator --- htdocs/core/lib/barcode.lib.php | 62 ++++++++++++++++++- .../barcode/doc/phpbarcode.modules.php | 6 +- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/htdocs/core/lib/barcode.lib.php b/htdocs/core/lib/barcode.lib.php index 75b932aff32..98776e8a63d 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) || @@ -193,7 +199,7 @@ 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"); $line = $guards[0]; for ($i = 1; $i < 13; $i++) { @@ -268,6 +274,56 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") ); } +/** + * 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 += 19; + } elseif ($a == 6) { + $pos += 11; + } elseif ($a == 11) { + $pos += 17; + } else { + $pos += 7; + } + } + + return array( + "error" => '', + "encoding" => $encoding, + "bars" => $bars, + "text" => $text + ); +} + /** * Encode result of genbarcode command * 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; } From 09cedb5a5ee13c52118a8e306e4a396d1034c384 Mon Sep 17 00:00:00 2001 From: Martin Neubauer Date: Fri, 12 Mar 2021 13:55:39 +0100 Subject: [PATCH 3/7] ISBN might start with 979 these days --- htdocs/core/lib/barcode.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/barcode.lib.php b/htdocs/core/lib/barcode.lib.php index 98776e8a63d..52e4d289c3a 100644 --- a/htdocs/core/lib/barcode.lib.php +++ b/htdocs/core/lib/barcode.lib.php @@ -237,7 +237,7 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") $ean = "978".$ean; } } - if (preg_match("/^978/", $ean)) { + if (preg_match("/^97[89]/", $ean)) { $encoding = "ISBN"; } if (strlen($ean) < 12 || strlen($ean) > 13) { From 85eb82c8c3263706dcb14e2ad84fc07a5ca1f769 Mon Sep 17 00:00:00 2001 From: Martin Neubauer Date: Fri, 12 Mar 2021 14:44:04 +0100 Subject: [PATCH 4/7] Tweak offsets for UPC barcode text --- htdocs/core/lib/barcode.lib.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/lib/barcode.lib.php b/htdocs/core/lib/barcode.lib.php index 52e4d289c3a..54bbc0a7666 100644 --- a/htdocs/core/lib/barcode.lib.php +++ b/htdocs/core/lib/barcode.lib.php @@ -306,11 +306,11 @@ function barcode_encode_upc($upc, $encoding = "UPC") } $text .= "$pos:12:{$upc[$a]}"; if ($a == 1) { - $pos += 19; + $pos += 15; } elseif ($a == 6) { - $pos += 11; - } elseif ($a == 11) { $pos += 17; + } elseif ($a == 11) { + $pos += 15; } else { $pos += 7; } From 67abd6d1d61fff1eb8d9cbdf5e07b1f6a8ddb421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Fri, 12 Mar 2021 17:05:11 +0100 Subject: [PATCH 5/7] go back actioncomm create after create project --- htdocs/comm/action/card.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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; From 6be6e306b1227718eac41072ce9ad8b89f9d1846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Fri, 12 Mar 2021 19:05:03 +0100 Subject: [PATCH 6/7] Update interface_50_modEventOrganization_EventOrganization.class.php --- ...face_50_modEventOrganization_EventOrganization.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php b/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php index 386a723bb5e..add743b6e57 100644 --- a/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php +++ b/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php @@ -74,9 +74,9 @@ class InterfaceEventOrganization extends DolibarrTriggers if (empty($conf->eventorganization->enabled)) { return 0; // Module not active, we do nothing } - + $error=0; - + // Actions if ($action == 'PROJECT_VALIDATE') { if (!empty($conf->global->EVENTORGANIZATION_TASK_LABEL) && !empty($object->usage_organize_event)) { @@ -84,7 +84,7 @@ class InterfaceEventOrganization extends DolibarrTriggers if (is_array($taskToDo) && count($taskToDo)>0) { // Load translation files required by the page $langs->loadLangs(array("eventorganization")); - + $this->db->begin(); foreach ($taskToDo as $taskLabel) { $task = new Task($this->db); From f507df994bedf10fe522c99af1df496285d6d50e Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 12 Mar 2021 18:07:16 +0000 Subject: [PATCH 7/7] Fixing style errors. --- ...nterface_50_modEventOrganization_EventOrganization.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php b/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php index add743b6e57..c30ee8c4190 100644 --- a/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php +++ b/htdocs/core/triggers/interface_50_modEventOrganization_EventOrganization.class.php @@ -84,7 +84,7 @@ class InterfaceEventOrganization extends DolibarrTriggers if (is_array($taskToDo) && count($taskToDo)>0) { // Load translation files required by the page $langs->loadLangs(array("eventorganization")); - + $this->db->begin(); foreach ($taskToDo as $taskLabel) { $task = new Task($this->db);