Explorar o código

Fix import of members

Laurent Destailleur %!s(int64=3) %!d(string=hai) anos
pai
achega
41bba0e879

+ 14 - 10
htdocs/core/modules/import/import_csv.modules.php

@@ -586,18 +586,22 @@ class ImportCsv extends ModeleImports
 										$arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
 									}
 								} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
-									$defaultref = '';
-									// TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
-									$obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
-									if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php")) {
-										require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
-										$modTask = new $obj;
-										$defaultref = $modTask->getNextValue(null, null);
-									}
-									if (is_numeric($defaultref) && $defaultref <= 0) {
+									if (strtolower($newval) == 'auto') {
 										$defaultref = '';
+
+										$classModForNumber = $objimport->array_import_convertvalue[0][$val]['class'];
+										$pathModForNumber = $objimport->array_import_convertvalue[0][$val]['path'];
+
+										if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
+											require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
+											$modForNumber = new $classModForNumber;
+											$defaultref = $modForNumber->getNextValue(null, null);
+										}
+										if (is_numeric($defaultref) && $defaultref <= 0) {
+											$defaultref = '';
+										}
+										$newval = $defaultref;
 									}
-									$newval = $defaultref;
 								} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute') {
 									$file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
 									$class = $objimport->array_import_convertvalue[0][$val]['class'];

+ 14 - 10
htdocs/core/modules/import/import_xlsx.modules.php

@@ -627,18 +627,22 @@ class ImportXlsx extends ModeleImports
 										$arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
 									}
 								} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
-									$defaultref = '';
-									// TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
-									$obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
-									if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT . "/core/modules/project/task/" . $conf->global->PROJECT_TASK_ADDON . ".php")) {
-										require_once DOL_DOCUMENT_ROOT . "/core/modules/project/task/" . $conf->global->PROJECT_TASK_ADDON . '.php';
-										$modTask = new $obj;
-										$defaultref = $modTask->getNextValue(null, null);
-									}
-									if (is_numeric($defaultref) && $defaultref <= 0) {
+									if (strtolower($newval) == 'auto') {
 										$defaultref = '';
+
+										$classModForNumber = $objimport->array_import_convertvalue[0][$val]['class'];
+										$pathModForNumber = $objimport->array_import_convertvalue[0][$val]['path'];
+
+										if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
+											require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
+											$modForNumber = new $classModForNumber;
+											$defaultref = $modForNumber->getNextValue(null, null);
+										}
+										if (is_numeric($defaultref) && $defaultref <= 0) {
+											$defaultref = '';
+										}
+										$newval = $defaultref;
 									}
-									$newval = $defaultref;
 								} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute') {
 									$file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
 									$class = $objimport->array_import_convertvalue[0][$val]['class'];

+ 183 - 0
htdocs/core/modules/member/mod_member_simple.php

