notify.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/notify.class.php
  21. * \ingroup notification
  22. * \brief File of class to manage notifications
  23. */
  24. require_once DOL_DOCUMENT_ROOT .'/core/class/CMailFile.class.php';
  25. /**
  26. * Class to manage notifications
  27. */
  28. class Notify
  29. {
  30. var $id;
  31. var $db;
  32. var $error;
  33. var $errors=array();
  34. var $author;
  35. var $ref;
  36. var $date;
  37. var $duree;
  38. var $note;
  39. var $fk_project;
  40. // Les codes actions sont definis dans la table llx_notify_def
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. function __construct($db)
  47. {
  48. $this->db = $db;
  49. }
  50. /**
  51. * Return message that say how many notification (and to which email) will occurs on requested event.
  52. * This is to show confirmation messages before event is recorded.
  53. *
  54. * @param string $action Id of action in llx_c_action_trigger
  55. * @param int $socid Id of third party
  56. * @param Object $object Object the notification is about
  57. * @return string Message
  58. */
  59. function confirmMessage($action,$socid,$object)
  60. {
  61. global $langs;
  62. $langs->load("mails");
  63. $listofnotiftodo=$this->getNotificationsArray($action,$socid,$object,0);
  64. $nb=-1;
  65. if (is_array($listofnotiftodo)) $nb=count($listofnotiftodo);
  66. if ($nb < 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
  67. if ($nb == 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("NoNotificationsWillBeSent");
  68. if ($nb == 1) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ANotificationsWillBeSent");
  69. if ($nb >= 2) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("SomeNotificationsWillBeSent",$nb);
  70. if (is_array($listofnotiftodo))
  71. {
  72. $i=0;
  73. foreach ($listofnotiftodo as $key => $val)
  74. {
  75. if ($i) $texte.=', ';
  76. else $texte.=' (';
  77. if ($val['isemailvalid']) $texte.=$val['email'];
  78. else $texte.=$val['emaildesc'];
  79. $i++;
  80. }
  81. if ($i) $texte.=')';
  82. }
  83. return $texte;
  84. }
  85. /**
  86. * Return number of notifications activated for action code (and third party)
  87. *
  88. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  89. * @param int $socid Id of third party or 0 for all thirdparties or -1 for no thirdparties
  90. * @param Object $object Object the notification is about (need it to check threshold value of some notifications)
  91. * @param int $userid Id of user or 0 for all users or -1 for no users
  92. * @return array|int <0 if KO, array of notifications to send if OK
  93. */
  94. function getNotificationsArray($notifcode,$socid=0,$object=null,$userid=0)
  95. {
  96. global $conf, $user;
  97. $error=0;
  98. $resarray=array();
  99. $valueforthreshold = 0;
  100. if (is_object($object)) $valueforthreshold = $object->total_ht;
  101. if (! $error)
  102. {
  103. if ($socid >= 0)
  104. {
  105. $sql = "SELECT a.code, c.email, c.rowid";
  106. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  107. $sql.= " ".MAIN_DB_PREFIX."socpeople as c,";
  108. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  109. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  110. $sql.= " WHERE n.fk_contact = c.rowid";
  111. $sql.= " AND a.rowid = n.fk_action";
  112. $sql.= " AND n.fk_soc = s.rowid";
  113. if ($notifcode)
  114. {
  115. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  116. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  117. }
  118. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  119. if ($socid > 0) $sql.= " AND s.rowid = ".$socid;
  120. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  121. $resql = $this->db->query($sql);
  122. if ($resql)
  123. {
  124. $num = $this->db->num_rows($resql);
  125. $i=0;
  126. while ($i < $num)
  127. {
  128. $obj = $this->db->fetch_object($resql);
  129. if ($obj)
  130. {
  131. $newval2=trim($obj->email);
  132. $isvalid=isValidEmail($newval2);
  133. if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
  134. }
  135. $i++;
  136. }
  137. }
  138. else
  139. {
  140. $error++;
  141. $this->error=$this->db->lasterror();
  142. }
  143. }
  144. }
  145. if (! $error)
  146. {
  147. if ($userid >= 0)
  148. {
  149. $sql = "SELECT a.code, c.email, c.rowid";
  150. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  151. $sql.= " ".MAIN_DB_PREFIX."user as c,";
  152. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a";
  153. $sql.= " WHERE n.fk_user = c.rowid";
  154. $sql.= " AND a.rowid = n.fk_action";
  155. if ($notifcode)
  156. {
  157. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  158. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  159. }
  160. $sql.= " AND c.entity IN (".getEntity('user', 1).")";
  161. if ($userid > 0) $sql.= " AND c.rowid = ".$userid;
  162. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  163. $resql = $this->db->query($sql);
  164. if ($resql)
  165. {
  166. $num = $this->db->num_rows($resql);
  167. $i=0;
  168. while ($i < $num)
  169. {
  170. $obj = $this->db->fetch_object($resql);
  171. if ($obj)
  172. {
  173. $newval2=trim($obj->email);
  174. $isvalid=isValidEmail($newval2);
  175. if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
  176. }
  177. $i++;
  178. }
  179. }
  180. else
  181. {
  182. $error++;
  183. $this->error=$this->db->lasterror();
  184. }
  185. }
  186. }
  187. if (! $error)
  188. {
  189. // List of notifications enabled for fixed email
  190. foreach($conf->global as $key => $val)
  191. {
  192. if ($notifcode)
  193. {
  194. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  195. }
  196. else
  197. {
  198. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  199. }
  200. $threshold = (float) $reg[1];
  201. if ($valueforthreshold < $threshold) continue;
  202. $tmpemail=explode(',',$val);
  203. foreach($tmpemail as $key2 => $val2)
  204. {
  205. $newval2=trim($val2);
  206. if ($newval2 == '__SUPERVISOREMAIL__')
  207. {
  208. if ($user->fk_user > 0)
  209. {
  210. $tmpuser=new User($this->db);
  211. $tmpuser->fetch($user->fk_user);
  212. if ($tmpuser->email) $newval2=trim($tmpuser->email);
  213. else $newval2='';
  214. }
  215. else $newval2='';
  216. }
  217. if ($newval2)
  218. {
  219. $isvalid=isValidEmail($newval2, 0);
  220. if (empty($resarray[$newval2])) $resarray[$newval2]=array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
  221. }
  222. }
  223. }
  224. }
  225. if ($error) return -1;
  226. //var_dump($resarray);
  227. return $resarray;
  228. }
  229. /**
  230. * Check if notification are active for couple action/company.
  231. * If yes, send mail and save trace into llx_notify.
  232. *
  233. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  234. * @param Object $object Object the notification deals on
  235. * @return int <0 if KO, or number of changes if OK
  236. */
  237. function send($notifcode, $object)
  238. {
  239. global $user,$conf,$langs,$mysoc,$dolibarr_main_url_root;
  240. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  241. dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
  242. $langs->load("other");
  243. // Define $urlwithroot
  244. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  245. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  246. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  247. // Define some vars
  248. $application = $mysoc->name;
  249. //if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE;
  250. $replyto = $conf->notification->email_from;
  251. $filename = basename($file);
  252. $mimefile = dol_mimetype($file);
  253. $object_type = '';
  254. $link = '';
  255. $num = 0;
  256. if (! in_array(
  257. $notifcode,
  258. array(
  259. 'BILL_VALIDATE',
  260. 'BILL_PAYED',
  261. 'ORDER_VALIDATE',
  262. 'PROPAL_VALIDATE',
  263. 'FICHINTER_VALIDATE',
  264. 'FICHINTER_ADD_CONTACT',
  265. 'ORDER_SUPPLIER_VALIDATE',
  266. 'ORDER_SUPPLIER_APPROVE',
  267. 'ORDER_SUPPLIER_REFUSE',
  268. 'SHIPPING_VALIDATE'
  269. )
  270. )
  271. )
  272. {
  273. return 0;
  274. }
  275. $oldref=(empty($object->oldref)?$object->ref:$object->oldref);
  276. $newref=(empty($object->newref)?$object->ref:$object->newref);
  277. // Check notification per third party
  278. $sql = "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
  279. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  280. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
  281. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  282. $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
  283. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  284. $sql.= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
  285. $sql.= " AND n.fk_soc = s.rowid";
  286. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  287. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  288. $sql .= " AND s.rowid = ".$object->socid;
  289. // Check notification per user
  290. $sql.= "\nUNION\n";
  291. /*
  292. $sql.= "SELECT 1 as user, c.email, c.rowid as cid, c.lastname, c.firstname, '$langs->defaultlang' as default_lang,";
  293. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  294. $sql.= " FROM ".MAIN_DB_PREFIX."user as c,";
  295. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  296. $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
  297. $sql.= " ".MAIN_DB_PREFIX."element_contact as ec";
  298. $sql.= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
  299. $sql.= " AND n.fk_user = ec.fk_socpeople";
  300. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  301. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  302. $sql .= " AND ec.element_id = ".$object->id;*/
  303. $sql.= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
  304. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  305. $sql.= " FROM ".MAIN_DB_PREFIX."user as c,";
  306. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  307. $sql.= " ".MAIN_DB_PREFIX."notify_def as n";
  308. $sql.= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
  309. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  310. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  311. $result = $this->db->query($sql);
  312. if ($result)
  313. {
  314. $num = $this->db->num_rows($result);
  315. if ($num > 0)
  316. {
  317. $i = 0;
  318. while ($i < $num && ! $error) // For each notification couple defined (third party/actioncode)
  319. {
  320. $obj = $this->db->fetch_object($result);
  321. $sendto = dolGetFirstLastname($obj->firstname,$obj->lastname) . " <".$obj->email.">";
  322. $notifcodedefid = $obj->adid;
  323. if (dol_strlen($obj->email))
  324. {
  325. // Set output language
  326. $outputlangs = $langs;
  327. if ($obj->default_lang && $obj->default_lang != $langs->defaultlang)
  328. {
  329. $outputlangs = new Translate('', $conf);
  330. $outputlangs->setDefaultLang($obj->default_lang);
  331. }
  332. switch ($notifcode) {
  333. case 'BILL_VALIDATE':
  334. $link='/compta/facture.php?facid='.$object->id;
  335. $dir_output = $conf->facture->dir_output;
  336. $object_type = 'facture';
  337. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
  338. break;
  339. case 'BILL_PAYED':
  340. $link='/compta/facture.php?facid='.$object->id;
  341. $dir_output = $conf->facture->dir_output;
  342. $object_type = 'facture';
  343. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed",$newref);
  344. break;
  345. case 'ORDER_VALIDATE':
  346. $link='/commande/card.php?id='.$object->id;
  347. $dir_output = $conf->commande->dir_output;
  348. $object_type = 'order';
  349. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
  350. break;
  351. case 'PROPAL_VALIDATE':
  352. $link='/comm/propal/card.php?id='.$object->id;
  353. $dir_output = $conf->propal->dir_output;
  354. $object_type = 'propal';
  355. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
  356. break;
  357. case 'FICHINTER_ADD_CONTACT':
  358. $link='/fichinter/card.php?id='.$object->id;
  359. $dir_output = $conf->facture->dir_output;
  360. $object_type = 'ficheinter';
  361. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$object->ref);
  362. break;
  363. case 'FICHINTER_VALIDATE':
  364. $link='/fichinter/card.php?id='.$object->id;
  365. $dir_output = $conf->facture->dir_output;
  366. $object_type = 'ficheinter';
  367. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$object->ref);
  368. break;
  369. case 'ORDER_SUPPLIER_VALIDATE':
  370. $link='/fourn/commande/card.php?id='.$object->id;
  371. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  372. $object_type = 'order_supplier';
  373. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  374. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$object->ref,$user->getFullName($langs));
  375. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  376. break;
  377. case 'ORDER_SUPPLIER_APPROVE':
  378. $link='/fourn/commande/card.php?id='.$object->id;
  379. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  380. $object_type = 'order_supplier';
  381. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  382. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  383. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  384. break;
  385. case 'ORDER_SUPPLIER_REFUSE':
  386. $link='/fourn/commande/card.php?id='.$object->id;
  387. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  388. $object_type = 'order_supplier';
  389. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  390. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$newref,$user->getFullName($langs));
  391. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  392. break;
  393. case 'SHIPPING_VALIDATE':
  394. $dir_output = $conf->expedition->dir_output.'/sending/';
  395. $object_type = 'order_supplier';
  396. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  397. break;
  398. }
  399. $ref = dol_sanitizeFileName($newref);
  400. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  401. if (! dol_is_file($pdf_path))
  402. {
  403. // We can't add PDF as it is not generated yet.
  404. $filepdf = '';
  405. }
  406. else
  407. {
  408. $filepdf = $pdf_path;
  409. }
  410. $subject = '['.$application.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification");
  411. $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  412. $message.= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  413. $message.= "\n";
  414. $message.= $mesg;
  415. if ($link) $message=dol_concatdesc($message,$urlwithroot.$link);
  416. $mailfile = new CMailFile(
  417. $subject,
  418. $sendto,
  419. $replyto,
  420. $message,
  421. array($file),
  422. array($mimefile),
  423. array($filename[count($filename)-1]),
  424. '',
  425. '',
  426. 0,
  427. -1
  428. );
  429. if ($mailfile->sendfile())
  430. {
  431. if ($obj->type_target == 'touserid') {
  432. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
  433. $sql.= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid?$object->socid:'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
  434. }
  435. else {
  436. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
  437. $sql.= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid?$object->socid:'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
  438. }
  439. if (! $this->db->query($sql))
  440. {
  441. dol_print_error($this->db);
  442. }
  443. }
  444. else
  445. {
  446. $error++;
  447. $this->errors[]=$mailfile->error;
  448. }
  449. }
  450. else
  451. {
  452. dol_syslog("No notification sent for ".$sendto." because email is empty");
  453. }
  454. $i++;
  455. }
  456. }
  457. else
  458. {
  459. dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".$object->socid);
  460. }
  461. }
  462. else
  463. {
  464. $error++;
  465. $this->errors[]=$this->db->lasterror();
  466. dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
  467. return -1;
  468. }
  469. // Check notification using fixed email
  470. if (! $error)
  471. {
  472. foreach($conf->global as $key => $val)
  473. {
  474. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  475. $threshold = (float) $reg[1];
  476. if (!empty($object->total_ht) && $object->total_ht <= $threshold)
  477. {
  478. dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
  479. continue;
  480. }
  481. $param='NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_'.$reg[1];
  482. $sendto = $conf->global->$param;
  483. $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
  484. if ($notifcodedefid <= 0) dol_print_error($this->db, 'Failed to get id from code');
  485. $object_type = '';
  486. $link = '';
  487. $num++;
  488. switch ($notifcode) {
  489. case 'BILL_VALIDATE':
  490. $link='/compta/facture.php?facid='.$object->id;
  491. $dir_output = $conf->facture->dir_output;
  492. $object_type = 'facture';
  493. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
  494. break;
  495. case 'BILL_PAYED':
  496. $link='/compta/facture.php?facid='.$object->id;
  497. $dir_output = $conf->facture->dir_output;
  498. $object_type = 'facture';
  499. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed",$newref);
  500. break;
  501. case 'ORDER_VALIDATE':
  502. $link='/commande/card.php?id='.$object->id;
  503. $dir_output = $conf->commande->dir_output;
  504. $object_type = 'order';
  505. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
  506. break;
  507. case 'PROPAL_VALIDATE':
  508. $link='/comm/propal/card.php?id='.$object->id;
  509. $dir_output = $conf->propal->dir_output;
  510. $object_type = 'propal';
  511. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
  512. break;
  513. case 'FICHINTER_ADD_CONTACT':
  514. $link='/fichinter/card.php?id='.$object->id;
  515. $dir_output = $conf->facture->dir_output;
  516. $object_type = 'ficheinter';
  517. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$newref);
  518. break;
  519. case 'FICHINTER_VALIDATE':
  520. $link='/fichinter/card.php?id='.$object->id;
  521. $dir_output = $conf->facture->dir_output;
  522. $object_type = 'ficheinter';
  523. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$newref);
  524. break;
  525. case 'ORDER_SUPPLIER_VALIDATE':
  526. $link='/fourn/commande/card.php?id='.$object->id;
  527. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  528. $object_type = 'order_supplier';
  529. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  530. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$newref,$user->getFullName($langs));
  531. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  532. break;
  533. case 'ORDER_SUPPLIER_APPROVE':
  534. $link='/fourn/commande/card.php?id='.$object->id;
  535. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  536. $object_type = 'order_supplier';
  537. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  538. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  539. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  540. break;
  541. case 'ORDER_SUPPLIER_APPROVE2':
  542. $link='/fourn/commande/card.php?id='.$object->id;
  543. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  544. $object_type = 'order_supplier';
  545. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  546. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  547. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  548. break;
  549. case 'ORDER_SUPPLIER_REFUSE':
  550. $link='/fourn/commande/card.php?id='.$object->id;
  551. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  552. $object_type = 'order_supplier';
  553. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  554. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$newref,$user->getFullName($langs));
  555. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  556. break;
  557. case 'SHIPPING_VALIDATE':
  558. $dir_output = $conf->expedition->dir_output.'/sending/';
  559. $object_type = 'order_supplier';
  560. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  561. break;
  562. }
  563. $ref = dol_sanitizeFileName($newref);
  564. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  565. if (! dol_is_file($pdf_path))
  566. {
  567. // We can't add PDF as it is not generated yet.
  568. $filepdf = '';
  569. }
  570. else
  571. {
  572. $filepdf = $pdf_path;
  573. }
  574. $subject = '['.$application.'] '.$langs->transnoentitiesnoconv("DolibarrNotification");
  575. $message = $langs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  576. $message.= $langs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  577. $message.= "\n";
  578. $message.= $mesg;
  579. if ($link) $message=dol_concatdesc($message,$urlwithroot.$link);
  580. // Replace keyword __SUPERVISOREMAIL__
  581. if (preg_match('/__SUPERVISOREMAIL__/', $sendto))
  582. {
  583. $newval='';
  584. if ($user->fk_user > 0)
  585. {
  586. $supervisoruser=new User($this->db);
  587. $supervisoruser->fetch($user->fk_user);
  588. if ($supervisoruser->email) $newval=trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
  589. }
  590. dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
  591. $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
  592. $sendto = preg_replace('/^[\s,]+/','',$sendto); // Clean start of string
  593. $sendto = preg_replace('/[\s,]+$/','',$sendto); // Clean end of string
  594. }
  595. if ($sendto)
  596. {
  597. $mailfile = new CMailFile(
  598. $subject,
  599. $sendto,
  600. $replyto,
  601. $message,
  602. array($file),
  603. array($mimefile),
  604. array($filename[count($filename)-1]),
  605. '',
  606. '',
  607. 0,
  608. -1
  609. );
  610. if ($mailfile->sendfile())
  611. {
  612. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
  613. $sql.= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid?$object->socid:'null').", null, 'email', 'tofixedemail', '".$object_type."', ".$object->id.", '".$this->db->escape($conf->global->$param)."')";
  614. if (! $this->db->query($sql))
  615. {
  616. dol_print_error($this->db);
  617. }
  618. }
  619. else
  620. {
  621. $error++;
  622. $this->errors[]=$mailfile->error;
  623. }
  624. }
  625. }
  626. }
  627. if (! $error) return $num;
  628. else return -1 * $error;
  629. }
  630. }