dolibarr  13.0.2
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  public $name = 'DolibarrContractsLinesExpired';
26  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
27  public $desc = 'Third parties with expired contract\'s lines';
28  public $require_admin = 0;
29 
30  public $require_module = array('contrat');
31 
35  public $picto = 'company';
36 
40  public $db;
41 
42  public $arrayofproducts = array();
43 
44 
50  public function __construct($db)
51  {
52  global $conf;
53 
54  $this->db = $db;
55 
56  $this->arrayofproducts = array();
57 
58  // List of services
59  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
60  $sql .= " WHERE entity IN (".getEntity('product').")";
61  if (empty($conf->global->CONTRACT_SUPPORT_PRODUCTS)) $sql .= " AND fk_product_type = 1"; // By default, only services
62  $sql .= " ORDER BY ref";
63  $result = $this->db->query($sql);
64  if ($result)
65  {
66  $num = $this->db->num_rows($result);
67  dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
68 
69  $i = 0;
70  while ($i < $num)
71  {
72  $obj = $this->db->fetch_object($result);
73  $i++;
74  $this->arrayofproducts[$i] = $obj->ref;
75  }
76  } else {
77  dol_print_error($this->db);
78  }
79  }
80 
81 
82  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
89  public function add_to_target($mailing_id)
90  {
91  // phpcs:enable
92  $key = GETPOST('filter', 'int');
93 
94  $cibles = array();
95  $j = 0;
96 
97  $product = '';
98  if ($key == '0')
99  {
100  $this->error = "Error: You must choose a filter";
101  $this->errors[] = $this->error;
102  return $this->error;
103  }
104 
105  $product = $this->arrayofproducts[$key];
106 
107  $now = dol_now();
108 
109  // La requete doit retourner: id, email, name
110  $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";
111  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
112  $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
113  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
114  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
115  $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
116  $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$this->db->escape($product)."'";
117  $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
118  $sql .= " ORDER BY s.email";
119 
120  // Stocke destinataires dans cibles
121  $result = $this->db->query($sql);
122  if ($result)
123  {
124  $num = $this->db->num_rows($result);
125  $i = 0;
126 
127  dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
128 
129  $old = '';
130  while ($i < $num)
131  {
132  $obj = $this->db->fetch_object($result);
133  if ($old <> $obj->email)
134  {
135  $cibles[$j] = array(
136  'email' => $obj->email,
137  'lastname' => $obj->name, // For thirdparties, lastname must be name
138  'firstname' => '', // For thirdparties, firstname is ''
139  'other' =>
140  ('DateStart='.dol_print_date($this->db->jdate($obj->date_ouverture), 'day')).';'.
141  ('DateEnd='.dol_print_date($this->db->jdate($obj->date_fin_validite), 'day')).';'.
142  ('Contract='.$obj->fk_contrat).';'.
143  ('ContactLine='.$obj->cdid),
144  'source_url' => $this->url($obj->id),
145  'source_id' => $obj->id,
146  'source_type' => 'thirdparty'
147  );
148  $old = $obj->email;
149  $j++;
150  }
151 
152  $i++;
153  }
154  } else {
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::addTargetsToDatabase($mailing_id, $cibles);
163  }
164 
165 
174  public function getSqlArrayForStats()
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  public 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  public 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  public function url($id)
242  {
243  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
244  }
245 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
dol_now($mode= 'auto')
Return date for now.
url($id)
Can include an URL link on each record provided by selector shown on target page. ...
$conf db
API class for accounts.
Definition: inc.php:54
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called &quot;...
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class to offer a selector of emailing targets with Rule &#39;services expired&#39;.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
add_to_target($mailing_id)
This is the main function that returns the array of emails.
Parent class of emailing target selectors modules.