Fix: optimisation du code

This commit is contained in:
Regis Houssin
2007-05-19 17:14:14 +00:00
parent 0b1d544dbd
commit b0266b641d
12 changed files with 959 additions and 1200 deletions

View File

@@ -38,9 +38,16 @@ require_once(DOL_DOCUMENT_ROOT ."/includes/modules/commande/modules_commande.php
*/
class mod_commande_saphir extends ModeleNumRefCommandes
{
var $prefixorder='';
var $ordernummatrice='';
var $error='';
var $prefix;
var $matrice;
var $numMatrice = Array();
var $yy;
var $mm;
var $numbitcounter;
var $searchLast;
var $searchLastWithNoYear;
var $searchLastWithPreviousYear;
var $error = '';
/** \brief Constructeur
*/
@@ -120,129 +127,149 @@ function info()
*/
function getExample()
{
global $conf;
global $conf,$langs;
$this->prefix = $conf->global->COMMANDE_NUM_PREFIX;
$this->matrice = $conf->global->COMMANDE_NUM_MATRICE;
if ($this->matrice != '')
{
$resultatMatrice = Array();
$numMatrice = '';
$matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: C pour commande), COM : prefix du client
$matriceYear = "[A]{2,4}"; // l'ann<6E>e est sur 2 ou 4 chiffres
$matriceMonth = "[M]{2}"; // le mois est sur 2 chiffres
$matriceCounter = "[C]{1,}"; //le compteur a un nombre de chiffres libre
$matriceTiret = "[-]{1}"; // on recherche si il y a des tirets de s<>paration
$matrice = Array('prefix'=>$matricePrefix,
'year'=>$matriceYear,
'month'=>$matriceMonth,
'counter'=>$matriceCounter
);
// on d<>termine l'emplacement des tirets
$resultTiret = preg_split('/'.$matriceTiret.'/',$this->matrice, -1, PREG_SPLIT_OFFSET_CAPTURE);
$j = 0;
// on d<>termine les objets de la matrice
for ($i = 0; $i < count($resultTiret); $i++)
{
foreach($resultTiret[$i] as $idResultTiret => $valueResultTiret)
{
// Ajout des tirets
if ($j != $resultTiret[$i][1])
{
$numMatrice .= '-';
$j = $resultTiret[$i][1];
}
foreach($matrice as $idMatrice => $valueMatrice)
{
$resultCount = eregi(''.$valueMatrice.'',$valueResultTiret,$resultatMatrice);
if ($resultCount)
{
// On r<>cup<75>re le pr<70>fix utilis<69>
if ($idMatrice == 'prefix' && $resultatMatrice[0] == 'COM')
{
if ($objsoc->prefix_comm)
{
$prefix = $objsoc->prefix_comm;
}
else
{
$prefix = 'COM';
}
$numMatrice .= $prefix;
}
else if ($idMatrice == 'prefix' && $resultatMatrice[0] == 'PREF')
{
$prefix = $this->prefix;
$numMatrice .= $prefix;
}
else if ($idMatrice == 'year')
{
// On r<>cup<75>re le nombre de chiffres pour l'ann<6E>e
$numbityear = $resultCount;
// On d<>fini le mois du d<>but d'ann<6E>e fiscale
$fiscal_current_month = date("n");
$create_month = $fiscal_current_month;
// On change d'ann<6E>e fiscal si besoin
if($conf->global->SOCIETE_FISCAL_MONTH_START && $fiscal_current_month >= $conf->global->SOCIETE_FISCAL_MONTH_START && $create_month >= $conf->global->SOCIETE_FISCAL_MONTH_START)
{
$yy = substr(strftime("%Y",mktime(0,0,0,date("m"),date("d"),date("Y")+1)),$numbityear);
}
else
{
$yy = substr(strftime("%Y",time()),$numbityear);
}
$numMatrice .= $yy;
}
else if ($idMatrice == 'month')
{
// On r<>cup<75>re le mois si besoin
$mm = strftime("%m",time());
$numMatrice .= $mm;
}
else if ($idMatrice == 'counter')
{
// On r<>cup<75>re le nombre de chiffres pour le compteur
$numbitcounter = $resultCount;
}
}
}
}
}
// On r<>cup<75>re le nombre de chiffres du compteur
$arg = '%0'.$numbitcounter.'s';
$num = sprintf($arg,$conf->global->COMMANDE_NUM_DELTA?$conf->global->COMMANDE_NUM_DELTA:1);
$numExample = '';
// Construction de l'exemple de num<75>rotation
$numExample = $numMatrice.$num;
return $numExample;
//On construit la matrice
$buildResult = $this->buildMatrice();
if ($buildResult == 1)
{
// On r<>cup<75>re le nombre de chiffres du compteur
$arg = '%0'.$this->numbitcounter.'s';
$num = sprintf($arg,$conf->global->COMMANDE_NUM_DELTA?$conf->global->COMMANDE_NUM_DELTA:1);
//On construit le num<75>ro <20> partir de la matrice
foreach($this->numMatrice as $objetMatrice)
{
if ($objetMatrice == '-') $numExample .= $objetMatrice;
if ($objetMatrice == '$prefix') $numExample .= $this->prefix;
if ($objetMatrice == '$yy') $numExample .= $this->yy;
if ($objetMatrice == '$mm') $numExample .= $this->mm;
if ($objetMatrice == '$num') $numExample .= $num;
}
}
else
{
$numExample = $langs->trans('NotConfigured');
}
return $numExample;
}
}
/** \brief Renvoi prochaine valeur attribu<62>e
/** \brief Renvoi prochaine valeur attribu<62>e
* \param objsoc Objet soci<63>t<EFBFBD>
* \param commande Objet commande
* \return string Valeur
*/
function getNextValue($objsoc=0)
function getNextValue($objsoc=0,$commande)
{
global $db,$conf;
//On construit la matrice
$buildResult = $this->buildMatrice($objsoc,$commande);
if ($buildResult == 1)
{
// On r<>cup<75>re la valeur max (r<>ponse imm<6D>diate car champ ind<6E>x<EFBFBD>)
$posindice = $this->numbitcounter;
$searchyy='';
$sql = "SELECT MAX(ref)";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
if ($conf->global->COMMANDE_NUM_RESTART_BEGIN_YEAR) $sql.= " WHERE ref like '".$this->searchLast."%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) $searchyy = substr($row[0],0,-$posindice);
}
if ($conf->global->COMMANDE_NUM_DELTA != '')
{
//on v<>rifie si il y a une ann<6E>e pr<70>c<EFBFBD>dente
//pour <20>viter que le delta soit appliqu<71> de nouveau sur la nouvelle ann<6E>e
$previousyy='';
$sql = "SELECT MAX(ref)";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
$sql.= " WHERE ref like '".$this->searchLastWithPreviousYear."%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) $previousyy = substr($row[0],0,-$posindice);
}
}
// Si au moins un champ respectant le mod<6F>le a <20>t<EFBFBD> trouv<75>e
if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy))
{
// Recherche rapide car restreint par un like sur champ index<65>
$sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
$sql.= " WHERE ref like '${searchyy}%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
$max = $row[0];
}
}
else if ($conf->global->COMMANDE_NUM_DELTA != '' && !eregi('^'.$this->searchLastWithPreviousYear.'',$previousyy))
{
// on applique le delta une seule fois
$max=$conf->global->COMMANDE_NUM_DELTA?$conf->global->COMMANDE_NUM_DELTA-1:0;
}
else
{
$max=0;
}
// On applique le nombre de chiffres du compteur
$arg = '%0'.$this->numbitcounter.'s';
$num = sprintf($arg,$max+1);
$numFinal = '';
foreach($this->numMatrice as $objetMatrice)
{
if ($objetMatrice == '-') $numFinal .= $objetMatrice;
if ($objetMatrice == '$prefix') $numFinal .= $this->prefix;
if ($objetMatrice == '$yy') $numFinal .= $this->yy;
if ($objetMatrice == '$mm') $numFinal .= $this->mm;
if ($objetMatrice == '$num') $numFinal .= $num;
}
dolibarr_syslog("mod_commande_saphir::getNextValue return ".$numFinal);
return $numFinal;
}
}
/** \brief Renvoie la r<>f<EFBFBD>rence de commande suivante non utilis<69>e
* \param objsoc Objet soci<63>t<EFBFBD>
* \param commande Objet commande
* \return string Texte descripif
*/
function commande_get_num($objsoc=0,$commande)
{
return $this->getNextValue($objsoc,$commande);
}
/** \brief Construction de la matrice de num<75>rotation
* \param objsoc Objet soci<63>t<EFBFBD>
* \return string Valeur
*/
function buildMatrice($objsoc=0,$commande='')
{
global $conf;
$this->prefix = $conf->global->COMMANDE_NUM_PREFIX;
$this->matrice = $conf->global->COMMANDE_NUM_MATRICE;
$this->searchLast = '';
$this->searchLastWithNoYear = '';
$this->searchLastWithPreviousYear = '';
if ($this->matrice != '')
{
$resultatMatrice = Array();
$numMatrice = Array();
$matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: C pour commande), COM : prefix du client
$matriceYear = "[A]{2,4}"; // l'ann<6E>e est sur 2 ou 4 chiffres
@@ -250,7 +277,7 @@ function info()
$matriceCounter = "[C]{1,}"; //le compteur a un nombre de chiffres libre
$matriceTiret = "[-]{1}"; // on recherche si il y a des tirets de s<>paration
$matrice = Array('prefix'=>$matricePrefix,
$matriceSearch = Array('prefix'=>$matricePrefix,
'year'=>$matriceYear,
'month'=>$matriceMonth,
'counter'=>$matriceCounter
@@ -270,43 +297,45 @@ function info()
// Ajout des tirets
if ($j != $resultTiret[$i][1])
{
$numMatrice[$k] = '-';
$searchLast .= '-';
$searchLastWithNoYear .= '-';
$searchLastWithPreviousYear .= '-';
$this->numMatrice[$k] = '-';
$this->searchLast .= '-';
$this->searchLastWithNoYear .= '-';
$this->searchLastWithPreviousYear .= '-';
$j = $resultTiret[$i][1];
$k++;
}
foreach($matrice as $idMatrice => $valueMatrice)
foreach($matriceSearch as $idMatrice => $valueMatrice)
{
$resultCount = eregi(''.$valueMatrice.'',$valueResultTiret,$resultatMatrice);
if ($resultCount)
{
// On r<>cup<75>re le pr<70>fix utilis<69>
if ($idMatrice == 'prefix' && $resultatMatrice[0] == 'COM')
if ($idMatrice == 'prefix')
{
if ($objsoc->prefix_comm)
if ($resultatMatrice[0] == 'COM')
{
$prefix = $objsoc->prefix_comm;
if ($objsoc->prefix_comm)
{
$this->prefix = $objsoc->prefix_comm;
}
else
{
$this->prefix = 'COM';
}
$this->numMatrice[$k] = '$prefix';
$this->searchLast .= $this->prefix;
$this->searchLastWithNoYear .= $this->prefix;
$this->searchLastWithPreviousYear .= $this->prefix;
$k++;
}
else
{
$prefix = 'COM';
else if ($resultatMatrice[0] == 'PREF')
{
$this->numMatrice[$k] = '$prefix';
$this->searchLast .= $this->prefix;
$this->searchLastWithNoYear .= $this->prefix;
$this->searchLastWithPreviousYear .= $this->prefix;
$k++;
}
$numMatrice[$k] = '$prefix';
$searchLast .= $prefix;
$searchLastWithNoYear .= $prefix;
$searchLastWithPreviousYear .= $prefix;
$k++;
}
else if ($idMatrice == 'prefix' && $resultatMatrice[0] == 'PREF')
{
$prefix = $this->prefix;
$numMatrice[$k] = '$prefix';
$searchLast .= $prefix;
$searchLastWithNoYear .= $prefix;
$searchLastWithPreviousYear .= $prefix;
$k++;
}
else if ($idMatrice == 'year')
{
@@ -327,125 +356,49 @@ function info()
// On change d'ann<6E>e fiscal si besoin
if($conf->global->SOCIETE_FISCAL_MONTH_START && $current_month >= $conf->global->SOCIETE_FISCAL_MONTH_START && $create_month >= $conf->global->SOCIETE_FISCAL_MONTH_START)
{
$yy = substr(strftime("%Y",mktime(0,0,0,date("m"),date("d"),date("Y")+1)),$numbityear);
$this->yy = substr(strftime("%Y",mktime(0,0,0,date("m"),date("d"),date("Y")+1)),$numbityear);
}
else
{
$yy = substr(strftime("%Y",time()),$numbityear);
$this->yy = substr(strftime("%Y",time()),$numbityear);
}
$numMatrice[$k] = '$yy';
$searchLast .= $yy;
$this->numMatrice[$k] = '$yy';
$this->searchLast .= $this->yy;
for ($l = 1; $l <= $numbityear; $l++)
{
$searchLastWithNoYear .= '[0-9]';
$this->searchLastWithNoYear .= '[0-9]';
}
$previousYear = substr(strftime("%Y",mktime(0,0,0,date("m"),date("d"),date("Y")-1)),$numbityear);
$searchLastWithPreviousYear .= $previousYear;
$this->searchLastWithPreviousYear .= $previousYear;
$k++;
}
else if ($idMatrice == 'month')
{
// On r<>cup<75>re le mois si besoin
$mm = strftime("%m",time());
$numMatrice[$k] = '$mm';
$searchLast .= $mm;
$searchLastWithNoYear .= $mm;
$searchLastWithPreviousYear .= $mm;
$this->mm = strftime("%m",time());
$this->numMatrice[$k] = '$mm';
$this->searchLast .= $this->mm;
$this->searchLastWithNoYear .= '[0-9][0-9]';
$this->searchLastWithPreviousYear .= $this->mm;
$k++;
}
else if ($idMatrice == 'counter')
{
// On r<>cup<75>re le nombre de chiffres pour le compteur
$numbitcounter = $resultCount;
$numMatrice[$k] = '$num';
$this->numbitcounter = $resultCount;
$this->numMatrice[$k] = '$num';
$k++;
}
}
}
}
}
// On r<>cup<75>re la valeur max (r<>ponse imm<6D>diate car champ ind<6E>x<EFBFBD>)
$posindice = $numbitcounter;
$searchyy='';
$sql = "SELECT MAX(ref)";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
if ($conf->global->COMMANDE_NUM_RESTART_BEGIN_YEAR) $sql.= " WHERE ref like '${searchLast}%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) $searchyy = substr($row[0],0,-$posindice);
}
if ($conf->global->COMMANDE_NUM_DELTA != '')
{
//on v<>rifie si il y a une ann<6E>e pr<70>c<EFBFBD>dente
//pour <20>viter que le delta soit appliqu<71> de nouveau sur la nouvelle ann<6E>e
$previousyy='';
$sql = "SELECT MAX(ref)";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
$sql.= " WHERE ref like '${searchLastWithPreviousYear}%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) $previousyy = substr($row[0],0,-$posindice);
}
}
// Si au moins un champ respectant le mod<6F>le a <20>t<EFBFBD> trouv<75>e
if (eregi('^'.$searchLastWithNoYear.'',$searchyy))
{
// Recherche rapide car restreint par un like sur champ index<65>
$sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))";
$sql.= " FROM ".MAIN_DB_PREFIX."commande";
$sql.= " WHERE ref like '${searchyy}%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
$max = $row[0];
}
}
else if ($conf->global->COMMANDE_NUM_DELTA != '' && !eregi('^'.$searchLastWithPreviousYear.'',$previousyy))
{
// on applique le delta une seule fois
$max=$conf->global->COMMANDE_NUM_DELTA?$conf->global->COMMANDE_NUM_DELTA-1:0;
}
else
{
$max=0;
}
// On applique le nombre de chiffres du compteur
$arg = '%0'.$numbitcounter.'s';
$num = sprintf($arg,$max+1);
$numFinal = '';
foreach($numMatrice as $objetMatrice)
{
if ($objetMatrice == '-') $numFinal .= $objetMatrice;
if ($objetMatrice == '$prefix') $numFinal .= $prefix;
if ($objetMatrice == '$yy') $numFinal .= $yy;
if ($objetMatrice == '$mm') $numFinal .= $mm;
if ($objetMatrice == '$num') $numFinal .= $num;
}
dolibarr_syslog("mod_commande_saphir::getNextValue return ".$numFinal);
return $numFinal;
}
}
/** \brief Renvoie la r<>f<EFBFBD>rence de commande suivante non utilis<69>e
* \param objsoc Objet soci<63>t<EFBFBD>
* \param commande Objet commande
* \return string Texte descripif
*/
function commande_get_num($objsoc=0)
{
return $this->getNextValue($objsoc);
return 1;
}
else
{
return -3;
}
}
}