From 2d3c700a7db9b7b149cc7382013552b3cd517b6c Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 16 Mar 2014 17:37:54 +0100
Subject: [PATCH] Fix: When cloning product, autogenerating barcode must not
 use same number. Qual: Moved dedicated permission of module to "advance"
 permissions.

---
 htdocs/core/lib/functions.lib.php             |  8 ++---
 htdocs/core/modules/DolibarrModules.class.php |  2 +-
 htdocs/core/modules/modBarcode.class.php      | 16 +++------
 htdocs/langs/en_US/products.lang              |  2 +-
 htdocs/product/class/product.class.php        | 17 ++++++----
 htdocs/product/fiche.php                      | 34 +++++++++++++------
 htdocs/societe/soc.php                        |  5 +--
 7 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 253ff1c5cc6..f59a9d20b80 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -2236,10 +2236,10 @@ function info_admin($text, $infoonimgalt = 0, $nodiv=0)
  *	Toutefois, il faut essayer de ne l'appeler qu'au sein de pages php, les classes devant
  *	renvoyer leur erreur par l'intermediaire de leur propriete "error".
  *
- *	@param	 DoliDB	$db      	Database handler
- *	@param  string	$error		String or array of errors strings to show
- *	@return void
- *  @see    dol_htmloutput_errors
+ *	@param	 	DoliDB	$db      	Database handler
+ *	@param  	mixed	$error		String or array of errors strings to show
+ *	@return 	void
+ *  @see    	dol_htmloutput_errors
  */
 function dol_print_error($db='',$error='')
 {
diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php
index 62a54437cd6..be498b04413 100644
--- a/htdocs/core/modules/DolibarrModules.class.php
+++ b/htdocs/core/modules/DolibarrModules.class.php
@@ -114,7 +114,7 @@ abstract class DolibarrModules
                 $sql=str_replace('__ENTITY__', $conf->entity, $sql);
 
                 dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror." sql=".$sql, LOG_DEBUG);
-                $result=$this->db->query($sql);
+                $result=$this->db->query($sql, $ignoreerror);
                 if (! $result)
                 {
                     if (! $ignoreerror)
diff --git a/htdocs/core/modules/modBarcode.class.php b/htdocs/core/modules/modBarcode.class.php
index bbc16750dd6..c51e177f97c 100644
--- a/htdocs/core/modules/modBarcode.class.php
+++ b/htdocs/core/modules/modBarcode.class.php
@@ -28,7 +28,7 @@
 include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
 
 /**
- *	Classe de description et activation du module Barcode
+ *	Class to describe Barcode
  */
 class modBarcode extends DolibarrModules
 {
@@ -77,22 +77,16 @@ class modBarcode extends DolibarrModules
 		$this->rights_class = 'barcode';
 
 		$this->rights[1][0] = 300; // id de la permission
-		$this->rights[1][1] = 'Lire les codes barres'; // libelle de la permission
+		$this->rights[1][1] = 'Read barcodes'; // libelle de la permission
 		$this->rights[1][2] = 'r'; // type de la permission (deprecie a ce jour)
 		$this->rights[1][3] = 1; // La permission est-elle une permission par defaut
-		$this->rights[1][4] = 'lire';
+		$this->rights[1][4] = 'lire_advance';
 
 		$this->rights[2][0] = 301; // id de la permission
-		$this->rights[2][1] = 'Creer/modifier les codes barres'; // libelle de la permission
+		$this->rights[2][1] = 'Create/modify barcodes'; // libelle de la permission
 		$this->rights[2][2] = 'w'; // type de la permission (deprecie a ce jour)
 		$this->rights[2][3] = 0; // La permission est-elle une permission par defaut
-		$this->rights[2][4] = 'creer';
-
-		$this->rights[4][0] = 302; // id de la permission
-		$this->rights[4][1] = 'Supprimer les codes barres'; // libelle de la permission
-		$this->rights[4][2] = 'd'; // type de la permission (deprecie a ce jour)
-		$this->rights[4][3] = 0; // La permission est-elle une permission par defaut
-		$this->rights[4][4] = 'supprimer';
+		$this->rights[2][4] = 'creer_advance';
 
         // Main menu entries
         $r=0;
diff --git a/htdocs/langs/en_US/products.lang b/htdocs/langs/en_US/products.lang
index a84d929550a..802ada84a54 100644
--- a/htdocs/langs/en_US/products.lang
+++ b/htdocs/langs/en_US/products.lang
@@ -168,7 +168,7 @@ CloneProduct=Clone product or service
 ConfirmCloneProduct=Are you sure you want to clone product or service <b>%s</b> ?
 CloneContentProduct=Clone all main informations of product/service
 ClonePricesProduct=Clone main informations and prices
-CloneCompositionProduct=Clone product/service composition
+CloneCompositionProduct=Clone virtual product/services
 ProductIsUsed=This product is used
 NewRefForClone=Ref. of new product/service
 CustomerPrices=Customers prices
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index fbe46281e00..818c0479c66 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -302,9 +302,9 @@ class Product extends CommonObject
 		$this->db->begin();
 
         // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
-        if ($this->barcode == -1)  $this->get_barcode($this,$this->barcode_type_code);
+		if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code);
 
-        // Check more parameters
+		// Check more parameters
         // If error, this->errors[] is filled
         $result = $this->verify();
 
@@ -582,7 +582,7 @@ class Product extends CommonObject
         if ($result >= 0)
         {
 	        // For automatic creation
-	        if ($this->barcode == -1) $this->get_barcode($this,$this->barcode_type_code);
+	        if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code);
 
 			$sql = "UPDATE ".MAIN_DB_PREFIX."product";
 			$sql.= " SET label = '" . $this->db->escape($this->libelle) ."'";
@@ -3259,16 +3259,18 @@ class Product extends CommonObject
 	}
 
     /**
-     *  Attribut un code barre a partir du module de controle des codes.
+     *  Get a barcode from the module to generate barcode values.
      *  Return value is stored into this->barcode
      *
      *	@param	Product		$object		Object product or service
-     *	@param	int			$type		Barcode type (ean, isbn, ...)
+     *	@param	string		$type		Barcode type (ean, isbn, ...)
      *  @return void
      */
     function get_barcode($object,$type='')
     {
         global $conf;
+        
+        $result='';
         if (! empty($conf->global->BARCODE_PRODUCT_ADDON_NUM))
         {
             $dirsociete=array_merge(array('/core/modules/barcode/'),$conf->modules_parts['barcode']);
@@ -3280,10 +3282,11 @@ class Product extends CommonObject
             $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
             $mod = new $var;
 
-            $this->barcode = $mod->getNextValue($object,$type);
+            $result=$mod->getNextValue($object,$type);
 
-            dol_syslog(get_class($this)."::get_barcode barcode=".$this->barcode." module=".$var);
+            dol_syslog(get_class($this)."::get_barcode barcode=".$result." module=".$var);
         }
+        return $result;
     }
 
     /**
diff --git a/htdocs/product/fiche.php b/htdocs/product/fiche.php
index b966c089d45..11826de4570 100644
--- a/htdocs/product/fiche.php
+++ b/htdocs/product/fiche.php
@@ -94,6 +94,9 @@ $hookmanager->initHooks(array('productcard'));
  * Actions
  */
 
