2004-12-01 18:00:54 +01:00
< ? php
2007-01-28 23:11:42 +01:00
/* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2006 - 2007 Laurent Destailleur < eldy @ users . sourceforge . net >
2004-12-01 18:00:54 +01:00
*
* 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 2 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 , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
* or see http :// www . gnu . org /
*
* $Id $
*/
2006-03-12 16:28:54 +01:00
/**
\file htdocs / includes / modules / societe / mod_codeclient_zebre . class . php
\ingroup societe
\brief Fichier de la classe des gestion zebre des codes clients
\version $Revision $
*/
2006-03-25 13:28:39 +01:00
require_once ( DOL_DOCUMENT_ROOT . " /includes/modules/societe/modules_societe.class.php " );
2006-03-12 16:28:54 +01:00
/**
\class mod_codeclient_zebre
2006-03-25 13:28:39 +01:00
\brief Classe permettant la gestion zebre des codes tiers
2006-03-12 16:28:54 +01:00
*/
2006-03-25 13:28:39 +01:00
class mod_codeclient_zebre extends ModeleThirdPartyCode
2004-12-01 18:00:54 +01:00
{
2007-01-28 23:11:42 +01:00
var $nom ; // Nom du modele
2007-10-30 20:39:50 +01:00
var $code_modifiable ; // Can be changed if valid
var $code_modifiable_invalide ; // Can be changed if not valid
var $code_modifiable_null ; // Can be changed if not defined
var $code_null ; // Can be undefined
2007-09-18 00:00:39 +02:00
var $version ; // 'development', 'experimental', 'dolibarr'
2007-10-30 20:39:50 +01:00
var $code_auto ; // Num<75> rotation automatique
2004-12-01 18:00:54 +01:00
2007-01-28 23:11:42 +01:00
/** \brief Constructeur classe
*/
function mod_codeclient_zebre ()
2004-12-08 14:11:04 +01:00
{
2007-01-28 23:11:42 +01:00
$this -> nom = " Z<EFBFBD> bre " ;
2007-09-18 00:00:39 +02:00
$this -> version = " dolibarr " ;
2007-10-30 20:39:50 +01:00
$this -> code_modifiable = 0 ; // Can be changed if valid
$this -> code_modifiable_invalide = 1 ; // Can be changed if not valid
$this -> code_modifiable_null = 1 ; // Can be changed if not defined
$this -> code_null = 0 ; // Can be undefined
2007-09-18 00:00:39 +02:00
$this -> code_auto = 0 ;
2004-12-08 14:11:04 +01:00
}
2007-01-28 23:11:42 +01:00
/**
* \brief Renvoie la description du module
* \return string Texte descripif
*/
function info ( $langs )
{
return " V<EFBFBD> rifie si le code client est de la forme CCCC9999. Les quatres premi<6D> res lettres <20> tant une repr<70> sentation mn<6D> motechnique, suivi du code postal en 2 chiffres et un num<75> ro d'ordre pour la prise en compte des doublons. " ;
}
2004-12-01 18:00:54 +01:00
2007-01-28 23:11:42 +01:00
/** \brief Renvoi la description du module
* \return string Texte descripif
*/
function getExample ( $langs )
{
return " ABCD7501 " ;
}
2004-12-01 18:00:54 +01:00
2007-01-28 23:11:42 +01:00
/**
* \brief V<EFBFBD> rifie la validit<EFBFBD> du code
* \param $db Handler acces base
* \param $code Code a v<EFBFBD> rifier / corriger
* \param $soc Objet societe
2007-01-29 23:41:35 +01:00
* \return int < 0 si KO , 0 si OK
2007-01-28 23:11:42 +01:00
*/
function verif ( $db , & $code , $soc )
{
2007-01-29 23:41:35 +01:00
$result = 0 ;
2007-01-28 23:11:42 +01:00
$code = strtoupper ( trim ( $code ));
2007-01-29 23:41:35 +01:00
if ( ! $code && $this -> code_null )
{
$result = 0 ;
2007-01-28 23:11:42 +01:00
}
else
{
2007-01-29 23:41:35 +01:00
if ( $this -> verif_syntax ( $code ) == 0 )
{
$i = 1 ;
$is_dispo = $this -> verif_dispo ( $db , $code , $soc );
while ( $is_dispo <> 0 && $i < 99 )
{
$arr = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ;
$code = substr ( $code , 0 , 6 ) . substr ( " 00 " . $i , - 2 );
$is_dispo = $this -> verif_dispo ( $db , $code , $soc );
$i ++ ;
}
if ( $is_dispo <> 0 )
{
$result =- 3 ;
}
2007-01-28 23:11:42 +01:00
}
else
{
2007-01-29 23:41:35 +01:00
if ( strlen ( trim ( $code )) == 0 )
{
$result =- 2 ;
}
else
{
$result =- 1 ;
}
2007-01-28 23:11:42 +01:00
}
}
2007-01-29 23:41:35 +01:00
dolibarr_syslog ( " mod_codeclient_zebre::verif result= " . $result );
return $result ;
2007-01-28 23:11:42 +01:00
}
2004-12-01 18:00:54 +01:00
2007-01-29 23:41:35 +01:00
2007-01-28 23:11:42 +01:00
/**
* \brief Renvoi une valeur correcte
2007-01-29 23:41:35 +01:00
* \param $db Handler acces base
* \param $code Code reference eventuel
* \return string Code correct , < 0 si KO
2007-01-28 23:11:42 +01:00
*/
function get_correct ( $db , $code )
{
if ( $this -> verif_syntax ( $code ) == 0 )
{
$i = 1 ;
2007-01-29 23:41:35 +01:00
$is_dispo = $this -> verif_dispo ( $db , $code , $soc );
2007-01-28 23:11:42 +01:00
while ( $is_dispo <> 0 && $i < 99 )
{
$arr = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ;
$code = substr ( $code , 0 , 6 ) . substr ( " 00 " . $i , - 2 );
2007-01-29 23:41:35 +01:00
$is_dispo = $this -> verif_dispo ( $db , $code , $soc );
2007-01-28 23:11:42 +01:00
$i ++ ;
}
return $is_dispo ;
}
else
{
return - 1 ;
}
2004-12-07 17:10:49 +01:00
}
2007-01-28 23:11:42 +01:00
/**
2007-01-29 23:41:35 +01:00
* \brief Renvoi si un code est pris ou non ( par autre tiers )
* \param $db Handler acces base
* \param $code Code a verifier
* \param $soc Objet societe
* \return int 0 si dispo , < 0 si erreur
2007-01-28 23:11:42 +01:00
*/
2007-01-29 23:41:35 +01:00
function verif_dispo ( $db , $code , $soc )
2007-01-28 23:11:42 +01:00
{
$sql = " SELECT code_client FROM " . MAIN_DB_PREFIX . " societe " ;
2007-01-29 23:41:35 +01:00
$sql .= " WHERE code_client = ' " . $code . " ' " ;
2007-06-12 00:51:47 +02:00
$sql .= " AND rowid != ' " . $soc -> id . " ' " ;
2007-01-28 23:11:42 +01:00
2007-01-29 23:41:35 +01:00
$resql = $db -> query ( $sql );
if ( $resql )
2007-01-28 23:11:42 +01:00
{
2007-01-29 23:41:35 +01:00
if ( $db -> num_rows ( $resql ) == 0 )
2007-01-28 23:11:42 +01:00
{
return 0 ;
}
else
{
return - 1 ;
}
}
else
{
2007-01-29 23:41:35 +01:00
return - 2 ;
2007-01-28 23:11:42 +01:00
}
}
2004-12-07 17:10:49 +01:00
2004-12-01 18:00:54 +01:00
2007-01-29 23:41:35 +01:00
/**
* \brief Renvoi si un code respecte la syntaxe
* \param $code Code a verifier
* \return int 0 si OK , < 0 si KO
*/
2007-01-28 23:11:42 +01:00
function verif_syntax ( & $code )
2004-12-01 18:00:54 +01:00
{
2007-01-28 23:11:42 +01:00
$res = 0 ;
if ( strlen ( $code ) <> 8 )
{
$res = - 1 ;
}
else
{
if ( $this -> is_alpha ( substr ( $code , 0 , 4 )) == 0 && $this -> is_num ( substr ( $code , 4 , 4 )) == 0 )
{
$res = 0 ;
}
else
{
$res = - 2 ;
}
}
return $res ;
2004-12-01 18:00:54 +01:00
}
2007-01-28 23:11:42 +01:00
function is_alpha ( $str )
{
$ok = 0 ;
// Je n'ai pas trouv<75> de fonction pour tester une chaine alpha sans les caract<63> re accentu<74> s
// dommage
$alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
for ( $i = 0 ; $i < 4 ; $i ++ )
{
if ( strpos ( $alpha , substr ( $str , $i , 1 )) === false )
{
$ok ++ ;
}
}
return $ok ;
}
2004-12-01 18:00:54 +01:00
2007-01-28 23:11:42 +01:00
function is_num ( $str )
2004-12-01 18:00:54 +01:00
{
2007-01-28 23:11:42 +01:00
$ok = 0 ;
$alpha = '0123456789' ;
for ( $i = 0 ; $i < 4 ; $i ++ )
{
if ( strpos ( $alpha , substr ( $str , $i , 1 )) === false )
{
$ok ++ ;
}
}
return $ok ;
2004-12-01 18:00:54 +01:00
}
}
?>