thirdparties_services_expired.modules.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. var $name='DolibarrContractsLinesExpired';
  23. var $desc='Third parties with expired contract\'s lines';
  24. var $require_admin=0;
  25. var $require_module=array('contrat');
  26. var $picto='company';
  27. var $db;
  28. var $arrayofproducts=array();
  29. /**
  30. * Constructor
  31. *
  32. * @param DoliDB $db Database handler
  33. */
  34. function __construct($db)
  35. {
  36. $this->db=$db;
  37. $this->arrayofproducts=array();
  38. // List of services
  39. $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
  40. $sql.= " WHERE entity IN (".getEntity('product', 1).")";
  41. $sql.= " AND fk_product_type = 1";
  42. $sql.= " ORDER BY ref";
  43. $result=$this->db->query($sql);
  44. if ($result)
  45. {
  46. $num = $this->db->num_rows($result);
  47. dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
  48. $i = 0;
  49. while ($i < $num)
  50. {
  51. $obj = $this->db->fetch_object($result);
  52. $i++;
  53. $this->arrayofproducts[$i]=$obj->ref;
  54. }
  55. }
  56. else
  57. {
  58. dol_print_error($this->db);
  59. }
  60. }
  61. /**
  62. * This is the main function that returns the array of emails
  63. *
  64. * @param int $mailing_id Id of mailing. No need to use it.
  65. * @param array $filtersarray If you used the formFilter function. Empty otherwise.
  66. * @return int <0 if error, number of emails added if ok
  67. */
  68. function add_to_target($mailing_id,$filtersarray=array())
  69. {
  70. $target = array();
  71. // ----- Your code start here -----
  72. $cibles = array();
  73. $j = 0;
  74. $product='';
  75. foreach($filtersarray as $key)
  76. {
  77. if ($key == '0') return "Error: You must choose a filter";
  78. $product=$this->arrayofproducts[$key];
  79. }
  80. $now=dol_now();
  81. // La requete doit retourner: id, email, name
  82. $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";
  83. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
  84. $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
  85. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  86. $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
  87. $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$product."'";
  88. $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
  89. $sql.= " ORDER BY s.email";
  90. // Stocke destinataires dans cibles
  91. $result=$this->db->query($sql);
  92. if ($result)
  93. {
  94. $num = $this->db->num_rows($result);
  95. $i = 0;
  96. dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
  97. $old = '';
  98. while ($i < $num)
  99. {
  100. $obj = $this->db->fetch_object($result);
  101. if ($old <> $obj->email)
  102. {
  103. $cibles[$j] = array(
  104. 'email' => $obj->email,
  105. 'lastname' => $obj->lastname,
  106. 'other' =>
  107. ('StartDate='.dol_print_date($this->db->jdate($obj->date_ouverture),'day')).';'.
  108. ('EndDate='.dol_print_date($this->db->jdate($obj->date_fin_validite),'day')).';'.
  109. ('Contract='.$obj->fk_contrat).';'.
  110. ('ContactLine='.$obj->cdid),
  111. 'source_url' => $this->url($obj->id),
  112. 'source_id' => $obj->id,
  113. 'source_type' => 'thirdparty'
  114. );
  115. $old = $obj->email;
  116. $j++;
  117. }
  118. $i++;
  119. }
  120. }
  121. else
  122. {
  123. dol_syslog($this->db->error());
  124. $this->error=$this->db->error();
  125. return -1;
  126. }
  127. // ----- Your code end here -----
  128. return parent::add_to_target($mailing_id, $cibles);
  129. }
  130. /**
  131. * On the main mailing area, there is a box with statistics.
  132. * If you want to add a line in this report you must provide an
  133. * array of SQL request that returns two field:
  134. * One called "label", One called "nb".
  135. *
  136. * @return array Array with SQL requests
  137. */
  138. function getSqlArrayForStats()
  139. {
  140. //var $statssql=array();
  141. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  142. return array();
  143. }
  144. /**
  145. * Return here number of distinct emails returned by your selector.
  146. * For example if this selector is used to extract 500 different
  147. * emails from a text file, this function must return 500.
  148. *
  149. * @param string $sql SQL request to use to count
  150. * @return int Number of recipients
  151. */
  152. function getNbOfRecipients($sql='')
  153. {
  154. $now=dol_now();
  155. // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
  156. // Example: return 500;
  157. $sql = "SELECT count(*) as nb";
  158. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
  159. $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
  160. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  161. $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
  162. $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
  163. $sql.= " AND p.ref IN ('".join("','",$this->arrayofproducts)."')";
  164. $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
  165. //print $sql;
  166. $a=parent::getNbOfRecipients($sql);
  167. return $a;
  168. }
  169. /**
  170. * This is to add a form filter to provide variant of selector
  171. * If used, the HTML select must be called "filter"
  172. *
  173. * @return string A html select zone
  174. */
  175. function formFilter()
  176. {
  177. global $langs;
  178. $s='';
  179. $s.='<select name="filter" class="flat">';
  180. if (count($this->arrayofproducts)) $s.='<option value="0">&nbsp;</option>';
  181. else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  182. foreach($this->arrayofproducts as $key => $val)
  183. {
  184. $s.='<option value="'.$key.'">'.$val.'</option>';
  185. }
  186. $s.='</select>';
  187. return $s;
  188. }
  189. /**
  190. * Can include an URL link on each record provided by selector shown on target page.
  191. *
  192. * @param int $id ID
  193. * @return string Url link
  194. */
  195. function url($id)
  196. {
  197. //$companystatic=new Societe($this->db);
  198. //$companystatic->id=$id;
  199. //$companystatic->nom='';
  200. //return $companystatic->getNomUrl(1); // Url too long
  201. return '<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$id.'">'.img_object('',"company").'</a>';
  202. }
  203. }
  204. ?>