Skip to content

Commit

Permalink
Fix: [ bug #1401 ] Problème avec la génération des codes barres
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed May 25, 2014
1 parent 6137d7e commit 4b9d1b9
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 53 deletions.
2 changes: 1 addition & 1 deletion htdocs/admin/barcode.php
Expand Up @@ -64,7 +64,7 @@
{
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'))
Expand Down
14 changes: 7 additions & 7 deletions htdocs/core/class/commonobject.class.php
Expand Up @@ -627,25 +627,25 @@ function fetch_thirdparty()

/**
* 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
Expand Down
6 changes: 3 additions & 3 deletions htdocs/core/class/html.form.class.php
Expand Up @@ -4087,11 +4087,11 @@ function showbarcode(&$object,$width=100)
// 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 '<!-- ErrorFetchBarcode -->';
}

// 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 ='<!-- url barcode = '.$url.' -->';
Expand Down
42 changes: 26 additions & 16 deletions htdocs/core/modules/barcode/mod_barcode_product_standard.php
Expand Up @@ -161,20 +161,23 @@ function getNextValue($objproduct,$type='')
/**
* 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;
Expand All @@ -190,7 +193,7 @@ function verif($db, &$code, $product, $type)
}
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)
Expand All @@ -215,7 +218,7 @@ function verif($db, &$code, $product, $type)
}
}

dol_syslog(get_class($this)."::verif type=".$type." result=".$result);
dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
return $result;
}

Expand Down Expand Up @@ -254,12 +257,13 @@ function verif_dispo($db, $code, $product)
}

/**
* 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;

Expand All @@ -273,7 +277,13 @@ function verif_syntax($code)
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;
}
Expand Down
1 change: 1 addition & 0 deletions htdocs/core/modules/modProduct.class.php
Expand Up @@ -245,6 +245,7 @@ function __construct($db)
$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);
Expand Down
6 changes: 3 additions & 3 deletions htdocs/core/modules/modService.class.php
@@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
Expand All @@ -17,7 +17,6 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
Expand Down Expand Up @@ -209,7 +208,8 @@ function __construct($db)
$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)
Expand Down
35 changes: 20 additions & 15 deletions htdocs/product/class/product.class.php
Expand Up @@ -267,7 +267,10 @@ function create($user,$notrigger=0)
$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';
Expand All @@ -277,7 +280,7 @@ function create($user,$notrigger=0)
{
// 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')
{
Expand Down Expand Up @@ -306,11 +309,11 @@ function create($user,$notrigger=0)

// 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";
Expand Down Expand Up @@ -453,7 +456,8 @@ function create($user,$notrigger=0)


/**
* 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
*/
Expand All @@ -470,7 +474,7 @@ function verify()
$result = -2;
}

$rescode = $this->check_barcode($this->barcode);
$rescode = $this->check_barcode($this->barcode,$this->barcode_type_code);
if ($rescode <> 0)
{
if ($rescode == -1)
Expand All @@ -492,15 +496,16 @@ function verify()
}

/**
* 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))
Expand All @@ -515,9 +520,9 @@ function check_barcode($valuetotest)
}

$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
Expand Down Expand Up @@ -566,7 +571,7 @@ function update($id, $user, $notrigger=false, $action='update')

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);
Expand All @@ -581,7 +586,7 @@ function update($id, $user, $notrigger=false, $action='update')
{
$result = $this->verify(); // We don't check when update called during a create because verify was already done
}

if ($result >= 0)
{
// For automatic creation
Expand All @@ -595,7 +600,7 @@ function update($id, $user, $notrigger=false, $action='update')
$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));

Expand All @@ -622,7 +627,7 @@ function update($id, $user, $notrigger=false, $action='update')
$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)
Expand Down

0 comments on commit 4b9d1b9

Please sign in to comment.