diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php
index 517cc9894f9..7fb9f76119b 100644
--- a/htdocs/core/modules/DolibarrModules.class.php
+++ b/htdocs/core/modules/DolibarrModules.class.php
@@ -1313,6 +1313,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
{
foreach ($this->cronjobs as $key => $value)
{
+ $entity = isset($this->cronjobs[$key]['entity'])?$this->cronjobs[$key]['entity']:$conf->entity;
$label = isset($this->cronjobs[$key]['label'])?$this->cronjobs[$key]['label']:'';
$jobtype = isset($this->cronjobs[$key]['jobtype'])?$this->cronjobs[$key]['jobtype']:'';
$class = isset($this->cronjobs[$key]['class'])?$this->cronjobs[$key]['class']:'';
@@ -1334,7 +1335,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
if ($objectname) $sql.= " AND objectname = '".$this->db->escape($objectname)."'";
if ($method) $sql.= " AND methodename = '".$this->db->escape($method)."'";
if ($command) $sql.= " AND command = '".$this->db->escape($command)."'";
- $sql.= " AND entity = ".$conf->entity;
+ $sql.= " AND entity = ".$entity; // Must be exact entity
$now=dol_now();
@@ -1371,7 +1372,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
if(is_int($unitfrequency)){ $sql.= "'".$this->db->escape($unitfrequency)."', "; }
if(is_int($priority)) {$sql.= "'".$this->db->escape($priority)."', ";}
if(is_int($status)){ $sql.= "'".$this->db->escape($status)."', "; }
- $sql.= $conf->entity.",";
+ $sql.= $entity.",";
$sql.= "'".$this->db->escape($test)."'";
$sql.= ")";
diff --git a/htdocs/core/modules/modCron.class.php b/htdocs/core/modules/modCron.class.php
index b148149910f..2ce3cf9d9f6 100644
--- a/htdocs/core/modules/modCron.class.php
+++ b/htdocs/core/modules/modCron.class.php
@@ -95,9 +95,9 @@ class modCron extends DolibarrModules
// Cronjobs
$this->cronjobs = array(
- 0=>array('label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>2, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>50, 'status'=>1, 'test'=>true),
- 1=>array('label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'none,auto,1,auto,10', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>90, 'status'=>0, 'test'=>in_array($db->type, array('mysql', 'mysqli'))),
- // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
+ 0=>array('entity'=>0, 'label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>2, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>50, 'status'=>1, 'test'=>true),
+ 1=>array('entity'=>0, 'label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'none,auto,1,auto,10', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>90, 'status'=>0, 'test'=>in_array($db->type, array('mysql', 'mysqli'))),
+ // 1=>array('entity'=>0, 'label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
);
// Permissions
diff --git a/htdocs/cron/card.php b/htdocs/cron/card.php
index 8c97bc4e2c8..707b75b95fe 100644
--- a/htdocs/cron/card.php
+++ b/htdocs/cron/card.php
@@ -614,6 +614,22 @@ else
print $langs->trans($object->note);
print "";
+ if (! empty($conf->multicompany->enabled))
+ {
+ print '
| ';
+ print $langs->trans('Entity')." | ";
+ if (! $object->entity)
+ {
+ print $langs->trans("AllEntities");
+ }
+ else
+ {
+ $mc->getInfo($obj->entity);
+ print $mc->label;
+ }
+ print " |
";
+ }
+
print '';
print '';
diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php
index be5ffbe965d..aeb0d7229b4 100644
--- a/htdocs/cron/class/cronjob.class.php
+++ b/htdocs/cron/class/cronjob.class.php
@@ -34,6 +34,7 @@ class Cronjob extends CommonObject
public $table_element='cronjob'; //!< Name of table without prefix where object is stored
public $picto = 'cron';
+ public $entity;
public $jobtype;
public $tms='';
public $datec='';
@@ -158,7 +159,7 @@ class Cronjob extends CommonObject
// Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
-
+ $sql.= "entity,";
$sql.= "datec,";
$sql.= "jobtype,";
$sql.= "label,";
@@ -188,6 +189,7 @@ class Cronjob extends CommonObject
$sql.= "libname,";
$sql.= "test";
$sql.= ") VALUES (";
+ $sql.= " ".(! isset($this->entity)?$conf->entity:$this->db->escape($this->entity)).",";
$sql.= " '".$this->db->idate($now)."',";
$sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").",";
$sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
@@ -270,8 +272,8 @@ class Cronjob extends CommonObject
function fetch($id)
{
$sql = "SELECT";
- $sql.= " t.rowid,";
-
+ $sql.= " t.rowid,";
+ $sql.= " t.entity,";
$sql.= " t.tms,";
$sql.= " t.datec,";
$sql.= " t.jobtype,";
@@ -315,7 +317,7 @@ class Cronjob extends CommonObject
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
-
+ $this->entity = $obj->entity;
$this->tms = $this->db->jdate($obj->tms);
$this->datec = $this->db->jdate($obj->datec);
$this->label = $obj->label;
@@ -422,7 +424,7 @@ class Cronjob extends CommonObject
}
}
- $sql.= " ORDER BY $sortfield $sortorder ";
+ $sql.= $this->db->order($sortfield,$sortorder);
if (!empty($limit) && !empty($offset)) {
$sql.= $this->db->plimit($limit + 1,$offset);
}
@@ -450,7 +452,6 @@ class Cronjob extends CommonObject
$line->id = $obj->rowid;
$line->ref = $obj->rowid;
-
$line->entity = $obj->entity;
$line->tms = $this->db->jdate($obj->tms);
$line->datec = $this->db->jdate($obj->datec);
@@ -580,7 +581,7 @@ class Cronjob extends CommonObject
// Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
-
+ $sql.= " entity=".(isset($this->entity)?$this->db->escape($this->entity):$conf->entity).",";
$sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
$sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
$sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
@@ -778,7 +779,7 @@ class Cronjob extends CommonObject
{
$this->id=0;
$this->ref=0;
-
+ $this->entity=0;
$this->tms='';
$this->datec='';
$this->label='';
@@ -935,6 +936,16 @@ class Cronjob extends CommonObject
return -1;
}
+ // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
+ // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
+ if ($conf->entity != $this->entity && $this->entity > 0)
+ {
+ dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
+ }
+ $savcurrententity = $conf->entity;
+ $conf->entity = $this->entity;
+ dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
+
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
$user=new User($this->db);
$result=$user->fetch('',$userlogin);
@@ -942,6 +953,7 @@ class Cronjob extends CommonObject
{
$this->error="User Error:".$user->error;
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
else
@@ -950,6 +962,7 @@ class Cronjob extends CommonObject
{
$this->error=" User user login:".$userlogin." do not exists";
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
}
@@ -981,6 +994,7 @@ class Cronjob extends CommonObject
$result = $this->update($user); // This include begin/commit
if ($result<0) {
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
@@ -1079,6 +1093,7 @@ class Cronjob extends CommonObject
{
$this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath;
dol_syslog(get_class($this) . "::run_jobs " . $this->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
// Load langs
@@ -1086,6 +1101,7 @@ class Cronjob extends CommonObject
if ($result<0)
{
dol_syslog(get_class($this) . "::run_jobs Cannot load module langs" . $langs->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
dol_syslog(get_class($this) . "::run_jobs " . $this->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG);
@@ -1148,13 +1164,12 @@ class Cronjob extends CommonObject
if ($result < 0)
{
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
+ $conf->entity = $savcurrententity;
return -1;
}
- else
- {
- return $error?-1:1;
- }
+ $conf->entity = $savcurrententity;
+ return $error?-1:1;
}
diff --git a/htdocs/cron/list.php b/htdocs/cron/list.php
index b4df819b976..7bca10ff883 100644
--- a/htdocs/cron/list.php
+++ b/htdocs/cron/list.php
@@ -245,7 +245,7 @@ $sql.= " t.nbrun,";
$sql.= " t.libname,";
$sql.= " t.test";
$sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
-$sql.= " WHERE 1 = 1";
+$sql.= " WHERE entity IN (0,".$conf->entity.")";
if ($search_status >= 0 && $search_status < 2) $sql.= " AND t.status = ".(empty($search_status)?'0':'1');
if ($search_status == 2) $sql.= " AND t.status = 2";
//Manage filter
diff --git a/scripts/cron/cron_run_jobs.php b/scripts/cron/cron_run_jobs.php
index 42f7a736b91..246fd3d75b4 100755
--- a/scripts/cron/cron_run_jobs.php
+++ b/scripts/cron/cron_run_jobs.php
@@ -85,7 +85,7 @@ if ($key != $conf->global->CRON_KEY)
// If param userlogin is reserved word 'firstadmin'
if ($userlogin == 'firstadmin')
{
- $sql='SELECT login from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
+ $sql='SELECT login, entity from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
$resql=$db->query($sql);
if ($resql)
{
@@ -93,7 +93,7 @@ if ($userlogin == 'firstadmin')
if ($obj)
{
$userlogin = $obj->login;
- echo "First admin user found is login '".$userlogin."'\n";
+ echo "First admin user found is login '".$userlogin."', entity ".$obj->entity."\n";
}
}
else dol_print_error($db);
@@ -132,7 +132,7 @@ if (! empty($id)) {
$filter['t.rowid']=$id;
}
-$result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter, 0);
+$result = $object->fetch_all('ASC,ASC,ASC','t.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
if ($result<0)
{
echo "Error: ".$object->error;
@@ -159,8 +159,8 @@ if(is_array($qualifiedjobs) && (count($qualifiedjobs)>0))
// Loop over job
foreach($qualifiedjobs as $line)
{
- dol_syslog("cron_run_jobs.php cronjobid: ".$line->id, LOG_DEBUG);
- echo "cron_run_jobs.php cronjobid: ".$line->id."\n";
+ dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
+ echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label."\n";
//If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now))