Enhance email collector module

This commit is contained in:
Laurent Destailleur 2020-08-20 23:03:57 +02:00
parent 3ebcd191bd
commit eb15a8e4db
6 changed files with 131 additions and 84 deletions

View File

@ -327,18 +327,18 @@ class modEmailCollector extends DolibarrModules
}
}
$tmpsql = "SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses' and entity = ".$conf->entity;
$tmpsql = "SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses_In' and entity = ".$conf->entity;
$tmpresql = $this->db->query($tmpsql);
if ($tmpresql) {
if ($this->db->num_rows($tmpresql) == 0) {
$descriptionB1 = 'This collector will scan your mailbox to find all emails that are an answer of an email sent from your application. An event with the email response will be recorded at the good place (Module Agenda must be enabled). For example, if your send a commercial proposal, order or invoice by email and your customer answers your email, the system will automatically find the answer and add it into your ERP.';
$sqlforexampleB1 = "INSERT INTO ".MAIN_DB_PREFIX."emailcollector_emailcollector (entity, ref, label, description, source_directory, date_creation, fk_user_creat, status)";
$sqlforexampleB1 .= " VALUES (".$conf->entity.", 'Collect_Responses', 'Example to collect any email responses', '".$this->db->escape($descriptionB1)."', 'INBOX', '".$this->db->idate(dol_now())."', ".$user->id.", 0)";
$sqlforexampleB1 .= " VALUES (".$conf->entity.", 'Collect_Responses_In', 'Example to collect any email responses', '".$this->db->escape($descriptionB1)."', 'INBOX', '".$this->db->idate(dol_now())."', ".$user->id.", 0)";
$sqlforexampleB2 = "INSERT INTO ".MAIN_DB_PREFIX."emailcollector_emailcollectorfilter (fk_emailcollector, type, date_creation, fk_user_creat, status)";
$sqlforexampleB2 .= " VALUES ((SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses' and entity = ".$conf->entity."), 'withtrackingid', '".$this->db->idate(dol_now())."', ".$user->id.", 1)";
$sqlforexampleB2 .= " VALUES ((SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses_In' and entity = ".$conf->entity."), 'isanswer', '".$this->db->idate(dol_now())."', ".$user->id.", 1)";
$sqlforexampleB3 = "INSERT INTO ".MAIN_DB_PREFIX."emailcollector_emailcollectoraction (fk_emailcollector, type, date_creation, fk_user_creat, status)";
$sqlforexampleB3 .= " VALUES ((SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses' and entity = ".$conf->entity."), 'recordevent', '".$this->db->idate(dol_now())."', ".$user->id.", 1)";
$sqlforexampleB3 .= " VALUES ((SELECT rowid FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector WHERE ref = 'Collect_Responses_In' and entity = ".$conf->entity."), 'recordevent', '".$this->db->idate(dol_now())."', ".$user->id.", 1)";
$sql[] = $sqlforexampleB1;
$sql[] = $sqlforexampleB2;

View File

@ -1261,90 +1261,128 @@ class EmailCollector extends CommonObject
$objectemail = null;
$reg = array();
if (!empty($headers['References']) && preg_match('/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host, '/').'/', $headers['References'], $reg))
if (!empty($headers['References']))
{
$trackid = $reg[1].$reg[2];
$arrayofreferences = preg_explode('/\s+/', $headers['References']);
if ($reg[1] == 'inv')
{
$objectid = $reg[2];
$objectemail = new Facture($this->db);
}
if ($reg[1] == 'proj')
{
$objectid = $reg[2];
$objectemail = new Project($this->db);
}
if ($reg[1] == 'con')
{
$objectid = $reg[2];
$objectemail = new Contact($this->db);
}
if ($reg[1] == 'thi')
{
$objectid = $reg[2];
$objectemail = new Societe($this->db);
}
if ($reg[1] == 'use')
{
$objectid = $reg[2];
$objectemail = new User($this->db);
}
if ($reg[1] == 'tic')
{
$objectid = $reg[2];
$objectemail = new Ticket($this->db);
}
foreach($arrayofreferences as $reference) {
print "Process reference ".$reference."<br>\n";
if (preg_match('/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host, '/').'/', $reference, $reg)) {
// This is a Dolibarr reference
$trackid = $reg[1].$reg[2];
if (is_object($objectemail))
{
$result = $objectemail->fetch($objectid);
if ($result > 0)
{
$fk_element_id = $objectemail->id;
$fk_element_type = $objectemail->element;
// Fix fk_element_type
if ($fk_element_type == 'facture') $fk_element_type = 'invoice';
if ($reg[1] == 'inv')
{
$objectid = $reg[2];
$objectemail = new Facture($this->db);
}
if ($reg[1] == 'proj')
{
$objectid = $reg[2];
$objectemail = new Project($this->db);
}
if ($reg[1] == 'con')
{
$objectid = $reg[2];
$objectemail = new Contact($this->db);
}
if ($reg[1] == 'thi')
{
$objectid = $reg[2];
$objectemail = new Societe($this->db);
}
if ($reg[1] == 'use')
{
$objectid = $reg[2];
$objectemail = new User($this->db);
}
if ($reg[1] == 'tic')
{
$objectid = $reg[2];
$objectemail = new Ticket($this->db);
}
} elseif (preg_match('/<(.*@.*)>/', $reference, $reg)) {
// This is an external reference, we check if we have it in our database
if (! is_object($objectemail)) {
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."ticket where email_msgid = '".$this->db->escape($reg[1])."'";
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
$objectid = $obj->rowid;
$objectemail = new Ticket($this->db);
} else {
$errorforemail++;
}
}
$thirdpartyid = $objectemail->fk_soc;
$contactid = $objectemail->fk_socpeople;
$projectid = isset($objectemail->fk_project) ? $objectemail->fk_project : $objectemail->fk_projet;
}
}
if (! is_object($objectemail)) {
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."projet where email_msgid = '".$this->db->escape($reg[1])."'";
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
$objectid = $obj->rowid;
$objectemail = new Project($this->db);
} else {
$errorforemail++;
}
}
}
// Project
if ($projectid > 0)
{
$result = $projectstatic->fetch($projectid);
if ($result <= 0) $projectstatic->id = 0;
else {
$projectid = $projectstatic->id;
$projectfoundby = 'trackid ('.$trackid.')';
if (empty($contactid)) $contactid = $projectstatic->fk_contact;
if (empty($thirdpartyid)) $thirdpartyid = $projectstatic->fk_soc;
}
}
// Contact
if ($contactid > 0)
{
$result = $contactstatic->fetch($contactid);
if ($result <= 0) $contactstatic->id = 0;
else {
$contactid = $contactstatic->id;
$contactfoundby = 'trackid ('.$trackid.')';
if (empty($thirdpartyid)) $thirdpartyid = $contactstatic->fk_soc;
}
}
// Thirdparty
if ($thirdpartyid > 0)
{
$result = $thirdpartystatic->fetch($thirdpartyid);
if ($result <= 0) $thirdpartystatic->id = 0;
else {
$thirdpartyid = $thirdpartystatic->id;
$thirdpartyfoundby = 'trackid ('.$trackid.')';
}
}
if (is_object($objectemail))
{
$result = $objectemail->fetch($objectid);
if ($result > 0)
{
$fk_element_id = $objectemail->id;
$fk_element_type = $objectemail->element;
// Fix fk_element_type
if ($fk_element_type == 'facture') $fk_element_type = 'invoice';
$thirdpartyid = $objectemail->fk_soc;
$contactid = $objectemail->fk_socpeople;
$projectid = isset($objectemail->fk_project) ? $objectemail->fk_project : $objectemail->fk_projet;
}
}
// Project
if ($projectid > 0)
{
$result = $projectstatic->fetch($projectid);
if ($result <= 0) $projectstatic->id = 0;
else {
$projectid = $projectstatic->id;
$projectfoundby = 'trackid ('.$trackid.')';
if (empty($contactid)) $contactid = $projectstatic->fk_contact;
if (empty($thirdpartyid)) $thirdpartyid = $projectstatic->fk_soc;
}
}
// Contact
if ($contactid > 0)
{
$result = $contactstatic->fetch($contactid);
if ($result <= 0) $contactstatic->id = 0;
else {
$contactid = $contactstatic->id;
$contactfoundby = 'trackid ('.$trackid.')';
if (empty($thirdpartyid)) $thirdpartyid = $contactstatic->fk_soc;
}
}
// Thirdparty
if ($thirdpartyid > 0)
{
$result = $thirdpartystatic->fetch($thirdpartyid);
if ($result <= 0) $thirdpartystatic->id = 0;
else {
$thirdpartyid = $thirdpartystatic->id;
$thirdpartyfoundby = 'trackid ('.$trackid.')';
}
}
if (is_object($objectemail))
{
break; // Exit loop of references. We already found an accurate reference
}
}
}
if (empty($contactid)) // Try to find contact using email
@ -1355,7 +1393,7 @@ class EmailCollector extends CommonObject
{
$contactid = $contactstatic->id;
$contactfoundby = 'email of contact ('.$from.')';
if ($contactstatic->socid > 0)
if (empty($thirdpartyid) && $contactstatic->socid > 0)
{
$result = $thirdpartystatic->fetch($contactstatic->socid);
if ($result > 0)

View File

@ -240,3 +240,9 @@ ALTER TABLE llx_product MODIFY COLUMN seuil_stock_alerte float;
ALTER TABLE llx_product MODIFY COLUMN desiredstock float;
ALTER TABLE llx_product_warehouse_properties MODIFY COLUMN seuil_stock_alerte float;
ALTER TABLE llx_product_warehouse_properties MODIFY COLUMN desiredstock float;
ALTER TABLE llx_projet ADD COLUMN email_msgid varchar(255);
ALTER TABLE llx_ticket ADD COLUMN email_msgid varchar(255);
ALTER TABLE llx_actioncomm ADD COLUMN reply_to varchar(255);

View File

@ -66,6 +66,7 @@ create table llx_actioncomm
email_tocc varchar(255), -- when event was an email, we store here the email_tocc
email_tobcc varchar(255), -- when event was an email, we store here the email_tobcc
errors_to varchar(255), -- when event was an email, we store here the erros_to
reply_to varchar(255), -- when event was an email, we store here the reply_to
recurid varchar(128), -- used to store event id to link each other all the repeating event record. It can be the 'iCalUID' as in RFC5545 (an id similar for all the same serie)
recurrule varchar(128), -- contains string with ical format recurring rule like 'FREQ=MONTHLY;INTERVAL=2;BYMONTHDAY=19' or 'FREQ=WEEKLY;BYDAY=MO'

View File

@ -39,6 +39,7 @@ create table llx_projet
fk_user_close integer DEFAULT NULL,
note_private text,
note_public text,
email_msgid varchar(255), -- if project or lead is created by email collector, we store here MSG ID
--budget_days real, -- budget in days is sum of field planned_workload of tasks
opp_amount double(24,8),
budget_amount double(24,8),

View File

@ -38,6 +38,7 @@ CREATE TABLE llx_ticket
date_read datetime,
date_close datetime,
notify_tiers_at_create tinyint,
email_msgid varchar(255), -- if ticket is created by email collector, we store here MSG ID
tms timestamp,
import_key varchar(14)
)ENGINE=innodb;