NEW Can include tag {uuu} into some numbering masks to replace with user

This commit is contained in:
Laurent Destailleur
2017-08-29 19:44:35 +02:00
parent 4d908c2dd0
commit 8f34de1036
5 changed files with 80 additions and 20 deletions

View File

@@ -707,9 +707,10 @@ function array2table($data,$tableMarkup=1,$tableoptions='',$troptions='',$tdopti
* @param string $date Date to use for the {y},{m},{d} tags. * @param string $date Date to use for the {y},{m},{d} tags.
* @param string $mode 'next' for next value or 'last' for last value * @param string $mode 'next' for next value or 'last' for last value
* @param bool $bentityon Activate the entity filter. Default is true (for modules not compatible with multicompany) * @param bool $bentityon Activate the entity filter. Default is true (for modules not compatible with multicompany)
* @param User $objuser Object user we need data from.
* @return string New value (numeric) or error message * @return string New value (numeric) or error message
*/ */
function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$mode='next', $bentityon=true) function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$mode='next', $bentityon=true, $objuser=null)
{ {
global $conf,$user; global $conf,$user;
@@ -780,6 +781,22 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$masktype_value=''; $masktype_value='';
} }
// Extract value for user
if (preg_match('/\{(u+)\}/i',$mask,$regType))
{
$lastname = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
if (is_object($objuser)) $lastname = $objuser->lastname;
$maskuser=$regType[1];
$maskuser_value=substr($lastname,0,dol_strlen($regType[1]));// get n first characters of user firstname (where n is length in mask)
$maskuser_value=str_pad($maskuser_value,dol_strlen($regType[1]),"#",STR_PAD_RIGHT); // we fill on right with # to have same number of char than into mask
}
else
{
$maskuser='';
$maskuser_value='';
}
// Personalized field {XXX-1} à {XXX-9} // Personalized field {XXX-1} à {XXX-9}
/*$maskperso=array(); /*$maskperso=array();
$maskpersonew=array(); $maskpersonew=array();
@@ -805,6 +822,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode); $maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode); $maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{(t+)\}/i',$masktype_value,$maskwithonlyymcode); $maskwithonlyymcode=preg_replace('/\{(t+)\}/i',$masktype_value,$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{(u+)\}/i',$maskuser_value,$maskwithonlyymcode);
/*foreach($maskperso as $key => $val) /*foreach($maskperso as $key => $val)
{ {
$maskwithonlyymcode=preg_replace('/'.$val.'/i', $maskpersonew[$key], $maskwithonlyymcode); $maskwithonlyymcode=preg_replace('/'.$val.'/i', $maskpersonew[$key], $maskwithonlyymcode);
@@ -951,6 +969,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),str_pad("",dol_strlen($maskcounter),"_"),$maskLike); $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),str_pad("",dol_strlen($maskcounter),"_"),$maskLike);
if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike); if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike);
if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike); if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike);
if ($maskuser) $maskLike = str_replace(dol_string_nospecial('{'.$maskuser.'}'),$maskuser_value,$maskLike);
// Get counter in database // Get counter in database
$counter=0; $counter=0;
@@ -994,6 +1013,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),$counterpadded,$maskLike); $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),$counterpadded,$maskLike);
if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike); if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike);
if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike); if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike);
if ($maskuser) $maskLike = str_replace(dol_string_nospecial('{'.$maskuser.'}'),$maskuser_value,$maskLike);
$ref=''; $ref='';
$sql = "SELECT ".$field." as ref"; $sql = "SELECT ".$field." as ref";
@@ -1114,6 +1134,14 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$masktype_maskafter=$masktype_value; $masktype_maskafter=$masktype_value;
$numFinal = str_replace($masktype_maskbefore,$masktype_maskafter,$numFinal); $numFinal = str_replace($masktype_maskbefore,$masktype_maskafter,$numFinal);
} }
// Now we replace the user
if ($maskuser)
{
$maskuser_maskbefore='{'.$maskuser.'}';
$maskuser_maskafter=$maskuser_value;
$numFinal = str_replace($maskuser_maskbefore,$maskuser_maskafter,$numFinal);
}
} }
dol_syslog("functions2::get_next_value return ".$numFinal,LOG_DEBUG); dol_syslog("functions2::get_next_value return ".$numFinal,LOG_DEBUG);

View File

@@ -118,11 +118,16 @@ class mod_expensereport_jade extends ModeleNumRefExpenseReport
else else
{ {
dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG); dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG);
return -1; return 0;
}
$date=$object->date_valid; // $object->date does not exists
if (empty($date))
{
$this->error = 'Date valid not defined';
return 0;
} }
//$date=time();
$date=$object->date;
$yymm = strftime("%y%m",$date); $yymm = strftime("%y%m",$date);
if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is

View File

@@ -82,12 +82,13 @@ class mod_expensereport_sand extends ModeleNumRefExpenseReport
*/ */
function getExample() function getExample()
{ {
global $conf,$langs,$mysoc; global $conf,$langs,$user;
$old_code_client=$mysoc->code_client; $exp=new ExpenseReport($this->db);
$mysoc->code_client='CCCCCCCCCC'; $exp->initAsSpecimen();
$numExample = $this->getNextValue($mysoc,''); $exp->fk_user_author = $user->id;
$mysoc->code_client=$old_code_client;
$numExample = $this->getNextValue($exp);
if (! $numExample) if (! $numExample)
{ {
@@ -99,11 +100,10 @@ class mod_expensereport_sand extends ModeleNumRefExpenseReport
/** /**
* Return next free value * Return next free value
* *
* @param Societe $objsoc Object thirdparty
* @param Object $object Object we need next value for * @param Object $object Object we need next value for
* @return string Value if KO, <0 if KO * @return string Value if KO, <0 if KO
*/ */
function getNextValue($objsoc,$object) function getNextValue($object)
{ {
global $db,$conf; global $db,$conf;
@@ -118,7 +118,21 @@ class mod_expensereport_sand extends ModeleNumRefExpenseReport
return 0; return 0;
} }
$numFinal=get_next_value($db,$mask,'expensereport','ref','',$objsoc,$object->date); $date=$object->date_valid; // $object->date does not exists
if (empty($date))
{
$this->error = 'Date valid not defined';
return 0;
}
$fuser = null;
if ($object->fk_user_author > 0)
{
$fuser=new User($db);
$fuser->fetch($object->fk_user_author);
}
$numFinal=get_next_value($db,$mask,'expensereport','ref','',null, $date, 'next', true, $fuser);
return $numFinal; return $numFinal;
} }

View File

@@ -80,6 +80,13 @@ class modExpenseReport extends DolibarrModules
$this->const[$r][4] = 0; $this->const[$r][4] = 0;
$r++; $r++;
$this->const[$r][0] = "EXPENSEREPORT_ADDON";
$this->const[$r][1] = "chaine";
$this->const[$r][2] = "mod_expensereport_jade";
$this->const[$r][3] = 'Name of manager to generate expense report ref number';
$this->const[$r][4] = 0;
$r++;
$this->const[$r][0] = "MAIN_DELAY_EXPENSEREPORTS"; $this->const[$r][0] = "MAIN_DELAY_EXPENSEREPORTS";
$this->const[$r][1] = "chaine"; $this->const[$r][1] = "chaine";
$this->const[$r][2] = "15"; $this->const[$r][2] = "15";

View File

@@ -730,6 +730,7 @@ class ExpenseReport extends CommonObject
$this->date_create = $now; $this->date_create = $now;
$this->date_debut = $now; $this->date_debut = $now;
$this->date_fin = $now; $this->date_fin = $now;
$this->date_valid = $now;
$this->date_approve = $now; $this->date_approve = $now;
$type_fees_id = 2; // TF_TRIP $type_fees_id = 2; // TF_TRIP
@@ -1060,6 +1061,7 @@ class ExpenseReport extends CommonObject
global $conf,$langs,$user; global $conf,$langs,$user;
$error = 0; $error = 0;
$now = dol_now();
// Protection // Protection
if ($this->statut == self::STATUS_VALIDATED) if ($this->statut == self::STATUS_VALIDATED)
@@ -1068,6 +1070,8 @@ class ExpenseReport extends CommonObject
return 0; return 0;
} }
$this->date_valid = $now; // Required for the getNextNum later.
// Define new ref // Define new ref
if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life
{ {
@@ -1077,16 +1081,17 @@ class ExpenseReport extends CommonObject
{ {
$num = $this->ref; $num = $this->ref;
} }
if (empty($num)) return -1;
$this->newref = $num; $this->newref = $num;
$now = dol_now();
$this->db->begin(); $this->db->begin();
// Validate // Validate
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element; $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET ref = '".$num."',"; $sql.= " SET ref = '".$num."',";
$sql.= " fk_statut = ".self::STATUS_VALIDATED.","; $sql.= " fk_statut = ".self::STATUS_VALIDATED.",";
$sql.= " date_valid='".$this->db->idate($now)."',"; $sql.= " date_valid='".$this->db->idate($this->date_valid)."',";
$sql.= " fk_user_valid = ".$user->id; $sql.= " fk_user_valid = ".$user->id;
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
@@ -1505,13 +1510,14 @@ class ExpenseReport extends CommonObject
else else
{ {
$this->error=$obj->error; $this->error=$obj->error;
$this->errors=$obj->errors;
//dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error); //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
return ""; return "";
} }
} }
else else
{ {
print $langs->trans("Error")." ".$langs->trans("Error_COMMANDE_ADDON_NotDefined"); print $langs->trans("Error")." ".$langs->trans("Error_EXPENSEREPORT_ADDON_NotDefined");
return ""; return "";
} }
} }