Fix multicompany: recurring invoice must be in same env than template.

This commit is contained in:
Laurent Destailleur 2016-09-10 04:33:23 +02:00
parent c28a62e051
commit d821df070b
4 changed files with 26 additions and 18 deletions

View File

@ -241,7 +241,7 @@ class FactureRec extends CommonInvoice
*/
function fetch($rowid, $ref='', $ref_ext='', $ref_int='')
{
$sql = 'SELECT f.rowid, f.titre, f.fk_soc, f.amount, f.tva, f.total, f.total_ttc, f.remise_percent, f.remise_absolue, f.remise';
$sql = 'SELECT f.rowid, f.entity, f.titre, f.fk_soc, f.amount, f.tva, f.total, f.total_ttc, f.remise_percent, f.remise_absolue, f.remise';
$sql.= ', f.date_lim_reglement as dlr';
$sql.= ', f.note_private, f.note_public, f.fk_user_author';
$sql.= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet';
@ -269,6 +269,7 @@ class FactureRec extends CommonInvoice
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
$this->entity = $obj->entity;
$this->titre = $obj->titre;
$this->ref = $obj->titre;
$this->ref_client = $obj->ref_client;
@ -754,7 +755,7 @@ class FactureRec extends CommonInvoice
if ($num) $this->output.=$langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
else $this->output.=$langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
while ($i < $num)
while ($i < $num) // Loop on each template invoice
{
$line = $db->fetch_object($resql);
@ -774,7 +775,7 @@ class FactureRec extends CommonInvoice
$facture->date = $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); // This will also update fields of recurring invoice
$invoiceidgenerated = $facture->create($user);
if ($invoiceidgenerated <= 0)
{
$this->errors = $facture->errors;
@ -794,14 +795,14 @@ class FactureRec extends CommonInvoice
if (! $error && $invoiceidgenerated >= 0)
{
$db->commit();
$db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
$nb_create++;
$this->output.=$langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
}
else
{
$db->rollback();
$db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
}
$i++;

View File

@ -239,7 +239,8 @@ class Facture extends CommonInvoice
if (! $this->cond_reglement_id) $this->cond_reglement_id = 0;
if (! $this->mode_reglement_id) $this->mode_reglement_id = 0;
$this->brouillon = 1;
if (empty($this->entity)) $this->entity = $conf->entity;
// Multicurrency (test on $this->multicurrency_tx because we sould take the default rate only if not using origin rate)
if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency,$this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
else $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
@ -279,8 +280,9 @@ class Facture extends CommonInvoice
$_facrec = new FactureRec($this->db);
$result=$_facrec->fetch($this->fac_rec);
$this->socid = $_facrec->socid;
$this->socid = $_facrec->socid; // Invoice created on same thirdparty than template
$this->entity = $_facrec->entity; // Invoice created in same entity than template
// Fields coming from GUI (priority on template). TODO Value of template should be used as default value on GUI so we can use here always value from GUI
$this->fk_project = GETPOST('projectid','int') > 0 ? GETPOST('projectid','int') : $_facrec->fk_project;
$this->note_public = GETPOST('note_public') ? GETPOST('note_public') : $_facrec->note_public;
@ -350,7 +352,7 @@ class Facture extends CommonInvoice
$sql.= ")";
$sql.= " VALUES (";
$sql.= "'(PROV)'";
$sql.= ", ".$conf->entity;
$sql.= ", ".$this->entity;
$sql.= ", ".($this->ref_ext?"'".$this->db->escape($this->ref_ext)."'":"null");
$sql.= ", '".$this->db->escape($this->type)."'";
$sql.= ", '".$socid."'";

View File

@ -368,6 +368,7 @@ class Cronjob extends CommonObject
$sql = "SELECT";
$sql.= " t.rowid,";
$sql.= " t.entity,";
$sql.= " t.tms,";
$sql.= " t.datec,";
$sql.= " t.jobtype,";
@ -442,6 +443,7 @@ 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);
$line->label = $obj->label;
@ -896,6 +898,7 @@ class Cronjob extends CommonObject
// Update last run date start (to track running jobs)
$this->datelastrun=$now;
$this->datelastresult=null;
$this->lastoutput='';
$this->lastresult='';
$this->nbrun=$this->nbrun + 1;
@ -1096,6 +1099,8 @@ class Cronjob extends CommonObject
dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG);
}
dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
$this->datelastresult=dol_now();
$result = $this->update($user); // This include begin/commit
if ($result < 0)

View File

@ -109,8 +109,8 @@ else
{
if (empty($user->id))
{
echo "User user login: ".$userlogin." does not exists";
dol_syslog("User user login:".$userlogin." does not exists", LOG_ERR);
echo "User login: ".$userlogin." does not exists";
dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
exit(-1);
}
}
@ -152,22 +152,22 @@ if(is_array($object->lines) && (count($object->lines)>0))
//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))
{
dol_syslog("cron_run_jobs.php:: torun line->datenextrun:".dol_print_date($line->datenextrun,'dayhourtext')." line->dateend:".dol_print_date($line->dateend,'dayhourtext')." now:".dol_print_date($now,'dayhourtext'));
dol_syslog("cron_run_jobs.php:: torun line->datenextrun:".dol_print_date($line->datenextrun,'dayhourrfc')." line->dateend:".dol_print_date($line->dateend,'dayhourrfc')." now:".dol_print_date($now,'dayhourrfc'));
$cronjob=new Cronjob($db);
$result=$cronjob->fetch($line->id);
if ($result<0)
{
echo "Error:".$cronjob->error;
dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR);
echo "Error cronjob->fetch: ".$cronjob->error;
dol_syslog("cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
exit(-1);
}
// Execute job
$result=$cronjob->run_jobs($userlogin);
if ($result<0)
{
echo "Error:".$cronjob->error;
dol_syslog("cron_run_jobs.php:: run_jobs Error".$cronjob->error, LOG_ERR);
echo "Error cronjob->run_job: ".$cronjob->error;
dol_syslog("cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
exit(-1);
}
@ -175,8 +175,8 @@ if(is_array($object->lines) && (count($object->lines)>0))
$result=$cronjob->reprogram_jobs($userlogin, $now);
if ($result<0)
{
echo "Error:".$cronjob->error;
dol_syslog("cron_run_jobs.php:: reprogram_jobs Error".$cronjob->error, LOG_ERR);
echo "Error cronjob->reprogram_job: ".$cronjob->error;
dol_syslog("cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
exit(-1);
}