@@ -0,0 +1,183 @@
+<?php
+/* Copyright (C) 2021		Laurent Destailleur	<eldy@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ * or see https://www.gnu.org/
+ */
+
+/**
+ *	\file       htdocs/core/modules/member/mod_member_simple.php
+ *	\ingroup    member
+ *	\brief      File with class to manage the numbering module Simple for member references
+ */
+
+require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_member.class.php';
+
+
+/**
+ * 	Class to manage the numbering module Simple for member references
+ */
+class mod_member_simple extends ModeleNumRefMembers
+{
+	/**
+	 * Dolibarr version of the loaded document
+	 * @var string
+	 */
+	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
+
+	public $prefix = 'MEM';
+
+	/**
+	 * @var string Error code (or message)
+	 */
+	public $error = '';
+
+	/**
+	 * @var string Nom du modele
+	 * @deprecated
+	 * @see $name
+	 */
+	public $nom = 'Simple';
+
+	/**
+	 * @var string model name
+	 */
+	public $name = 'Simple';
+
+
+	/**
+	 *  Return description of numbering module
+	 *
+	 *  @return     string      Text with description
+	 */
+	public function info()
+	{
+		global $langs;
+		return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
+	}
+
+
+	/**
+	 *  Return an example of numbering module values
+	 *
+	 * 	@return     string      Example
+	 */
+	public function getExample()
+	{
+		return $this->prefix."0501-0001";
+	}
+
+
+	/**
+	 *  Checks if the numbers already in the database do not
+	 *  cause conflicts that would prevent this numbering working.
+	 *
+	 *   @return     boolean     false if conflict, true if ok
+	 */
+	public function canBeActivated()
+	{
+		global $conf, $langs, $db;
+
+		$coyymm = '';
+		$max = '';
+
+		$posindice = strlen($this->prefix) + 6;
+		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
+		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
+		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
+		$sql .= " AND entity = ".$conf->entity;
+		$resql = $db->query($sql);
+		if ($resql) {
+			$row = $db->fetch_row($resql);
+			if ($row) {
+				$coyymm = substr($row[0], 0, 6);
+				$max = $row[0];
+			}
+		}
+		if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
+			return true;
+		} else {
+			$langs->load("errors");
+			$this->error = $langs->trans('ErrorNumRefModel', $max);
+			return false;
+		}
+	}
+
+
+	/**
+	 *  Return next value
+	 *
+	 *  @param  Societe		$objsoc		Object third party
+	 *  @param  Object		$object		Object we need next value for
+	 *  @return	string					Value if OK, 0 if KO
+	 */
+	public function getNextValue($objsoc, $object)
+	{
+		global $db, $conf;
+
+		/*
+		// First, we get the max value
+		$posindice = strlen($this->prefix) + 6;
+		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
+		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
+		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
+		$sql .= " AND entity = ".$conf->entity;
+
+		$resql = $db->query($sql);
+		if ($resql) {
+			$obj = $db->fetch_object($resql);
+			if ($obj) {
+				$max = intval($obj->max);
+			} else {
+				$max = 0;
+			}
+		} else {
+			dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
+			return -1;
+		}
+
+		$date = empty($object->date_c) ? dol_now() : $object->date_c;
+
+		//$yymm = strftime("%y%m",time());
+		$yymm = strftime("%y%m", $date);
+
+		if ($max >= (pow(10, 4) - 1)) {
+			$num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
+		} else {
+			$num = sprintf("%04s", $max + 1);
+		}
+
+		dol_syslog("mod_member_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
+		return $this->prefix.$yymm."-".$num;
+		*/
+
+		// For the moment, the ref of a member is the rowid
+		$sql = "SELECT MAX(rowid) as max";
+		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
+
+		$resql = $db->query($sql);
+		if ($resql) {
+			$obj = $db->fetch_object($resql);
+			if ($obj) {
+				$max = intval($obj->max);
+			} else {
+				$max = 0;
+			}
+		} else {
+			dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
+			return -1;
+		}
+		return ($max + 1);
+	}
+}

+ 96 - 0
htdocs/core/modules/member/modules_member.class.php

@@ -60,3 +60,99 @@ abstract class ModelePDFMember extends CommonDocGenerator
 		return $list;
 	}
 }
