28include_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
36 public $name =
'ContactCompanies';
38 public $desc =
'Contacts of thirdparties (prospects, customers, suppliers...)';
39 public $require_module = array(
"societe");
40 public $require_admin = 0;
42 public $enabled =
'isModEnabled("societe")';
47 public $picto =
'contact';
73 $langs->load(
"commercial");
76 $statssql[0] =
"SELECT '".$this->db->escape($langs->trans(
"NbOfCompaniesContacts")).
"' as label,";
77 $statssql[0] .=
" count(distinct(c.email)) as nb";
78 $statssql[0] .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as c";
79 $statssql[0] .=
" WHERE c.entity IN (".getEntity(
'contact').
")";
80 $statssql[0] .=
" AND c.email <> ''";
81 $statssql[0] .=
" AND c.statut = 1";
99 $sql =
"SELECT count(distinct(c.email)) as nb";
100 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as c";
101 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = c.fk_soc";
102 $sql .=
" WHERE c.entity IN (".getEntity(
'contact').
")";
103 $sql .=
" AND c.email <> ''";
104 if (empty($this->evenunsubscribe)) {
105 $sql .=
" AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe as mu WHERE mu.email = c.email and mu.entity = ".((int) $conf->entity).
")";
108 $sql .=
" AND c.statut = 1";
111 return parent::getNbOfRecipients($sql);
125 $langs->loadLangs(array(
"commercial",
"companies",
"suppliers",
"categories"));
130 $sql =
"SELECT sp.poste, count(distinct(sp.email)) AS nb";
131 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as sp";
132 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
133 $sql .=
" AND sp.email <> ''";
134 $sql .=
" AND sp.statut = 1";
135 $sql .=
" AND (sp.poste IS NOT NULL AND sp.poste <> '')";
136 $sql .=
" GROUP BY sp.poste";
137 $sql .=
" ORDER BY sp.poste";
138 $resql = $this->db->query($sql);
140 $s .=
'<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="'.dol_escape_htmltag($langs->trans(
"PostOrFunction")).
'">';
141 $s .=
'<option value="-1">'.$langs->trans(
"PostOrFunction").
'</option>';
143 $num = $this->db->num_rows($resql);
147 $obj = $this->db->fetch_object($resql);
148 $s .=
'<option value="'.dol_escape_htmltag($obj->poste).
'">'.
dol_escape_htmltag($obj->poste).
' ('.$obj->nb.
')</option>';
152 $s .=
'<option disabled="disabled" value="">'.$langs->trans(
"None").
'</option>';
162 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
164 $sql .=
" ".MAIN_DB_PREFIX.
"socpeople as sp,";
165 $sql .=
" ".MAIN_DB_PREFIX.
"categorie as c,";
166 $sql .=
" ".MAIN_DB_PREFIX.
"categorie_contact as cs";
167 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
168 $sql .=
" AND sp.email <> ''";
169 $sql .=
" AND sp.statut = 1";
170 $sql .=
" AND cs.fk_categorie = c.rowid";
171 $sql .=
" AND cs.fk_socpeople = sp.rowid";
172 $sql .=
" GROUP BY c.label";
173 $sql .=
" ORDER BY c.label";
174 $resql = $this->db->query($sql);
176 $s .=
'<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">';
177 $s .=
'<option value="-1">'.$langs->trans(
"ContactCategoriesShort").
'</option>';
179 $num = $this->db->num_rows($resql);
183 $obj = $this->db->fetch_object($resql);
184 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
188 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactWithCategoryFound").
'</option>';
198 $s .=
'<select id="filter_contact" name="filter" class="flat maxwidth200">';
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);
205 $num = $this->db->num_rows($resql);
207 $s .=
'<option value="-1">'.$langs->trans(
"NatureOfThirdParty").
'</option>';
209 $s .=
'<option value="-1">'.$langs->trans(
"ContactsAllShort").
'</option>';
211 $s .=
'<option value="prospects">'.$langs->trans(
"ThirdPartyProspects").
'</option>';
215 $obj = $this->db->fetch_object($resql);
216 $level = $langs->trans($obj->code);
217 if ($level == $obj->code) {
218 $level = $langs->trans($obj->label);
220 $labeltoshow = $langs->trans(
"ThirdPartyProspects").
' <span class="opacitymedium">('.$langs->trans(
"ProspectLevelShort").
'='.$level.
')</span>';
221 $s .=
'<option value="prospectslevel'.$obj->code.
'" data-html="'.
dol_escape_htmltag($labeltoshow).
'">'.$labeltoshow.
'</option>';
227 $s .=
'<option value="customers">'.$langs->trans(
"ThirdPartyCustomers").
'</option>';
229 $s .=
'<option value="suppliers">'.$langs->trans(
"ThirdPartySuppliers").
'</option>';
236 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
238 $sql .=
" ".MAIN_DB_PREFIX.
"socpeople as sp,";
239 $sql .=
" ".MAIN_DB_PREFIX.
"categorie as c,";
240 $sql .=
" ".MAIN_DB_PREFIX.
"categorie_societe as cs";
241 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
242 $sql .=
" AND sp.email <> ''";
243 $sql .=
" AND sp.statut = 1";
244 $sql .=
" AND cs.fk_categorie = c.rowid";
245 $sql .=
" AND cs.fk_soc = sp.fk_soc";
246 $sql .=
" GROUP BY c.label";
247 $sql .=
" ORDER BY c.label";
248 $resql = $this->db->query($sql);
250 $s .=
'<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
251 $s .=
'<option value="-1">'.$langs->trans(
"CustomersProspectsCategoriesShort").
'</option>';
253 $num = $this->db->num_rows($resql);
257 $obj = $this->db->fetch_object($resql);
258 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
262 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactLinkedToThirdpartieWithCategoryFound").
'</option>';
273 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
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.entity IN (".getEntity(
'contact').
")";
279 $sql .=
" AND sp.email <> ''";
280 $sql .=
" AND sp.statut = 1";
281 $sql .=
" AND cs.fk_categorie = c.rowid";
282 $sql .=
" AND cs.fk_soc = sp.fk_soc";
283 $sql .=
" GROUP BY c.label";
284 $sql .=
" ORDER BY c.label";
285 $resql = $this->db->query($sql);
287 $s .=
'<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
288 $s .=
'<option value="-1">'.$langs->trans(
"SuppliersCategoriesShort").
'</option>';
290 $num = $this->db->num_rows($resql);
294 $obj = $this->db->fetch_object($resql);
295 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
299 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactLinkedToThirdpartieWithCategoryFound").
'</option>';
310 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formadmin.class.php';
312 $s .=
img_picto($langs->trans(
"DefaultLang"),
'language',
'class="pictofixedwidth"');
313 $s .= $formadmin->select_language(
GETPOST(
'filter_lang',
'aZ09'),
'filter_lang', 0,
null, $langs->trans(
"DefaultLang"), 0, 0,
'', 0, 0, 0,
null, 1);
328 return '<a href="'.DOL_URL_ROOT.
'/contact/card.php?id='.
$id.
'">'.
img_object(
'',
"contact").
'</a>';
342 global $conf, $langs;
344 $filter =
GETPOST(
'filter',
'alpha');
345 $filter_jobposition =
GETPOST(
'filter_jobposition',
'alpha');
346 $filter_category =
GETPOST(
'filter_category',
'alpha');
347 $filter_category_customer =
GETPOST(
'filter_category_customer',
'alpha');
348 $filter_category_supplier =
GETPOST(
'filter_category_supplier',
'alpha');
349 $filter_lang =
GETPOST(
'filter_lang',
'alpha');
354 $prospectlevel = array();
355 $sql =
"SELECT code, label";
356 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_prospectlevel";
357 $sql .=
" WHERE active > 0";
358 $sql .=
" ORDER BY label";
359 $resql = $this->db->query($sql);
361 $num = $this->db->num_rows($resql);
364 $obj = $this->db->fetch_object($resql);
365 $prospectlevel[$obj->code] = $obj->label;
373 $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,";
374 $sql .=
" s.nom as companyname";
375 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as sp";
376 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = sp.fk_soc";
377 if ($filter_category !=
'all' && $filter_category !=
'-1') {
378 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c";
379 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as cs";
381 if ($filter_category_customer !=
'all' && $filter_category_customer !=
'-1') {
382 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c2";
383 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c2s";
385 if ($filter_category_supplier !=
'all' && $filter_category_supplier !=
'-1') {
386 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c3";
387 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as c3s";
389 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
390 $sql .=
" AND sp.email <> ''";
392 if (empty($this->evenunsubscribe)) {
393 $sql .=
" AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe as mu WHERE mu.email = sp.email and mu.entity = ".((int) $conf->entity).
")";
396 $sql .=
" AND sp.statut = 1";
397 $sql .=
" AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX.
"mailing_cibles WHERE fk_mailing=".((int) $mailing_id).
")";
400 if ($filter_category !=
'all' && $filter_category !=
'-1') {
401 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
402 $sql .=
" AND c.label = '".$this->db->escape($filter_category).
"'";
404 if ($filter_category_customer !=
'all' && $filter_category_customer !=
'-1') {
405 $sql .=
" AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
406 $sql .=
" AND c2.label = '".$this->db->escape($filter_category_customer).
"'";
408 if ($filter_category_supplier !=
'all' && $filter_category_supplier !=
'-1') {
409 $sql .=
" AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
410 $sql .=
" AND c3.label = '".$this->db->escape($filter_category_supplier).
"'";
414 if (!empty($filter_lang) && $filter_lang !=
'-1') {
415 $sql .=
" AND sp.default_lang LIKE '".$this->db->escape($filter_lang).
"%'";
422 if ($key ==
'prospects') {
423 $sql .=
" AND s.client = 2";
425 foreach ($prospectlevel as $codelevel => $valuelevel) {
426 if ($key ==
'prospectslevel'.$codelevel) {
427 $sql .=
" AND s.fk_prospectlevel = '".$this->db->escape($codelevel).
"'";
430 if ($key ==
'customers') {
431 $sql .=
" AND s.client = 1";
433 if ($key ==
'suppliers') {
434 $sql .=
" AND s.fournisseur = 1";
438 $key = $filter_jobposition;
439 if (!empty($key) && $key !=
'all' && $key !=
'-1') {
440 $sql .=
" AND sp.poste = '".$this->db->escape($key).
"'";
443 $sql .=
" ORDER BY sp.email";
447 $result = $this->db->query($sql);
449 $num = $this->db->num_rows($result);
453 dol_syslog(get_class($this).
"::add_to_target mailing ".$num.
" targets found");
457 $obj = $this->db->fetch_object($result);
458 if ($old != $obj->email) {
460 'email' => $obj->email,
461 'fk_contact' => $obj->fk_contact,
462 'lastname' => $obj->lastname,
463 'firstname' => $obj->firstname,
465 ($langs->transnoentities(
"ThirdParty").
'='.$obj->companyname).
';'.
466 ($langs->transnoentities(
"UserTitle").
'='.($obj->civility_id ? $langs->transnoentities(
"Civility".$obj->civility_id) :
'')).
';'.
467 ($langs->transnoentities(
"PostOrFunction").
'='.$obj->jobposition),
468 'source_url' => $this->
url($obj->id),
469 'source_id' => $obj->id,
470 'source_type' =>
'contact'
480 $this->error = $this->db->error();
484 return parent::addTargetsToDatabase($mailing_id, $cibles);
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Parent class of emailing target selectors modules.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...