dolibarr  7.0.0-beta
thirdparties_services_expired.modules.php
Go to the documentation of this file.
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 
16 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
17 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
18 
19 
24 {
25  var $name='DolibarrContractsLinesExpired';
26  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
27  var $desc='Third parties with expired contract\'s lines';
28  var $require_admin=0;
29 
30  var $require_module=array('contrat');
31  var $picto='company';
32  var $db;
33  var $arrayofproducts=array();
34 
35 
41  function __construct($db)
42  {
43  global $conf;
44 
45  $this->db=$db;
46 
47  $this->arrayofproducts=array();
48 
49  // List of services
50  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
51  $sql.= " WHERE entity IN (".getEntity('product').")";
52  if (empty($conf->global->CONTRACT_SUPPORT_PRODUCTS)) $sql.= " AND fk_product_type = 1"; // By default, only services
53  $sql.= " ORDER BY ref";
54  $result=$this->db->query($sql);
55  if ($result)
56  {
57  $num = $this->db->num_rows($result);
58  dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
59 
60  $i = 0;
61  while ($i < $num)
62  {
63  $obj = $this->db->fetch_object($result);
64  $i++;
65  $this->arrayofproducts[$i]=$obj->ref;
66  }
67 
68  }
69  else
70  {
71  dol_print_error($this->db);
72  }
73  }
74 
75 
83  function add_to_target($mailing_id,$filtersarray=array())
84  {
85  $target = array();
86 
87  // ----- Your code start here -----
88 
89  $cibles = array();
90  $j = 0;
91 
92  $product='';
93  foreach($filtersarray as $key)
94  {
95  if ($key == '0') return "Error: You must choose a filter";
96  $product=$this->arrayofproducts[$key];
97  }
98 
99  $now=dol_now();
100 
101  // La requete doit retourner: id, email, name
102  $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";
103  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
104  $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
105  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
106  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
107  $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
108  $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$product."'";
109  $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
110  $sql.= " ORDER BY s.email";
111 
112  // Stocke destinataires dans cibles
113  $result=$this->db->query($sql);
114  if ($result)
115  {
116  $num = $this->db->num_rows($result);
117  $i = 0;
118 
119  dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
120 
121  $old = '';
122  while ($i < $num)
123  {
124  $obj = $this->db->fetch_object($result);
125  if ($old <> $obj->email)
126  {
127  $cibles[$j] = array(
128  'email' => $obj->email,
129  'lastname' => $obj->name, // For thirdparties, lastname must be name
130  'firstname' => '', // For thirdparties, firstname is ''
131  'other' =>
132  ('DateStart='.dol_print_date($this->db->jdate($obj->date_ouverture),'day')).';'.
133  ('DateEnd='.dol_print_date($this->db->jdate($obj->date_fin_validite),'day')).';'.
134  ('Contract='.$obj->fk_contrat).';'.
135  ('ContactLine='.$obj->cdid),
136  'source_url' => $this->url($obj->id),
137  'source_id' => $obj->id,
138  'source_type' => 'thirdparty'
139  );
140  $old = $obj->email;
141  $j++;
142  }
143 
144  $i++;
145  }
146  }
147  else
148  {
149  dol_syslog($this->db->lasterror());
150  $this->error=$this->db->lasterror();
151  return -1;
152  }
153 
154  // ----- Your code end here -----
155 
156  return parent::add_to_target($mailing_id, $cibles);
157  }
158 
159 
169  {
170 
171  //var $statssql=array();
172  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
173 
174  return array();
175  }
176 
177 
186  function getNbOfRecipients($sql='')
187  {
188  $now=dol_now();
189 
190  // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
191  // Example: return 500;
192  $sql = "SELECT count(*) as nb";
193  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
194  $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
195  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
196  $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
197  $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
198  $sql.= " AND p.ref IN ('".join("','",$this->arrayofproducts)."')";
199  $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
200 
201  $a=parent::getNbOfRecipients($sql);
202 
203  return $a;
204  }
205 
212  function formFilter()
213  {
214  global $langs;
215 
216  $s='';
217  $s.='<select name="filter" class="flat">';
218  if (count($this->arrayofproducts)) $s.='<option value="0">&nbsp;</option>';
219  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
220  foreach($this->arrayofproducts as $key => $val)
221  {
222  $s.='<option value="'.$key.'">'.$val.'</option>';
223  }
224  $s.='</select>';
225  return $s;
226  }
227 
228 
235  function url($id)
236  {
237  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('',"company").'</a>';
238  }
239 
240 }
241 
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
url($id)
Can include an URL link on each record provided by selector shown on target page. ...
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='gmt')
Return date for now.
Class to offer a selector of emailing targets with Rule 'services expired'.
add_to_target($mailing_id, $filtersarray=array())
This is the main function that returns the array of emails.
Parent class of emailing target selectors modules.