thirdparties_services_expired.modules.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This file is an example to follow to add your own email selector inside
  5. * the Dolibarr email tool.
  6. * Follow instructions given in README file to know what to change to build
  7. * your own emailing list selector.
  8. * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
  9. */
  10. /**
  11. * \file htdocs/core/modules/mailings/thirdparties_services_expired.modules.php
  12. * \ingroup mailing
  13. * \brief File of class to offer a selector of emailing targets with Rule 'services expired'.
  14. */
  15. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  16. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  17. /**
  18. * Class to offer a selector of emailing targets with Rule 'services expired'.
  19. */
  20. class mailing_thirdparties_services_expired extends MailingTargets
  21. {
  22. public $name = 'DolibarrContractsLinesExpired';
  23. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  24. public $desc = 'Third parties with expired contract\'s lines';
  25. public $require_admin = 0;
  26. public $require_module = array('contrat');
  27. public $enabled = '$conf->societe->enabled';
  28. /**
  29. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  30. */
  31. public $picto = 'company';
  32. /**
  33. * @var DoliDB Database handler.
  34. */
  35. public $db;
  36. public $arrayofproducts = array();
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. public function __construct($db)
  43. {
  44. global $conf;
  45. $this->db = $db;
  46. $this->arrayofproducts = array();
  47. // List of services
  48. $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
  49. $sql .= " WHERE entity IN (".getEntity('product').")";
  50. if (empty($conf->global->CONTRACT_SUPPORT_PRODUCTS)) {
  51. $sql .= " AND fk_product_type = 1"; // By default, only services
  52. }
  53. $sql .= " ORDER BY ref";
  54. $result = $this->db->query($sql);
  55. if ($result) {
  56. $num = $this->db->num_rows($result);
  57. dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
  58. $i = 0;
  59. while ($i < $num) {
  60. $obj = $this->db->fetch_object($result);
  61. $i++;
  62. $this->arrayofproducts[$i] = $obj->ref;
  63. }
  64. } else {
  65. dol_print_error($this->db);
  66. }
  67. }
  68. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  69. /**
  70. * This is the main function that returns the array of emails
  71. *
  72. * @param int $mailing_id Id of mailing. No need to use it.
  73. * @return int <0 if error, number of emails added if ok
  74. */
  75. public function add_to_target($mailing_id)
  76. {
  77. // phpcs:enable
  78. $key = GETPOST('filter', 'int');
  79. $cibles = array();
  80. $j = 0;
  81. $product = '';
  82. if ($key == '0') {
  83. $this->error = "Error: You must choose a filter";
  84. $this->errors[] = $this->error;
  85. return $this->error;
  86. }
  87. $product = $this->arrayofproducts[$key];
  88. $now = dol_now();
  89. // La requete doit retourner: id, email, name
  90. $sql = "SELECT s.rowid as id, s.email, s.nom as name, cd.rowid as cdid, cd.date_ouverture, cd.date_fin_validite, cd.fk_contrat";
  91. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
  92. $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
  93. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  94. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  95. $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
  96. $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$this->db->escape($product)."'";
  97. $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
  98. $sql .= " ORDER BY s.email";
  99. // Stocke destinataires dans cibles
  100. $result = $this->db->query($sql);
  101. if ($result) {
  102. $num = $this->db->num_rows($result);
  103. $i = 0;
  104. dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
  105. $old = '';
  106. while ($i < $num) {
  107. $obj = $this->db->fetch_object($result);
  108. if ($old <> $obj->email) {
  109. $cibles[$j] = array(
  110. 'email' => $obj->email,
  111. 'lastname' => $obj->name, // For thirdparties, lastname must be name
  112. 'firstname' => '', // For thirdparties, firstname is ''
  113. 'other' =>
  114. ('DateStart='.dol_print_date($this->db->jdate($obj->date_ouverture), 'day')).';'.
  115. ('DateEnd='.dol_print_date($this->db->jdate($obj->date_fin_validite), 'day')).';'.
  116. ('Contract='.$obj->fk_contrat).';'.
  117. ('ContactLine='.$obj->cdid),
  118. 'source_url' => $this->url($obj->id),
  119. 'source_id' => $obj->id,
  120. 'source_type' => 'thirdparty'
  121. );
  122. $old = $obj->email;
  123. $j++;
  124. }
  125. $i++;
  126. }
  127. } else {
  128. dol_syslog($this->db->lasterror());
  129. $this->error = $this->db->lasterror();
  130. return -1;
  131. }
  132. // ----- Your code end here -----
  133. return parent::addTargetsToDatabase($mailing_id, $cibles);
  134. }
  135. /**
  136. * On the main mailing area, there is a box with statistics.
  137. * If you want to add a line in this report you must provide an
  138. * array of SQL request that returns two field:
  139. * One called "label", One called "nb".
  140. *
  141. * @return array Array with SQL requests
  142. */
  143. public function getSqlArrayForStats()
  144. {
  145. //var $statssql=array();
  146. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  147. return array();
  148. }
  149. /**
  150. * Return here number of distinct emails returned by your selector.
  151. * For example if this selector is used to extract 500 different
  152. * emails from a text file, this function must return 500.
  153. *
  154. * @param string $sql SQL request to use to count
  155. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  156. */
  157. public function getNbOfRecipients($sql = '')
  158. {
  159. $now = dol_now();
  160. // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
  161. // Example: return 500;
  162. $sql = "SELECT count(*) as nb";
  163. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
  164. $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
  165. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  166. $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
  167. $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
  168. $sql .= " AND p.ref IN (".$this->db->sanitize("'".join("','", $this->arrayofproducts)."'", 1).")";
  169. $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
  170. $a = parent::getNbOfRecipients($sql);
  171. return $a;
  172. }
  173. /**
  174. * This is to add a form filter to provide variant of selector
  175. * If used, the HTML select must be called "filter"
  176. *
  177. * @return string A html select zone
  178. */
  179. public function formFilter()
  180. {
  181. global $langs;
  182. $s = '<select id="filter_services_expired" name="filter" class="flat">';
  183. if (count($this->arrayofproducts)) {
  184. $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
  185. } else {
  186. $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  187. }
  188. foreach ($this->arrayofproducts as $key => $val) {
  189. $s .= '<option value="'.$key.'">'.$val.'</option>';
  190. }
  191. $s .= '</select>';
  192. $s .= ajax_combobox("filter_services_expired");
  193. return $s;
  194. }
  195. /**
  196. * Can include an URL link on each record provided by selector shown on target page.
  197. *
  198. * @param int $id ID
  199. * @return string Url link
  200. */
  201. public function url($id)
  202. {
  203. return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
  204. }
  205. }