dolibarr  7.0.0-beta
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@capnetworks.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 
42  var $db;
43 
44 
50  function __construct($db)
51  {
52  $this->db=$db;
53  }
54 
55 
65  {
66  global $conf, $langs;
67 
68  $langs->load("commercial");
69 
70  $statssql=array();
71  $statssql[0] = "SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label,";
72  $statssql[0].= " count(distinct(c.email)) as nb";
73  $statssql[0].= " FROM ".MAIN_DB_PREFIX."socpeople as c";
74  $statssql[0].= " WHERE c.entity IN (".getEntity('societe').")";
75  $statssql[0].= " AND c.email != ''"; // Note that null != '' is false
76  $statssql[0].= " AND c.no_email = 0";
77  $statssql[0].= " AND c.statut = 1";
78 
79  return $statssql;
80  }
81 
82 
91  function getNbOfRecipients($sql='')
92  {
93  global $conf;
94 
95  $sql = "SELECT count(distinct(c.email)) as nb";
96  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
97  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
98  $sql.= " WHERE c.entity IN (".getEntity('societe').")";
99  $sql.= " AND c.email != ''"; // Note that null != '' is false
100  $sql.= " AND c.no_email = 0";
101  $sql.= " AND c.statut = 1";
102 
103  // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
104  return parent::getNbOfRecipients($sql);
105  }
106 
107 
113  function formFilter()
114  {
115  global $langs;
116  $langs->load("companies");
117  $langs->load("commercial");
118  $langs->load("suppliers");
119  $langs->load("categories");
120 
121  $s='';
122 
123  // Add filter on job position
124  $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
125  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
126  $sql.= " WHERE sp.entity IN (".getEntity('societe').")";
127  /*$sql.= " AND sp.email != ''"; // Note that null != '' is false
128  $sql.= " AND sp.no_email = 0";
129  $sql.= " AND sp.statut = 1";*/
130  $sql.= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
131  $sql.= " GROUP BY sp.poste";
132  $sql.= " ORDER BY sp.poste";
133  $resql = $this->db->query($sql);
134 
135  $s.=$langs->trans("PostOrFunction").': ';
136  $s.='<select name="filter_jobposition" class="flat">';
137  $s.='<option value="all">&nbsp;</option>';
138  if ($resql)
139  {
140  $num = $this->db->num_rows($resql);
141  $i = 0;
142  while ($i < $num)
143  {
144  $obj = $this->db->fetch_object($resql);
145  $s.='<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
146  $i++;
147  }
148  }
149  else dol_print_error($this->db);
150  $s.='</select>';
151 
152  $s.=' ';
153 
154  // Filter on contact category
155  $s .= $langs->trans("ContactCategoriesShort").': ';
156  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
157  $sql.= " FROM ";
158  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
159  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
160  $sql.= " ".MAIN_DB_PREFIX."categorie_contact as cs";
161  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
162  //$sql.= " AND sp.no_email = 0";
163  //$sql.= " AND sp.email != ''";
164  //$sql.= " AND sp.entity IN (".getEntity('societe').")";
165  $sql.= " AND cs.fk_categorie = c.rowid";
166  $sql.= " AND cs.fk_socpeople = sp.rowid";
167  $sql.= " GROUP BY c.label";
168  $sql.= " ORDER BY c.label";
169  $resql = $this->db->query($sql);
170 
171  $s.='<select name="filter_category" class="flat">';
172  $s.='<option value="all">&nbsp;</option>';
173  if ($resql)
174  {
175  $num = $this->db->num_rows($resql);
176  if ($num)
177  {
178  $i = 0;
179  while ($i < $num)
180  {
181  $obj = $this->db->fetch_object($resql);
182  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
183  $i++;
184  }
185  }
186  else
187  {
188  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
189  }
190  }
191  else dol_print_error($this->db);
192  $s.='</select>';
193 
194  $s.='<br>';
195 
196  // Add prospect of a particular level
197  $s.=$langs->trans("NatureOfThirdParty").': ';
198  $s.='<select name="filter" class="flat">';
199  $sql = "SELECT code, label";
200  $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
201  $sql.= " WHERE active > 0";
202  $sql.= " ORDER BY label";
203  $resql = $this->db->query($sql);
204  if ($resql)
205  {
206  $num = $this->db->num_rows($resql);
207  if ($num) $s.='<option value="all">&nbsp;</option>';
208  else $s.='<option value="all">'.$langs->trans("ContactsAllShort").'</option>';
209  $s.='<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
210 
211  $i = 0;
212  while ($i < $num)
213  {
214  $obj = $this->db->fetch_object($resql);
215  $level=$langs->trans($obj->code);
216  if ($level == $obj->code) $level=$langs->trans($obj->label);
217  $s.='<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
218  $i++;
219  }
220  }
221  else dol_print_error($this->db);
222  $s.='<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
223  //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
224  $s.='<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
225  $s.='</select>';
226 
227  $s.= ' ';
228 
229  // Filter on thirdparty category
230  $s .= $langs->trans("CustomersProspectsCategoriesShort").': ';
231  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
232  $sql.= " FROM ";
233  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
234  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
235  $sql.= " ".MAIN_DB_PREFIX."categorie_societe as cs";
236  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
237  //$sql.= " AND sp.no_email = 0";
238  //$sql.= " AND sp.email != ''";
239  //$sql.= " AND sp.entity IN (".getEntity('societe').")";
240  $sql.= " AND cs.fk_categorie = c.rowid";
241  $sql.= " AND cs.fk_soc = sp.fk_soc";
242  $sql.= " GROUP BY c.label";
243  $sql.= " ORDER BY c.label";
244  $resql = $this->db->query($sql);
245 
246  $s.='<select name="filter_category_customer" class="flat">';
247  $s.='<option value="all">&nbsp;</option>';
248  if ($resql)
249  {
250  $num = $this->db->num_rows($resql);
251  if ($num)
252  {
253  $i = 0;
254  while ($i < $num)
255  {
256  $obj = $this->db->fetch_object($resql);
257  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
258  $i++;
259  }
260  }
261  else
262  {
263  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
264  }
265  }
266  else dol_print_error($this->db);
267  $s.='</select>';
268 
269  $s.= ' ';
270 
271  // Filter on thirdparty category
272  $s .= $langs->trans("SuppliersCategoriesShort").': ';
273  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
274  $sql.= " FROM ";
275  $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
276  $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
277  $sql.= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
278  $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
279  //$sql.= " AND sp.no_email = 0";
280  //$sql.= " AND sp.email != ''";
281  //$sql.= " AND sp.entity IN (".getEntity('societe').")";
282  $sql.= " AND cs.fk_categorie = c.rowid";
283  $sql.= " AND cs.fk_soc = sp.fk_soc";
284  $sql.= " GROUP BY c.label";
285  $sql.= " ORDER BY c.label";
286  $resql = $this->db->query($sql);
287 
288  $s.='<select name="filter_category_supplier" class="flat">';
289  $s.='<option value="all">&nbsp;</option>';
290  if ($resql)
291  {
292  $num = $this->db->num_rows($resql);
293  if ($num)
294  {
295  $i = 0;
296  while ($i < $num)
297  {
298  $obj = $this->db->fetch_object($resql);
299  $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
300  $i++;
301  }
302  }
303  else
304  {
305  $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
306  }
307  }
308  else dol_print_error($this->db);
309  $s.='</select>';
310 
311  return $s;
312  }
313 
314 
321  function url($id)
322  {
323  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('',"contact").'</a>';
324  }
325 
326 
334  function add_to_target($mailing_id,$filtersarray=array())
335  {
336  global $conf, $langs;
337 
338  $filter = GETPOST('filter','alpha');
339  $filter_jobposition = GETPOST('filter_jobposition','alpha');
340  $filter_category = GETPOST('filter_category','alpha');
341  $filter_category_customer = GETPOST('filter_category_customer','alpha');
342  $filter_category_supplier = GETPOST('filter_category_supplier','alpha');
343 
344  $cibles = array();
345 
346  // List prospects levels
347  $prospectlevel=array();
348  $sql = "SELECT code, label";
349  $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
350  $sql.= " WHERE active > 0";
351  $sql.= " ORDER BY label";
352  $resql = $this->db->query($sql);
353  if ($resql)
354  {
355  $num = $this->db->num_rows($resql);
356  $i = 0;
357  while ($i < $num)
358  {
359  $obj = $this->db->fetch_object($resql);
360  $prospectlevel[$obj->code]=$obj->label;
361  $i++;
362  }
363  }
364  else dol_print_error($this->db);
365 
366  // Request must return: id, email, fk_contact, lastname, firstname, other
367  $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,";
368  $sql.= " s.nom as companyname";
369  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
370  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
371  if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c";
372  if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
373  if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c2";
374  if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
375  if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c3";
376  if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
377  $sql.= " WHERE sp.entity IN (".getEntity('societe').")";
378  $sql.= " AND sp.email <> ''";
379  $sql.= " AND sp.no_email = 0";
380  $sql.= " AND sp.statut = 1";
381  $sql.= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
382  // Filter on category
383  if ($filter_category <> 'all') $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
384  if ($filter_category <> 'all') $sql.= " AND c.label = '".$this->db->escape($filter_category)."'";
385  if ($filter_category_customer <> 'all') $sql.= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
386  if ($filter_category_customer <> 'all') $sql.= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
387  if ($filter_category_supplier <> 'all') $sql.= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
388  if ($filter_category_supplier <> 'all') $sql.= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
389  // Filter on nature
390  $key = $filter;
391  {
392  //print "xx".$key;
393  if ($key == 'prospects') $sql.= " AND s.client=2";
394  foreach($prospectlevel as $codelevel=>$valuelevel) if ($key == 'prospectslevel'.$codelevel) $sql.= " AND s.fk_prospectlevel='".$codelevel."'";
395  if ($key == 'customers') $sql.= " AND s.client=1";
396  if ($key == 'suppliers') $sql.= " AND s.fournisseur=1";
397  }
398  // Filter on job position
399  $key = $filter_jobposition;
400  if (! empty($key) && $key != 'all') $sql.= " AND sp.poste ='".$this->db->escape($key)."'";
401  $sql.= " ORDER BY sp.email";
402  //print "wwwwwwx".$sql;
403 
404  // Stocke destinataires dans cibles
405  $result=$this->db->query($sql);
406  if ($result)
407  {
408  $num = $this->db->num_rows($result);
409  $i = 0;
410  $j = 0;
411 
412  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
413 
414  $old = '';
415  while ($i < $num)
416  {
417  $obj = $this->db->fetch_object($result);
418  if ($old <> $obj->email)
419  {
420  $cibles[$j] = array(
421  'email' => $obj->email,
422  'fk_contact' => $obj->fk_contact,
423  'lastname' => $obj->lastname,
424  'firstname' => $obj->firstname,
425  'other' =>
426  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
427  ($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
428  ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
429  'source_url' => $this->url($obj->id),
430  'source_id' => $obj->id,
431  'source_type' => 'contact'
432  );
433  $old = $obj->email;
434  $j++;
435  }
436 
437  $i++;
438  }
439  }
440  else
441  {
442  dol_syslog($this->db->error());
443  $this->error=$this->db->error();
444  return -1;
445  }
446 
447  return parent::add_to_target($mailing_id, $cibles);
448  }
449 
450 }
451 
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
__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.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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)
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:1013
Class to offer a selector of emailing targets from contacts.
Parent class of emailing target selectors modules.