153 public function create($user, $notrigger = 0)
159 if (isset($this->fk_element)) {
160 $this->fk_element = (int) $this->fk_element;
162 if (isset($this->type_element)) {
163 $this->type_element = trim($this->type_element);
166 if (isset($this->
name)) {
169 if (isset($this->filtervalue)) {
170 $this->filtervalue = trim($this->filtervalue);
177 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing_advtarget(";
180 $sql .=
"fk_element,";
181 $sql .=
"type_element,";
182 $sql .=
"filtervalue,";
183 $sql .=
"fk_user_author,";
185 $sql .=
"fk_user_mod";
186 $sql .=
") VALUES (";
187 $sql .=
" ".(!isset($this->
name) ?
'NULL' :
"'".$this->db->escape($this->
name).
"'").
",";
188 $sql .=
" ".((int)
$conf->entity).
",";
189 $sql .=
" ".(!isset($this->fk_element) ?
'NULL' :
"'".$this->db->escape($this->fk_element).
"'").
",";
190 $sql .=
" ".(!isset($this->type_element) ?
'NULL' :
"'".$this->db->escape($this->type_element).
"'").
",";
191 $sql .=
" ".(!isset($this->filtervalue) ?
'NULL' :
"'".$this->db->escape($this->filtervalue).
"'").
",";
192 $sql .=
" ".((int) $user->id).
",";
193 $sql .=
" '".$this->db->idate(
dol_now()).
"',";
194 $sql .=
" ".((int) $user->id);
199 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
200 $resql = $this->db->query($sql);
203 $this->errors[] =
"Error ".$this->db->lasterror();
207 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing_advtarget");
212 foreach ($this->errors as $errmsg) {
213 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
214 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
216 $this->db->rollback();
292 $sql .=
" t.entity,";
293 $sql .=
" t.fk_element,";
294 $sql .=
" t.type_element,";
295 $sql .=
" t.filtervalue,";
296 $sql .=
" t.fk_user_author,";
298 $sql .=
" t.fk_user_mod,";
301 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_advtarget as t";
303 $sql .=
" WHERE t.fk_element = ".((int) $id).
" AND type_element = 'mailing'";
305 $sql .=
" WHERE t.fk_element = ".((int) $this->fk_element).
" AND type_element = 'mailing'";
308 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
309 $resql = $this->db->query($sql);
311 if ($this->db->num_rows($resql)) {
312 $obj = $this->db->fetch_object($resql);
314 $this->
id = $obj->rowid;
316 $this->
name = $obj->name;
317 $this->entity = $obj->entity;
318 $this->fk_element = $obj->fk_element;
319 $this->type_element = $obj->type_element;
320 $this->filtervalue = $obj->filtervalue;
321 $this->fk_user_author = $obj->fk_user_author;
322 $this->datec = $this->db->jdate($obj->datec);
323 $this->fk_user_mod = $obj->fk_user_mod;
324 $this->tms = $this->db->jdate($obj->tms);
326 $this->db->free($resql);
330 $this->error =
"Error ".$this->db->lasterror();
331 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
355 $sql .=
" t.entity,";
356 $sql .=
" t.fk_element,";
357 $sql .=
" t.type_element,";
358 $sql .=
" t.filtervalue,";
359 $sql .=
" t.fk_user_author,";
361 $sql .=
" t.fk_user_mod,";
364 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_advtarget as t";
366 $sql .=
" WHERE t.fk_element = ".((int) $id).
" AND type_element = '".$this->db->escape($type_element).
"'";
368 $sql .=
" WHERE t.fk_element = ".((int) $this->fk_element).
" AND type_element = '".$this->db->escape($type_element).
"'";
371 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
372 $resql = $this->db->query($sql);
374 if ($this->db->num_rows($resql)) {
375 $obj = $this->db->fetch_object($resql);
377 $this->
id = $obj->rowid;
379 $this->
name = $obj->name;
380 $this->entity = $obj->entity;
381 $this->fk_element = $obj->fk_element;
382 $this->type_element = $obj->type_element;
383 $this->filtervalue = $obj->filtervalue;
384 $this->fk_user_author = $obj->fk_user_author;
385 $this->datec = $this->db->jdate($obj->datec);
386 $this->fk_user_mod = $obj->fk_user_mod;
387 $this->tms = $this->db->jdate($obj->tms);
389 $this->db->free($resql);
393 $this->error =
"Error ".$this->db->lasterror();
394 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
406 public function update($user, $notrigger = 0)
408 global
$conf, $langs;
412 if (isset($this->fk_element)) {
413 $this->fk_element = (int) $this->fk_element;
415 if (isset($this->type_element)) {
416 $this->type_element = trim($this->type_element);
418 if (isset($this->
name)) {
421 if (isset($this->filtervalue)) {
422 $this->filtervalue = trim($this->filtervalue);
429 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_advtarget SET";
431 $sql .=
" name=".(isset($this->
name) ?
"'".$this->db->escape($this->
name).
"'" :
"''").
",";
432 $sql .=
" entity=".((int)
$conf->entity).
",";
433 $sql .=
" fk_element=".(isset($this->fk_element) ? $this->fk_element :
"null").
",";
434 $sql .=
" type_element=".(isset($this->type_element) ?
"'".$this->db->escape($this->type_element).
"'" :
"null").
",";
435 $sql .=
" filtervalue=".(isset($this->filtervalue) ?
"'".$this->db->escape($this->filtervalue).
"'" :
"null").
",";
436 $sql .=
" fk_user_mod=".((int) $user->id);
438 $sql .=
" WHERE rowid=".((int) $this->
id);
441 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
442 $resql = $this->db->query($sql);
445 $this->errors[] =
"Error ".$this->db->lasterror();
450 foreach ($this->errors as $errmsg) {
451 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
452 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
454 $this->db->rollback();
541 global $langs,
$conf, $extrafields;
545 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
546 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"societe_extrafields as te ON te.fk_object=t.rowid ";
550 $sqlwhere[] =
't.entity IN ('.getEntity(
'societe').
')';
552 if (count($arrayquery) > 0) {
553 if (array_key_exists(
'cust_saleman', $arrayquery)) {
554 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as saleman ON saleman.fk_soc = t.rowid";
556 if (array_key_exists(
'cust_categ', $arrayquery)) {
557 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_societe as custcateg ON custcateg.fk_soc = t.rowid";
560 if (!empty($arrayquery[
'cust_name'])) {
561 $sqlwhere[] = $this->
transformToSQL(
't.nom', $arrayquery[
'cust_name']);
563 if (!empty($arrayquery[
'cust_code'])) {
564 $sqlwhere[] = $this->
transformToSQL(
't.code_client', $arrayquery[
'cust_code']);
566 if (!empty($arrayquery[
'cust_adress'])) {
567 $sqlwhere[] = $this->
transformToSQL(
't.address', $arrayquery[
'cust_adress']);
569 if (!empty($arrayquery[
'cust_zip'])) {
570 $sqlwhere[] = $this->
transformToSQL(
't.zip', $arrayquery[
'cust_zip']);
572 if (!empty($arrayquery[
'cust_city'])) {
573 $sqlwhere[] = $this->
transformToSQL(
't.town', $arrayquery[
'cust_city']);
575 if (!empty($arrayquery[
'cust_mothercompany'])) {
576 $str = $this->
transformToSQL(
'nom', $arrayquery[
'cust_mothercompany']);
577 $sqlwhere[] =
" (t.parent IN (SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe WHERE (".$str.
")))";
579 if (!empty($arrayquery[
'cust_status']) && count($arrayquery[
'cust_status']) > 0) {
580 $sqlwhere[] =
" (t.status IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_status'])).
"))";
582 if (!empty($arrayquery[
'cust_typecust']) && count($arrayquery[
'cust_typecust']) > 0) {
583 $sqlwhere[] =
" (t.client IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typecust'])).
"))";
585 if (!empty($arrayquery[
'cust_comm_status']) && count($arrayquery[
'cust_comm_status']) > 0) {
586 $sqlwhere[] =
" (t.fk_stcomm IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_comm_status'])).
"))";
588 if (!empty($arrayquery[
'cust_prospect_status']) && count($arrayquery[
'cust_prospect_status']) > 0) {
589 $sqlwhere[] =
" (t.fk_prospectlevel IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
'cust_prospect_status']).
"'", 1).
"))";
591 if (!empty($arrayquery[
'cust_typeent']) && count($arrayquery[
'cust_typeent']) > 0) {
592 $sqlwhere[] =
" (t.fk_typent IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typeent'])).
"))";
594 if (!empty($arrayquery[
'cust_saleman']) && count($arrayquery[
'cust_saleman']) > 0) {
595 $sqlwhere[] =
" (saleman.fk_user IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_saleman'])).
"))";
597 if (!empty($arrayquery[
'cust_state']) && count($arrayquery[
'cust_state']) > 0) {
598 $sqlwhere[] =
" (t.fk_departement IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_state'])).
"))";
600 if (!empty($arrayquery[
'cust_country']) && count($arrayquery[
'cust_country']) > 0) {
601 $sqlwhere[] =
" (t.fk_pays IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_country'])).
"))";
603 if (!empty($arrayquery[
'cust_effectif_id']) && count($arrayquery[
'cust_effectif_id']) > 0) {
604 $sqlwhere[] =
" (t.fk_effectif IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_effectif_id'])).
"))";
606 if (!empty($arrayquery[
'cust_categ']) && count($arrayquery[
'cust_categ']) > 0) {
607 $sqlwhere[] =
" (custcateg.fk_categorie IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_categ'])).
"))";
609 if (!empty($arrayquery[
'cust_language']) && count($arrayquery[
'cust_language']) > 0) {
610 $sqlwhere[] =
" (t.default_lang IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
'cust_language']).
"'", 1).
"))";
615 $socstatic =
new Societe($this->db);
616 $elementtype = $socstatic->table_element;
618 $extrafields->fetch_name_optionals_label($elementtype);
620 foreach ($extrafields->attributes[$elementtype][
'label'] as $key => $val) {
621 if (($extrafields->attributes[$elementtype][
'type'][$key] ==
'varchar') ||
622 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'text')) {
623 if (!empty($arrayquery[
'options_'.$key])) {
624 $sqlwhere[] =
" (te.".$key.
" LIKE '".$this->db->escape($arrayquery[
"options_".$key]).
"')";
626 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'int') ||
627 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'double')) {
628 if (!empty($arrayquery[
'options_'.$key.
'_max'])) {
629 $sqlwhere[] =
" (te.".$key.
" >= ".((float) $arrayquery[
"options_".$key.
"_max"]).
" AND te.".$key.
" <= ".((float) $arrayquery[
"options_".$key.
'_min']).
")";
631 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'date') ||
632 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'datetime')) {
633 if (!empty($arrayquery[
'options_'.$key.
'_end_dt'])) {
634 $sqlwhere[] =
" (te.".$key.
" >= '".$this->db->idate($arrayquery[
"options_".$key.
"_st_dt"]).
"' AND te.".$key.
" <= '".$this->db->idate($arrayquery[
"options_".$key.
"_end_dt"]).
"')";
636 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'boolean') {
637 if ($arrayquery[
'options_'.$key] !=
'') {
638 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
640 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'link') {
641 if ($arrayquery[
'options_'.$key] > 0) {
642 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
644 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'chkbxlst'
645 && is_array($arrayquery[
'options_'.$key])) {
646 if (count($arrayquery[
'options_'.$key])) {
649 $sqlwhereselllist=
"";
650 foreach ($arrayquery[
'options_'.$key] as $data) {
653 $sqlwhereselllist .= ($i2 > 0 ?
" OR (" :
"(").$field.
" LIKE '".$this->db->escape($data).
",%'";
654 $sqlwhereselllist .=
" OR ".$field.
" = '".$this->db->escape($data).
"'";
655 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
"'";
656 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
",%'";
657 $sqlwhereselllist .=
")";
661 if (!empty($sqlwhereselllist)) {
662 $sqlwhere[] =
"( ".$sqlwhereselllist.
" )";
666 if (is_array($arrayquery[
'options_'.$key])) {
667 $sqlwhere[] =
" (te.".$key.
" IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
"options_".$key]).
"'", 1).
"))";
668 } elseif (!empty($arrayquery[
'options_'.$key])) {
669 $sqlwhere[] =
" (te.".$key.
" LIKE '".$this->db->escape($arrayquery[
"options_".$key]).
"')";
675 if (count($sqlwhere) > 0) {
676 $sql .=
" WHERE ".implode(
" AND ", $sqlwhere);
681 dol_syslog(get_class($this).
"::query_thirdparty", LOG_DEBUG);
682 $resql = $this->db->query($sql);
684 $this->thirdparty_lines = array();
685 $num = $this->db->num_rows($resql);
690 $obj = $this->db->fetch_object($resql);
691 $this->thirdparty_lines[$i] = $obj->rowid;
696 $this->db->free($resql);
700 $this->error =
"Error ".$this->db->lasterror();
701 dol_syslog(get_class($this).
"::query_thirdparty ".$this->error, LOG_ERR);
717 global $langs,
$conf;
721 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
722 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object=t.rowid ";
724 if (!empty($withThirdpartyFilter)) {
725 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"societe as ts ON ts.rowid=t.fk_soc";
726 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"societe_extrafields as tse ON tse.fk_object=ts.rowid ";
731 $sqlwhere[] =
't.entity IN ('.getEntity(
'contact').
')';
733 if (count($arrayquery) > 0) {
734 if (array_key_exists(
'contact_categ', $arrayquery)) {
735 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid ";
738 if (!empty($arrayquery[
'contact_lastname'])) {
739 $sqlwhere[] = $this->
transformToSQL(
't.lastname', $arrayquery[
'contact_lastname']);
741 if (!empty($arrayquery[
'contact_firstname'])) {
742 $sqlwhere[] = $this->
transformToSQL(
't.firstname', $arrayquery[
'contact_firstname']);
744 if (!empty($arrayquery[
'contact_country']) && count($arrayquery[
'contact_country'])) {
745 $sqlwhere[] =
" (t.fk_pays IN (".$this->db->sanitize($this->db->escape(implode(
',', $arrayquery[
'contact_country']))).
"))";
747 if (!empty($arrayquery[
'contact_status']) && count($arrayquery[
'contact_status']) > 0) {
748 $sqlwhere[] =
" (t.statut IN (".$this->db->sanitize($this->db->escape(implode(
',', $arrayquery[
'contact_status']))).
"))";
750 if (!empty($arrayquery[
'contact_civility']) && count($arrayquery[
'contact_civility']) > 0) {
751 $sqlwhere[] =
" (t.civility IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
'contact_civility']).
"'", 1).
"))";
753 if ($arrayquery[
'contact_no_email'] !=
'') {
755 if (!empty($arrayquery[
'contact_no_email'])) {
756 $tmpwhere .=
"(t.email IN (SELECT email FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe WHERE t.entity IN (".
getEntity(
'mailing').
") AND email = '".$this->db->escape($arrayquery[
'contact_no_email']).
"'))";
758 $tmpwhere .=
"(t.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe WHERE t.entity IN (".
getEntity(
'mailing').
") AND email = '".$this->db->escape($arrayquery[
'contact_no_email']).
"'))";
760 $sqlwhere[] = $tmpwhere;
762 if ($arrayquery[
'contact_update_st_dt'] !=
'') {
763 $sqlwhere[] =
" (t.tms >= '".$this->db->idate($arrayquery[
'contact_update_st_dt']).
"' AND t.tms <= '".$this->db->idate($arrayquery[
'contact_update_end_dt']).
"')";
765 if ($arrayquery[
'contact_create_st_dt'] !=
'') {
766 $sqlwhere[] =
" (t.datec >= '".$this->db->idate($arrayquery[
'contact_create_st_dt']).
"' AND t.datec <= '".$this->db->idate($arrayquery[
'contact_create_end_dt']).
"')";
768 if (!empty($arrayquery[
'contact_categ']) && count($arrayquery[
'contact_categ']) > 0) {
769 $sqlwhere[] =
" (contactcateg.fk_categorie IN (".$this->db->escape(implode(
",", $arrayquery[
'contact_categ'])).
"))";
774 $contactstatic =
new Contact($this->db);
775 $elementtype = $contactstatic->table_element;
780 $extrafields->fetch_name_optionals_label($elementtype);
782 $extrafields->fetch_name_optionals_label($elementtype);
784 foreach ($extrafields->attributes[$elementtype][
'label'] as $key => $val) {
785 if (($extrafields->attributes[$elementtype][
'type'][$key] ==
'varchar') ||
786 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'text')) {
787 if (!empty($arrayquery[
'options_'.$key.
'_cnct'])) {
788 $sqlwhere[] =
" (te.".$key.
" LIKE '".$this->db->escape($arrayquery[
"options_".$key.
"_cnct"]).
"')";
790 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'int') ||
791 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'double')) {
792 if (!empty($arrayquery[
'options_'.$key.
'_max_cnct'])) {
793 $sqlwhere[] =
" (te.".$key.
" >= ".((float) $arrayquery[
"options_".$key.
"_max_cnct"]).
" AND te.".$key.
" <= ".((float) $arrayquery[
"options_".$key.
"_min_cnct"]).
")";
795 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'date') ||
796 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'datetime')) {
797 if (!empty($arrayquery[
'options_'.$key.
'_end_dt_cnct'])) {
798 $sqlwhere[] =
" (te.".$key.
" >= '".$this->db->idate($arrayquery[
"options_".$key.
"_st_dt_cnct"]).
"' AND te.".$key.
" <= '".$this->db->idate($arrayquery[
"options_".$key.
"_end_dt_cnct"]).
"')";
800 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'boolean') {
801 if ($arrayquery[
'options_'.$key.
'_cnct'] !=
'') {
802 if ($arrayquery[
'options_'.$key.
'_cnct'] == 0) {
803 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key.
"_cnct"]).
" OR ((te.".$key.
" IS NULL) AND (te.fk_object IS NOT NULL)))";
805 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key.
"_cnct"]).
")";
808 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'link') {
809 if ($arrayquery[
'options_'.$key.
"_cnct"] > 0) {
810 $sqlwhere[]=
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key.
"_cnct"]).
")";
812 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'chkbxlst'
813 && is_array($arrayquery[
'options_'.$key.
'_cnct'])) {
814 if (count($arrayquery[
'options_'.$key.
'_cnct'])) {
817 $sqlwhereselllist=
"";
818 foreach ($arrayquery[
'options_'.$key.
'_cnct'] as $data) {
821 $sqlwhereselllist .= ($i2 > 0 ?
" OR (" :
"(").$field.
" LIKE '".$this->db->escape($data).
",%'";
822 $sqlwhereselllist .=
" OR ".$field.
" = '".$this->db->escape($data).
"'";
823 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
"'";
824 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
",%'";
825 $sqlwhereselllist .=
")";
829 if (!empty($sqlwhereselllist)) {
830 $sqlwhere[] =
"( ".$sqlwhereselllist.
" )";
834 if (is_array($arrayquery[
'options_'.$key.
'_cnct'])) {
835 $sqlwhere[] =
" (te.".$key.
" IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
"options_".$key.
"_cnct"]).
"'", 1).
"))";
836 } elseif (!empty($arrayquery[
'options_'.$key.
'_cnct'])) {
837 $sqlwhere[] =
" (te.".$key.
" LIKE '".$this->db->escape($arrayquery[
"options_".$key.
"_cnct"]).
"')";
842 if (!empty($withThirdpartyFilter)) {
843 if (array_key_exists(
'cust_saleman', $arrayquery)) {
844 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as saleman ON saleman.fk_soc = ts.rowid";
846 if (array_key_exists(
'cust_categ', $arrayquery)) {
847 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_societe as custcateg ON custcateg.fk_soc = ts.rowid";
850 if (!empty($arrayquery[
'cust_name'])) {
851 $sqlwhere[] = $this->
transformToSQL(
'ts.nom', $arrayquery[
'cust_name']);
853 if (!empty($arrayquery[
'cust_code'])) {
854 $sqlwhere[] = $this->
transformToSQL(
'ts.code_client', $arrayquery[
'cust_code']);
856 if (!empty($arrayquery[
'cust_adress'])) {
857 $sqlwhere[] = $this->
transformToSQL(
'ts.address', $arrayquery[
'cust_adress']);
859 if (!empty($arrayquery[
'cust_zip'])) {
860 $sqlwhere[] = $this->
transformToSQL(
'ts.zip', $arrayquery[
'cust_zip']);
862 if (!empty($arrayquery[
'cust_city'])) {
863 $sqlwhere[] = $this->
transformToSQL(
'ts.town', $arrayquery[
'cust_city']);
865 if (!empty($arrayquery[
'cust_mothercompany'])) {
866 $str = $this->
transformToSQL(
'nom', $arrayquery[
'cust_mothercompany']);
867 $sqlwhere[] =
" (ts.parent IN (SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe WHERE (".$str.
")))";
869 if (!empty($arrayquery[
'cust_status']) && count($arrayquery[
'cust_status']) > 0) {
870 $sqlwhere[] =
" (ts.status IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_status'])).
"))";
872 if (!empty($arrayquery[
'cust_typecust']) && count($arrayquery[
'cust_typecust']) > 0) {
873 $sqlwhere[] =
" (ts.client IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typecust'])).
"))";
875 if (!empty($arrayquery[
'cust_comm_status']) && count($arrayquery[
'cust_comm_status']) > 0) {
876 $sqlwhere[] =
" (ts.fk_stcomm IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_comm_status'])).
"))";
878 if (!empty($arrayquery[
'cust_prospect_status']) && count($arrayquery[
'cust_prospect_status']) > 0) {
879 $sqlwhere[] =
" (ts.fk_prospectlevel IN ('".$this->db->sanitize(implode(
"','", $arrayquery[
'cust_prospect_status'])).
"'))";
881 if (!empty($arrayquery[
'cust_typeent']) && count($arrayquery[
'cust_typeent']) > 0) {
882 $sqlwhere[] =
" (ts.fk_typent IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typeent'])).
"))";
884 if (!empty($arrayquery[
'cust_saleman']) && count($arrayquery[
'cust_saleman']) > 0) {
885 $sqlwhere[] =
" (saleman.fk_user IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_saleman'])).
"))";
890 if (!empty($arrayquery[
'cust_state']) && count($arrayquery[
'cust_state']) > 0) {
891 $sqlwhere[] =
" (t.fk_departement IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_state'])).
"))";
893 if (!empty($arrayquery[
'cust_country']) && count($arrayquery[
'cust_country']) > 0) {
894 $sqlwhere[] =
" (ts.fk_pays IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_country'])).
"))";
896 if (!empty($arrayquery[
'cust_effectif_id']) && count($arrayquery[
'cust_effectif_id']) > 0) {
897 $sqlwhere[] =
" (ts.fk_effectif IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_effectif_id'])).
"))";
899 if (!empty($arrayquery[
'cust_categ']) && count($arrayquery[
'cust_categ']) > 0) {
900 $sqlwhere[] =
" (custcateg.fk_categorie IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_categ'])).
"))";
902 if (!empty($arrayquery[
'cust_language']) && count($arrayquery[
'cust_language']) > 0) {
903 $sqlwhere[] =
" (ts.default_lang IN ('".$this->db->sanitize(implode(
"','", $arrayquery[
'cust_language'])).
"'))";
908 $socstatic =
new Societe($this->db);
909 $elementtype = $socstatic->table_element;
915 $extrafields->fetch_name_optionals_label($elementtype);
917 foreach ($extrafields->attributes[$elementtype][
'label'] as $key => $val) {
918 if (($extrafields->attributes[$elementtype][
'type'][$key] ==
'varchar') ||
919 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'text')) {
920 if (!empty($arrayquery[
'options_'.$key])) {
921 $sqlwhere[] =
" (tse.".$key.
" LIKE '".$this->db->escape($arrayquery[
'options_'.$key]).
"')";
923 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'int') ||
924 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'double')) {
925 if (!empty($arrayquery[
'options_'.$key.
'_max'])) {
926 $sqlwhere[] =
" (tse.".$key.
" >= ".((float) $arrayquery[
"options_".$key.
"_max"]).
" AND tse.".$key.
" <= ".((float) $arrayquery[
"options_".$key.
"_min"]).
")";
928 } elseif (($extrafields->attributes[$elementtype][
'type'][$key] ==
'date') ||
929 ($extrafields->attributes[$elementtype][
'type'][$key] ==
'datetime')) {
930 if (!empty($arrayquery[
'options_'.$key.
'_end_dt'])) {
931 $sqlwhere[] =
" (tse.".$key.
" >= '".$this->db->idate($arrayquery[
"options_".$key.
"_st_dt"]).
"' AND tse.".$key.
" <= '".$this->db->idate($arrayquery[
"options_".$key.
"_end_dt"]).
"')";
933 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'boolean') {
934 if ($arrayquery[
'options_'.$key] !=
'') {
935 $sqlwhere[] =
" (tse.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
937 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'link') {
938 if ($arrayquery[
'options_'.$key] > 0) {
939 $sqlwhere[]=
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
941 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'chkbxlst'
942 && is_array($arrayquery[
'options_'.$key])) {
943 if (count($arrayquery[
'options_'.$key])) {
945 $field =
"tse.".$key;
946 $sqlwhereselllist=
"";
947 foreach ($arrayquery[
'options_'.$key] as $data) {
950 $sqlwhereselllist .= ($i2 > 0 ?
" OR (" :
"(").$field.
" LIKE '".$this->db->escape($data).
",%'";
951 $sqlwhereselllist .=
" OR ".$field.
" = '".$this->db->escape($data).
"'";
952 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
"'";
953 $sqlwhereselllist .=
" OR ".$field.
" LIKE '%,".$this->db->escape($data).
",%'";
954 $sqlwhereselllist .=
")";
958 if (!empty($sqlwhereselllist)) {
959 $sqlwhere[] =
"( ".$sqlwhereselllist.
" )";
963 if (is_array($arrayquery[
'options_'.$key])) {
964 $sqlwhere[] =
" (tse.".$key.
" IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
"options_".$key]).
"'", 1).
"))";
965 } elseif (!empty($arrayquery[
'options_'.$key])) {
966 $sqlwhere[] =
" (tse.".$key.
" LIKE '".$this->db->escape($arrayquery[
"options_".$key]).
"')";
973 if (count($sqlwhere) > 0) {
974 $sql .=
" WHERE ".implode(
" AND ", $sqlwhere);
978 dol_syslog(get_class($this).
"::query_contact", LOG_DEBUG);
979 $resql = $this->db->query($sql);
981 $this->contact_lines = array();
982 $num = $this->db->num_rows($resql);
987 $obj = $this->db->fetch_object($resql);
989 $this->contact_lines[$i] = $obj->rowid;
994 $this->db->free($resql);
998 $this->error =
"Error ".$this->db->lasterror();
999 dol_syslog(get_class($this).
"::query_contact ".$this->error, LOG_ERR);