<?php

require_once '../controller/PHPMailer/PHPMailerAutoload.php';
require_once '../model/mUsuarios.php';
$mUsuarios = new mUsuarios();
$mail = new PHPMailer(true);
//Passing true to the constructor enables the use of exceptions for error handling
try {
    $mail->setFrom('*****@*****.**', 'Temposatis');
    $mail->addReplyTo('*****@*****.**', 'Temposatis');
    $usuarios = split(",", $_POST['txtUsuarios']);
    // print_r($_POST);
    for ($i = 0; $i < count($usuarios); $i++) {
        $datosUsuario = $mUsuarios->getDatosUsuario($usuarios[$i]);
        while ($row = $datosUsuario->fetch_array(MYSQLI_ASSOC)) {
            $nombre = $row['paterno'] . " " . $row['materno'] . " " . $row['nombre'];
            $correo = $row['correo'];
        }
        //fin while
        $mail->ClearAddresses();
        $mail->addAddress($correo, $nombre);
        // $mail->Subject = 'COMUNICADO TEMPOSATIS';
        $mail->Subject = $_POST['txtAsunto'];
        $mail->msgHTML("<h1>COMUNICADO DE TEMPOSATIS</h1>");
        $mail->IsHTML(true);
        $mail->CharSet = 'UTF-8';
        $mail->AltBody = 'ESTE ES UN COMUNICADO DE TEMPOSATIS';
        //Attach an image file
        $archivo = $_FILES['archivoPDF']['name'];
        $vartemp = $_FILES['archivoPDF']['tmp_name'];
        $mail->AddAttachment($vartemp, $archivo);
 function notificacion($id_usuario, $id_venta)
 {
     // include_once("../controller/cMensajesEmail.php");
     include_once "../controller/cMensajesSMS.php";
     include_once "../controller/cFallidos.php";
     include_once "../model/mUsuarios.php";
     include_once "../model/mVentas.php";
     include_once '../model/mSistema.php';
     require_once '../controller/PHPMailer/PHPMailerAutoload.php';
     //Create a new PHPMailer instance
     $mail = new PHPMailer();
     //Set who the message is to be sent from
     $mail->setFrom('*****@*****.**', 'Temposatis');
     $mail->addReplyTo('*****@*****.**', 'Temposatis');
     $mail->CharSet = 'UTF-8';
     $cFallidos = new cFallidos();
     $mSistema = new mSistema();
     // $mail=new cMensajesEmail();
     $sms = new cMensajesSMS();
     $mUsuarios = new mUsuarios();
     $mVentas = new mVentas();
     $correosNotificaciones = $mSistema->getCorreos();
     $datosUsuario = $mUsuarios->getDatosUsuario($id_usuario);
     $datosVenta = $mVentas->getVenta($id_venta);
     $tipoContacto = $mUsuarios->getRelUsuariosContacto($id_usuario);
     /*****obtención de datos del usuario***/
     while ($row = $datosUsuario->fetch_array(MYSQLI_ASSOC)) {
         $nombre = $row['paterno'] . " " . $row['materno'] . " " . $row['nombre'];
         $correo = $row['correo'];
         $sucursal = $row['sucursal'];
         $telefono_movil = "52" . $row['telefono_movil'];
         $telefono = $row['telefono'];
     }
     /*****obtención de datos de la venta***/
     while ($row = $datosVenta->fetch_array(MYSQLI_ASSOC)) {
         $referencia = $row['referencia'];
         $marca = $row['marca'];
         $descripcion = $row['descripcion'];
         $numero_serie = $row['numero_serie'];
         $incentivo = $row['incentivo'];
     }
     /***envio de correos a los adminsitradores o quien este como nueva notificacion***/
     while ($fila = $correosNotificaciones->fetch_array(MYSQLI_ASSOC)) {
         //Set who the message is to be sent to
         $mail->ClearAddresses();
         $mail->addAddress($fila['descripcion']);
         //Set the subject line
         $mail->Subject = "Vendedor Registro nueva venta";
         $body = "<table border='1px'>\n\t\t\t\t\t<thead>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>SUCURSAL</th>\n\t\t\t\t\t\t\t<th>VENDEDOR</th>\n\t\t\t\t\t\t\t<th>CORREO DEL VENDEDOR</th>\n\t\t\t\t\t\t\t<th>TELÉFONO(S) DEL VENDEDOR</th>\n\t\t\t\t\t\t\t<th>REFERENCIA</th>\n\t\t\t\t\t\t\t<th>MARCA</th>\n\t\t\t\t\t\t\t<th>DESCRIPCIÓN</th>\n\t\t\t\t\t\t\t<th>NÚMERO DE SERIE</th>\n\t\t\t\t\t\t\t<th>INCENTIVO</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</thead>\n\t\t\t\t\t<tbody>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>" . strtoupper($sucursal) . "</td>\n\t\t\t\t\t\t\t<td>" . strtoupper($nombre) . "</td>\n\t\t\t\t\t\t\t<td>" . $correo . "</td>\n\t\t\t\t\t\t\t<td>" . $telefono . " " . $telefono_movil . "</td>\n\t\t\t\t\t\t\t<td>" . $referencia . "</td>\n\t\t\t\t\t\t\t<td>" . $marca . "</td>\n\t\t\t\t\t\t\t<td>" . $descripcion . "</td>\n\t\t\t\t\t\t\t<td>" . $numero_serie . "</td>\n\t\t\t\t\t\t\t<td>" . $incentivo . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</tbody>\n\t\t\t\t</table>";
         $mail->msgHTML("<h1>SE HA REGISTRADO UNA NUEVA VENTA CON EL FOLIO: {$id_venta}. </h1>" . $body);
         //Replace the plain text body with one created manually
         $mail->AltBody = "Vendedor Registro nueva venta SUCURSAL:" . strtoupper($sucursal) . ". VENDEDOR:" . strtoupper($nombre) . ". CORREO:" . $correo . ". TELÉFONO:" . $telefono . " " . $telefono_movil . ". REFERENCIA:" . $referencia . ". MARCA:" . $marca . ". DESCRIPCIÓN:" . $descripcion . ". NÚMERO DE SERIE:" . $numero_serie . ". INCENTIVO:" . $incentivo;
         if (!$mail->send()) {
             // echo "Mailer Error: " . $mail->ErrorInfo;
             $cFallidos->setFallido($_SESSION['id_usuario'], $_SESSION['id_sucursal'], "Ocurrio error al enviar correo a :" . $fila['descripcion'] . " " . $mail->ErrorInfo, 2);
         }
         /*$mail->setDestinatario($fila['descripcion']);
         		$mail->setAsunto("Vendedor Registro nueva venta");
         		$mail->setBody("<h1>SE HA REGISTRADO UNA NUEVA VENTA CON EL FOLIO: $id_venta. </h1>".$body);
         		$mail->send();*/
     }
     /*****Envio de notificaciones al vendedor***/
     while ($row = $tipoContacto->fetch_array(MYSQLI_ASSOC)) {
         //mensaje al vendedor
         if ($row['id_tipo_contacto'] == 1) {
             //correo
             $body = "<table border='1px'>\n\t\t\t\t\t<thead>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>REFERENCIA</th>\n\t\t\t\t\t\t\t<th>MARCA</th>\n\t\t\t\t\t\t\t<th>DESCRIPCIÓN</th>\n\t\t\t\t\t\t\t<th>NÚMERO DE SERIE</th>\n\t\t\t\t\t\t\t<th>INCENTIVO</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</thead>\n\t\t\t\t\t<tbody>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>" . $referencia . "</td>\n\t\t\t\t\t\t\t<td>" . $marca . "</td>\n\t\t\t\t\t\t\t<td>" . $descripcion . "</td>\n\t\t\t\t\t\t\t<td>" . $numero_serie . "</td>\n\t\t\t\t\t\t\t<td>" . $incentivo . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</tbody>\n\t\t\t\t</table>";
             $mail->ClearAddresses();
             $mail->addAddress($correo, $nombre);
             $mail->SUBJECT = "REGISTRO DE NUEVA VENTA";
             $mail->msgHTML("<h1>SE HA REGISTRADO UNA NUEVA VENTA CON EL FOLIO: {$id_venta}. </h1>" . $body);
             //Replace the plain text body with one created manually
             $mail->AltBody = "REGISTRO DE NUEVA VENTA. REFERENCIA:" . $referencia . ". MARCA:" . $marca . ". DESCRIPCIÓN:" . $descripcion . ". NÚMERO DE SERIE:" . $numero_serie . ". INCENTIVO:" . $incentivo;
             if (!$mail->send()) {
                 // echo "Mailer Error: " . $mail->ErrorInfo;
                 $cFallidos->setFallido($_SESSION['id_usuario'], $_SESSION['id_sucursal'], "Ocurrio error al enviar correo a :{$correo}" . $mail->ErrorInfo, 2);
             }
             /*$mail->setDestinatario($correo);
             		$mail->setAsunto("Registro de nueva venta");
             		$mail->setBody("<h1>SE HA REGISTRADO UNA NUEVA VENTA CON EL FOLIO: $id_venta.</h1>".$body);
             		$mail->send();*/
         } else {
             //sms
             if ($telefono_movil != "") {
                 //si contiene un telefono movil
                 // echo $telefono_movil;
                 include_once "../controller/cMensajesSMS.php";
                 $cMensajes = new cMensajesSMS();
                 $resp = $cMensajes->send($telefono_movil, "Se a registrado una nueva venta con el folio: {$id_venta}", false);
                 if (!$resp) {
                     return "\nmensaje enviado al numero " . $telefono_movil;
                 } else {
                     return strstr($resp, 'ERROR');
                 }
             }
             //fin if
         }
     }
 }