diff --git a/htdocs/admin/commande.php b/htdocs/admin/commande.php index 30d11a85407..7eb7220fde0 100644 --- a/htdocs/admin/commande.php +++ b/htdocs/admin/commande.php @@ -201,10 +201,12 @@ if ($handle) print ''.$langs->trans("Activate").''; } print ''; + + $commande=new Commande($db); - // Info - $htmltooltip=''; - $nextval=$module->getNextValue($mysoc); + // Info + $htmltooltip=''; + $nextval=$module->getNextValue($mysoc,$commande); if ($nextval != $langs->trans("NotAvailable")) { $htmltooltip=''.$langs->trans("NextValue").': '.$nextval; diff --git a/htdocs/admin/facture.php b/htdocs/admin/facture.php index b66c5383f2d..207a5836bc2 100644 --- a/htdocs/admin/facture.php +++ b/htdocs/admin/facture.php @@ -250,11 +250,11 @@ while (($file = readdir($handle))!==false) } print ''; - $facture=new Facture($db); + $facture=new Facture($db); - // Info - $htmltooltip=''; - $facture->type=0; + // Info + $htmltooltip=''; + $facture->type=0; $nextval=$module->getNextValue($mysoc,$facture); if ($nextval != $langs->trans("NotAvailable")) { diff --git a/htdocs/admin/fournisseur.php b/htdocs/admin/fournisseur.php index d1158a5cc25..26ad2eae243 100644 --- a/htdocs/admin/fournisseur.php +++ b/htdocs/admin/fournisseur.php @@ -202,10 +202,12 @@ if ($handle) print ''.$langs->trans("Activate").''; } print ''; + + $commande=new CommandeFournisseur($db); // Info $htmltooltip=''; - $nextval=$module->getNextValue($mysoc); + $nextval=$module->getNextValue($mysoc,$commande); if ($nextval != $langs->trans("NotAvailable")) { $htmltooltip=''.$langs->trans("NextValue").': '.$nextval; diff --git a/htdocs/admin/livraison.php b/htdocs/admin/livraison.php index 5fb39432385..1f0d1a1f75f 100644 --- a/htdocs/admin/livraison.php +++ b/htdocs/admin/livraison.php @@ -32,6 +32,7 @@ */ require("./pre.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/livraison/livraison.class.php"); $langs->load("admin"); $langs->load("bills"); @@ -191,9 +192,11 @@ if ($handle) } print ''; - // Info - $htmltooltip=''; - $nextval=$module->getNextValue($mysoc); + $livraison=new Livraison($db); + + // Info + $htmltooltip=''; + $nextval=$module->getNextValue($mysoc,$livraison); if ($nextval != $langs->trans("NotAvailable")) { $htmltooltip=''.$langs->trans("NextValue").': '.$nextval; diff --git a/htdocs/admin/propale.php b/htdocs/admin/propale.php index cabc79057e1..9f212b99056 100644 --- a/htdocs/admin/propale.php +++ b/htdocs/admin/propale.php @@ -215,10 +215,12 @@ if ($handle) print ''.$langs->trans("Activate").''; } print ''; + + $propale=new Propal($db); // Info $htmltooltip=''; - $nextval=$module->getNextValue($mysoc); + $nextval=$module->getNextValue($mysoc,$propale); if ($nextval != $langs->trans("NotAvailable")) { $htmltooltip=''.$langs->trans("NextValue").': '.$nextval; diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php index 9ef00a53d29..c494c06166a 100644 --- a/htdocs/commande/commande.class.php +++ b/htdocs/commande/commande.class.php @@ -174,7 +174,7 @@ class Commande extends CommonObject $obj = new $classname(); $numref = ""; - $numref = $obj->getNextValue($soc); + $numref = $obj->getNextValue($soc,$this); if ( $numref != "") { diff --git a/htdocs/fourn/commande/modules/mod_commande_fournisseur_orchidee.php b/htdocs/fourn/commande/modules/mod_commande_fournisseur_orchidee.php index bd17faff0f3..2ced28e3841 100644 --- a/htdocs/fourn/commande/modules/mod_commande_fournisseur_orchidee.php +++ b/htdocs/fourn/commande/modules/mod_commande_fournisseur_orchidee.php @@ -38,9 +38,16 @@ require_once(DOL_DOCUMENT_ROOT ."/fourn/commande/modules/modules_commandefournis */ class mod_commande_fournisseur_orchidee extends ModeleNumRefSuppliersOrders { - 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 */ @@ -122,127 +129,145 @@ function info() { global $conf; - $this->prefixorder = $conf->global->COMMANDE_FOURNISSEUR_NUM_PREFIX; - $this->ordernummatrice = $conf->global->COMMANDE_FOURNISSEUR_NUM_MATRICE; + $buildResult = $this->buildMatrice(); - if ($this->ordernummatrice != '') - { - $resultatMatrice = Array(); - $numMatrice = ''; - - $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: CF pour commande fournisseur), COM : prefix du client - $matriceYear = "[A]{2,4}"; // l'anné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->ordernummatrice, -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ère le préfix utilisé - 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->prefixorder; - $numMatrice .= $prefix; - } - else if ($idMatrice == 'year') - { - // On récupère le nombre de chiffres pour l'année - $numbityear = $resultCount; - // On défini le mois du début d'année fiscale - $fiscal_current_month = date("n"); - $create_month = $fiscal_current_month; - - // On change d'anné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ère le mois si besoin - $mm = strftime("%m",time()); - $numMatrice .= $mm; - } - else if ($idMatrice == 'counter') - { - // On récupère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - } - } - } - } - } - - // On récupère le nombre de chiffres du compteur - $arg = '%0'.$numbitcounter.'s'; - $num = sprintf($arg,$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA?$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA:1); + if ($buildResult == 1) + { + // On récupère le nombre de chiffres du compteur + $arg = '%0'.$this->numbitcounter.'s'; + $num = sprintf($arg,$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA?$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA:1); - // Construction de l'exemple de numérotation - $numExample = $numMatrice.$num; - - return $numExample; + //On construit le numéro à 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ée * \param objsoc Objet société * \param commande Objet commande fournisseur * \return string Valeur */ - function getNextValue($objsoc) + function getNextValue($objsoc=0,$commande='') { global $db,$conf; - $this->prefixorder = $conf->global->COMMANDE_FOURNISSEUR_NUM_PREFIX; - $this->ordernummatrice = $conf->global->COMMANDE_FOURNISSEUR_NUM_MATRICE; + $buildResult = $this->buildMatrice($objsoc,$commande); - if ($this->ordernummatrice != '') + if ($buildResult == 1) + { + + // On récupère la valeur max (réponse immédiate car champ indéxé) + $posindice = $this->numbitcounter; + $searchyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; + if ($conf->global->COMMANDE_FOURNISSEUR_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_FOURNISSEUR_NUM_DELTA != '') + { + //on vérifie si il y a une année précédente + //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année + $previousyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; + $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èle a été trouvée + if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy)) + { + // Recherche rapide car restreint par un like sur champ indexé + $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; + $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; + $sql.= " WHERE ref like '${searchyy}%'"; + $resql=$db->query($sql); + if ($resql) + { + $row = $db->fetch_row($resql); + $max = $row[0]; + } + } + else if ($conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA != '' && !eregi('^'.$this->searchLastWithPreviousYear.'',$previousyy)) + { + // on applique le delta une seule fois + $max=$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA?$conf->global->COMMANDE_FOURNISSEUR_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_fournisseur_orchidee::getNextValue return ".$numFinal); + return $numFinal; + } + } + + + /** \brief Renvoie la référence de commande suivante non utilisée + * \param objsoc Objet société + * \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érotation + * \param objsoc Objet société + * \return string Valeur + */ + function buildMatrice($objsoc=0,$commande='') + { + global $conf; + + $this->prefix = $conf->global->COMMANDE_FOURNISSEUR_NUM_PREFIX; + $this->matrice = $conf->global->COMMANDE_FOURNISSEUR_NUM_MATRICE; + $this->searchLast = ''; + $this->searchLastWithNoYear = ''; + $this->searchLastWithPreviousYear = ''; + + if ($this->matrice != '') { $resultatMatrice = Array(); - $numMatrice = Array(); $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: CF pour commande fournisseur), COM : prefix du client $matriceYear = "[A]{2,4}"; // l'année est sur 2 ou 4 chiffres @@ -250,14 +275,14 @@ 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 ); // on détermine l'emplacement des tirets - $resultTiret = preg_split('/'.$matriceTiret.'/',$this->ordernummatrice, -1, PREG_SPLIT_OFFSET_CAPTURE); + $resultTiret = preg_split('/'.$matriceTiret.'/',$this->matrice, -1, PREG_SPLIT_OFFSET_CAPTURE); $j = 0; $k = 0; @@ -270,43 +295,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ère le préfix utilisé - 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->prefixorder; - $numMatrice[$k] = '$prefix'; - $searchLast .= $prefix; - $searchLastWithNoYear .= $prefix; - $searchLastWithPreviousYear .= $prefix; - $k++; } else if ($idMatrice == 'year') { @@ -327,125 +354,49 @@ function info() // On change d'anné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è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ère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - $numMatrice[$k] = '$num'; + $this->numbitcounter = $resultCount; + $this->numMatrice[$k] = '$num'; $k++; } } } } } - - // On récupère la valeur max (réponse immédiate car champ indéxé) - $posindice = $numbitcounter; - $comyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; - if ($conf->global->COMMANDE_FOURNISSEUR_NUM_RESTART_BEGIN_YEAR) $sql.= " WHERE ref like '${searchLast}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - if ($row) $comyy = substr($row[0],0,-$posindice); - } - - if ($conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA != '') - { - //on vérifie si il y a une année précédente - //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année - $lastyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; - $sql.= " WHERE ref like '${searchLastWithPreviousYear}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - if ($row) $lastyy = substr($row[0],0,-$posindice); - } - } - - // Si au moins un champ respectant le modèle a été trouvée - if (eregi('^'.$searchLastWithNoYear.'',$comyy)) - { - // Recherche rapide car restreint par un like sur champ indexé - $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur"; - $sql.= " WHERE ref like '${comyy}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - $max = $row[0]; - } - } - else if ($conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA != '' && !eregi('^'.$searchLastWithPreviousYear.'',$lastyy)) - { - // on applique le delta une seule fois - $max=$conf->global->COMMANDE_FOURNISSEUR_NUM_DELTA?$conf->global->COMMANDE_FOURNISSEUR_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_fournisseur_orchidee::getNextValue return ".$numFinal); - return $numFinal; - } - } - - - /** \brief Renvoie la référence de commande suivante non utilisée - * \param objsoc Objet société - * \param commande Objet commande - * \return string Texte descripif - */ - function commande_get_num($objsoc=0) - { - return $this->getNextValue($objsoc); + return 1; + } + else + { + return -3; + } } } diff --git a/htdocs/includes/modules/commande/mod_commande_saphir.php b/htdocs/includes/modules/commande/mod_commande_saphir.php index 72cf177eb79..ec4e997cbb4 100644 --- a/htdocs/includes/modules/commande/mod_commande_saphir.php +++ b/htdocs/includes/modules/commande/mod_commande_saphir.php @@ -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é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ère le préfix utilisé - 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ère le nombre de chiffres pour l'année - $numbityear = $resultCount; - // On défini le mois du début d'année fiscale - $fiscal_current_month = date("n"); - $create_month = $fiscal_current_month; - - // On change d'anné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ère le mois si besoin - $mm = strftime("%m",time()); - $numMatrice .= $mm; - } - else if ($idMatrice == 'counter') - { - // On récupère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - } - } - } - } - } - - // On récupè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érotation - $numExample = $numMatrice.$num; - - return $numExample; + //On construit la matrice + $buildResult = $this->buildMatrice(); + + if ($buildResult == 1) + { + // On récupè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éro à 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ée + /** \brief Renvoi prochaine valeur attribuée * \param objsoc Objet société - * \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ère la valeur max (réponse immédiate car champ indéxé) + $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ée précédente + //pour éviter que le delta soit appliqué de nouveau sur la nouvelle anné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èle a été trouvée + if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy)) + { + // Recherche rapide car restreint par un like sur champ indexé + $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érence de commande suivante non utilisée + * \param objsoc Objet société + * \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érotation + * \param objsoc Objet société + * \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é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ère le préfix utilisé - 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é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è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ère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - $numMatrice[$k] = '$num'; + $this->numbitcounter = $resultCount; + $this->numMatrice[$k] = '$num'; $k++; } } } } } - - // On récupère la valeur max (réponse immédiate car champ indéxé) - $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ée précédente - //pour éviter que le delta soit appliqué de nouveau sur la nouvelle anné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èle a été trouvée - if (eregi('^'.$searchLastWithNoYear.'',$searchyy)) - { - // Recherche rapide car restreint par un like sur champ indexé - $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érence de commande suivante non utilisée - * \param objsoc Objet société - * \param commande Objet commande - * \return string Texte descripif - */ - function commande_get_num($objsoc=0) - { - return $this->getNextValue($objsoc); + return 1; + } + else + { + return -3; + } } } diff --git a/htdocs/includes/modules/facture/pluton/pluton.modules.php b/htdocs/includes/modules/facture/pluton/pluton.modules.php index 119ca203555..74f3ab8c577 100644 --- a/htdocs/includes/modules/facture/pluton/pluton.modules.php +++ b/htdocs/includes/modules/facture/pluton/pluton.modules.php @@ -38,10 +38,17 @@ require_once(DOL_DOCUMENT_ROOT ."/includes/modules/facture/modules_facture.php") */ class mod_facture_pluton extends ModeleNumRefFactures { - var $prefixinvoice=''; - var $prefixcreditnote=''; - var $facturenummatrice=''; - var $error=''; + var $prefixinvoice; + var $prefixcreditnote; + var $matrice; + var $numMatrice = Array(); + var $yy; + var $mm; + var $numbitcounter; + var $searchLast; + var $searchLastWithNoYear; + var $searchLastWithPreviousYear; + var $error = ''; /** \brief Renvoi la description du modele de numérotation * \return string Texte descripif @@ -133,129 +140,34 @@ function info() */ function getExample() { - global $conf; + global $conf,$langs; - $this->prefixinvoice = $conf->global->FACTURE_NUM_PREFIX; - $this->prefixcreditnote = $conf->global->AVOIR_NUM_PREFIX; - $this->matrice = $conf->global->FACTURE_NUM_MATRICE; + $numExample = ''; + + $buildResult = $this->buildMatrice(); - if ($this->matrice != '') - { - $resultatMatrice = Array(); - $numMatrice = ''; - - $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: FA pour facture et AV pour avoir), COM : prefix du client - $matriceYear = "[A]{2,4}"; // l'anné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ère le préfix utilisé - 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') - { - // Les avoirs peuvent suivre la numérotation des factures - if (!$conf->global->AVOIR_NUM_WITH_INVOICE && $facture->type == 2) - { - $prefix = $this->prefixcreditnote; - } - else - { - $prefix = $this->prefixinvoice; - } - $numMatrice .= $prefix; - } - else if ($idMatrice == 'year') - { - // On récupère le nombre de chiffres pour l'année - $numbityear = $resultCount; - // On défini le mois du début d'année fiscale - $fiscal_current_month = date("n"); - - if (is_object($facture) && $facture->date) - { - $create_month = strftime("%m",$facture->date); - } - else - { - $create_month = $fiscal_current_month; - } - - // On change d'anné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ère le mois si besoin - $mm = strftime("%m",time()); - $numMatrice .= $mm; - } - else if ($idMatrice == 'counter') - { - // On récupère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - } - } - } - } - } - - // On récupère le nombre de chiffres du compteur - $arg = '%0'.$numbitcounter.'s'; - $num = sprintf($arg,$conf->global->FACTURE_NUM_DELTA?$conf->global->FACTURE_NUM_DELTA:1); + if ($buildResult == 1) + { + // On récupère le nombre de chiffres du compteur + $arg = '%0'.$this->numbitcounter.'s'; + $num = sprintf($arg,$conf->global->FACTURE_NUM_DELTA?$conf->global->FACTURE_NUM_DELTA:1); - // Construction de l'exemple de numérotation - $numExample = $numMatrice.$num; - - return $numExample; + //On construit le numéro à 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ée * \param objsoc Objet société @@ -266,22 +178,126 @@ function info() { global $db,$conf; + $buildResult = $this->buildMatrice($objsoc,$facture); + + if ($buildResult == 1) + { + + // On récupère la valeur max (réponse immédiate car champ indéxé) + $posindice = $this->numbitcounter; + $searchyy=''; + $sql = "SELECT MAX(facnumber)"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture"; + if ($conf->global->FACTURE_NUM_RESTART_BEGIN_YEAR) $sql.= " WHERE facnumber 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->PROPALE_NUM_DELTA != '') + { + //on vérifie si il y a une année précédente + //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année + $previousyy=''; + $sql = "SELECT MAX(facnumber)"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture"; + $sql.= " WHERE facnumber 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èle a été trouvée + if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy)) + { + // Recherche rapide car restreint par un like sur champ indexé + $sql = "SELECT MAX(0+SUBSTRING(facnumber,-".$posindice."))"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture"; + $sql.= " WHERE facnumber like '${searchyy}%'"; + $resql=$db->query($sql); + if ($resql) + { + $row = $db->fetch_row($resql); + $max = $row[0]; + } + } + else if ($conf->global->PROPALE_NUM_DELTA != '' && !eregi('^'.$this->searchLastWithPreviousYear.'',$previousyy)) + { + // on applique le delta une seule fois + $max=$conf->global->FACTURE_NUM_DELTA?$conf->global->FACTURE_NUM_DELTA-1:0; + } + else + { + $max=0; + } + + // On replace le prefix de l'avoir + if ($conf->global->AVOIR_NUM_WITH_INVOICE && $facture->type == 2) + { + $this->prefix = $this->prefixcreditnote; + } + + // 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_facture_pluton::getNextValue return ".$numFinal); + return $numFinal; + } + } + + + /** \brief Renvoie la référence de commande suivante non utilisée + * \param objsoc Objet société + * \param facture Objet facture + * \return string Texte descripif + */ + function getNumRef($objsoc=0,$facture) + { + return $this->getNextValue($objsoc,$facture); + } + + /** \brief Construction de la matrice de numérotation + * \param objsoc Objet société + * \return string Valeur + */ + function buildMatrice($objsoc=0,$facture='') + { + global $conf; + $this->prefixinvoice = $conf->global->FACTURE_NUM_PREFIX; $this->prefixcreditnote = $conf->global->AVOIR_NUM_PREFIX; $this->matrice = $conf->global->FACTURE_NUM_MATRICE; + $this->searchLast = ''; + $this->searchLastWithNoYear = ''; + $this->searchLastWithPreviousYear = ''; if ($this->matrice != '') { $resultatMatrice = Array(); - $numMatrice = Array(); - $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: FA pour facture et AV pour avoir), COM : prefix du client + $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: FA pour facture, AV pour avoir), COM : prefix du client $matriceYear = "[A]{2,4}"; // l'anné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, + $matriceSearch = Array('prefix'=>$matricePrefix, 'year'=>$matriceYear, 'month'=>$matriceMonth, 'counter'=>$matriceCounter @@ -301,51 +317,55 @@ 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ère le préfix utilisé - 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') + { + // Les avoirs peuvent suivre la numérotation des factures + if (!$conf->global->AVOIR_NUM_WITH_INVOICE && $facture->type == 2) + { + $thisPrefix = $this->prefixcreditnote; + } + else + { + $thisPrefix = $this->prefixinvoice; + } + $this->prefix = $thisPrefix; + $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') - { - // Les avoirs peuvent suivre la numérotation des factures - if (!$conf->global->AVOIR_NUM_WITH_INVOICE && $facture->type == 2) - { - $prefix = $this->prefixcreditnote; - } - else - { - $prefix = $this->prefixinvoice; - } - $numMatrice[$k] = '$prefix'; - $searchLast .= $prefix; - $searchLastWithNoYear .= $prefix; - $searchLastWithPreviousYear .= $prefix; - $k++; } else if ($idMatrice == 'year') { @@ -366,132 +386,50 @@ function info() // On change d'anné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è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ère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - $numMatrice[$k] = '$num'; + $this->numbitcounter = $resultCount; + $this->numMatrice[$k] = '$num'; $k++; } } } } } - - // On récupère la valeur max (réponse immédiate car champ indéxé) - $posindice = $numbitcounter; - $searchyy=''; - $sql = "SELECT MAX(facnumber)"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture"; - if ($conf->global->FACTURE_NUM_RESTART_BEGIN_YEAR) $sql.= " WHERE facnumber like '${searchLast}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - if ($row) $fayy = substr($row[0],0,-$posindice); - } - - if ($conf->global->PROPALE_NUM_DELTA != '') - { - //on vérifie si il y a une année précédente - //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année - $previousyy=''; - $sql = "SELECT MAX(facnumber)"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture"; - $sql.= " WHERE facnumber 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èle a été trouvée - if (eregi('^'.$searchLastWithNoYear.'',$searchyy)) - { - // Recherche rapide car restreint par un like sur champ indexé - $sql = "SELECT MAX(0+SUBSTRING(facnumber,-".$posindice."))"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture"; - $sql.= " WHERE facnumber like '${searchyy}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - $max = $row[0]; - } - } - else if ($conf->global->PROPALE_NUM_DELTA != '' && !eregi('^'.$searchLastWithPreviousYear.'',$previousyy)) - { - // on applique le delta une seule fois - $max=$conf->global->FACTURE_NUM_DELTA?$conf->global->FACTURE_NUM_DELTA-1:0; - } - else - { - $max=0; - } - - // On replace le prefix de l'avoir - if ($conf->global->AVOIR_NUM_WITH_INVOICE && $facture->type == 2) - { - $prefix = $this->prefixcreditnote; - } - - // 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_facture_pluton::getNextValue return ".$numFinal); - return $numFinal; + return 1; + } + else + { + return -3; + } } - } - - - /** \brief Renvoie la référence de commande suivante non utilisée - * \param objsoc Objet société - * \param facture Objet facture - * \return string Texte descripif - */ - function getNumRef($objsoc=0,$facture) - { - return $this->getNextValue($objsoc,$facture); - } } ?> \ No newline at end of file diff --git a/htdocs/includes/modules/propale/mod_propale_saphir.php b/htdocs/includes/modules/propale/mod_propale_saphir.php index a8028b5e72c..53a8eba4d7e 100644 --- a/htdocs/includes/modules/propale/mod_propale_saphir.php +++ b/htdocs/includes/modules/propale/mod_propale_saphir.php @@ -38,9 +38,16 @@ require_once(DOL_DOCUMENT_ROOT ."/includes/modules/propale/modules_propale.php") */ class mod_propale_saphir extends ModeleNumRefPropales { - var $prefixproposal=''; - var $proposalnummatrice=''; - 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,138 @@ function info() */ function getExample() { - global $conf; + global $conf,$langs; - $this->prefix = $conf->global->PROPALE_NUM_PREFIX; - $this->matrice = $conf->global->PROPALE_NUM_MATRICE; - - if ($this->matrice != '') - { - $resultatMatrice = Array(); - $numMatrice = ''; - - $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: PR pour propal), COM : prefix du client - $matriceYear = "[A]{2,4}"; // l'anné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ère le préfix utilisé - 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ère le nombre de chiffres pour l'année - $numbityear = $resultCount; - // On défini le mois du début d'année fiscale - $fiscal_current_month = date("n"); - $create_month = $fiscal_current_month; - - // On change d'anné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ère le mois si besoin - $mm = strftime("%m",time()); - $numMatrice .= $mm; - } - else if ($idMatrice == 'counter') - { - // On récupère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - } - } - } - } - } - - // On récupère le nombre de chiffres du compteur - $arg = '%0'.$numbitcounter.'s'; - $num = sprintf($arg,$conf->global->PROPALE_NUM_DELTA?$conf->global->PROPALE_NUM_DELTA:1); + $numExample = ''; - // Construction de l'exemple de numérotation - $numExample = $numMatrice.$num; - - return $numExample; + $buildResult = $this->buildMatrice(); + + if ($buildResult == 1) + { + // On récupère le nombre de chiffres du compteur + $arg = '%0'.$this->numbitcounter.'s'; + $num = sprintf($arg,$conf->global->PROPALE_NUM_DELTA?$conf->global->PROPALE_NUM_DELTA:1); + + //On construit le numéro à 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ée * \param objsoc Objet société * \param propale Objet propale * \return string Valeur */ - function getNextValue($objsoc=0) + function getNextValue($objsoc=0,$propale='') { global $db,$conf; + $buildResult = $this->buildMatrice($objsoc,$propale); + + if ($buildResult == 1) + { + + // On récupère la valeur max (réponse immédiate car champ indéxé) + $posindice = $this->numbitcounter; + $searchyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."propal"; + if ($conf->global->PROPALE_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->PROPALE_NUM_DELTA != '') + { + //on vérifie si il y a une année précédente + //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année + $previousyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."propal"; + $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èle a été trouvée + if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy)) + { + // Recherche rapide car restreint par un like sur champ indexé + $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; + $sql.= " FROM ".MAIN_DB_PREFIX."propal"; + $sql.= " WHERE ref like '${searchyy}%'"; + $resql=$db->query($sql); + if ($resql) + { + $row = $db->fetch_row($resql); + $max = $row[0]; + } + } + else if ($conf->global->PROPALE_NUM_DELTA != '' && !eregi('^'.$this->searchLastWithPreviousYear.'',$previousyy)) + { + // on applique le delta une seule fois + $max=$conf->global->PROPALE_NUM_DELTA?$conf->global->PROPALE_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_propale_saphir::getNextValue return ".$numFinal); + return $numFinal; + } + } + + /** \brief Construction de la matrice de numérotation + * \param objsoc Objet société + * \return string Valeur + */ + function buildMatrice($objsoc=0,$propale='') + { + global $conf; + $this->prefix = $conf->global->PROPALE_NUM_PREFIX; $this->matrice = $conf->global->PROPALE_NUM_MATRICE; + $this->searchLast = ''; + $this->searchLastWithNoYear = ''; + $this->searchLastWithPreviousYear = ''; if ($this->matrice != '') { $resultatMatrice = Array(); - $numMatrice = Array(); $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: PR pour propale), COM : prefix du client $matriceYear = "[A]{2,4}"; // l'année est sur 2 ou 4 chiffres @@ -250,7 +266,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 +286,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ère le préfix utilisé - 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,115 +345,50 @@ function info() // On change d'anné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è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ère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - $numMatrice[$k] = '$num'; + $this->numbitcounter = $resultCount; + $this->numMatrice[$k] = '$num'; $k++; } } } } } - - // On récupère la valeur max (réponse immédiate car champ indéxé) - $posindice = $numbitcounter; - $searchyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."propal"; - if ($conf->global->PROPALE_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->PROPALE_NUM_DELTA != '') - { - //on vérifie si il y a une année précédente - //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année - $previousyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."propal"; - $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èle a été trouvée - if (eregi('^'.$searchLastWithNoYear.'',$searchyy)) - { - // Recherche rapide car restreint par un like sur champ indexé - $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; - $sql.= " FROM ".MAIN_DB_PREFIX."propal"; - $sql.= " WHERE ref like '${searchyy}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - $max = $row[0]; - } - } - else if ($conf->global->PROPALE_NUM_DELTA != '' && !eregi('^'.$searchLastWithPreviousYear.'',$previousyy)) - { - // on applique le delta une seule fois - $max=$conf->global->PROPALE_NUM_DELTA?$conf->global->PROPALE_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_propale_saphir::getNextValue return ".$numFinal); - return $numFinal; + return 1; + } + else + { + return -3; + } } - } } ?> \ No newline at end of file diff --git a/htdocs/livraison/livraison.class.php b/htdocs/livraison/livraison.class.php index 12bf451c940..1393b465ffe 100644 --- a/htdocs/livraison/livraison.class.php +++ b/htdocs/livraison/livraison.class.php @@ -290,7 +290,7 @@ class Livraison extends CommonObject $livref = substr($this->ref, 1, 4); if ($livref == 'PROV') { - $this->ref = $objMod->livraison_get_num($soc); + $this->ref = $objMod->livraison_get_num($soc,$this); } // Tester si non dejà au statut validé. Si oui, on arrete afin d'éviter diff --git a/htdocs/livraison/mods/mod_livraison_saphir.php b/htdocs/livraison/mods/mod_livraison_saphir.php index 6655fea826b..93a6cbce417 100644 --- a/htdocs/livraison/mods/mod_livraison_saphir.php +++ b/htdocs/livraison/mods/mod_livraison_saphir.php @@ -38,9 +38,16 @@ require_once(DOL_DOCUMENT_ROOT ."/livraison/mods/modules_livraison.php"); */ class mod_livraison_saphir extends ModeleNumRefDeliveryOrder { - 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 */ @@ -52,8 +59,8 @@ class mod_livraison_saphir extends ModeleNumRefDeliveryOrder /** \brief Renvoi la description du modele de numérotation * \return string Texte descripif */ -function info() - { + function info() + { global $conf,$langs; $langs->load("bills"); @@ -120,15 +127,151 @@ function info() */ function getExample() { - global $conf; + global $conf,$langs; - $this->prefix = $conf->global->LIVRAISON_NUM_PREFIX; - $this->matrice = $conf->global->LIVRAISON_NUM_MATRICE; + $numExample = ''; + + $buildResult = $this->buildMatrice(); + + if ($buildResult == 1) + { + // On récupère le nombre de chiffres du compteur + $arg = '%0'.$this->numbitcounter.'s'; + $num = sprintf($arg,$conf->global->LIVRAISON_NUM_DELTA?$conf->global->LIVRAISON_NUM_DELTA:1); + + //On construit le numéro à 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ée + * \param objsoc Objet société + * \param commande Objet commande + * \return string Valeur + */ + function getNextValue($objsoc=0,$livraison='') + { + global $db,$conf; + + $buildResult = $this->buildMatrice($objsoc,$livraison); + + if ($buildResult == 1) + { + + // On récupère la valeur max (réponse immédiate car champ indéxé) + $posindice = $this->numbitcounter; + $searchyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; + if ($conf->global->LIVRAISON_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->LIVRAISON_NUM_DELTA != '') + { + //on vérifie si il y a une année précédente + //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année + $previousyy=''; + $sql = "SELECT MAX(ref)"; + $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; + $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èle a été trouvée + if (eregi('^'.$this->searchLastWithNoYear.'',$searchyy)) + { + // Recherche rapide car restreint par un like sur champ indexé + $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; + $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; + $sql.= " WHERE ref like '${searchyy}%'"; + $resql=$db->query($sql); + if ($resql) + { + $row = $db->fetch_row($resql); + $max = $row[0]; + } + } + else if ($conf->global->LIVRAISON_NUM_DELTA != '' && !eregi('^'.$this->searchLastWithPreviousYear.'',$previousyy)) + { + // on applique le delta une seule fois + $max=$conf->global->LIVRAISON_NUM_DELTA?$conf->global->LIVRAISON_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_livraison_saphir::getNextValue return ".$numFinal); + return $numFinal; + } + } + + + /** \brief Renvoie la référence de commande suivante non utilisée + * \param objsoc Objet société + * \param commande Objet commande + * \return string Texte descripif + */ + function livraison_get_num($objsoc=0,$livraison='') + { + return $this->getNextValue($objsoc,$livraison); + } + + + /** \brief Construction de la matrice de numérotation + * \param objsoc Objet société + * \return string Valeur + */ + function buildMatrice($objsoc=0,$livraison='') + { + global $conf; + + $this->prefix = $conf->global->LIVRAISON_NUM_PREFIX; + $this->matrice = $conf->global->LIVRAISON_NUM_MATRICE; + $this->searchLast = ''; + $this->searchLastWithNoYear = ''; + $this->searchLastWithPreviousYear = ''; if ($this->matrice != '') { $resultatMatrice = Array(); - $numMatrice = ''; $matricePrefix = "PREF|COM"; // PREF : prefix libre (ex: BL pour bon de livraison), COM : prefix du client $matriceYear = "[A]{2,4}"; // l'année est sur 2 ou 4 chiffres @@ -136,121 +279,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, - '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ère le préfix utilisé - 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ère le nombre de chiffres pour l'année - $numbityear = $resultCount; - // On défini le mois du début d'année fiscale - $fiscal_current_month = date("n"); - $create_month = $fiscal_current_month; - - // On change d'anné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ère le mois si besoin - $mm = strftime("%m",time()); - $numMatrice .= $mm; - } - else if ($idMatrice == 'counter') - { - // On récupère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - } - } - } - } - } - - // On récupère le nombre de chiffres du compteur - $arg = '%0'.$numbitcounter.'s'; - $num = sprintf($arg,$conf->global->LIVRAISON_NUM_DELTA?$conf->global->LIVRAISON_NUM_DELTA:1); - - // Construction de l'exemple de numérotation - $numExample = $numMatrice.$num; - - return $numExample; - } - } - - /** \brief Renvoi prochaine valeur attribuée - * \param objsoc Objet société - * \param commande Objet commande - * \return string Valeur - */ - function getNextValue($objsoc=0) - { - global $db,$conf; - - $this->prefix = $conf->global->LIVRAISON_NUM_PREFIX; - $this->matrice = $conf->global->LIVRAISON_NUM_MATRICE; - - 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é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, + $matriceSearch = Array('prefix'=>$matricePrefix, 'year'=>$matriceYear, 'month'=>$matriceMonth, 'counter'=>$matriceCounter @@ -270,43 +299,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ère le préfix utilisé - 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') { @@ -315,9 +346,9 @@ function info() // On défini le mois du début d'année fiscale $current_month = date("n"); - if (is_object($commande) && $commande->date) + if (is_object($livraison) && $livraison->date) { - $create_month = strftime("%m",$commande->date); + $create_month = strftime("%m",$livraison->date); } else { @@ -327,126 +358,50 @@ function info() // On change d'anné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è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ère le nombre de chiffres pour le compteur - $numbitcounter = $resultCount; - $numMatrice[$k] = '$num'; + $this->numbitcounter = $resultCount; + $this->numMatrice[$k] = '$num'; $k++; } } } } } - - // On récupère la valeur max (réponse immédiate car champ indéxé) - $posindice = $numbitcounter; - $searchyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; - if ($conf->global->LIVRAISON_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->LIVRAISON_NUM_DELTA != '') - { - //on vérifie si il y a une année précédente - //pour éviter que le delta soit appliqué de nouveau sur la nouvelle année - $previousyy=''; - $sql = "SELECT MAX(ref)"; - $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; - $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èle a été trouvée - if (eregi('^'.$searchLastWithNoYear.'',$searchyy)) - { - // Recherche rapide car restreint par un like sur champ indexé - $sql = "SELECT MAX(0+SUBSTRING(ref,-".$posindice."))"; - $sql.= " FROM ".MAIN_DB_PREFIX."livraison"; - $sql.= " WHERE ref like '${searchyy}%'"; - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - $max = $row[0]; - } - } - else if ($conf->global->LIVRAISON_NUM_DELTA != '' && !eregi('^'.$searchLastWithPreviousYear.'',$previousyy)) - { - // on applique le delta une seule fois - $max=$conf->global->LIVRAISON_NUM_DELTA?$conf->global->LIVRAISON_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_livraison_saphir::getNextValue return ".$numFinal); - return $numFinal; + return 1; + } + else + { + return -3; + } } - } - - - /** \brief Renvoie la référence de commande suivante non utilisée - * \param objsoc Objet société - * \param commande Objet commande - * \return string Texte descripif - */ - function commande_get_num($objsoc=0) - { - return $this->getNextValue($objsoc); - } } ?> \ No newline at end of file