Fix: Error in numbering module for receiving receipts

This commit is contained in:
Laurent Destailleur 2010-07-22 22:44:00 +00:00
parent 744bd69757
commit 5139288c18

View File

@ -40,6 +40,8 @@ class mod_livraison_jade extends ModeleNumRefDeliveryOrder
var $error = ''; var $error = '';
var $nom = "Jade"; var $nom = "Jade";
var $prefix='BL';
/** /**
* \brief Renvoi la description du modele de numerotation * \brief Renvoi la description du modele de numerotation
@ -57,7 +59,42 @@ class mod_livraison_jade extends ModeleNumRefDeliveryOrder
*/ */
function getExample() function getExample()
{ {
return "BL0600001"; return $this->prefix."0501-0001";
}
/** \brief Test si les numeros deja en vigueur dans la base ne provoquent pas de
* de conflits qui empechera cette numerotation de fonctionner.
* \return boolean false si conflit, true si ok
*/
function canBeActivated()
{
global $langs,$conf;
$langs->load("bills");
// Check invoice num
$fayymm=''; $max='';
$posindice=8;
$sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
$sql.= " FROM ".MAIN_DB_PREFIX."livraison";
$sql.= " WHERE facnumber LIKE '".$this->prefix."____-%'";
$sql.= " AND entity = ".$conf->entity;
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
}
if ($fayymm && ! preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i',$fayymm))
{
$langs->load("errors");
$this->error=$langs->trans('ErrorNumRefModel',$max);
return false;
}
return true;
} }
/** /**
@ -70,49 +107,34 @@ class mod_livraison_jade extends ModeleNumRefDeliveryOrder
{ {
global $db,$conf; global $db,$conf;
// D'abord on recupere la valeur max (reponse immediate car champ indexe) // D'abord on recupere la valeur max
$blyy=''; $posindice=8;
$sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
$sql = "SELECT MAX(ref)";
$sql.= " FROM ".MAIN_DB_PREFIX."livraison"; $sql.= " FROM ".MAIN_DB_PREFIX."livraison";
$sql.= " WHERE entity = ".$conf->entity; $sql.= " WHERE ref LIKE '".$this->prefix."____-%'";
$resql=$db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
if ($row) $blyy = substr($row[0],0,4);
}
// Si au moins un champ respectant le modele a ete trouvee
if (preg_match('/BL[0-9][0-9]/i',$blyy))
{
// Recherche rapide car restreint par un like sur champ indexe
$posindice=5;
$sql = "SELECT MAX(SUBSTRING(ref,$posindice)) as max";
$sql.= " FROM ".MAIN_DB_PREFIX."livraison";
$sql.= " WHERE ref like '".$blyy."%'";
$sql.= " AND entity = ".$conf->entity; $sql.= " AND entity = ".$conf->entity;
$resql=$db->query($sql); $resql=$db->query($sql);
dol_syslog("mod_livraison_jade::getNextValue sql=".$sql);
if ($resql) if ($resql)
{ {
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
if ($obj) $max = intval($obj->max); if ($obj) $max = intval($obj->max);
else $max=0; else $max=0;
} }
}
else else
{ {
$max=0; dol_syslog("mod_livraison_jade::getNextValue sql=".$sql, LOG_ERR);
return -1;
} }
$date = $delivery->date_delivery; $date=$delivery->date;
$yy = strftime("%y",$date); if (empty($date)) $date=dol_now();
$num = sprintf("%05s",$max+1); $yymm = strftime("%y%m",$date);
$num = sprintf("%04s",$max+1);
return "BL$yy$num"; dol_syslog("mod_livraison_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
return $this->prefix.$yymm."-".$num;
} }