From 4b9d1b901bccd39b6cd8967ae13b84f8221b0e00 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 25 May 2014 17:14:33 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20[=20bug=20#1401=20]=20Probl=C3=A8me=20av?= =?UTF-8?q?ec=20la=20g=C3=A9n=C3=A9ration=20des=20codes=20barres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/admin/barcode.php | 2 +- htdocs/core/class/commonobject.class.php | 14 +++--- htdocs/core/class/html.form.class.php | 6 +-- .../barcode/mod_barcode_product_standard.php | 42 ++++++++++------- htdocs/core/modules/modProduct.class.php | 1 + htdocs/core/modules/modService.class.php | 6 +-- htdocs/product/class/product.class.php | 35 ++++++++------- htdocs/product/fiche.php | 45 +++++++++++++++---- 8 files changed, 98 insertions(+), 53 deletions(-) diff --git a/htdocs/admin/barcode.php b/htdocs/admin/barcode.php index 1d8110c00d6..988b49efd07 100644 --- a/htdocs/admin/barcode.php +++ b/htdocs/admin/barcode.php @@ -64,7 +64,7 @@ else if ($action == 'update') { if (GETPOST('submit_GENBARCODE_LOCATION')) { - $location = GETPOST('genbarcodelocation','alpha'); + $location = GETPOST('GENBARCODE_LOCATION','alpha'); $res = dolibarr_set_const($db, "GENBARCODE_LOCATION",$location,'chaine',0,'',$conf->entity); } if (GETPOST('submit_PRODUIT_DEFAULT_BARCODE_TYPE')) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index a6e8b5fe3b2..095a1a9ba12 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -627,25 +627,25 @@ abstract class CommonObject /** * Load data for barcode into properties ->barcode_type* - * Properties ->barcode_type is used to find others. - * If not defined, ->element must be defined to know default barcode type. + * Properties ->barcode_type that is id of barcode type is used to find other properties, but + * if it is not defined, ->element must be defined to know default barcode type. * - * @return int <0 if KO, >=0 if OK + * @return int <0 if KO, 0 if can't guess type of barcode (ISBN, EAN13...), >0 if OK (all barcode properties loaded) */ function fetch_barcode() { global $conf; dol_syslog(get_class($this).'::fetch_barcode this->element='.$this->element.' this->barcode_type='.$this->barcode_type); - + $idtype=$this->barcode_type; - if (! $idtype) + if (empty($idtype) && $idtype != '0') // If type of barcode no set, we try to guess. If set to '0' it means we forced to have type remain not defined { if ($this->element == 'product') $idtype = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE; else if ($this->element == 'societe') $idtype = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY; - else dol_print_error('','Call fetch_barcode with barcode_type not defined and cant be guessed'); + else dol_syslog('Call fetch_barcode with barcode_type not defined and cant be guessed', LOG_WARNING); } - + if ($idtype > 0) { if (empty($this->barcode_type) || empty($this->barcode_type_code) || empty($this->barcode_type_label) || empty($this->barcode_type_coder)) // If data not already loaded diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index d3111d38a05..d16c9f19a5a 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -4087,11 +4087,11 @@ class Form // Complete object if not complete if (empty($object->barcode_type_code) || empty($object->barcode_type_coder)) { - $result = $object->fetch_barcode(); - //Check if fetch_barcode() failed + $result = $object->fetch_barcode(); + //Check if fetch_barcode() failed if ($result < 1) return ''; } - + // Barcode image $url=DOL_URL_ROOT.'/viewimage.php?modulepart=barcode&generator='.urlencode($object->barcode_type_coder).'&code='.urlencode($object->barcode).'&encoding='.urlencode($object->barcode_type_code); $out =''; diff --git a/htdocs/core/modules/barcode/mod_barcode_product_standard.php b/htdocs/core/modules/barcode/mod_barcode_product_standard.php index 37273397ee6..9c96584221c 100644 --- a/htdocs/core/modules/barcode/mod_barcode_product_standard.php +++ b/htdocs/core/modules/barcode/mod_barcode_product_standard.php @@ -161,20 +161,23 @@ class mod_barcode_product_standard extends ModeleNumRefBarCode /** * Check validity of code according to its rules * - * @param DoliDB $db Database handler - * @param string &$code Code to check/correct - * @param Product $product Object product - * @param int $type 0 = customer/prospect , 1 = supplier - * @return int 0 if OK - * -1 ErrorBadCustomerCodeSyntax - * -2 ErrorCustomerCodeRequired - * -3 ErrorCustomerCodeAlreadyUsed - * -4 ErrorPrefixRequired + * @param DoliDB $db Database handler + * @param string &$code Code to check/correct + * @param Product $product Object product + * @param int $thirdparty_type 0 = customer/prospect , 1 = supplier + * @param string $type type of barcode (EAN, ISBN, ...) + * @return int 0 if OK + * -1 ErrorBadCustomerCodeSyntax + * -2 ErrorCustomerCodeRequired + * -3 ErrorCustomerCodeAlreadyUsed + * -4 ErrorPrefixRequired */ - function verif($db, &$code, $product, $type) + function verif($db, &$code, $product, $thirdparty_type, $type) { global $conf; + //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit; + require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php'; $result=0; @@ -190,7 +193,7 @@ class mod_barcode_product_standard extends ModeleNumRefBarCode } else { - if ($this->verif_syntax($code) >= 0) + if ($this->verif_syntax($code, $type) >= 0) { $is_dispo = $this->verif_dispo($db, $code, $product); if ($is_dispo <> 0) @@ -215,7 +218,7 @@ class mod_barcode_product_standard extends ModeleNumRefBarCode } } - dol_syslog(get_class($this)."::verif type=".$type." result=".$result); + dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result); return $result; } @@ -254,12 +257,13 @@ class mod_barcode_product_standard extends ModeleNumRefBarCode } /** - * Renvoi si un code respecte la syntaxe + * Return if a barcode value match syntax * - * @param string $code Code a verifier + * @param string $codefortest Code to check syntax + * @param string $typefortest Type of barcode (ISBN, EAN, ...) * @return int 0 if OK, <0 if KO */ - function verif_syntax($code) + function verif_syntax($codefortest, $typefortest) { global $conf; @@ -273,7 +277,13 @@ class mod_barcode_product_standard extends ModeleNumRefBarCode return ''; } - $result=check_value($mask,$code); + $newcodefortest=$codefortest; + if (in_array($typefortest,array('EAN13','ISBN'))) // We remove the CRC char not included into mask + { + $newcodefortest=substr($newcodefortest,0,12); + } + + $result=check_value($mask,$newcodefortest); return $result; } diff --git a/htdocs/core/modules/modProduct.class.php b/htdocs/core/modules/modProduct.class.php index 49b08003a23..05c934a8ea6 100644 --- a/htdocs/core/modules/modProduct.class.php +++ b/htdocs/core/modules/modProduct.class.php @@ -245,6 +245,7 @@ class modProduct extends DolibarrModules $this->import_tables_array[$r]=array('p'=>MAIN_DB_PREFIX.'product','extra'=>MAIN_DB_PREFIX.'product_extrafields'); $this->import_tables_creator_array[$r]=array('p'=>'fk_user_author'); // Fields to store import user id $this->import_fields_array[$r]=array('p.ref'=>"Ref*",'p.label'=>"Label*",'p.description'=>"Description",'p.url'=>"PublicUrl",'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",'p.weight'=>"Weight",'p.duration'=>"Duration",'p.customcode'=>'CustomCode','p.price'=>"SellingPriceHT",'p.price_ttc'=>"SellingPriceTTC",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell*",'p.tobuy'=>"OnBuy*",'p.fk_product_type'=>"Type*",'p.finished'=>'Nature','p.datec'=>'DateCreation*'); + if (! empty($conf->barcode->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.barcode'=>'BarCode')); // Add extra fields $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product' AND entity = ".$conf->entity; $resql=$this->db->query($sql); diff --git a/htdocs/core/modules/modService.class.php b/htdocs/core/modules/modService.class.php index a998f41298e..2b9bb8d44f8 100644 --- a/htdocs/core/modules/modService.class.php +++ b/htdocs/core/modules/modService.class.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004-2010 Laurent Destailleur + * Copyright (C) 2004-2014 Laurent Destailleur * Copyright (C) 2004 Sebastien Di Cintio * Copyright (C) 2004 Benoit Mortier * Copyright (C) 2005-2012 Regis Houssin @@ -17,7 +17,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ /** @@ -209,7 +208,8 @@ class modService extends DolibarrModules $this->import_tables_array[$r]=array('p'=>MAIN_DB_PREFIX.'product','extra'=>MAIN_DB_PREFIX.'product_extrafields'); $this->import_tables_creator_array[$r]=array('p'=>'fk_user_author'); // Fields to store import user id $this->import_fields_array[$r]=array('p.ref'=>"Ref*",'p.label'=>"Label*",'p.description'=>"Description",'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",'p.weight'=>"Weight",'p.duration'=>"Duration",'p.customcode'=>'CustomCode','p.price'=>"SellingPriceHT",'p.price_ttc'=>"SellingPriceTTC",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell*",'p.tobuy'=>"OnBuy*",'p.fk_product_type'=>"Type*",'p.finished'=>'Nature','p.datec'=>'DateCreation*'); - // Add extra fields + if (! empty($conf->barcode->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.barcode'=>'BarCode')); + // Add extra fields $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product'"; $resql=$this->db->query($sql); if ($resql) // This can fail when class is used on old database (during migration for example) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ae1c0f2f43c..5eaf183428d 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -267,7 +267,10 @@ class Product extends CommonObject $this->accountancy_code_buy = trim($this->accountancy_code_buy); $this->accountancy_code_sell= trim($this->accountancy_code_sell); - // Check parameters + // Barcode value + $this->barcode=trim($this->barcode); + + // Check parameters if (empty($this->libelle)) { $this->error='ErrorMandatoryParametersNotProvided'; @@ -277,7 +280,7 @@ class Product extends CommonObject { // Load object modCodeProduct $module=(! empty($conf->global->PRODUCT_CODEPRODUCT_ADDON)?$conf->global->PRODUCT_CODEPRODUCT_ADDON:'mod_codeproduct_leopard'); - if ($module != 'mod_codeproduct_leopard') // Do not load module file + if ($module != 'mod_codeproduct_leopard') // Do not load module file for leopard { if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php') { @@ -306,11 +309,11 @@ class Product extends CommonObject // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts) if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code); - + // Check more parameters // If error, this->errors[] is filled $result = $this->verify(); - + if ($result >= 0) { $sql = "SELECT count(*) as nb"; @@ -453,7 +456,8 @@ class Product extends CommonObject /** - * Check properties of product are ok (like name, barcode, ...) + * Check properties of product are ok (like name, barcode, ...). + * All properties must be already loaded on object (this->barcode, this->barcode_type_code, ...). * * @return int 0 if OK, <0 if KO */ @@ -470,7 +474,7 @@ class Product extends CommonObject $result = -2; } - $rescode = $this->check_barcode($this->barcode); + $rescode = $this->check_barcode($this->barcode,$this->barcode_type_code); if ($rescode <> 0) { if ($rescode == -1) @@ -492,15 +496,16 @@ class Product extends CommonObject } /** - * Check customer code + * Check barcode * * @param string $valuetotest Value to test + * @param string $typefortest Type of barcode (ISBN, EAN, ...) * @return int 0 if OK * -1 ErrorBadBarCodeSyntax * -2 ErrorBarCodeRequired * -3 ErrorBarCodeAlreadyUsed */ - function check_barcode($valuetotest) + function check_barcode($valuetotest,$typefortest) { global $conf; if (! empty($conf->barcode->enabled) && ! empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) @@ -515,9 +520,9 @@ class Product extends CommonObject } $mod = new $module(); - - dol_syslog(get_class($this)."::check_barcode barcode=".$valuetotest." module=".$module); - $result = $mod->verif($this->db, $valuetotest, $this, 0); + + dol_syslog(get_class($this)."::check_barcode value=".$valuetotest." type=".$typefortest." module=".$module); + $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest); return $result; } else @@ -566,7 +571,7 @@ class Product extends CommonObject if (empty($this->country_id)) $this->country_id = 0; - //Gencod + // Barcode value $this->barcode=trim($this->barcode); $this->accountancy_code_buy = trim($this->accountancy_code_buy); @@ -581,7 +586,7 @@ class Product extends CommonObject { $result = $this->verify(); // We don't check when update called during a create because verify was already done } - + if ($result >= 0) { // For automatic creation @@ -595,7 +600,7 @@ class Product extends CommonObject $sql.= ", recuperableonly = " . $this->tva_npr; $sql.= ", localtax1_tx = " . $this->localtax1_tx; $sql.= ", localtax2_tx = " . $this->localtax2_tx; - + $sql.= ", barcode = ". (empty($this->barcode)?"null":"'".$this->db->escape($this->barcode)."'"); $sql.= ", fk_barcode_type = ". (empty($this->barcode_type)?"null":$this->db->escape($this->barcode_type)); @@ -622,7 +627,7 @@ class Product extends CommonObject $sql.= ", accountancy_code_sell= '" . $this->accountancy_code_sell."'"; $sql.= ", desiredstock = " . ((isset($this->desiredstock) && $this->desiredstock != '') ? $this->desiredstock : "null"); $sql.= " WHERE rowid = " . $id; - + dol_syslog(get_class($this)."update sql=".$sql); $resql=$this->db->query($sql); if ($resql) diff --git a/htdocs/product/fiche.php b/htdocs/product/fiche.php index 21fa78abd7f..0e85f897d29 100644 --- a/htdocs/product/fiche.php +++ b/htdocs/product/fiche.php @@ -36,6 +36,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; if (! empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; @@ -122,7 +123,7 @@ if (empty($reshook)) // Barcode value if ($action == 'setbarcode' && $createbarcode) { - $result=$object->check_barcode(GETPOST('barcode')); + $result=$object->check_barcode(GETPOST('barcode'),GETPOT('barcode_type_code')); if ($result >= 0) { @@ -206,7 +207,20 @@ if (empty($reshook)) $object->barcode_type = GETPOST('fk_barcode_type'); $object->barcode = GETPOST('barcode'); - + // Set barcode_type_xxx from barcode_type id + $stdobject=new GenericObject($db); + $stdobject->element='product'; + $stdobject->barcode_type=GETPOST('fk_barcode_type'); + $result=$stdobject->fetch_barcode(); + if ($result < 0) + { + $error++; + setEventMessage('Failed to get bar code type information '.$stdobject->error, 'errors'); + } + $object->barcode_type_code = $stdobject->barcode_type_code; + $object->barcode_type_coder = $stdobject->barcode_type_coder; + $object->barcode_type_label = $stdobject->barcode_type_label; + $object->description = dol_htmlcleanlastbr(GETPOST('desc')); $object->url = GETPOST('url'); $object->note = dol_htmlcleanlastbr(GETPOST('note')); @@ -307,7 +321,20 @@ if (empty($reshook)) $object->barcode_type = GETPOST('fk_barcode_type'); $object->barcode = GETPOST('barcode'); - + // Set barcode_type_xxx from barcode_type id + $stdobject=new GenericObject($db); + $stdobject->element='product'; + $stdobject->barcode_type=GETPOST('fk_barcode_type'); + $result=$stdobject->fetch_barcode(); + if ($result < 0) + { + $error++; + setEventMessage('Failed to get bar code type information '.$stdobject->error, 'errors'); + } + $object->barcode_type_code = $stdobject->barcode_type_code; + $object->barcode_type_coder = $stdobject->barcode_type_coder; + $object->barcode_type_label = $stdobject->barcode_type_label; + $object->accountancy_code_sell = GETPOST('accountancy_code_sell'); $object->accountancy_code_buy = GETPOST('accountancy_code_buy'); @@ -798,8 +825,8 @@ else if ($showbarcode) { - print ''.$langs->trans('BarcodeType').''; - if (isset($_POST['fk_barcode_type'])) + print ''.$langs->trans('BarcodeType').''; + if (isset($_POST['fk_barcode_type'])) { $fk_barcode_type=GETPOST('fk_barcode_type'); } @@ -1066,6 +1093,7 @@ else } else { + $fk_barcode_type=$object->barcode_type; if (empty($fk_barcode_type) && ! empty($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE)) $fk_barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE; } require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php'; @@ -1250,7 +1278,7 @@ else if (empty($conf->global->PRODUCT_DISABLE_CUSTOM_INFO)) $nblignes+=2; if ($object->isservice()) $nblignes++; else $nblignes+=4; - + // Photo if ($showphoto || $showbarcode) { @@ -1260,7 +1288,7 @@ else if ($showbarcode) print $form->showbarcode($object); print ''; } - + print ''; // Type @@ -1272,7 +1300,7 @@ else print $form->editfieldval("Type",'fk_product_type',$object->type,$object,$user->rights->produit->creer||$user->rights->service->creer,$typeformat); print ''; } - + if ($showbarcode) { // Barcode type @@ -1309,6 +1337,7 @@ else print '
'; print ''; print ''; + print ''; print ''; print ' '; }