<?php

function function_sendmail_asignadocuenta( $entity ) {
  require_once 'modules/Emails/mail.php';
    global $adb;
    global $site_URL;

    $data = $entity->data;

    /*Para Enviar correo al asignado a la cuenta*/
    if($data['parent_id']!=''){
    $parent_id_ = explode("x", $data['parent_id'])[1];
    $id_ = explode("x", $data['id'])[1];
    $ticket_no = $data['ticket_no'];
    $ticket_title = $data['ticket_title'];
    $ticketcategories = $data['ticketcategories'];
    $proyectosencible = $data['cf_941'];

    $query = $adb->pquery("SELECT vtiger_users.email1, accountname FROM	vtiger_account INNER JOIN vtiger_crmentity ON vtiger_account.accountid = vtiger_crmentity.crmid INNER JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id WHERE crmid = ".$parent_id_, array());
    $email1 = $adb->query_result($query, 0, 'email1');
    $accountname = $adb->query_result($query, 0, 'accountname');
    
    $url = $site_URL . "index.php?module=HelpDesk&view=Detail&record=$id_";
$mensaje1 = <<<EOT
<!DOCTYPE html>
<html>
<head>
  <title>Notificacion VTIGER </title>
</head>
<body>
<p>
<h2>Se registró una incidencia de Categoría '$ticketcategories' a la cuenta: $accountname</h2>
<h3>Ticket Nro $ticket_no : $ticket_title</h3> 
Ver: <a href='$url'> $ticket_no - $ticket_title</a>
</body>
</html>
EOT;

//echo $email1;
//echo $mensaje1;
//echo '------------------</br>';
$bcc='jjpena@creantis.com';
  send_mail('HelpDesk', $email1, 'CRM Fursys', 'vtiger-admin@fursys.com.pe',"CRM Fursys",$mensaje1, $cc='', $bcc, $atc="",$eid="",$logo='',$ug='');
    }
    /*Para Enviar correo al jefe de Operaciones H25*/
    if($ticketcategories == 'Mantenimiento' || $ticketcategories == 'Mudanza y/o Remodelacion'){
$mensaje2 = <<<EOT
<!DOCTYPE html>
<html>
<head>
  <title>Notificacion VTIGER </title>
</head>
<body>
<p>
<h2>Un usuario de Ventas ha creado una Incidencia de Categoría '$ticketcategories'</h2>
<h3>Ticket Nro $ticket_no : $ticket_title</h3> 
Ver: <a href='$url'> $ticket_no - $ticket_title</a>
</body>
</html>
EOT;

    $query = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H25'", array());
    $email2 = $adb->query_result($query, 0, 'email1');
    //echo $email2;
    //echo $mensaje2;
    $bcc='jjpena@creantis.com';
    send_mail('HelpDesk', $email2, 'CRM Fursys', 'vtiger-admin@fursys.com.pe',"CRM Fursys",$mensaje2, $cc='', $bcc, $atc="",$eid="",$logo='',$ug='');
    }elseif ($ticketcategories == 'Producto Defectuoso'){
$mensaje2 = <<<EOT
<!DOCTYPE html>
<html>
<head>
  <title>Notificacion VTIGER </title>
</head>
<body>
<p>
<h2>Se ha registrado una Incidencia de Categoría '$ticketcategories'</h2>
<h3>Ticket Nro $ticket_no : $ticket_title</h3> 
Ver: <a href='$url'> $ticket_no - $ticket_title</a>
</body>
</html>
EOT;
    $query = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H25'", array());
    $email2 = $adb->query_result($query, 0, 'email1');
$bcc='jjpena@creantis.com';
    send_mail('HelpDesk', $email2, 'CRM Fursys', 'vtiger-admin@fursys.com.pe',"CRM Fursys",$mensaje2, $cc='', $bcc, $atc="",$eid="",$logo='',$ug='');

    }elseif ($ticketcategories == 'Repuestos por Venta'){
$mensaje3 = <<<EOT
<!DOCTYPE html>
<html>
<head>
  <title>Notificacion VTIGER </title>
</head>
<body>
<p>
<h2>Se ha registrado una Incidencia de Categoría '$ticketcategories'</h2>
<h3>Ticket Nro $ticket_no : $ticket_title</h3> 
Ver: <a href='$url'> $ticket_no - $ticket_title</a>
</body>
</html>
EOT;
        $query = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H24'", array());
        $email3 = $adb->query_result($query, 0, 'email1');
        //echo $email3;
        //echo $mensaje3;
        $bcc='jjpena@creantis.com';
        send_mail('HelpDesk', $email3, 'CRM Fursys', 'vtiger-admin@fursys.com.pe',"CRM Fursys",$mensaje3, $cc='', $bcc, $atc="",$eid="",$logo='',$ug='');

    }elseif ($ticketcategories == 'Consultas y Quejas' && $proyectosencible == '1'){
        /* Gerente de Operaciones H23 --- Gerente Comercial H4 ---  Gerente de Administracion H24*/
        $query_go = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H23'", array());
        $email_go = $adb->query_result($query_go, 0, 'email1');
        //$query_gc = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H4'", array());
        //$email_gc = $adb->query_result($query_gc, 0, 'email1');
        $query_ga = $adb->pquery("SELECT email1 FROM vtiger_users INNER JOIN vtiger_user2role ON vtiger_users.id = vtiger_user2role.userid WHERE roleid = 'H24'", array());
        $email_ga = $adb->query_result($query_ga, 0, 'email1');

$mensaje4 = <<<EOT
<!DOCTYPE html>
<html>
<head>
  <title>Notificacion VTIGER </title>
</head>
<body>
<p>
<h2>Se ha registrado una Incidencia de Categoría '$ticketcategories'</h2>
<h3>Ticket Nro $ticket_no : $ticket_title</h3> 
Ver: <a href='$url'> $ticket_no - $ticket_title</a>
</body>
</html>
EOT;
        $email4 = $email_go.','.$email_ga.','.'fiorella.berio@fursysperu.com.pe';
        //echo $email4;
        //echo $mensaje4;
        $bcc='jpena@creantis.com';
        send_mail('HelpDesk', $email4, 'CRM Fursys', 'vtiger-admin@fursys.com.pe',"CRM Fursys",$mensaje4, $cc='', $bcc, $atc="",$eid="",$logo='',$ug='');

    }
}