<?php
class basededatos{
	
	
	function enviar_correo_diario($link, $turno, $fecha, $observacion, $especie_id, $exportador_cod, $packing_cod, $packing_nombre){
		$rstotal = mysql_query("SELECT COUNT(*) as total_datos FROM `planilla_datos` INNER JOIN planilla_control ON planilla_control.folio_planilla=planilla_datos.folio_planilla WHERE planilla_control.fecha_inspeccion='$fecha' AND planilla_control.exportador_cod='$exportador_cod' AND planilla_control.especie_cod='$especie_id' AND planilla_control.packing_cod='$packing_cod' AND planilla_control.turno='$turno'");
	                if($rowtotal = mysql_fetch_array($rstotal)){
	                    $total_datos=$rowtotal['total_datos'];
	                }
	                if($total_datos==0){
	                    $total_datos=1;
	                }

	$rsverde = mysql_query("SELECT COUNT(*) as verde FROM `planilla_datos` INNER JOIN planilla_control ON planilla_control.folio_planilla=planilla_datos.folio_planilla WHERE planilla_control.fecha_inspeccion='$fecha' AND planilla_control.exportador_cod='$exportador_cod' AND planilla_control.especie_cod='$especie_id' AND planilla_control.packing_cod='$packing_cod' AND planilla_control.turno='$turno' AND planilla_datos.cumple_norma='VERDE'");
	                if($rowverde = mysql_fetch_array($rsverde)){
	                    $total_verde=$rowverde['verde'];
	                    $total_verde=($total_verde*100)/$total_datos;
	                    $total_verde=round($total_verde, 1, PHP_ROUND_HALF_UP);
	                }

	$rsamarillo = mysql_query("SELECT COUNT(*) as amarillo FROM `planilla_datos` INNER JOIN planilla_control ON planilla_control.folio_planilla=planilla_datos.folio_planilla WHERE planilla_control.fecha_inspeccion='$fecha' AND planilla_control.exportador_cod='$exportador_cod' AND planilla_control.especie_cod='$especie_id' AND planilla_control.packing_cod='$packing_cod' AND planilla_control.turno='$turno' AND planilla_datos.cumple_norma='AMARILLO'");
	                if($rowamarillo = mysql_fetch_array($rsamarillo)){
	                    $total_amarillo=$rowamarillo['amarillo'];
	                    $total_amarillo=($total_amarillo*100)/$total_datos;
	                    $total_amarillo=round($total_amarillo, 1, PHP_ROUND_HALF_UP);
	                }
	$rsrojo = mysql_query("SELECT COUNT(*) as rojo FROM `planilla_datos` INNER JOIN planilla_control ON planilla_control.folio_planilla=planilla_datos.folio_planilla WHERE planilla_control.fecha_inspeccion='$fecha' AND planilla_control.exportador_cod='$exportador_cod' AND planilla_control.especie_cod='$especie_id' AND planilla_control.packing_cod='$packing_cod' AND planilla_control.turno='$turno' AND planilla_datos.cumple_norma='ROJO'");
	                if($rowrojo = mysql_fetch_array($rsrojo)){
	                    $total_rojo=$rowrojo['rojo'];
	                    $total_rojo=($total_rojo*100)/$total_datos;
	                    $total_rojo=round($total_rojo, 1, PHP_ROUND_HALF_UP);
	               }
	
	//Create a new PHPMailer instance
	$mail = new PHPMailer;
	//Tell PHPMailer to use SMTP
	$mail->isSMTP();
	$mail->CharSet = "UTF-8";
	//Enable SMTP debugging
	// 0 = off (for production use)
	// 1 = client messages
	// 2 = client and server messages
	$mail->SMTPDebug = 2;
	//Ask for HTML-friendly debug output
	$mail->Debugoutput = 'html';
	//Set the hostname of the mail server
	$mail->Host = 'smtp.gmail.com';
	// use
	// $mail->Host = gethostbyname('smtp.gmail.com');
	// if your network does not support SMTP over IPv6
	//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
	$mail->Port = 587;
	//Set the encryption system to use - ssl (deprecated) or tls
	$mail->SMTPSecure = 'tls';
	//Whether to use SMTP authentication
	$mail->SMTPAuth = true;
	//Username to use for SMTP authentication - use full email address for gmail
	$mail->Username = "inglobochile2016@gmail.com";
	//Password to use for SMTP authentication
	$mail->Password = "inglobo2017";
	//Set who the message is to be sent from
	$mail->setFrom('inglobochile2016@gmail.com', 'Inglobo');
	//Set an alternative reply-to address
	//$mail->addReplyTo('replyto@example.com', 'First Last');
	//Set who the message is to be sent to

	$rs2 = mysql_query("SELECT correo, nombre FROM exportadores_mail WHERE estado='ACTIVO' AND exportador_cod='$exportador_cod'");
	        while($row2 = mysql_fetch_array($rs2)){
	            $nombre=$row2['nombre'];
	            $mail2=$row2['correo'];
	            //$mail->AddBCC($mail2, $nombre);
	            $mail->addAddress($mail2, $nombre);
	        }
	//Set the subject line
	$mail->Subject = 'IngloboChile';
	//Read an HTML message body from an external file, convert referencedimages to embedded,
	//convert HTML into a basic plain-text alternative body
	//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
	//Replace the plain text body with one created manually
	$mail->Body = "<div style='padding:10px; font-family: Calibri, Candara, Segoe, 'Segoe UI', Optima, Arial, sans-serif; font-size: 11px; color: #000000;'>Sres: <br/>Se adjunta información de las mediciones realizadas en el proceso del día ".$fecha3." Turno ".$turno." correspondiente a cerezas de la planta ".$packing_nombre.".<br/>
			<br/><table width='500'>
			<tr>
				<th colspan='3'>EVALUACION TURNO</th>
			</tr>
			<tr>
				<th bgcolor='#00FF00' width='".$total_verde."%'>".$total_verde."%</th>
				<th bgcolor='#FFFF00' width='".$total_amarillo."%'>".$total_amarillo."%</th>
				<th bgcolor='#FF0000' width='".$total_rojo."%'>".$total_rojo."%</th>
			</tr>
			</table><br/>

			<br/>Observación: ".$observacion."<br/><br/>
	        <a href='".$link."'>VER INFORME</a><br/>
			<br/><br/>Saludos Atte.<br/>INGLOBOCHILE LTDA<br/></div>";
			
			$mail->IsHTML(true);
	//Attach an image file
	//$mail->addAttachment('images/phpmailer_mini.png');
	//send the message, check for errors
	if (!$mail->send()) {
	    return echo "BIEN";
	} else {
	    return echo "ERROR";
	}
	}
}
?>