+
+
+
+/**
+ *  Classe mere des modeles de numerotation des references de members
+ */
+abstract class ModeleNumRefMembers
+{
+	/**
+	 * @var string Error code (or message)
+	 */
+	public $error = '';
+
+	/**
+	 *  Return if a module can be used or not
+	 *
+	 *  @return		boolean     true if module can be used
+	 */
+	public function isEnabled()
+	{
+		return true;
+	}
+
+	/**
+	 *  Renvoi la description par defaut du modele de numerotation
+	 *
+	 *  @return     string      Texte descripif
+	 */
+	public function info()
+	{
+		global $langs;
+		$langs->load("members");
+		return $langs->trans("NoDescription");
+	}
+
+	/**
+	 *  Return an example of numbering
+	 *
+	 *  @return     string      Example
+	 */
+	public function getExample()
+	{
+		global $langs;
+		$langs->load("members");
+		return $langs->trans("NoExample");
+	}
+
+	/**
+	 *  Checks if the numbers already in the database do not
+	 *  cause conflicts that would prevent this numbering working.
+	 *
+	 *  @return     boolean     false if conflict, true if ok
+	 */
+	public function canBeActivated()
+	{
+		return true;
+	}
+
+	/**
+	 *  Renvoi prochaine valeur attribuee
+	 *
+	 *	@param	Societe		$objsoc		Object third party
+	 *  @param  Object		$object		Object we need next value for
+	 *	@return	string					Valeur
+	 */
+	public function getNextValue($objsoc, $object)
+	{
+		global $langs;
+		return $langs->trans("NotAvailable");
+	}
+
+	/**
+	 *  Renvoi version du module numerotation
+	 *
+	 *  @return     string      Valeur
+	 */
+	public function getVersion()
+	{
+		global $langs;
+		$langs->load("admin");
+
+		if ($this->version == 'development') {
+			return $langs->trans("VersionDevelopment");
+		} elseif ($this->version == 'experimental') {
+			return $langs->trans("VersionExperimental");
+		} elseif ($this->version == 'dolibarr') {
+			return DOL_VERSION;
+		} elseif ($this->version) {
+			return $this->version;
+		} else {
+			return $langs->trans("NotAvailable");
+		}
+	}
+}
+
+

+ 26 - 1
htdocs/core/modules/modAdherent.class.php

@@ -338,6 +338,7 @@ class modAdherent extends DolibarrModules
 		$this->import_tables_array[$r] = array('a'=>MAIN_DB_PREFIX.'adherent', 'extra'=>MAIN_DB_PREFIX.'adherent_extrafields');
 		$this->import_tables_creator_array[$r] = array('a'=>'fk_user_author'); // Fields to store import user id
 		$this->import_fields_array[$r] = array(
+			'a.ref' => 'Member Ref*',
 			'a.civility'=>"UserTitle", 'a.lastname'=>"Lastname*", 'a.firstname'=>"Firstname", 'a.gender'=>"Gender", 'a.login'=>"Login*", "a.pass"=>"Password",
 			"a.fk_adherent_type"=>"MemberType*", 'a.morphy'=>'MemberNature*', 'a.societe'=>'Company', 'a.address'=>"Address", 'a.zip'=>"Zip", 'a.town'=>"Town",
 			'a.state_id'=>'StateId', 'a.country'=>"CountryId", 'a.phone'=>"PhonePro", 'a.phone_perso'=>"PhonePerso", 'a.phone_mobile'=>"PhoneMobile",
@@ -358,7 +359,27 @@ class modAdherent extends DolibarrModules
 			}
 		}
 		// End add extra fields
-		$this->import_convertvalue_array[$r] = array();
+		$this->import_convertvalue_array[$r] = array(
+			'a.ref'=>array(
+				'rule'=>'getrefifauto',
+				'class'=>(empty($conf->global->MEMBER_ADDON) ? 'mod_member_simple' : $conf->global->MEMBER_ADDON),
+				'path'=>"/core/modules/member/".(empty($conf->global->MEMBER_ADDON) ? 'mod_member_simple' : $conf->global->MEMBER_ADDON).'.php'
+			),
+			'a.state_id' => array(
+				'rule' => 'fetchidfromcodeid',
+				'classfile' => '/core/class/cstate.class.php',
+				'class' => 'Cstate',
+				'method' => 'fetch',
+				'dict' => 'DictionaryStateCode'
+			),
+			'a.country' => array(
+				'rule' => 'fetchidfromcodeid',
+				'classfile' => '/core/class/ccountry.class.php',
+				'class' => 'Ccountry',
+				'method' => 'fetch',
+				'dict' => 'DictionaryCountry'
+			)
+		);
 		if (!empty($conf->societe->enabled)) {
 			$this->import_convertvalue_array[$r]['a.fk_soc'] = array('rule'=>'fetchidfromref', 'classfile'=>'/societe/class/societe.class.php', 'class'=>'Societe', 'method'=>'fetch', 'element'=>'ThirdParty');
 		}