+$createbarcode=empty($conf->barcode->enabled)?0:1;
+if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->creer_advance)) $createbarcode=0;
+
 $parameters=array('id'=>$id, 'ref'=>$ref, 'objcanvas'=>$objcanvas);
 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action);    // Note that $action and $object may have been modified by some hooks
 $error=$hookmanager->error; $errors=$hookmanager->errors;
@@ -109,15 +112,15 @@ if (empty($reshook))
     }
 
     // Barcode type
-    if ($action ==	'setfk_barcode_type' && $user->rights->barcode->creer)
+    if ($action ==	'setfk_barcode_type' && $createbarcode)
     {
-    	$result = $object->setValueFrom('fk_barcode_type', GETPOST('fk_barcode_type'));
+        $result = $object->setValueFrom('fk_barcode_type', GETPOST('fk_barcode_type'));
     	header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
     	exit;
     }
 
     // Barcode value
-    if ($action ==	'setbarcode' && $user->rights->barcode->creer)
+    if ($action ==	'setbarcode' && $createbarcode)
     {
     	$result=$object->check_barcode(GETPOST('barcode'));
 
@@ -354,7 +357,8 @@ if (empty($reshook))
                 $object->status = 0;
                 $object->status_buy = 0;
                 $object->id = null;
-
+                $object->barcode = -1;
+                
                 if ($object->check())
                 {
                     $id = $object->create($user);
@@ -401,8 +405,16 @@ if (empty($reshook))
                         else
                         {
                             $db->rollback();
-                            setEventMessage($langs->trans($object->error), 'errors');
-                            dol_print_error($db,$object->error);
+                            if (count($object->errors)) 
+                            {
+                            	setEventMessage($object->errors, 'errors');
+                            	dol_print_error($db,$object->errors);
+                            }
+                            else 
+                            {
+                            	setEventMessage($langs->trans($object->error), 'errors');
+                            	dol_print_error($db,$object->error);
+                            }
                         }
                     }
                 }
@@ -780,8 +792,8 @@ else
 			print '</td></tr>';
 		}
 
-
-        $showbarcode=(! empty($conf->barcode->enabled) && $user->rights->barcode->lire);
+        $showbarcode=empty($conf->barcode->enabled)?0:1;
+        if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode=0;
 
         if ($showbarcode)
         {
@@ -1038,7 +1050,8 @@ else
 			}
 
             // Barcode
-            $showbarcode=(! empty($conf->barcode->enabled) && $user->rights->barcode->lire);
+            $showbarcode=empty($conf->barcode->enabled)?0:1;
+            if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode=0;
 
 	        if ($showbarcode)
 	        {
@@ -1207,7 +1220,8 @@ else
             dol_fiche_head($head, 'card', $titre, 0, $picto);
 
             $showphoto=$object->is_photo_available($conf->product->multidir_output[$object->entity]);
-            $showbarcode=(! empty($conf->barcode->enabled) && $user->rights->barcode->lire);
+            $showbarcode=empty($conf->barcode->enabled)?0:1;
+            if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode=0;
 
             // En mode visu
             print '<table class="border" width="100%"><tr>';
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index 5731ce0196a..3ff26dcffcd 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -1476,8 +1476,9 @@ else
         dol_htmloutput_errors($error,$errors);
 
         $showlogo=$object->logo;
-        $showbarcode=(! empty($conf->barcode->enabled) && $user->rights->barcode->lire);
-
+        $showbarcode=empty($conf->barcode->enabled)?0:1;
+        if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode=0;
+        
         print '<table class="border" width="100%">';
 
         // Ref
-- 
GitLab