diff --git a/class/actions_sendfacrecmail.class.php b/class/actions_sendfacrecmail.class.php index 772af3e..40e79a6 100644 --- a/class/actions_sendfacrecmail.class.php +++ b/class/actions_sendfacrecmail.class.php @@ -73,60 +73,91 @@ class Actionssendfacrecmail * @param HookManager $hookmanager Hook manager propagated to allow calling another hook * @return int < 0 on error, 0 on success, 1 to replace standard code */ - public function doActions($parameters, &$object, &$action, $hookmanager) - { - global $conf, $user, $langs; - - $error = 0; // Error counter - - $fp = fopen('/tmp/vardump.txt', 'w'); - fwrite($fp, serialize($parameters, $object, $action, $hookmanager)); - fclose($fp); - return 0; - - /* print_r($parameters); print_r($object); echo "action: " . $action; */ - if (in_array($parameters['currentcontext'], array('somecontext1','somecontext2'))) // do something only for the context 'somecontext1' or 'somecontext2' - { - // Do what you want here... - // You can for example call global vars like $fieldstosearchall to overwrite them, or update database depending on $action and $_POST values. - } - - if (! $error) { - $this->results = array('myreturn' => 999); - $this->resprints = 'A text to show'; - return 0; // or return 1 to replace standard code - } else { - $this->errors[] = 'Error message'; - return -1; - } - } - public function writeSQL($parameters, &$object, &$action, $hookmanager) - { - global $conf, $user, $langs; - - $error = 0; // Error counter - //$object = "SELECT rowid FROM llx_facture_rec WHERE false"; - - $fp = fopen('/tmp/writesql-vardump.txt', 'w'); - fwrite($fp, serialize($parameters)); - fwrite($fp, serialize($object)); - fwrite($fp, serialize($action)); - fwrite($fp, serialize($hookmanager)); - fclose($fp); - return 0; - } public function generatedInvoice($parameters, &$object, &$action, $hookmanager) { global $conf, $user, $langs; $error = 0; // Error counter - $fp = fopen('/tmp/generatedinvoice-vardump.txt', 'w'); - fwrite($fp, serialize($parameters)); - fwrite($fp, serialize($object)); - fwrite($fp, serialize($action)); - fwrite($fp, serialize($hookmanager)); - fclose($fp); - return 0; + $facturerec = $parameters['facturerec']; + + // On n'envoie la facture que si elle est validée + if ( ! $object->brouillon) { + if ($this->envoiMail($object, $facturerec)) { + dol_syslog("Success sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ")."); + } else { + $this->errors[] = "Error sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ")."; + dol_syslog("Error sending email for " . $facturerec->ref . " (id:" . $facturerec->id . ")."); + $error++; + } + } + + return ($error ? -1 : 0); + } + + /** + * Fonction écrite un peu à l'arrache mais l'existant de Dolibarr + * ne semble pas très accessible. + * + * @return boolean True si envoi réussi + */ + function envoiMail($facture, $recurringFacture) + { + global $mysoc, $langs, $conf; + + // récupération du template du mail + // (pas très précise mais je commence à en avoir marre de creuser tout dolibarr pour trouver les bonnes fonctions...) + $result = $this->db->query("SELECT * from " . MAIN_DB_PREFIX . "c_email_templates WHERE module = 'sendfacrecmail' and active = 1 and enabled = '1' ORDER BY tms DESC LIMIT 1"); + if ( ! $result or ! ($template = $this->db->fetch_object($result))) { + return false; + } + + // L'objet n'est pas à jour ('manque last_main_doc entre autres) + $facture->fetch($facture->id); + + // Préparation des remplacements dans le sujet et le corps du mail + $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $facture); + //complete_substitutions_array($substitutionarray, $langs, $facture); // lourd et n'a rien ajouté lors de mes tests + // Par contre, il nous manque quelques trucs utiles... + if ( ! empty($facture->linkedObjects['contrat'])) { + $contrat = reset($facture->linkedObjects['contrat']); // on prend le premier qui vient. + $substitutionarray['__CONTRACT_REF__'] = $contrat->ref; + } + + // Substitutions + $subject = make_substitutions($template->topic, $substitutionarray, $langs); + $body = make_substitutions($template->content, $substitutionarray, $langs); + + // On regarde si on doit joindre le fichier + $filePath = array(); + $fileMime = array(); + $fileName = array(); + if ($template->joinfiles) { + $filePath = array(DOL_DATA_ROOT . '/' . $facture->last_main_doc); + $fileMime = array('application/pdf'); // FIXME: à rendre dynamique, même si ce sera toujours du PDF ? + $fileName = array(basename($facture->last_main_doc)); + } + + // envoi du mail + $mailfile = new CMailFile( + $subject, // sujet + $facture->thirdparty->name . ' <' . $facture->thirdparty->email . '>', //destinataire + $conf->global->MAIN_MAIL_EMAIL_FROM, // expéditeur (from) + $body, // corps du mail + $filePath, + $fileMime, + $fileName, + '', // CC + '', // BCC + 0, //deliveryreceipt + 0, //msgishtml + $conf->global->MAIN_MAIL_ERRORS_TO, //errors-to + '', // css + '', // trackid + '', // moreinheader + 'standard', // sendcontext + $conf->global->MAIN_MAIL_ERRORS_TO); //reply-to + + return $mailfile->sendfile(); } } diff --git a/core/modules/modsendfacrecmail.class.php b/core/modules/modsendfacrecmail.class.php index c1fb776..05527af 100644 --- a/core/modules/modsendfacrecmail.class.php +++ b/core/modules/modsendfacrecmail.class.php @@ -69,7 +69,7 @@ class modsendfacrecmail extends DolibarrModules $this->editor_url = 'https://www.bugness.org'; // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated' or a version string like 'x.y.z' - $this->version = '0.1.0'; + $this->version = '0.1.1'; //Url to the file with your last numberversion of this module //$this->url_last_version = 'http://www.example.com/versionmodule.txt'; @@ -314,21 +314,17 @@ class modsendfacrecmail extends DolibarrModules */ public function init($options='') { - $result=$this->_load_tables('/sendfacrecmail/sql/'); - if ($result < 0) return -1; // Do not activate module if not allowed errors found on module SQL queries (the _load_table run sql with run_sql with error allowed parameter to 'default') - - // Create extrafields - include_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - - //$result1=$extrafields->addExtraField('myattr1', "New Attr 1 label", 'boolean', 1, 3, 'thirdparty', 0, 0, '', '', 1, '', 0, 0, '', '', 'sendfacrecmail@sendfacrecmail', '$conf->sendfacrecmail->enabled'); - //$result2=$extrafields->addExtraField('myattr2', "New Attr 2 label", 'varchar', 1, 10, 'project', 0, 0, '', '', 1, '', 0, 0, '', '', 'sendfacrecmail@sendfacrecmail', '$conf->sendfacrecmail->enabled'); - //$result3=$extrafields->addExtraField('myattr3', "New Attr 3 label", 'varchar', 1, 10, 'bank_account', 0, 0, '', '', 1, '', 0, 0, '', '', 'sendfacrecmail@sendfacrecmail', '$conf->sendfacrecmail->enabled'); - //$result4=$extrafields->addExtraField('myattr4', "New Attr 4 label", 'select', 1, 3, 'thirdparty', 0, 1, '', array('options'=>array('code1'=>'Val1','code2'=>'Val2','code3'=>'Val3')), 1 '', 0, 0, '', '', 'sendfacrecmail@sendfacrecmail', '$conf->sendfacrecmail->enabled'); - //$result5=$extrafields->addExtraField('myattr5', "New Attr 5 label", 'text', 1, 10, 'user', 0, 0, '', '', 1, '', 0, 0, '', '', 'sendfacrecmail@sendfacrecmail', '$conf->sendfacrecmail->enabled'); - $sql = array(); + // we check if our model already exists + $result = $this->db->query("SELECT COUNT(*) AS cpt FROM " . MAIN_DB_PREFIX."c_email_templates WHERE module = 'sendfacrecmail'"); + if ($result) { + $row = $this->db->fetch_object($result); + if ($row->cpt == 0) { + $sql[] = "INSERT INTO " . MAIN_DB_PREFIX."c_email_templates (module, type_template, lang, label, joinfiles, topic, content) VALUES ('sendfacrecmail', 'thirdparty', 'fr_FR', 'Envoi automatique de facture via sendfacrecmail', '1', '[__MYCOMPANY_NAME__] Nouvelle facture __REF__', 'Bonjour,\n\nNouvelle facture ci-jointe.\n\nEn cas de règlement par virement, merci d''indiquer dans le libellé la référence du contrat : __CONTRACT_REF__ .\n\nCordialement,\nle nouveau robot de l''équipe tréso.')"; + } + } + return $this->_init($sql, $options); } @@ -344,6 +340,8 @@ class modsendfacrecmail extends DolibarrModules { $sql = array(); + $sql[] = "DELETE FROM " . MAIN_DB_PREFIX."c_email_templates WHERE module = 'sendfacrecmail'"; + return $this->_remove($sql, $options); } }