dolibarr  9.0.0
contacts1.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * or see http://www.gnu.org/
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
28 
29 
34 {
35  var $name='ContactCompanies'; // Identifiant du module mailing
36  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
37  var $desc='Contacts of thirdparties (prospects, customers, suppliers...)';
38  var $require_module=array("societe"); // Module mailing actif si modules require_module actifs
39  var $require_admin=0; // Module mailing actif pour user admin ou non
40  var $picto='contact';
41 
45  public $db;
46 
47 
53  function __construct($db)
54  {
55  $this->db=$db;
56  }
57 
58 
68  {
69  global $conf, $langs;
70 
71  $langs->load("commercial");
72 
73  $statssql=array();
74  $statssql[0] = "SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label,";
75  $statssql[0].= " count(distinct(c.email)) as nb";
76  $statssql[0].= " FROM ".MAIN_DB_PREFIX."socpeople as c";
77  $statssql[0].= " WHERE c.entity IN (".getEntity('socpeople').")";
78  $statssql[0].= " AND c.email != ''"; // Note that null != '' is false
79  $statssql[0].= " AND c.no_email = 0";
80  $statssql[0].= " AND c.statut = 1";
81 
82  return $statssql;
83  }
84 
85 
94  function getNbOfRecipients($sql='')
95  {
96  global $conf;
97 
98  $sql = "SELECT count(distinct(c.email)) as nb";
99  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
100  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
101  $sql.= " WHERE c.entity IN (".getEntity('socpeople').")";
102  $sql.= " AND c.email != ''"; // Note that null != '' is false
103  $sql.= " AND c.no_email = 0";
104  $sql.= " AND c.statut = 1";
105 
106  // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
107  return parent::getNbOfRecipients($sql);
108  }
109 
110 
116  function formFilter()
117  {
118  global $langs;
119 
120  // Load translation files required by the page
121  $langs->loadLangs(array("commercial","companies","suppliers","categories"));
122 
123  $s='';
124 
125  // Add filter on job position
126  $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
127  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
128  $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
129  /*$sql.= " AND sp.email != ''"; // Note that null != '' is false
130  $sql.= " AND sp.no_email = 0";
131  $sql.= " AND sp.statut = 1";*/
132  $sql.= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
133  $sql.= " GROUP BY sp.poste";
134  $sql.= " ORDER BY sp.poste";
135  $resql = $this->db->query($sql);
136 
137  $s.=$langs->trans("PostOrFunction").': ';
138  $s.='<select name="filter_jobposition" class="flat">';
139  $s.='<option value="all">&nbsp;</option>';
140  if ($resql)
141  {
142  $num = $this->db->num_rows($resql);
143  $i = 0;
144  while ($i < $num)
145  {
146  $obj = $this->db->fetch_object($resql);
147  $s.='<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
148  $i++;
149  }
150  }
151  else dol_print_error($this->db);
152  $s.='</select>';
153 
154  $s.=' ';
155 
156  // Filter on contact category
157  $s .= $langs->trans("ContactCategoriesShort").': ';
158  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
159  $sql.= " FROM ";
160  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
161  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
162  $sql.= " ".MAIN_DB_PREFIX."categorie_contact as cs";
163  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
164  //$sql.= " AND sp.no_email = 0";
165  //$sql.= " AND sp.email != ''";
166  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
167  $sql.= " AND cs.fk_categorie = c.rowid";
168  $sql.= " AND cs.fk_socpeople = sp.rowid";
169  $sql.= " GROUP BY c.label";
170  $sql.= " ORDER BY c.label";
171  $resql = $this->db->query($sql);
172 
173  $s.='<select name="filter_category" class="flat">';
174  $s.='<option value="all">&nbsp;</option>';
175  if ($resql)
176  {
177  $num = $this->db->num_rows($resql);
178  if ($num)
179  {
180  $i = 0;
181  while ($i < $num)
182  {
183  $obj = $this->db->fetch_object($resql);
184  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
185  $i++;
186  }
187  }
188  else
189  {
190  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
191  }
192  }
193  else dol_print_error($this->db);
194  $s.='</select>';
195 
196  $s.='<br>';
197 
198  // Add prospect of a particular level
199  $s.=$langs->trans("NatureOfThirdParty").': ';
200  $s.='<select name="filter" class="flat">';
201  $sql = "SELECT code, label";
202  $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
203  $sql.= " WHERE active > 0";
204  $sql.= " ORDER BY label";
205  $resql = $this->db->query($sql);
206  if ($resql)
207  {
208  $num = $this->db->num_rows($resql);
209  if ($num) $s.='<option value="all">&nbsp;</option>';
210  else $s.='<option value="all">'.$langs->trans("ContactsAllShort").'</option>';
211  $s.='<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
212 
213  $i = 0;
214  while ($i < $num)
215  {
216  $obj = $this->db->fetch_object($resql);
217  $level=$langs->trans($obj->code);
218  if ($level == $obj->code) $level=$langs->trans($obj->label);
219  $s.='<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
220  $i++;
221  }
222  }
223  else dol_print_error($this->db);
224  $s.='<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
225  //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
226  $s.='<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
227  $s.='</select>';
228 
229  $s.= ' ';
230 
231  // Filter on thirdparty category
232  $s .= $langs->trans("CustomersProspectsCategoriesShort").': ';
233  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
234  $sql.= " FROM ";
235  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
236  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
237  $sql.= " ".MAIN_DB_PREFIX."categorie_societe as cs";
238  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
239  //$sql.= " AND sp.no_email = 0";
240  //$sql.= " AND sp.email != ''";
241  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
242  $sql.= " AND cs.fk_categorie = c.rowid";
243  $sql.= " AND cs.fk_soc = sp.fk_soc";
244  $sql.= " GROUP BY c.label";
245  $sql.= " ORDER BY c.label";
246  $resql = $this->db->query($sql);
247 
248  $s.='<select name="filter_category_customer" class="flat">';
249  $s.='<option value="all">&nbsp;</option>';
250  if ($resql)
251  {
252  $num = $this->db->num_rows($resql);
253  if ($num)
254  {
255  $i = 0;
256  while ($i < $num)
257  {
258  $obj = $this->db->fetch_object($resql);
259  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
260  $i++;
261  }
262  }
263  else
264  {
265  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
266  }
267  }
268  else dol_print_error($this->db);
269  $s.='</select>';
270 
271  $s.= ' ';
272 
273  // Filter on thirdparty category
274  $s .= $langs->trans("SuppliersCategoriesShort").': ';
275  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
276  $sql.= " FROM ";
277  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
278  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
279  $sql.= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
280  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
281  //$sql.= " AND sp.no_email = 0";
282  //$sql.= " AND sp.email != ''";
283  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
284  $sql.= " AND cs.fk_categorie = c.rowid";
285  $sql.= " AND cs.fk_soc = sp.fk_soc";
286  $sql.= " GROUP BY c.label";
287  $sql.= " ORDER BY c.label";
288  $resql = $this->db->query($sql);
289 
290  $s.='<select name="filter_category_supplier" class="flat">';
291  $s.='<option value="all">&nbsp;</option>';
292  if ($resql)
293  {
294  $num = $this->db->num_rows($resql);
295  if ($num)
296  {
297  $i = 0;
298  while ($i < $num)
299  {
300  $obj = $this->db->fetch_object($resql);
301  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
302  $i++;
303  }
304  }
305  else
306  {
307  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
308  }
309  }
310  else dol_print_error($this->db);
311  $s.='</select>';
312 
313  return $s;
314  }
315 
316 
323  function url($id)
324  {
325  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('',"contact").'</a>';
326  }
327 
328 
329  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
337  function add_to_target($mailing_id,$filtersarray=array())
338  {
339  // phpcs:enable
340  global $conf, $langs;
341 
342  $filter = GETPOST('filter','alpha');
343  $filter_jobposition = GETPOST('filter_jobposition','alpha');
344  $filter_category = GETPOST('filter_category','alpha');
345  $filter_category_customer = GETPOST('filter_category_customer','alpha');
346  $filter_category_supplier = GETPOST('filter_category_supplier','alpha');
347 
348  $cibles = array();
349 
350  // List prospects levels
351  $prospectlevel=array();
352  $sql = "SELECT code, label";
353  $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
354  $sql.= " WHERE active > 0";
355  $sql.= " ORDER BY label";
356  $resql = $this->db->query($sql);
357  if ($resql)
358  {
359  $num = $this->db->num_rows($resql);
360  $i = 0;
361  while ($i < $num)
362  {
363  $obj = $this->db->fetch_object($resql);
364  $prospectlevel[$obj->code]=$obj->label;
365  $i++;
366  }
367  }
368  else dol_print_error($this->db);
369 
370  // Request must return: id, email, fk_contact, lastname, firstname, other
371  $sql = "SELECT sp.rowid as id, sp.email as email, sp.rowid as fk_contact, sp.lastname, sp.firstname, sp.civility as civility_id, sp.poste as jobposition,";
372  $sql.= " s.nom as companyname";
373  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
374  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
375  if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c";
376  if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
377  if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c2";
378  if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
379  if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c3";
380  if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
381  $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
382  $sql.= " AND sp.email <> ''";
383  $sql.= " AND sp.no_email = 0";
384  $sql.= " AND sp.statut = 1";
385  $sql.= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
386  // Filter on category
387  if ($filter_category <> 'all') $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
388  if ($filter_category <> 'all') $sql.= " AND c.label = '".$this->db->escape($filter_category)."'";
389  if ($filter_category_customer <> 'all') $sql.= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
390  if ($filter_category_customer <> 'all') $sql.= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
391  if ($filter_category_supplier <> 'all') $sql.= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
392  if ($filter_category_supplier <> 'all') $sql.= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
393  // Filter on nature
394  $key = $filter;
395  {
396  //print "xx".$key;
397  if ($key == 'prospects') $sql.= " AND s.client=2";
398  foreach($prospectlevel as $codelevel=>$valuelevel) if ($key == 'prospectslevel'.$codelevel) $sql.= " AND s.fk_prospectlevel='".$codelevel."'";
399  if ($key == 'customers') $sql.= " AND s.client=1";
400  if ($key == 'suppliers') $sql.= " AND s.fournisseur=1";
401  }
402  // Filter on job position
403  $key = $filter_jobposition;
404  if (! empty($key) && $key != 'all') $sql.= " AND sp.poste ='".$this->db->escape($key)."'";
405  $sql.= " ORDER BY sp.email";
406  //print "wwwwwwx".$sql;
407 
408  // Stocke destinataires dans cibles
409  $result=$this->db->query($sql);
410  if ($result)
411  {
412  $num = $this->db->num_rows($result);
413  $i = 0;
414  $j = 0;
415 
416  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
417 
418  $old = '';
419  while ($i < $num)
420  {
421  $obj = $this->db->fetch_object($result);
422  if ($old <> $obj->email)
423  {
424  $cibles[$j] = array(
425  'email' => $obj->email,
426  'fk_contact' => $obj->fk_contact,
427  'lastname' => $obj->lastname,
428  'firstname' => $obj->firstname,
429  'other' =>
430  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
431  ($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
432  ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
433  'source_url' => $this->url($obj->id),
434  'source_id' => $obj->id,
435  'source_type' => 'contact'
436  );
437  $old = $obj->email;
438  $j++;
439  }
440 
441  $i++;
442  }
443  }
444  else
445  {
446  dol_syslog($this->db->error());
447  $this->error=$this->db->error();
448  return -1;
449  }
450 
451  return parent::add_to_target($mailing_id, $cibles);
452  }
453 }
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
__construct($db)
Constructor.
add_to_target($mailing_id, $filtersarray=array())
Ajoute destinataires dans table des cibles.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
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 from contacts.
Parent class of emailing target selectors modules.