Fix sort by field in all events (action and emailing type) and fix multiple contacts in one event

This commit is contained in:
Lionel VESSILLER 2019-03-05 14:55:04 +01:00
parent b7066d92cc
commit 9b797eb980

View File

@ -1222,21 +1222,30 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
$numaction = 0 ; $numaction = 0 ;
$now=dol_now('tzuser'); $now=dol_now('tzuser');
// Open DSI -- Fix order by -- Begin
$sortfield_list = explode(',', $sortfield);
$sortfield_label_list = array('a.id' => 'id', 'a.datep' => 'dp', 'a.percent' => 'percent');
$sortfield_new_list = array();
foreach ($sortfield_list as $sortfield_value) {
$sortfield_new_list[] = $sortfield_label_list[trim($sortfield_value)];
}
$sortfield_new = implode(',', $sortfield_new_list);
if (! empty($conf->agenda->enabled)) if (! empty($conf->agenda->enabled))
{ {
// Recherche histo sur actioncomm // Recherche histo sur actioncomm
if (is_object($objcon) && $objcon->id) { if (is_object($objcon) && $objcon->id) {
$sql = "SELECT DISTINCT a.id, a.label,"; $sql = "SELECT DISTINCT a.id, a.label as note,";
} }
else else
{ {
$sql = "SELECT a.id, a.label,"; $sql = "SELECT a.id, a.label as note,";
} }
$sql.= " a.datep as dp,"; $sql.= " a.datep as dp,";
$sql.= " a.datep2 as dp2,"; $sql.= " a.datep2 as dp2,";
$sql.= " a.note, a.percent,"; $sql.= " a.percent as percent, 'action' as type,";
$sql.= " a.fk_element, a.elementtype,"; $sql.= " a.fk_element, a.elementtype,";
$sql.= " a.fk_user_author, a.fk_contact,"; $sql.= " a.fk_contact,";
$sql.= " c.code as acode, c.libelle as alabel, c.picto as apicto,"; $sql.= " c.code as acode, c.libelle as alabel, c.picto as apicto,";
$sql.= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; $sql.= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname";
if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql.= ", sp.lastname, sp.firstname"; if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql.= ", sp.lastname, sp.firstname";
@ -1248,8 +1257,10 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id";
$force_filter_contact = FALSE;
if (is_object($objcon) && $objcon->id) { if (is_object($objcon) && $objcon->id) {
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources as r ON a.id = r.fk_actioncomm"; $force_filter_contact = TRUE;
$sql.= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm_resources as r ON a.id = r.fk_actioncomm";
$sql.= " AND r.element_type = '" . $db->escape($objcon->table_element) . "' AND r.fk_element = " . $objcon->id; $sql.= " AND r.element_type = '" . $db->escape($objcon->table_element) . "' AND r.fk_element = " . $objcon->id;
} }
@ -1266,9 +1277,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql.= ", ".MAIN_DB_PREFIX."ticket as o"; elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql.= ", ".MAIN_DB_PREFIX."ticket as o";
$sql.= " WHERE a.entity IN (".getEntity('agenda').")"; $sql.= " WHERE a.entity IN (".getEntity('agenda').")";
if (is_object($objcon) && $objcon->id) { if ($force_filter_contact === FALSE) {
$sql.= " AND a.fk_contact = ".$objcon->id;
} else {
if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur')) && $filterobj->id) $sql.= " AND a.fk_soc = ".$filterobj->id; if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur')) && $filterobj->id) $sql.= " AND a.fk_soc = ".$filterobj->id;
elseif (is_object($filterobj) && get_class($filterobj) == 'Project' && $filterobj->id) $sql.= " AND a.fk_project = ".$filterobj->id; elseif (is_object($filterobj) && get_class($filterobj) == 'Project' && $filterobj->id) $sql.= " AND a.fk_project = ".$filterobj->id;
elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent')
@ -1316,9 +1325,37 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))"; if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
elseif ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))"; elseif ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
if (is_array($filters) && $filters['search_agenda_label']) $sql.= natural_search('a.label', $filters['search_agenda_label']); if (is_array($filters) && $filters['search_agenda_label']) $sql.= natural_search('a.label', $filters['search_agenda_label']);
}
// Add also event from emailings. TODO This should be replaced by an automatic event ? May be it's too much for very large emailing.
if (! empty($conf->mailing->enabled) && ! empty($objcon->email)
&& (empty($actioncode) || $actioncode == 'AC_OTH_AUTO' || $actioncode == 'AC_EMAILING'))
{
$langs->load("mails");
$sql2 = "SELECT m.rowid as id, m.titre as note, mc.date_envoi as dp, mc.date_envoi as dp2, '100' as percent, 'mailing' as type";
$sql2.= ", '' as fk_element, '' as elementtype, '' as contact_id";
$sql2.= ", 'AC_EMAILING' as acode, '' as alabel, '' as apicto";
$sql2.= ", u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; // User that valid action
if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql2.= ", '' as lastname, '' as firstname";
elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql2.= ", '' as lastname, '' as firstname";
elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql2.= ", '' as ref";
elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql2.= ", '' as ref";
elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql2.= ", '' as ref";
$sql2.= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
$sql2.= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
$sql2.= " AND mc.statut = 1";
$sql2.= " AND u.rowid = m.fk_user_valid";
$sql2.= " AND mc.fk_mailing=m.rowid";
}
if (!empty($sql) && !empty($sql2)) {
$sql = $sql . " UNION " . $sql2;
} else if (empty($sql) && !empty($sql2)) {
$sql = $sql2;
}
//TODO Add limit in nb of results //TODO Add limit in nb of results
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield_new, $sortorder);
dol_syslog("company.lib::show_actions_done", LOG_DEBUG); dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
@ -1330,6 +1367,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
{ {
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
if ($obj->type == 'action') {
$contactaction = new ActionComm($db); $contactaction = new ActionComm($db);
$contactaction->id=$obj->id; $contactaction->id=$obj->id;
$result = $contactaction->fetchResources(); $result = $contactaction->fetchResources();
@ -1344,12 +1382,12 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && $obj->datep > $now)) $tododone='todo'; if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && $obj->datep > $now)) $tododone='todo';
$histo[$numaction]=array( $histo[$numaction]=array(
'type'=>'action', 'type'=>$obj->type,
'tododone'=>$tododone, 'tododone'=>$tododone,
'id'=>$obj->id, 'id'=>$obj->id,
'datestart'=>$db->jdate($obj->dp), 'datestart'=>$db->jdate($obj->dp),
'dateend'=>$db->jdate($obj->dp2), 'dateend'=>$db->jdate($obj->dp2),
'note'=>$obj->label, 'note'=>$obj->note,
'percent'=>$obj->percent, 'percent'=>$obj->percent,
'userid'=>$obj->user_id, 'userid'=>$obj->user_id,
@ -1370,51 +1408,15 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
'libelle'=>$obj->alabel, // deprecated 'libelle'=>$obj->alabel, // deprecated
'apicto'=>$obj->apicto 'apicto'=>$obj->apicto
); );
} else {
$numaction++;
$i++;
}
}
else
{
dol_print_error($db);
}
}
// Add also event from emailings. TODO This should be replaced by an automatic event ? May be it's too much for very large emailing.
if (! empty($conf->mailing->enabled) && ! empty($objcon->email)
&& (empty($actioncode) || $actioncode == 'AC_OTH_AUTO' || $actioncode == 'AC_EMAILING'))
{
$langs->load("mails");
$sql = "SELECT m.rowid as id, mc.date_envoi as da, m.titre as note, '100' as percentage,";
$sql.= " 'AC_EMAILING' as acode,";
$sql.= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; // User that valid action
$sql.= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
$sql.= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
$sql.= " AND mc.statut = 1";
$sql.= " AND u.rowid = m.fk_user_valid";
$sql.= " AND mc.fk_mailing=m.rowid";
$sql.= " ORDER BY mc.date_envoi DESC, m.rowid DESC";
dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
$resql=$db->query($sql);
if ($resql)
{
$i = 0 ;
$num = $db->num_rows($resql);
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$histo[$numaction]=array( $histo[$numaction]=array(
'type'=>'mailing', 'type'=>$obj->type,
'tododone'=>'done', 'tododone'=>'done',
'id'=>$obj->id, 'id'=>$obj->id,
'datestart'=>$db->jdate($obj->da), 'datestart'=>$db->jdate($obj->dp),
'dateend'=>$db->jdate($obj->da), 'dateend'=>$db->jdate($obj->dp2),
'note'=>$obj->note, 'note'=>$obj->note,
'percent'=>$obj->percentage, 'percent'=>$obj->percent,
'acode'=>$obj->acode, 'acode'=>$obj->acode,
'userid'=>$obj->user_id, 'userid'=>$obj->user_id,
@ -1423,16 +1425,16 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin
'userlastname'=>$obj->user_lastname, 'userlastname'=>$obj->user_lastname,
'userphoto'=>$obj->user_photo 'userphoto'=>$obj->user_photo
); );
}
$numaction++; $numaction++;
$i++; $i++;
} }
$db->free($resql);
} }
else else
{ {
dol_print_error($db); dol_print_error($db);
} }
}
if (! empty($conf->agenda->enabled) || (! empty($conf->mailing->enabled) && ! empty($objcon->email))) if (! empty($conf->agenda->enabled) || (! empty($conf->mailing->enabled) && ! empty($objcon->email)))
{ {