27include_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
35 public $name =
'ContactCompanies';
37 public $desc =
'Contacts of thirdparties (prospects, customers, suppliers...)';
38 public $require_module = array(
"societe");
39 public $require_admin = 0;
41 public $enabled =
'isModEnabled("societe")';
46 public $picto =
'contact';
77 $langs->load(
"commercial");
80 $statssql[0] =
"SELECT '".$this->db->escape($langs->trans(
"NbOfCompaniesContacts")).
"' as label,";
81 $statssql[0] .=
" count(distinct(c.email)) as nb";
82 $statssql[0] .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as c";
83 $statssql[0] .=
" WHERE c.entity IN (".getEntity(
'contact').
")";
84 $statssql[0] .=
" AND c.email <> ''";
85 $statssql[0] .=
" AND c.statut = 1";
101 $sql =
"SELECT count(distinct(c.email)) as nb";
102 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as c";
103 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = c.fk_soc";
104 $sql .=
" WHERE c.entity IN (".getEntity(
'contact').
")";
105 $sql .=
" AND c.email <> ''";
106 if (empty($this->evenunsubscribe)) {
107 $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).
")";
110 $sql .=
" AND c.statut = 1";
113 return parent::getNbOfRecipients($sql);
127 $langs->loadLangs(array(
"commercial",
"companies",
"suppliers",
"categories"));
132 $sql =
"SELECT sp.poste, count(distinct(sp.email)) AS nb";
133 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as sp";
134 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
135 $sql .=
" AND sp.email <> ''";
136 $sql .=
" AND sp.statut = 1";
137 $sql .=
" AND (sp.poste IS NOT NULL AND sp.poste <> '')";
138 $sql .=
" GROUP BY sp.poste";
139 $sql .=
" ORDER BY sp.poste";
140 $resql = $this->db->query($sql);
142 $s .=
'<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="'.dol_escape_htmltag($langs->trans(
"PostOrFunction")).
'">';
143 $s .=
'<option value="-1">'.$langs->trans(
"PostOrFunction").
'</option>';
145 $num = $this->db->num_rows($resql);
149 $obj = $this->db->fetch_object($resql);
150 $s .=
'<option value="'.dol_escape_htmltag($obj->poste).
'">'.
dol_escape_htmltag($obj->poste).
' ('.$obj->nb.
')</option>';
154 $s .=
'<option disabled="disabled" value="">'.$langs->trans(
"None").
'</option>';
164 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
166 $sql .=
" ".MAIN_DB_PREFIX.
"socpeople as sp,";
167 $sql .=
" ".MAIN_DB_PREFIX.
"categorie as c,";
168 $sql .=
" ".MAIN_DB_PREFIX.
"categorie_contact as cs";
169 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
170 $sql .=
" AND sp.email <> ''";
171 $sql .=
" AND sp.statut = 1";
172 $sql .=
" AND cs.fk_categorie = c.rowid";
173 $sql .=
" AND cs.fk_socpeople = sp.rowid";
174 $sql .=
" GROUP BY c.label";
175 $sql .=
" ORDER BY c.label";
176 $resql = $this->db->query($sql);
178 $s .=
'<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">';
179 $s .=
'<option value="-1">'.$langs->trans(
"ContactCategoriesShort").
'</option>';
181 $num = $this->db->num_rows($resql);
185 $obj = $this->db->fetch_object($resql);
186 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
190 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactWithCategoryFound").
'</option>';
200 $s .=
'<select id="filter_contact" name="filter" class="flat maxwidth200">';
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);
207 $num = $this->db->num_rows($resql);
209 $s .=
'<option value="-1">'.$langs->trans(
"NatureOfThirdParty").
'</option>';
211 $s .=
'<option value="-1">'.$langs->trans(
"ContactsAllShort").
'</option>';
213 $s .=
'<option value="prospects">'.$langs->trans(
"ThirdPartyProspects").
'</option>';
217 $obj = $this->db->fetch_object($resql);
218 $level = $langs->trans($obj->code);
219 if ($level == $obj->code) {
220 $level = $langs->trans($obj->label);
222 $labeltoshow = $langs->trans(
"ThirdPartyProspects").
' <span class="opacitymedium">('.$langs->trans(
"ProspectLevelShort").
'='.$level.
')</span>';
223 $s .=
'<option value="prospectslevel'.$obj->code.
'" data-html="'.
dol_escape_htmltag($labeltoshow).
'">'.$labeltoshow.
'</option>';
229 $s .=
'<option value="customers">'.$langs->trans(
"ThirdPartyCustomers").
'</option>';
231 $s .=
'<option value="suppliers">'.$langs->trans(
"ThirdPartySuppliers").
'</option>';
238 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
240 $sql .=
" ".MAIN_DB_PREFIX.
"socpeople as sp,";
241 $sql .=
" ".MAIN_DB_PREFIX.
"categorie as c,";
242 $sql .=
" ".MAIN_DB_PREFIX.
"categorie_societe as cs";
243 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
244 $sql .=
" AND sp.email <> ''";
245 $sql .=
" AND sp.statut = 1";
246 $sql .=
" AND cs.fk_categorie = c.rowid";
247 $sql .=
" AND cs.fk_soc = sp.fk_soc";
248 $sql .=
" GROUP BY c.label";
249 $sql .=
" ORDER BY c.label";
250 $resql = $this->db->query($sql);
252 $s .=
'<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
253 $s .=
'<option value="-1">'.$langs->trans(
"CustomersProspectsCategoriesShort").
'</option>';
255 $num = $this->db->num_rows($resql);
259 $obj = $this->db->fetch_object($resql);
260 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
264 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactLinkedToThirdpartieWithCategoryFound").
'</option>';
275 $sql =
"SELECT c.label, count(distinct(sp.email)) AS nb";
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.entity IN (".getEntity(
'contact').
")";
281 $sql .=
" AND sp.email <> ''";
282 $sql .=
" AND sp.statut = 1";
283 $sql .=
" AND cs.fk_categorie = c.rowid";
284 $sql .=
" AND cs.fk_soc = sp.fk_soc";
285 $sql .=
" GROUP BY c.label";
286 $sql .=
" ORDER BY c.label";
287 $resql = $this->db->query($sql);
289 $s .=
'<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
290 $s .=
'<option value="-1">'.$langs->trans(
"SuppliersCategoriesShort").
'</option>';
292 $num = $this->db->num_rows($resql);
296 $obj = $this->db->fetch_object($resql);
297 $s .=
'<option value="'.$obj->label.
'">'.$obj->label.
' ('.$obj->nb.
')</option>';
301 $s .=
'<option value="-1" disabled="disabled">'.$langs->trans(
"NoContactLinkedToThirdpartieWithCategoryFound").
'</option>';
312 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formadmin.class.php';
314 $s .=
img_picto($langs->trans(
"DefaultLang"),
'language',
'class="pictofixedwidth"');
315 $s .= $formadmin->select_language(
GETPOST(
'filter_lang',
'aZ09'),
'filter_lang', 0,
null, $langs->trans(
"DefaultLang"), 0, 0,
'', 0, 0, 0,
null, 1);
330 return '<a href="'.DOL_URL_ROOT.
'/contact/card.php?id='.$id.
'">'.
img_object(
'',
"contact").
'</a>';
344 global $conf, $langs;
346 $filter =
GETPOST(
'filter',
'alpha');
347 $filter_jobposition =
GETPOST(
'filter_jobposition',
'alpha');
348 $filter_category =
GETPOST(
'filter_category',
'alpha');
349 $filter_category_customer =
GETPOST(
'filter_category_customer',
'alpha');
350 $filter_category_supplier =
GETPOST(
'filter_category_supplier',
'alpha');
351 $filter_lang =
GETPOST(
'filter_lang',
'alpha');
356 $prospectlevel = array();
357 $sql =
"SELECT code, label";
358 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_prospectlevel";
359 $sql .=
" WHERE active > 0";
360 $sql .=
" ORDER BY label";
361 $resql = $this->db->query($sql);
363 $num = $this->db->num_rows($resql);
366 $obj = $this->db->fetch_object($resql);
367 $prospectlevel[$obj->code] = $obj->label;
375 $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,";
376 $sql .=
" s.nom as companyname";
377 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as sp";
378 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = sp.fk_soc";
379 if ($filter_category !=
'all' && $filter_category !=
'-1') {
380 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c";
381 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as cs";
383 if ($filter_category_customer !=
'all' && $filter_category_customer !=
'-1') {
384 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c2";
385 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c2s";
387 if ($filter_category_supplier !=
'all' && $filter_category_supplier !=
'-1') {
388 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c3";
389 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as c3s";
391 $sql .=
" WHERE sp.entity IN (".getEntity(
'contact').
")";
392 $sql .=
" AND sp.email <> ''";
394 if (empty($this->evenunsubscribe)) {
395 $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).
")";
398 $sql .=
" AND sp.statut = 1";
399 $sql .=
" AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX.
"mailing_cibles WHERE fk_mailing=".((int) $mailing_id).
")";
402 if ($filter_category !=
'all' && $filter_category !=
'-1') {
403 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
404 $sql .=
" AND c.label = '".$this->db->escape($filter_category).
"'";
406 if ($filter_category_customer !=
'all' && $filter_category_customer !=
'-1') {
407 $sql .=
" AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
408 $sql .=
" AND c2.label = '".$this->db->escape($filter_category_customer).
"'";
410 if ($filter_category_supplier !=
'all' && $filter_category_supplier !=
'-1') {
411 $sql .=
" AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
412 $sql .=
" AND c3.label = '".$this->db->escape($filter_category_supplier).
"'";
416 if (!empty($filter_lang) && $filter_lang !=
'-1') {
417 $sql .=
" AND sp.default_lang LIKE '".$this->db->escape($filter_lang).
"%'";
424 if ($key ==
'prospects') {
425 $sql .=
" AND s.client = 2";
427 foreach ($prospectlevel as $codelevel => $valuelevel) {
428 if ($key ==
'prospectslevel'.$codelevel) {
429 $sql .=
" AND s.fk_prospectlevel = '".$this->db->escape($codelevel).
"'";
432 if ($key ==
'customers') {
433 $sql .=
" AND s.client = 1";
435 if ($key ==
'suppliers') {
436 $sql .=
" AND s.fournisseur = 1";
440 $key = $filter_jobposition;
441 if (!empty($key) && $key !=
'all' && $key !=
'-1') {
442 $sql .=
" AND sp.poste = '".$this->db->escape($key).
"'";
445 $sql .=
" ORDER BY sp.email";
449 $result = $this->db->query($sql);
451 $num = $this->db->num_rows($result);
455 dol_syslog(get_class($this).
"::add_to_target mailing ".$num.
" targets found");
459 $obj = $this->db->fetch_object($result);
460 if ($old <> $obj->email) {
462 'email' => $obj->email,
463 'fk_contact' => $obj->fk_contact,
464 'lastname' => $obj->lastname,
465 'firstname' => $obj->firstname,
467 ($langs->transnoentities(
"ThirdParty").
'='.$obj->companyname).
';'.
468 ($langs->transnoentities(
"UserTitle").
'='.($obj->civility_id ? $langs->transnoentities(
"Civility".$obj->civility_id) :
'')).
';'.
469 ($langs->transnoentities(
"PostOrFunction").
'='.$obj->jobposition),
470 'source_url' => $this->
url($obj->id),
471 'source_id' => $obj->id,
472 'source_type' =>
'contact'
482 $this->error = $this->db->error();
486 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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...