PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/modules/mailings/thirdparties_services_expired.modules.php

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