From 6ea0919ebb2868c68bc31a0bd6077389fffda39f Mon Sep 17 00:00:00 2001 From: Chl Date: Fri, 17 Aug 2018 20:47:01 +0200 Subject: [PATCH 1/5] facture-rec.class.php: reindent --- .../facture/class/facture-rec.class.php | 93 ++++++++++--------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index 56f87d2eac3..bf4360a57f3 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -1025,30 +1025,33 @@ class FactureRec extends CommonInvoice $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 - if ($restictoninvoiceid > 0) $sql.=' AND rowid = '.$restictoninvoiceid; + if ($restictoninvoiceid > 0) + $sql.=' AND rowid = '.$restictoninvoiceid; $sql.= $db->order('entity', 'ASC'); //print $sql;exit; $resql = $db->query($sql); if ($resql) { - $i=0; - $num = $db->num_rows($resql); + $i=0; + $num = $db->num_rows($resql); - if ($num) $this->output.=$langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n"; - else $this->output.=$langs->trans("NoQualifiedRecurringInvoiceTemplateFound"); + if ($num) + $this->output.=$langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n"; + else + $this->output.=$langs->trans("NoQualifiedRecurringInvoiceTemplateFound"); - $saventity = $conf->entity; + $saventity = $conf->entity; - while ($i < $num) // Loop on each template invoice. If $num = 0, test is false at first pass. + while ($i < $num) // Loop on each template invoice. If $num = 0, test is false at first pass. { $line = $db->fetch_object($resql); - $db->begin(); + $db->begin(); - $invoiceidgenerated = 0; + $invoiceidgenerated = 0; - $facturerec = new FactureRec($db); + $facturerec = new FactureRec($db); $facturerec->fetch($line->rowid); if ($facturerec->id > 0) @@ -1058,44 +1061,44 @@ class FactureRec extends CommonInvoice dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity); - $facture = new Facture($db); + $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 || $forcevalidation)) - { - $result = $facture->validate($user); - if ($result <= 0) - { - $this->errors = $facture->errors; - $this->error = $facture->error; - $error++; - } - } - if (! $error && $facturerec->generate_pdf) - { - // We refresh the object in order to have all necessary data (like date_lim_reglement) - $facture->fetch($facture->id); - $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 || $forcevalidation)) + { + $result = $facture->validate($user); + if ($result <= 0) + { + $this->errors = $facture->errors; + $this->error = $facture->error; + $error++; + } + } + if (! $error && $facturerec->generate_pdf) + { + // We refresh the object in order to have all necessary data (like date_lim_reglement) + $facture->fetch($facture->id); + $result = $facture->generateDocument($facturerec->modelpdf, $langs); + if ($result <= 0) + { + $this->errors = $facture->errors; + $this->error = $facture->error; + $error++; + } + } } else { @@ -1114,7 +1117,7 @@ class FactureRec extends CommonInvoice } else { - $db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref); + $db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref); } $i++; From 95b5d8790d27e856934f630873267d29f8b18489 Mon Sep 17 00:00:00 2001 From: Chl Date: Fri, 17 Aug 2018 19:56:29 +0200 Subject: [PATCH 2/5] corr. typo --- htdocs/compta/facture/class/facture-rec.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index bf4360a57f3..f35d8f88835 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -998,11 +998,11 @@ class FactureRec extends CommonInvoice * * WARNING: This method change temporarly context $conf->entity to be in correct context for each recurring invoice found. * - * @param int $restictoninvoiceid 0=All qualified template invoices found. > 0 = restrict action on invoice ID + * @param int $restrictioninvoiceid 0=All qualified template invoices found. > 0 = restrict action on invoice ID * @param int $forcevalidation 1=Force validation of invoice whatever is template auto_validate flag. * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) */ - function createRecurringInvoices($restictoninvoiceid=0, $forcevalidation=0) + function createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0) { global $conf, $langs, $db, $user; @@ -1017,7 +1017,7 @@ class FactureRec extends CommonInvoice $tmparray=dol_getdate($now); $today = dol_mktime(23,59,59,$tmparray['mon'],$tmparray['mday'],$tmparray['year']); // Today is last second of current day - dol_syslog("createRecurringInvoices restictoninvoiceid=".$restictoninvoiceid." forcevalidation=".$forcevalidation); + dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation); $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec'; $sql.= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency @@ -1025,8 +1025,8 @@ class FactureRec extends CommonInvoice $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 - if ($restictoninvoiceid > 0) - $sql.=' AND rowid = '.$restictoninvoiceid; + if ($restrictioninvoiceid > 0) + $sql.=' AND rowid = '.$restrictioninvoiceid; $sql.= $db->order('entity', 'ASC'); //print $sql;exit; From 864ce8534120747acc62c5d5d1ada43460056a1b Mon Sep 17 00:00:00 2001 From: Chl Date: Fri, 17 Aug 2018 20:39:46 +0200 Subject: [PATCH 3/5] NEW adding hooks to createRecurringInvoices() For example : send the newly generated invoice by mail. We send the counter and the total so launching some action on the last loop (generated invoice or error) is possible. (bonus: correcting error reporting $this->errors to $this->errors[] ) --- .../facture/class/facture-rec.class.php | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index f35d8f88835..6cff7e5ef57 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -32,6 +32,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'); /** @@ -1004,14 +1005,16 @@ class FactureRec extends CommonInvoice */ function createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0) { - global $conf, $langs, $db, $user; + global $conf, $langs, $db, $user, $hookmanager; + $error=0; + $nb_create=0; // Load translation files required by the page - $langs->loadLangs(array("main","bills")); + $langs->loadLangs(array("main","bills")); - $nb_create=0; + $hookmanager->initHooks(array('createrecurringinvoices')); $now = dol_now(); $tmparray=dol_getdate($now); @@ -1029,6 +1032,12 @@ class FactureRec extends CommonInvoice $sql.=' AND rowid = '.$restrictioninvoiceid; $sql.= $db->order('entity', 'ASC'); //print $sql;exit; + $parameters = array( + 'entity' => $conf->entity, + 'restrictioninvoiceid' => $restrictioninvoiceid, + 'forcevalidation' => $forcevalidation, + ); + $reshook = $hookmanager->executeHooks('writeSQL', $parameters, $sql); // note that $sql might be modified by hooks $resql = $db->query($sql); if ($resql) @@ -1051,6 +1060,7 @@ class FactureRec extends CommonInvoice $invoiceidgenerated = 0; + $facture = null; $facturerec = new FactureRec($db); $facturerec->fetch($line->rowid); @@ -1073,7 +1083,7 @@ class FactureRec extends CommonInvoice $invoiceidgenerated = $facture->create($user); if ($invoiceidgenerated <= 0) { - $this->errors = $facture->errors; + $this->errors[] = $facture->errors; $this->error = $facture->error; $error++; } @@ -1082,7 +1092,7 @@ class FactureRec extends CommonInvoice $result = $facture->validate($user); if ($result <= 0) { - $this->errors = $facture->errors; + $this->errors[] = $facture->errors; $this->error = $facture->error; $error++; } @@ -1094,7 +1104,7 @@ class FactureRec extends CommonInvoice $result = $facture->generateDocument($facturerec->modelpdf, $langs); if ($result <= 0) { - $this->errors = $facture->errors; + $this->errors[] = $facture->errors; $this->error = $facture->error; $error++; } @@ -1120,6 +1130,16 @@ class FactureRec extends CommonInvoice $db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref); } + $parameters = array( + 'cpt' => $i, + 'total' => $num, + 'errorCount' => $error, + 'invoiceidgenerated' => $invoiceidgenerated, + 'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks. + 'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks. + ); + $reshook = $hookmanager->executeHooks('generatedInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null) + $i++; } From b67101c77cf00f6164baff4b2cd15b8c97cd7b26 Mon Sep 17 00:00:00 2001 From: Chl Date: Mon, 3 Dec 2018 17:03:28 +0100 Subject: [PATCH 4/5] corr. #10154 --- htdocs/compta/facture/class/facture-rec.class.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index 6cff7e5ef57..fed1ca8c79c 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -32,7 +32,6 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; -require_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'); /** @@ -1033,7 +1032,6 @@ class FactureRec extends CommonInvoice $sql.= $db->order('entity', 'ASC'); //print $sql;exit; $parameters = array( - 'entity' => $conf->entity, 'restrictioninvoiceid' => $restrictioninvoiceid, 'forcevalidation' => $forcevalidation, ); @@ -1083,7 +1081,7 @@ class FactureRec extends CommonInvoice $invoiceidgenerated = $facture->create($user); if ($invoiceidgenerated <= 0) { - $this->errors[] = $facture->errors; + $this->errors = $facture->errors; $this->error = $facture->error; $error++; } @@ -1092,7 +1090,7 @@ class FactureRec extends CommonInvoice $result = $facture->validate($user); if ($result <= 0) { - $this->errors[] = $facture->errors; + $this->errors = $facture->errors; $this->error = $facture->error; $error++; } @@ -1104,7 +1102,7 @@ class FactureRec extends CommonInvoice $result = $facture->generateDocument($facturerec->modelpdf, $langs); if ($result <= 0) { - $this->errors[] = $facture->errors; + $this->errors = $facture->errors; $this->error = $facture->error; $error++; } From c377350c357615b381ef29b677e4f2c58e623a32 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 14 Dec 2018 16:09:12 +0100 Subject: [PATCH 5/5] Update facture-rec.class.php --- htdocs/compta/facture/class/facture-rec.class.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index fed1ca8c79c..23d30f0ad32 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -1006,15 +1006,12 @@ class FactureRec extends CommonInvoice { global $conf, $langs, $db, $user, $hookmanager; - $error=0; $nb_create=0; // Load translation files required by the page $langs->loadLangs(array("main","bills")); - $hookmanager->initHooks(array('createrecurringinvoices')); - $now = dol_now(); $tmparray=dol_getdate($now); $today = dol_mktime(23,59,59,$tmparray['mon'],$tmparray['mday'],$tmparray['year']); // Today is last second of current day @@ -1035,7 +1032,7 @@ class FactureRec extends CommonInvoice 'restrictioninvoiceid' => $restrictioninvoiceid, 'forcevalidation' => $forcevalidation, ); - $reshook = $hookmanager->executeHooks('writeSQL', $parameters, $sql); // note that $sql might be modified by hooks + $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks $resql = $db->query($sql); if ($resql) @@ -1136,7 +1133,7 @@ class FactureRec extends CommonInvoice 'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks. 'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks. ); - $reshook = $hookmanager->executeHooks('generatedInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null) + $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null) $i++; }