<?php

	require_once "core_classes.php";					
	require_once "PHPMailer.php";
	require_once "SMTP.php";	
	require_once "Exception.php";
	require_once "OAuth.php";
	require_once "config.php";
	
	require_once "dompdf/autoload.inc.php";
	use Dompdf\Dompdf;
	
	/*****************************[MANIOBRAS REALIZADAS]***************************************/
		$tablaServicios = "<div><h4>MANIOBRAS REALIZADAS</h4><hr></div>";
		$tablaServicios = $tablaServicios."<table width='100%'><thead><tr><th>Cliente</th><th>Folio</th><th>Buque</th><th>Servicio</th><th>Costo Servicio</th></tr></thead><tbody>";
		$fila;
		$totalRealizadas = 0.00;
		// get settings
		$stmt = $conn->prepare("call servicio_consultas_email()");
		$stmt->execute();	

		$response = new root_response;
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
		{ 				
			$fila = "<tr><td>".$row["NomCliente"]."</td><td>"."AMA".$row["IdServicio"]."</td><td>".$row["NomBuque"]."</td><td>".$row["TipoServicio"]."</td><td>".$row["monto"]."</td></tr>";
		
			$tablaServicios = $tablaServicios.$fila;
			$totalRealizadas = $row["total"];
		};
	
	$tablaServicios = $tablaServicios. "</tbody></table><hr><div align='right' style='padding-right:100px'><strong>Total:  </strong>$ ". $totalRealizadas ."</div>";
		//$tablaServicios = $tablaServicios. "</tbody></table>";
	
	/*****************************[MANIOBRAS FACTURADAS]***************************************/	
	
	/*
		$tablaFacturados = "<div><h4>MANIOBRAS FACTURADAS</h4><hr></div>";
		$tablaFacturados = $tablaFacturados."<table width='100%'><thead><tr><th>Cliente</th><th>Folio</th><th>Buque</th><th>Servicio</th><th>Monto</th></tr></thead><tbody>";
		$filaFacturado;
		$totalFacturados = 0.00;
		// get settings
		$stmt = $conn->prepare("call servicio_consultas_email_facturados()");
		$stmt->execute();	

		$response = new root_response;
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
		{ 				
			$filaFacturado = "<tr><td>".$row["NomCliente"]."</td><td>"."AMA".$row["IdServicio"]."</td><td>".$row["NomBuque"]."</td><td>".$row["TipoServicio"]."</td><td>".$row["monto"]."</td></tr>";
		
			$tablaFacturados = $tablaFacturados.$filaFacturado;
			$totalFacturados = $row["total"];
		};
	
		$tablaFacturados = $tablaFacturados. "</tbody></table><hr><div align='right' style='padding-right:100px'><strong>Total:  </strong>$ ". $totalFacturados ."</div>";
		
	/*****************************[MANIOBRAS PENDIENTES DE COBRO]***************************************
		$tablaPendientesCobro = "<div><h4>MANIOBRAS PENDIENTES DE COBRO</h4><hr></div>";	
		$tablaPendientesCobro = $tablaPendientesCobro."<table width='100%'><thead><tr><th>Cliente</th><th>Folio</th><th>Buque</th><th>Servicio</th><th>Monto</th></tr></thead><tbody>";
		$filaCobro;
		$totalPendientes = 0.00;
		// get settings
		$stmt = $conn->prepare("call servicio_consultas_email_pendientes()");
		$stmt->execute();	

		$response = new root_response;
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
		{ 				
			$filaCobro = "<tr><td>".$row["NomCliente"]."</td><td>"."AMA".$row["IdServicio"]."</td><td>".$row["NomBuque"]."</td><td>".$row["TipoServicio"]."</td><td>".$row["monto"]."</td></tr>";
		
			$tablaPendientesCobro = $tablaPendientesCobro.$filaCobro;
		    $totalPendientes = $row["total"];
		};
	
		$tablaPendientesCobro = $tablaPendientesCobro. "</tbody></table><hr><div align='right' style='padding-right:100px'><strong>Total:  </strong>$ ". $totalPendientes ."</div>";
	
	/*****************************[MANIOBRAS COBRADAS]***************************************	
		$tablaCobradas = "<div><h4>MANIOBRAS COBRADAS</h4><hr></div>";
		$tablaCobradas = $tablaCobradas."<table width='100%'><thead><tr><th>Cliente</th><th>Folio</th><th>Buque</th><th>Servicio</th><th>Monto</th></tr></thead><tbody>";
		$filaCobrada;
		$totalCobradas  = 0.00;
		// get settings
		$stmt = $conn->prepare("call servicio_consultas_email_cobrados()");
		$stmt->execute();	

		$response = new root_response;
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
		{ 				
			$filaCobrada = "<tr><td>".$row["NomCliente"]."</td><td>"."AMA".$row["IdServicio"]."</td><td>".$row["NomBuque"]."</td><td>".$row["TipoServicio"]."</td><td>".$row["monto"]."</td></tr>";
		
			$tablaCobradas = $tablaCobradas.$filaCobrada;
			$totalCobradas = $row["total"];
		};
	
		$tablaCobradas = $tablaCobradas. "</tbody></table><hr><div align='right' style='padding-right:100px'><strong>Total:  </strong>$ ". $totalCobradas ."</div>";
	/******************************************************************************************/
		
	$vNombreArchivo = trim(date('YmdHis'));
	echo "Nombre archivo ->". $vNombreArchivo;
	//$pNidServicio = filter_input(INPUT_POST, 'pNidServicio');
	//$pHtmlProforma = filter_input(INPUT_POST, 'pHtmlProforma');
				
	$content = '<html>
				<body>
					<div>
						<table  width="100%">
							<thead>
								<tr>
									<th>
									<img src="img/logo.png" height="60" width="150">
									</th>
									<th>
										<h2><strong>MANIOBRAS OPTIMIZADAS DE VERACRUZ S.A. DE C.V.</strong></h2>
										<h2><strong>REPORTE DE MANIOBRAS DEL DIA '.date('d/m/Y', strtotime('-1 day')).'</strong></h2>
									</th>
									<th>
									</th>
								</tr>
							</thead>
						</table>
					</div>';
	$content .= $tablaServicios;
	$content .= $tablaFacturados;
	$content .= $tablaPendientesCobro;
	$content .= $tablaCobradas;
	$content .= '</body></html>';

	$dompdf = new Dompdf();
	$dompdf->loadHtml($content);
	$dompdf->setPaper('letter', 'landscape'); // (Opcional) Configurar papel y orientación
	$dompdf->render(); // Generar el PDF desde contenido HTML
	$pdf = $dompdf->output(); // Obtener el PDF generado
	//$dompdf->stream(); // Enviar el PDF generado al navegador
	
	$filePath = "notificacionesPdf/".$vNombreArchivo .".pdf";

	file_put_contents($filePath, $pdf);
			
	/********************************************************************/	

	$pBodyMail = "Listado de maniobras realizadas el dia de hoy: " . date('Y-m-d H:i:s');
	
	$mail = new PHPMailer\PHPMailer\PHPMailer();
	 
	$response = new root_response;
	try {
		
		        $mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.moversa.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 587;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'amarres@moversa.mx';                     // SMTP username
				$mail->Password   = '!%-i8n_{;y?A';                               // SMTP password
		
		//Recipients
		$mail->setFrom('amarres@moversa.mx', 'Notificacion Amarradores.');
		
		
		$mail->addAddress("direccion@moversa.mx");// Name is optional
		$mail->addAddress("gerencia@moversa.mx");
		$mail->addAddress("admon@moversa.mx");
		$mail->addAddress("yeredy@moversa.mx");
		$mail->addAddress("martha@moversa.mx");
		$mail->addAddress("comercial@moversa.mx");
		//$mail->addBCC("whernandez@setiveracruz.com.mx");
		//$mail->addAddress("");// Name is optional	
		//$mail->addBCC("isc.ghernandez@gmail.com");
		$mail->addAttachment('notificacionesPdf/'.$vNombreArchivo.'.pdf');         // Add attachments	
		// Content
		$mail->isHTML(true);                                  // Set email format to HTML
		$mail->Subject = "Reporte de Servicios de Amarradores.";
		$mail->Body    = $pBodyMail;
		$mail->send();

		echo "Notificacion enviada correctamente...!!!";	
					
	} catch (Exception $e) {
		echo "Error al enviar la notificacion...!!!";
	}
?>
