From 3e11959826bdd258ecec02b568c5c4f96d432962 Mon Sep 17 00:00:00 2001 From: KHELIFA Date: Wed, 21 Nov 2018 10:13:07 +0100 Subject: [PATCH] =?UTF-8?q?Module=20SynergiesTechContrat:=20task=20#1109?= =?UTF-8?q?=20Ajout=20d'une=20tache=20planifier=20d'activation=20des=20ser?= =?UTF-8?q?vices=20des=20contrats=20p=C3=A9riodiquement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../class/invoicescontracttools.class.php | 140 ++++++++++++++++++ .../modules/modsynergiestechcontrat.class.php | 1 + .../langs/fr_FR/synergiestechcontrat.lang | 3 +- .../langs/fr_FR/synergiestechcontrat.lang.rej | 8 +- 4 files changed, 148 insertions(+), 4 deletions(-) diff --git a/htdocs/custom/synergiestechcontrat/class/invoicescontracttools.class.php b/htdocs/custom/synergiestechcontrat/class/invoicescontracttools.class.php index ddf40cdca31b6..e79fb3f829cc4 100755 --- a/htdocs/custom/synergiestechcontrat/class/invoicescontracttools.class.php +++ b/htdocs/custom/synergiestechcontrat/class/invoicescontracttools.class.php @@ -1988,6 +1988,99 @@ public function getAllExtraFieldsToString(&$object, $rl = '
') { return $out; } + /** + * Activate all contracts. + * A result may also be provided into this->output. + * + * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) + */ + public function activateContracts() + { + global $conf, $langs, $db, $user; + + $langs->load('synergiestechcontrat@synergiestechcontrat'); + $now = dol_now(); + $nbok = 0; + $all_error = 0; + $ref_contracts = array(); + $this->output = ''; + + $sql = "SELECT c.rowid FROM " . MAIN_DB_PREFIX . "contrat as c" . + " LEFT JOIN " . MAIN_DB_PREFIX . "contratdet as cd ON c.rowid = cd.fk_contrat" . + " LEFT JOIN " . MAIN_DB_PREFIX . "contrat_extrafields as cef ON c.rowid = cef.fk_object" . + " WHERE cef.startdate <= '" . $db->idate($now) . "'" . + " AND cef.realdate > '" . $db->idate($now) . "'" . + " AND cd.statut != 4" . + " GROUP BY c.rowid"; + + dol_syslog(__METHOD__); + $resql = $db->query($sql); + if ($resql) { + require_once (DOL_DOCUMENT_ROOT."/contrat/class/contrat.class.php"); + + while ($obj = $db->fetch_object($resql)) { + $error = 0; + $db->begin(); + + $contract = new Contrat($db); + $result = $contract->fetch($obj->rowid); + if ($result > 0) { + $contract->fetch_thirdparty(); + + if ($this->activateContract($user, $contract) < 0) { + $error++; + setEventMessages($langs->trans("Contract") . ' : ' . $contract->ref, $this->errors, 'errors'); + dol_syslog(__METHOD__ . ' ' . $langs->trans("Contract") . ' : ' . $contract->ref . ' Errors: ' . implode('; ', $this->errors), LOG_ERR); + $this->output .= $langs->trans("Contract") . ' : ' . $contract->ref . ' Errors: ' . implode('; ', $this->errors) . "\n"; + } + + $label = $langs->trans('STCContractActivateEventLabel', $contract->ref); + $message = $langs->trans('Author') . ' : ' . $user->login; + + $result = $this->addEvent($contract, 'AC_OTH_AUTO', $label, $message); + if ($result < 0) { + $error++; + setEventMessages($langs->trans("Contract") . ' : ' . $contract->ref, $this->errors, 'errors'); + dol_syslog(__METHOD__ . ' ' . $langs->trans("Contract") . ' : ' . $contract->ref . ' Errors: ' . implode('; ', $this->errors), LOG_ERR); + $this->output .= $langs->trans("Contract") . ' : ' . $contract->ref . ' Errors: ' . implode('; ', $this->errors) . "\n"; + } else { + $ref_contracts[] = '- ' . $contract->ref; + $nbok++; + } + } elseif ($result == 0) { + $error++; + setEventMessage($langs->trans("ErrorRecordNotFound") . ' : ID:' . $obj->rowid, 'errors'); + dol_syslog(__METHOD__ . ' ' . $langs->trans("ErrorRecordNotFound") . ' : ID:' . $obj->rowid, LOG_ERR); + $this->output .= $langs->trans("ErrorRecordNotFound") . ' : ID:' . $obj->rowid . "\n"; + } else { + $error++; + setEventMessages($contract->error, $contract->errors, 'errors'); + dol_syslog(__METHOD__ . ' Errors: ' . $contract->error . '; ' . implode('; ', $contract->errors), LOG_ERR); + $this->output .= ' Errors: ' . $contract->error . '; ' . implode('; ', $contract->errors) . "\n"; + } + + if ($error) { + $all_error += $error; + $db->rollback(); + } else { + $db->commit(); + } + } + } + + if ($nbok > 0) { + setEventMessages($langs->trans('STCContractActivated', $nbok), $ref_contracts, 'warnings'); + dol_syslog(__METHOD__ . ' ' . $langs->trans('STCContractActivated', $nbok) . ' ' . implode(' ', $ref_contracts)); + $this->output .= $langs->trans('STCContractActivated', $nbok) . ' ' . implode(' ', $ref_contracts) . "\n"; + } else { + setEventMessage($langs->trans('STCNoContractActivated'), 'warnings'); + dol_syslog(__METHOD__ . ' ' . $langs->trans('STCNoContractActivated')); + $this->output .= $langs->trans('STCNoContractActivated') . "\n"; + } + + return $all_error ? $all_error : 0; + } + /** * Terminate all contracts. * A result may also be provided into this->output. @@ -2074,4 +2167,51 @@ public function terminateContracts() return $all_error ? $all_error : 0; } + + /** + * Activate all lines of a contract + * + * @param User $user Object User making action + * @param Contrat $contract Object of a contract + * @return void + */ + function activateContract($user, &$contract) + { + $this->db->begin(); + + // Load lines + $contract->fetch_lines(); + + $ok = true; + foreach ($contract->lines as $contratline) { + // Active line not already active + if ($contratline->statut != 4) { + $contratline->date_ouverture = dol_now(); + $contratline->fk_user_ouverture = $user->id; + $contratline->statut = '4'; + $result = $contratline->update($user); + if ($result < 0) { + $this->errors = array_merge($this->errors, $contratline->errors); + $ok = false; + break; + } + } + } + + if ($contract->statut == 0) { + $result = $contract->validate($user); + if ($result < 0) { + $this->errors[] = $contract->error; + $ok = false; + } + } + + if ($ok) { + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); + return -1; + } + } } diff --git a/htdocs/custom/synergiestechcontrat/core/modules/modsynergiestechcontrat.class.php b/htdocs/custom/synergiestechcontrat/core/modules/modsynergiestechcontrat.class.php index 2583244f4f0ad..ae1727b71037f 100644 --- a/htdocs/custom/synergiestechcontrat/core/modules/modsynergiestechcontrat.class.php +++ b/htdocs/custom/synergiestechcontrat/core/modules/modsynergiestechcontrat.class.php @@ -223,6 +223,7 @@ public function __construct($db) // Cronjobs (List of cron jobs entries to add when module is enabled) $this->cronjobs = array( 0=>array('label'=>'TerminateContracts', 'jobtype'=>'method', 'class'=>'/custom/synergiestechcontrat/class/invoicescontracttools.class.php', 'objectname'=>'InvoicesContractTools', 'method'=>'terminateContracts', 'parameters'=>'', 'comment'=>'Terminate contracts periodically', 'frequency'=>1, 'unitfrequency'=>3600*24), + 1=>array('label'=>'ActivateContracts', 'jobtype'=>'method', 'class'=>'/custom/synergiestechcontrat/class/invoicescontracttools.class.php', 'objectname'=>'InvoicesContractTools', 'method'=>'activateContracts', 'parameters'=>'', 'comment'=>'Activate contracts periodically', 'frequency'=>1, 'unitfrequency'=>3600*24), // 0=>array('label'=>'MyJob label', 'jobtype'=>'method', 'class'=>'/synergiestechcontrat/class/synergiestechcontratmyjob.class.php', 'objectname'=>'synergiestechcontratMyJob', 'method'=>'myMethod', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true) ); // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true), diff --git a/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang b/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang index 4cd77e403b055..ddb20ed649d5c 100644 --- a/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang +++ b/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang @@ -260,9 +260,10 @@ STCRenewalOfTheContractEndedEventDescription = Le contrat %s n'a pas été re STCContractExtraFields = Attributs supplémentaires du contrat ######################################################################################################## -# Terminate Event +# Activate / Terminate Event ######################################################################################################## STCContractTerminateEventLabel = Contrat %s résilié. +STCContractActivateEventLabel = Contrat %s activé. ############################################################ ### Actions diff --git a/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang.rej b/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang.rej index 9284954f0b8a0..c7cdb9c6f6c26 100644 --- a/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang.rej +++ b/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang.rej @@ -1,9 +1,11 @@ diff a/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang b/htdocs/custom/synergiestechcontrat/langs/fr_FR/synergiestechcontrat.lang (rejected hunks) -@@ -113,6 +113,7 @@ STCTerminateContracts = Résilier les contrats - STCNoContractsToTerminate = Aucun contrat à résilier +@@ -114,6 +114,9 @@ STCNoContractsToTerminate = Aucun contrat à résilier STCContractTerminated = Les %s contrat(s) suivant(s) ont été résilié(s) : STCNoContractTerminated = Aucun contrat n'a été résilié -+TerminateContracts = Résiliations des contrats + TerminateContracts = Résiliations des contrats ++STCContractActivated = Les %s contrat(s) suivant(s) ont été activé(s) : ++STCNoContractActivated = Aucun contrat n'a été activé ++ActivateContracts = Activations des contrats ######################################################################################################## # Invoices Contracts Tools Class