Logic hook on Emails module

Hi everyone,

I'm developing a logic hook on Emails module to create a new Account with the info that I receive in this email and relate this account with the email, my issue is when I install the logic hook the functionality to create a new Case for the email doesn't work and my logic hook either, so I want to know, how can I deploy my logic hook working at the same time with the sugar process to create cases to the email?

Best Regards

Parents Reply Children
  • Yes, sure, this is my last version, I tried to put it on the Cases' module with an after relationship add, to find the relationship with the Emails' module.

    class catch_email_class
        {

            public function catch_email_method($bean, $event, $arguments)
            {
                if ($arguments['related_module'] != 'Emails') {
                    return;
                }

                // Capturo el correo electronico
                $beanEmails = BeanFactory::retrieveBean('Emails'$arguments['related_id']);
                $from = $beanEmails->from_addr;
                $to = $beanEmails->to_addrs;
                $subject = $beanEmails->name;
                $body = $bean->description;

                $value = '] PETICIONES, QUEJAS Y RECLAMOS';
                $reclamo = stripos($subject$value);
                if ($reclamo === false) {
                    return;
                }

                //Capturar datos del cuerpo del correo
                $email = catch_email_class::getEmail($body);
                $GLOBALS['log']->fatal('email: ' . $email);

                $tlf = catch_email_class::getDatos($body'Telefono:');
                $identificacion = catch_email_class::getDatos($body'identidad:');
                $solicitud = catch_email_class::getDatos($body'Solicitud:');
                $tienda = catch_email_class::getDatos($body'Center:');
                $cliente = catch_email_class::getDatos($body'Cliente,');
                $factura = catch_email_class::getDatos($body'factura:');

                // Verificar si existe un cliente un email al extraido del cuerpo del correo
                $beanCE0 = BeanFactory::newBean('Accounts');

                require_once 'include/SugarQuery/SugarQuery.php';
                $sugarQuery0 = new SugarQuery();
                $sugarQuery0->select(['id']);
                $sugarQuery0->from($beanCE0);
                $sugarQuery0->where()
                ->equals('email1'$email);
                $result0 = $sugarQuery0->getOne();

                if ($result0) {
                    //Vincular el correo  al cliente
                    //Crea una relación entre cliente y correo
                    $beanAccount = BeanFactory::retrieveBean('Accounts'$result0);
                    $beanAccount->load_relationship('archived_emails');
                    $beanAccount->archived_emails->add($beanEmails->id);
                    $beanAccount->save();
                    $GLOBALS['log']->fatal(print_r($result0true));
                    $GLOBALS['log']->fatal('Cuenta');
                    $GLOBALS['log']->fatal(print_r($beanAccounttrue));
                } else {
                    $newAccountID = catch_email_class::create_account($email$tlf$identificacion$subject);
                    $beanAccount = BeanFactory::retrieveBean('Accounts'$newAccountID);
                    $beanAccount->load_relationship('archived_emails');
                    $beanAccount->archived_emails->add($beanEmails->id);
                    $beanAccount->save();
                    $GLOBALS['log']->fatal('create_account');
                }

                $bean->tienda_c = $tienda;
                $bean->documentoidentidad_c = $identificacion;
                $bean->nombrecompleto_c = $cliente;
                $bean->telefonomovil_c = $tlf;
                $bean->account_id = $beanAccount->id;
                $bean->email_c = $email;
                $bean->numerofactura_c = $factura;

                if ($solicitud == 'Peticiones') {
                    $bean->type = 'Peticiones';
                } elseif ($solicitud == 'Quejas') {
                    $bean->type = 'Quejas';
                } elseif ($solicitud == 'Reclamo') {
                    $bean->type = 'Reclamo';
                } elseif ($solicitud == 'Sugerencia') {
                    $bean->type = 'Sugerencia';
                } elseif ($solicitud == 'Felicitaciones') {
                    $bean->type = 'Felicitaciones';
                }
            }
            
            public function getEmail($excerpt)
            {
                preg_match('/[A-Za-z0-9._%+-]+@[a-z0-9._%+-]+\.[a-z]+/'$excerpt$m);
                return $m[0];
            }

            public function getDatos($desciption_html, $texto)
            {
                $patron = "/[\n\s,]+/";
                $array = preg_split($patron$desciption);
                for ($i = 0;$i < sizeof($array);$i++) {
                    if ($array[$i] == $texto && $texto == 'Center:') {
                        $dato = trim($array[$i + 1. ' - ' . $array[$i + 3]);
                    } elseif ($array[$i] == $texto) {
                        $dato = trim($array[$i + 1]);
                    }
                }
                return $dato;
            }

            public function create_account($email, $tlf, $identificacion, $subject)
            {
                //Agrega un nuevo account con los datos del correo recibido y el name
                $beanAccountNew = BeanFactory::newBean('Accounts');
                $name = explode(':'$subject);
                $nameN = count($name) - 1;
                $beanAccountNew->name = $name[$nameN];
                $beanAccountNew->numerodeidentificacion_c = $identificacion;
                $beanAccountNew->movil_c = $tlf;
                $beanAccountNew->email1 = $email;
                $beanAccountNew->save();

                //Recupera el id insertado
                $accountID = $beanAccountNew->id;
                return $accountID;
            }
        }