<?php
	ini_set('display_errors', '0');
	//session_start();
	//$_SESSION['num_identificacion']= '9999999999';

require_once('../DOM/dompdf_config.inc.php');
//if(strlen($reg['last_name'])>0){
	
if(!ini_get('allow_url_fopen')){
	echo "Es necesario activar la directiva allow_url_fopen del php.ini";
	exit;
}



	require_once($_SERVER['DOCUMENT_ROOT'].'/model/clsDocumento.php');
	$objDocumento = new clsDocumento();
	$reg = $objDocumento->MET_pendientes();
	//print_r($reg);
	unset($objDocumento);

	foreach($reg as $r)
	{
		$tipo_txt='factura';
		if($r['tip_documento']=='C'){
			$tipo_txt='nota de credito';
		}
	// http://192.168.1.208/mail/index.php?id_form=025-002-000014935&id_cliente=0600452940&cliente=CADENA  MARIA PIEDAD&tipo=F&mail=FVILLACISC@HOTMAIL.COM&tipo_txt=factura&anio=2015
		/*$html = file_get_contents("http://192.168.1.208/mail/index.php?id_form=".$r['sri_documento']."&id_cliente=".$r['num_identificacion']."&cliente=".$r['nom_cliente']."&anio=".
					$r['num_anio']."&tipo=".$r['tip_documento']."&mail=".$r['des_email']."&tipo_txt=".$tipo_txt);*/
	//	print_r($r);
		echo '<br>';




		
		$id_form = $r['sri_documento'];
		$cliente = $r['nom_cliente'];
		$tipo = $r['tip_documento'];
		$tipo_txt = $tipo_txt;
		$anio =  $r['num_anio'];
		$id_cliente =  $r['num_identificacion'];
		$dir_mail =  $r['des_email'];

		//echo $id_form;//exit;

		if($id_form=='' || $cliente=='' || $tipo=='' || $anio=='' ||$tipo_txt==''){
			echo 'Faltan datos';
			exit;
		}
		
		//		$html = file_get_contents("http://".$_SERVER['HTTP_HOST']."/asa/index.php?c=".$id_form);
		//		$html = file_get_contents("http://192.168.1.208/form/plantilla_factura.php?doc=001-008-000011813&id_cliente=0923256168&num_anio=2015&tip_consulta=F");	
				
				if($tipo=='F'){
					$html = file_get_contents("http://192.168.1.208/form/plantilla_factura.php?doc=".$id_form."&id_cliente=".$id_cliente."&num_anio=".$anio."&tip_consulta=".$tipo);	
				}else{
					$html = file_get_contents("http://192.168.1.208/form/plantilla_notacredito.php?doc=".$id_form."&id_cliente=".$id_cliente."&num_anio=".$anio."&tip_consulta=".$tipo);	
				}

	//echo $html;exit;
				if(strlen($html)>0){
					// Obtenemos el código html de la página web que nos interesa
					$dompdf = new DOMPDF();
					$dompdf->set_paper("A4", "portrait"); 
					// Creamos una instancia a la clase
					$dompdf->load_html(utf8_decode($html));
					$dompdf->render();
					$pdf = $dompdf->output();
					file_put_contents("../files/".$id_form.''.".pdf", $pdf);
				}
		?>
		<?php

			$html = file_get_contents("http://192.168.1.208/form/generar_xml_mail.php?doc=".$id_form."&id_cliente=".$id_cliente."&num_anio=".$anio."&tip_consulta=".$tipo);	

			require_once('../script/mail/class.phpmailer.php');

			$mail             = new PHPMailer();
			$mail->IsSMTP();                           // telling the class to use SMTP
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->Host       = "smtpout.secureserver.net"; // set the SMTP server
			$mail->Port       = 25;                    // set the SMTP port
//			$mail->SMTPSecure = "ssl";
//			$mail->SMTPDebug  = true;
//print_r([$_GET]);exit;

			$mail->Username   = "mail".$_GET['id']."@gloriasaltos.com"; // SMTP account username
//echo $mail->Username;exit;
			$mail->Password   = "mail2k152k16";        // SMTP account password
/*
			if(!isset($_GET['id'])){
				$mail->Username   = "mail0@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==0){
				$mail->Username   = "mail0@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==1){
				$mail->Username   = "mail1@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==2){
				$mail->Username   = "mail2@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==3){
				$mail->Username   = "mail3@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==4){
				$mail->Username   = "mail4@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==5){
				$mail->Username   = "mail5@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==6){
				$mail->Username   = "mail6@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==7){
				$mail->Username   = "mail7@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==8){
				$mail->Username   = "mail8@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
			if($_GET['id']==9){
				$mail->Username   = "mail9@gloriasaltos.com"; // SMTP account username
				$mail->Password   = "mail2k152k16";        // SMTP account password
			}
*/
			$body             = 'Estimado cliente: <b>'.$cliente.'</b> <br><br>Le informamos que se encuentra generada y autorizada por el SRI la '.$tipo_txt.' No. '.$id_form.'<br><br>
			Le recordamos que tambi&eacute;n puede encontrarla en <a href="http://efactura.gloriasaltos.com/">efactura.gloriasaltos.com</a><br><br>Saludos Cordiales,<br><br>
			Gloria Saltos<br>Belleza - Spa - Peluquer&iacute;a';
	
			$body             = eregi_replace("[\]",'',$body);
			$mail->AddReplyTo("facturacion@gloriasaltos.com");
			$mail->SetFrom('facturacion@gloriasaltos.com');
		//	$address = "noreply@asa.com";
		/*direcciones*/
			$array = explode("|", $dir_mail);
			foreach($array as $correo)
			{
				$mail->AddAddress($correo);
			}
		/*direcciones*/
		//	$mail->AddBCC('oeramos@gloriasaltos.com');
			//$mail->AddBCC('documentoselectronicos@gloriasaltos.com');
			$mail->AddBCC('facturacion@gloriasaltos.com');
		//	$mail->AddBCC('oeramos@gloriasaltos.com');
			//$mail->AddAddress('gerente@gloriasaltos.com');
			$mail->Subject    = "Gloria Saltos - Nuevo Documento generado ".$id_form;
			$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
			$mail->MsgHTML($body);
			$mail->AddAttachment("../files/".$id_form.''.".pdf");      // attachment
			$mail->AddAttachment("../files/".$id_form.''.".xml");      // attachment
			//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
			if(!$mail->Send()) {
//			  echo "Mailer Error: " . $mail->ErrorInfo." DOC: ".$id_form;
			  echo "" . $mail->ErrorInfo;
			  $pos = strrpos($mail->ErrorInfo, 'Relay quota');
//echo $pos;exit;
				if($pos>0){
					exit;
				}

//exit;
			} else {
			  echo "Message sent! DOC: ".$id_form;
				//require($_SERVER['DOCUMENT_ROOT'].'/model/clsDocumento.php');
				$objDocumento = new clsDocumento();
				$reg = $objDocumento->MET_notificada($tipo, $anio, $id_form, $id_cliente);
				unset($objDocumento);
			}

	}
?>
