<?php

function function_mail_cumpleanios( $entity ) {
	require_once 'modules/Emails/mail.php';
    global $adb;
    global $site_URL;
    $data = $entity->data;

    // echo "<pre>";
    $contactid = explode("x", $data['id'])[1];
    $birthday = $data['birthday'];
    $assigned_user_id = explode("x", $data['assigned_user_id'])[1];
    $firstname = $data['firstname'];
    $lastname = $data['lastname'];

    // $fecha_nac = new DateTime($birthday,  new DateTimeZone('America/Lima'));
    // $fecha_act = new DateTime(date("Y-m-d"),  new DateTimeZone('America/Lima'));
    // $diff = $fecha_nac->diff($fecha_act);

    $fecha_act = new DateTime(date("Y-m-d"),  new DateTimeZone('America/Lima'));
    $fecha_act_form = new DateTime(date("Y-m-d"),  new DateTimeZone('America/Lima'));
    $fecha_act = $fecha_act->format("Y-m-d");
    $fecha_act_form = $fecha_act_form->format("d/m/Y");

    $birthday = substr($birthday, 5);
    $fecha_act = substr($fecha_act, 5);
    // error_reporting(-1);
    if ( $fecha_act == $birthday ) {
      // echo "ok";
        $query = "SELECT email1 FROM vtiger_users WHERE id = '$assigned_user_id'";
        $resultado = $adb->pquery($query,array());
        $email = $adb->query_result($resultado, 0, 'email1');
        // echo $email;
        // $email = 'hcumbicusr@gmail.com';
        
        // $cc_emails_str = "hcumbicus@creantis.com";

        $mensaje = '<html><head><title></title></head><body>
        <h2>Notificación de cumpleaños:</h2>
      <p>El sr(a) "'.$firstname.' '.$lastname.'" está cuempliendo años el día de hoy '.$fecha_act_form.'</p>
      Contacto: <a href="'.$site_URL.'index.php?module=Contacts&view=Detail&record='.$contactid.'">Contacto : '.$firstname.' '.$lastname.'</a>
    	</body>
    	</html>
    	';
      // echo $mensaje;
        send_mail('Contacts', $email, 'CRM Fursys', 'vtiger-admin@fursys.com.pe','CRM-Fursys: cumpleaños '.$firstname.' '.$lastname, $mensaje, $cc_emails_str, $bcc='', $atc="",$eid="",$logo='',$ug='');
        // die();
    }

}

/**   Function used to send email
  *   $module 		-- current module
  *   $to_email 	-- to email address
  *   $from_name	-- currently loggedin user name
  *   $from_email	-- currently loggedin vtiger_users's email id. you can give as '' if you are not in HelpDesk module
  *   $subject		-- subject of the email you want to send
  *   $contents		-- body of the email you want to send
  *   $cc		-- add email ids with comma seperated. - optional
  *   $bcc		-- add email ids with comma seperated. - optional.
  *   $attachment	-- whether we want to attach the currently selected file or all vtiger_files.[values = current,all] - optional
  *   $emailid		-- id of the email object which will be used to get the vtiger_attachments
  */
//function send_mail($module,$to_email,$from_name,$from_email,$subject,$contents,$cc='',$bcc='',$attachment='',$emailid='',$logo='', $useGivenFromEmailAddress=false)