@@ -367,6 +388,7 @@ class modAdherent extends DolibarrModules
 			'a.civility'=>'code@'.MAIN_DB_PREFIX.'c_civility', 'a.fk_adherent_type'=>'rowid@'.MAIN_DB_PREFIX.'adherent_type', 'a.morphy'=>'(phy|mor)',
 			'a.statut'=>'^[0|1]', 'a.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$', 'a.datefin'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$');
 		$this->import_examplevalues_array[$r] = array(
+			'a.ref'=>"auto or MEM2010-1234",
 			'a.civility'=>"MR", 'a.lastname'=>'Smith', 'a.firstname'=>'John', 'a.gender'=>'man or woman', 'a.login'=>'jsmith', 'a.pass'=>'passofjsmith', 'a.fk_adherent_type'=>'1',
 			'a.morphy'=>'"mor" or "phy"', 'a.societe'=>'JS company', 'a.address'=>'21 jump street', 'a.zip'=>'55000', 'a.town'=>'New York', 'a.country'=>'1',
 			'a.email'=>'jsmith@example.com', 'a.birth'=>'1972-10-10', 'a.statut'=>"0 or 1", 'a.note_public'=>"This is a public comment on member",
@@ -375,6 +397,9 @@ class modAdherent extends DolibarrModules
 		if (!empty($conf->societe->enabled)) {
 			$this->import_examplevalues_array[$r]['a.fk_soc'] = "rowid or name";
 		}
+		//$this->import_run_sql_after_array[$r] = array(
+		//	'UPDATE '.MAIN_DB_PREFIX."adherent as a SET a.ref = a.rowid WHERE a.ref LIKE '(PROV%)'"
+		//);
 
 		// Cronjobs
 		$arraydate = dol_getdate(dol_now());

+ 1 - 1
htdocs/core/modules/modProjet.class.php

@@ -309,7 +309,7 @@ class modProjet extends DolibarrModules
 			$this->import_fieldshidden_array[$r] = array('t.fk_user_creat'=>'user->id', 'extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'projet_task'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
 			$this->import_convertvalue_array[$r] = array(
 				't.fk_projet'=>array('rule'=>'fetchidfromref', 'classfile'=>'/projet/class/project.class.php', 'class'=>'Project', 'method'=>'fetch', 'element'=>'Project'),
-				't.ref'=>array('rule'=>'getrefifauto')
+				't.ref'=>array('rule'=>'getrefifauto', 'class'=>(empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON), 'path'=>"/core/modules/project/task/".(empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON).'.php')
 			);
 			//$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
 			$this->import_regex_array[$r] = array('t.dateo'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$', 't.datee'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$', 't.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$');

+ 1 - 1
htdocs/core/modules/modStock.class.php

@@ -415,7 +415,7 @@ class modStock extends DolibarrModules
 		);
 		$this->import_updatekeys_array[$r] = array('ps.fk_product'=>'Product', 'ps.fk_entrepot'=>"Warehouse");
 		$this->import_run_sql_after_array[$r] = array(    // Because we may change data that are denormalized, we must update dernormalized data after.
-			'UPDATE '.MAIN_DB_PREFIX.'product p SET p.stock= (SELECT SUM(ps.reel) FROM '.MAIN_DB_PREFIX.'product_stock ps WHERE ps.fk_product = p.rowid);'
+			'UPDATE '.MAIN_DB_PREFIX.'product as p SET p.stock = (SELECT SUM(ps.reel) FROM '.MAIN_DB_PREFIX.'product_stock ps WHERE ps.fk_product = p.rowid);'
 		);
 	}