dolibarr  9.0.0
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 
36  public $db;
37 
38  var $arrayofproducts=array();
39 
40 
46  function __construct($db)
47  {
48  global $conf;
49 
50  $this->db=$db;
51 
52  $this->arrayofproducts=array();
53 
54  // List of services
55  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
56  $sql.= " WHERE entity IN (".getEntity('product').")";
57  if (empty($conf->global->CONTRACT_SUPPORT_PRODUCTS)) $sql.= " AND fk_product_type = 1"; // By default, only services
58  $sql.= " ORDER BY ref";
59  $result=$this->db->query($sql);
60  if ($result)
61  {
62  $num = $this->db->num_rows($result);
63  dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
64 
65  $i = 0;
66  while ($i < $num)
67  {
68  $obj = $this->db->fetch_object($result);
69  $i++;
70  $this->arrayofproducts[$i]=$obj->ref;
71  }
72  }
73  else
74  {
75  dol_print_error($this->db);
76  }
77  }
78 
79 
80  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
88  function add_to_target($mailing_id,$filtersarray=array())
89  {
90  // phpcs:enable
91  $target = array();
92 
93  // ----- Your code start here -----
94 
95  $cibles = array();
96  $j = 0;
97 
98  $product='';
99  foreach($filtersarray as $key)
100  {
101  if ($key == '0') return "Error: You must choose a filter";
102  $product=$this->arrayofproducts[$key];
103  }
104 
105  $now=dol_now();
106 
107  // La requete doit retourner: id, email, name
108  $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";
109  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
110  $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
111  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
112  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
113  $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
114  $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$product."'";
115  $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
116  $sql.= " ORDER BY s.email";
117 
118  // Stocke destinataires dans cibles
119  $result=$this->db->query($sql);
120  if ($result)
121  {
122  $num = $this->db->num_rows($result);
123  $i = 0;
124 
125  dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
126 
127  $old = '';
128  while ($i < $num)
129  {
130  $obj = $this->db->fetch_object($result);
131  if ($old <> $obj->email)
132  {
133  $cibles[$j] = array(
134  'email' => $obj->email,
135  'lastname' => $obj->name, // For thirdparties, lastname must be name
136  'firstname' => '', // For thirdparties, firstname is ''
137  'other' =>
138  ('DateStart='.dol_print_date($this->db->jdate($obj->date_ouverture),'day')).';'.
139  ('DateEnd='.dol_print_date($this->db->jdate($obj->date_fin_validite),'day')).';'.
140  ('Contract='.$obj->fk_contrat).';'.
141  ('ContactLine='.$obj->cdid),
142  'source_url' => $this->url($obj->id),
143  'source_id' => $obj->id,
144  'source_type' => 'thirdparty'
145  );
146  $old = $obj->email;
147  $j++;
148  }
149 
150  $i++;
151  }
152  }
153  else
154  {
155  dol_syslog($this->db->lasterror());
156  $this->error=$this->db->lasterror();
157  return -1;
158  }
159 
160  // ----- Your code end here -----
161 
162  return parent::add_to_target($mailing_id, $cibles);
163  }
164 
165 
175  {
176 
177  //var $statssql=array();
178  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
179 
180  return array();
181  }
182 
183 
192  function getNbOfRecipients($sql='')
193  {
194  $now=dol_now();
195 
196  // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
197  // Example: return 500;
198  $sql = "SELECT count(*) as nb";
199  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
200  $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
201  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
202  $sql.= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
203  $sql.= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
204  $sql.= " AND p.ref IN ('".join("','",$this->arrayofproducts)."')";
205  $sql.= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
206 
207  $a=parent::getNbOfRecipients($sql);
208 
209  return $a;
210  }
211 
218  function formFilter()
219  {
220  global $langs;
221 
222  $s=$langs->trans("ProductOrService");
223  $s.='<select name="filter" class="flat">';
224  if (count($this->arrayofproducts)) $s.='<option value="0">&nbsp;</option>';
225  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
226  foreach($this->arrayofproducts as $key => $val)
227  {
228  $s.='<option value="'.$key.'">'.$val.'</option>';
229  }
230  $s.='</select>';
231  return $s;
232  }
233 
234 
241  function url($id)
242  {
243  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('',"company").'</a>';
244  }
245 }
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.
dol_now($mode='gmt')
Return date for now.
Class to offer a selector of emailing targets with Rule &#39;services expired&#39;.
add_to_target($mailing_id, $filtersarray=array())
This is the main function that returns the array of emails.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Parent class of emailing target selectors modules.