diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index aeed921d136..799fd25c5ae 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -973,6 +973,7 @@ class FactureRec extends CommonInvoice $sql.= " AND (date_when IS NULL OR date_when <= '".$db->idate($today)."')"; $sql.= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)'; $sql.= ' AND suspended = 0'; + $sql.= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here $sql.= $db->order('entity', 'ASC'); //print $sql;exit; @@ -987,7 +988,7 @@ class FactureRec extends CommonInvoice $saventity = $conf->entity; - while ($i < $num) // Loop on each template invoice + while ($i < $num) // Loop on each template invoice. If $num = 0, test is false at first pass. { $line = $db->fetch_object($resql); @@ -996,49 +997,59 @@ class FactureRec extends CommonInvoice $facturerec = new FactureRec($db); $facturerec->fetch($line->rowid); - // Set entity context - $conf->entity = $facturerec->entity; + if ($facturerec->id > 0) + { + // Set entity context + $conf->entity = $facturerec->entity; - dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity); + dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity); - $error=0; + $error=0; - $facture = new Facture($db); - $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice - $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice + $facture = new Facture($db); + $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice + $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice - $facture->type = self::TYPE_STANDARD; - $facture->brouillon = 1; - $facture->date = (empty($facturerec->date_when)?$now:$facturerec->date_when); // We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later. - $facture->socid = $facturerec->socid; + $facture->type = self::TYPE_STANDARD; + $facture->brouillon = 1; + $facture->date = (empty($facturerec->date_when)?$now:$facturerec->date_when); // We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later. + $facture->socid = $facturerec->socid; - $invoiceidgenerated = $facture->create($user); - if ($invoiceidgenerated <= 0) - { - $this->errors = $facture->errors; - $this->error = $facture->error; - $error++; - } - if (! $error && $facturerec->auto_validate) - { - $result = $facture->validate($user); - if ($result <= 0) - { - $this->errors = $facture->errors; - $this->error = $facture->error; - $error++; - } - } - if (! $error && $facturerec->generate_pdf) - { - $result = $facture->generateDocument($facturerec->modelpdf, $langs); - if ($result <= 0) - { - $this->errors = $facture->errors; - $this->error = $facture->error; - $error++; - } - } + $invoiceidgenerated = $facture->create($user); + if ($invoiceidgenerated <= 0) + { + $this->errors = $facture->errors; + $this->error = $facture->error; + $error++; + } + if (! $error && $facturerec->auto_validate) + { + $result = $facture->validate($user); + if ($result <= 0) + { + $this->errors = $facture->errors; + $this->error = $facture->error; + $error++; + } + } + if (! $error && $facturerec->generate_pdf) + { + $result = $facture->generateDocument($facturerec->modelpdf, $langs); + if ($result <= 0) + { + $this->errors = $facture->errors; + $this->error = $facture->error; + $error++; + } + } + } + else + { + $error++; + $this->error="Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n"; + $this->errors[]="Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity; + dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity); + } if (! $error && $invoiceidgenerated >= 0) { diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index f890391431b..20d332e7cbd 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1044,8 +1044,9 @@ class Cronjob extends CommonObject { dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG); - // Create Object for the call module + // Create Object for the called module $object = new $this->objectname($this->db); + if ($this->entity > 0) $object->entity = $this->entity; // We work on a dedicated entity $params_arr = array_map('trim', explode(",",$this->params));