diff --git a/htdocs/includes/modules/societe/mod_codeclient_tigre.php b/htdocs/includes/modules/societe/mod_codeclient_tigre.php index 8902370b12a..852fc436d92 100644 --- a/htdocs/includes/modules/societe/mod_codeclient_tigre.php +++ b/htdocs/includes/modules/societe/mod_codeclient_tigre.php @@ -49,6 +49,7 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode var $numbitcounter; // Nombre de chiffres du compteur var $substrBegin; var $substrEnd; + var $prefixIsRequired; /** \brief Constructeur classe @@ -62,6 +63,7 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode $this->code_modifiable_null = 1; $this->code_null = 0; $this->code_auto = 1; + $this->prefixIsRequired = 0; } @@ -284,17 +286,33 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode else if (is_object($objsoc) && !$objsoc->prefix_comm && $maskElement[$i] == '{pre}') { $error++; + $this->prefixIsRequired = 1; } else if (!is_object($objsoc) && $maskElement[$i] == '{pre}') { - $maskRebuild .= 'ABC'; + if (is_string($objsoc) && $objsoc) + { + $prefixLength = strlen($objsoc); + $maskRebuild .= $objsoc; + } + else if (is_bool($objsoc) && $objsoc == 0) + { + $prefixLength = 3; + $maskRebuild .= 'ABC'; + } + else + { + $error++; + $this->prefixIsRequired = 1; + } + if ($foundCounter==0) { - $substrBegin += 3; + $substrBegin += $prefixLength; } else { - $substrEnd += 3; + $substrEnd += $prefixLength; } } @@ -355,6 +373,7 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode //print 'begin='.$this->substrBegin.'
'; //print 'end='.$this->substrEnd.'
'; //print 'searchcode='.$this->searchcode.'
'; + //print $error; return $maskRebuild; } @@ -384,6 +403,10 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode { $result=-3; } + else if ($soc->prefixIsRequired && !$soc->prefix_comm) + { + $result=-4; + } else { $result=0; @@ -414,24 +437,7 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode */ function get_correct($db, $code) { - $return='001'; - $sql = "SELECT MAX(code_client) as maxval FROM ".MAIN_DB_PREFIX."societe"; - $resql=$db->query($sql); - if ($resql) - { - $obj=$db->fetch_object($resql); - if ($obj) - { - $newval=$obj->maxval+1; - $return=sprintf('%03d',$newval); - return $return; - } - } - else - { - return -1; - } } @@ -476,23 +482,6 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode function verif_syntax($code) { $res = 0; - - if (strlen($code) < 3) - { - $res = -1; - } - else - { - if (eregi('[0-9][0-9][0-9]+',$code)) - { - $res = 0; - } - else - { - $res = -2; - } - - } return $res; } @@ -503,17 +492,6 @@ class mod_codeclient_tigre extends ModeleThirdPartyCode function is_num($str) { $ok = 0; - - $alpha = '0123456789'; - - for ($i = 0 ; $i < length($str) ; $i++) - { - if (strpos($alpha, substr($str,$i, 1)) === false) - { - $ok++; - } - } - return $ok; } diff --git a/htdocs/soc.php b/htdocs/soc.php index d7d3ed85a19..f730ee53614 100644 --- a/htdocs/soc.php +++ b/htdocs/soc.php @@ -158,6 +158,10 @@ if ((! $_POST["getcustomercode"] && ! $_POST["getsuppliercode"]) $oldsoc=new Societe($db); $result=$oldsoc->fetch($socid); + // On vérifie si un tiers devient client ou fournisseur pour l'obtention d'un code automatiqe + if (!$soc->client && $soc->code_client == "automaticCode") $soc->code_client = ''; + if (!$soc->fournisseur && $soc->code_fournisseur == "automaticCode") $soc->code_fournisseur = ''; + $result = $soc->update($socid,$user,1,$oldsoc->codeclient_modifiable(),$oldsoc->codefournisseur_modifiable()); if ($result >= 0) { @@ -342,6 +346,7 @@ if ($_POST["getcustomercode"] || $_POST["getsuppliercode"] || print '
'; if ($modCodeClient->code_auto == 1) { + if ($soc->client == 1 || $soc->client == 2) print ''; print $langs->trans('AutomaticallyGenerated').' '; } else @@ -364,6 +369,7 @@ if ($_POST["getcustomercode"] || $_POST["getsuppliercode"] || print '
'; if ($modCodeFournisseur->code_auto == 1) { + if ($soc->fournisseur == 1) print ''; print $langs->trans('AutomaticallyGenerated').' '; } else @@ -616,7 +622,7 @@ elseif ($_GET["action"] == 'edit' || $_POST["action"] == 'edit') $soc->typent_id=$_POST["typent_id"]; $soc->effectif_id=$_POST["effectif_id"]; - $soc->tva_assuj = $_POST["assujtva_value"]; + $soc->tva_assuj = $_POST["assujtva_value"]; $soc->tva_intra_code=$_POST["tva_intra_code"]; $soc->tva_intra_num=$_POST["tva_intra_num"]; @@ -666,7 +672,12 @@ elseif ($_GET["action"] == 'edit' || $_POST["action"] == 'edit') print ''.$langs->trans('CustomerCode').''; print '
'; - if ($soc->codeclient_modifiable()) + if (!$soc->code_client && $modCodeClient->code_auto == 1) + { + if (!$soc->client) print ''; + print $langs->trans('AutomaticallyGenerated').' '; + } + else if ($soc->codeclient_modifiable()) { print ''; } @@ -676,7 +687,7 @@ elseif ($_GET["action"] == 'edit' || $_POST["action"] == 'edit') print ''; } print ''; - $s=$modCodeClient->getToolTip($langs,$soc,0); + $s=$modCodeClient->getToolTip($langs,$soc,0); print $form->textwithhelp('',$s,1); print '
'; @@ -690,7 +701,12 @@ elseif ($_GET["action"] == 'edit' || $_POST["action"] == 'edit') print '
'.$langs->trans('SupplierCode').''; print '
'; - if ($soc->codefournisseur_modifiable()) + if (!$soc->code_fournisseur && $modCodeFournisseur->code_auto == 1) + { + if (!$soc->fournisseur) print ''; + print $langs->trans('AutomaticallyGenerated').' '; + } + else if ($soc->codefournisseur_modifiable()) { print ''; } @@ -700,7 +716,7 @@ elseif ($_GET["action"] == 'edit' || $_POST["action"] == 'edit') print ''; } print ''; - $s=$modCodeFournisseur->getToolTip($langs,$soc,1); + $s=$modCodeFournisseur->getToolTip($langs,$soc,1); print $form->textwithhelp('',$s,1); print '
'; diff --git a/htdocs/societe.class.php b/htdocs/societe.class.php index ef9e32ead73..7b73c8b4d7d 100644 --- a/htdocs/societe.class.php +++ b/htdocs/societe.class.php @@ -81,6 +81,7 @@ class Societe var $prospect; var $fournisseur; + var $prefixIsRequired; var $code_client; var $code_fournisseur; var $code_compta; @@ -134,6 +135,10 @@ class Societe dolibarr_syslog("Societe::create ".$this->nom); $this->db->begin(); + + // Pour code automatique + if ($this->code_client == "automaticCode") $this->get_codeclient($this->prefix_comm,0); + if ($this->code_fournisseur == "automaticCode") $this->get_codefournisseur($this->prefix_comm,1); $result = $this->verify(); @@ -241,6 +246,10 @@ class Societe { $this->error .= "Ce code client est déjà utilisé.\n"; } + if ($rescode == -4) + { + $this->error .= "Vous devez renseigner le préfix.\n"; + } $result = -3; } } @@ -263,6 +272,10 @@ class Societe { $this->error .= "Ce code fournisseur est déjà utilisé.\n"; } + if ($rescode == -4) + { + $this->error .= "Vous devez renseigner le préfix.\n"; + } $result = -3; } } @@ -316,6 +329,10 @@ class Societe $this->effectif_id=trim($this->effectif_id); $this->forme_juridique_code=trim($this->forme_juridique_code); + + // Pour code client/fournisseur automatique + if ($this->code_client == "automaticCode") $this->get_codeclient($this->prefix_comm,0); + if ($this->code_fournisseur == "automaticCode") $this->get_codefournisseur($this->prefix_comm,1); $result = $this->verify(); // Verifie que nom obligatoire et code client ok et unique @@ -1416,6 +1433,45 @@ class Societe return $this->bank_account->verif(); } + /** + * \brief Attribut un code client à partir du module de controle des codes. + * \return code_client Code client automatique + */ + function get_codeclient($objsoc=0,$type=0) + { + global $conf; + if ($conf->global->SOCIETE_CODECLIENT_ADDON) + { + require_once DOL_DOCUMENT_ROOT.'/includes/modules/societe/'.$conf->global->SOCIETE_CODECLIENT_ADDON.'.php'; + $var = $conf->global->SOCIETE_CODECLIENT_ADDON; + $mod = new $var; + + $this->code_client = $mod->getNextValue($objsoc,$type); + $this->prefixIsRequired = $mod->prefixIsRequired; + + dolibarr_syslog("Societe::get_codeclient code_client=".$this->code_client." module=".$var); + } + } + + /** + * \brief Attribut un code fournisseur à partir du module de controle des codes. + * \return code_fournisseur Code fournisseur automatique + */ + function get_codefournisseur($objsoc=0,$type=1) + { + global $conf; + if ($conf->global->SOCIETE_CODEFOURNISSEUR_ADDON) + { + require_once DOL_DOCUMENT_ROOT.'/includes/modules/societe/'.$conf->global->SOCIETE_CODEFOURNISSEUR_ADDON.'.php'; + $var = $conf->global->SOCIETE_CODEFOURNISSEUR_ADDON; + $mod = new $var; + + $this->code_fournisseur = $mod->getNextValue($objsoc,$type); + $this->prefixIsRequired = $mod->prefixIsRequired; + + dolibarr_syslog("Societe::get_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$var); + } + } /** * \brief Verifie si un code client est modifiable en fonction des parametres