<?php
set_time_limit(300);
include_once('conex.php');

$nrodoc = "";
if (isset($_REQUEST['p']))
	$nrodoc = $_REQUEST['p'];

$XvFileExportTmp = "ped_" . $nrodoc . ".pdf";

$SqlTmp = "SELECT m_pedido.num_ped, m_pedido.cdg_vend, m_pedido.cdg_cpag, m_pedido.cdg_mon, m_pedido.fec_ped,
		m_pedido.num_ocom, m_pedido.imp_stot, m_pedido.imp_tigv, m_pedido.imp_tdct, m_pedido.imp_ttot,
		m_pedido.por_tdct, m_pedido.por_tigv, m_pedido.obs_ped, m_pedido.swt_ped, m_pedido.ruc_cli, m_pedido.swt_cot,
		m_pedido.fec_anul, m_pedido.swt_ptv, m_pedido.ori_area, m_pedido.cdg_area, m_pedido.num_cot, m_pedido.cdg_usr,
		m_pedido.cdg_prio, m_pedido.imp_aju, m_pedido.cdg_loc, m_pedido.swt_dist, m_pedido.ref_ped, m_pedido.swt_prod,
		m_pedido.imp_tisc, m_pedido.num_nsal, d_pedido.obs_pprd, d_pedido.cdg_prod, d_pedido.pre_pprd, d_pedido.dct_fic,
		d_pedido.dct_pprd, d_pedido.imp_tprd, d_pedido.can_pprd, d_pedido.imp_igv, d_pedido.pre_igv, m_client.swt_alt,
		m_client.cdg_alt, m_client.des_cli, m_client.dir_cli, m_client.tel_cli, m_client.fax_cli, m_client.rep_cli, m_produc.des_prod,
		m_vended.des_vend, m_produc.cdg_prv, moneda.des_item AS des_mon, moneda.abr_item AS abr_mon,
		undmed.abr_item AS abr_umed, conpag.des_item AS des_cpag, distrito.abr_item AS abr_dist, priorid.des_item AS des_prio,
		m_pedido.fec_ent, d_pedido.imp_iva, d_pedido.num_item, m_pedido.hra_ped, m_pedido.num_mesa, m_pedido.num_pers,
		d_pedido.imp_prod, d_pedido.swt_impr, M_produc.abr_prod, m_pedido.cnt_ped, m_pedido.tip_ptv, m_pedido.por_carg,
		m_pedido.val_carg, m_pedido.cdg_caja
	FROM m_pedido, d_pedido, m_client, d_tablas undmed, d_tablas conpag, d_tablas moneda, d_tablas distrito, d_tablas priorid, m_produc, m_vended
	WHERE undmed.cdg_tab = 'UNM'
		AND conpag.cdg_tab = 'CPG'
		AND moneda.cdg_tab = 'MON'
		AND priorid.cdg_tab = 'PRI'
		AND distrito.cdg_tab = 'DST'
		AND m_pedido.num_ped = '$nrodoc'
		AND m_pedido.num_ped = d_pedido.num_ped
		AND m_pedido.ruc_cli = m_client.ruc_cli
		AND m_pedido.cdg_vend = m_vended.cdg_vend
		AND m_pedido.cdg_cpag = conpag.num_item
		AND m_pedido.cdg_mon = moneda.num_item
		AND m_pedido.cdg_prio = priorid.num_item
		AND d_pedido.cdg_prod = m_produc.cdg_prod
		AND m_client.cdg_udis = distrito.num_item
		AND m_produc.cdg_umed = undmed.num_item
	ORDER BY d_pedido.num_item";

$XvSubTotal = 0;
$XvIGV = 0;
$Observa = "";
$XvTotPed = 0;
$XvImpTdct = 0;
$XvPorTdCt = 0;
$XvPorTigv = 0;

$XqTabla = pg_query($SqlTmp) or die('La consulta fallo (Pedido): ' . pg_last_error());
$XvCantFila = pg_num_rows($XqTabla);
if ($XvCantFila > 0){
	$pHTMLTmp = '<style type="text/css">
	</style>
	<page backtop="5mm" backbottom="5mm" backleft="3mm" backright="5mm" style="font-size: 10pt">
	<table style="width:100%; border-collapse: collapse;">
		<tr>
			<td style="width:100%; font-size:14px; font-weight:bold;">NOMBRE DE LA EMPRESA, S.A.</td>
		</tr>
		<tr>
			<td style="width:100%">RUC: 2020202020</td>
		</tr>
		<tr>
			<td style="width:100%">Dirección: av. caasd calle sdlk sdñlk sdñlk asdñlf añle rawñelr </td>
		</tr>
		<tr>
			<td style="width:100%">Teléfono: 920545487 - Email: asdasdsa@sdsdfd.com</td>
		</tr>

	</table>
	<p>&nbsp;</p>';	
	$i = 0;
	while ($data = pg_fetch_array($XqTabla)){
		if ($i == 0){
			$XvSubTotal = $data["imp_stot"];
			$XvIGV = $data["imp_tigv"];
			$Observa = $data["obs_ped"];
			$XvTotPed = $data["imp_ttot"];
			$XvImpTdct = $data["imp_tdct"];
			$XvPorTdCt = $data["por_tdct"];
			$XvPorTigv = $data["por_tigv"];
			$pHTMLTmp = $pHTMLTmp . 
				'<table style="width:100%; border-collapse: collapse;">
				  <tr>
					<td style="width:50%"><b>Nombre / Razon Social:</b> <br>'.$data["des_cli"].' </td>
					<td style="width:50%;text-align:right;">Pedido: <b>'.$data["num_ped"].'</b></td>
				  </tr>
				  <tr>
					<td>RUC:&nbsp;'. $data["cdg_alt"] . '</td>
					<td style="text-align:right;">Fecha:&nbsp;'. $data["fec_ped"] . '</td>
				  </tr>
				  <tr>
					<td>Dirección:&nbsp;'. $data["dir_cli"] . '</td>
					<td style="text-align:right;">Vendedor:&nbsp;'. $data["des_vend"] . '</td>
				  </tr>
				  <tr>
					<td>Cond. Pago:&nbsp;'. $data["des_cpag"] . '</td>
					<td style="text-align:right;">Moneda:&nbsp;'. $data["des_mon"] . '</td>
				  </tr>
				  <tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				  </tr>
				</table>

				<table style="width:100%; border-collapse: collapse;">
				  <tr align="center" bgcolor="#999999">
					<th style="width:10%; height:34px;">C&oacute;digo</th>
					<th style="width:30%; height:34px;">Descripci&oacute;n</th>
					<th style="width:15%; height:34px; text-align:right;">Cantidad&nbsp;</th>
					<th style="width:15%; height:34px; text-align:right;">Precio&nbsp;</th>
					<th style="width:7%; height:34px; text-align:right;">% DCT.&nbsp;</th>
					<th style="width:7%; height:34px; text-align:right;">% FIC.&nbsp;</th>
					<th style="width:16%; height:34px; text-align:right;">Importe&nbsp;</th>
				  </tr>
				  <tbody>';
			$i++;
		}
		$pHTMLTmp = $pHTMLTmp . '<tr style="font-size:10px;">
			<td style="width:10%; height:20px;">'.$data["cdg_prv"].'</td>
			<td style="width:30%; height:20px;">'.$data["des_prod"].'</td>
			<td style="width:15%; text-align:right;height:20px;">' . number_format($data["can_pprd"],2) . '&nbsp;</td>
			<td style="width:15%; text-align:right;height:20px;">' . number_format($data["pre_pprd"],2) . '&nbsp;</td>
			<td style="width:7%; height:20px; text-align:right;">' . number_format($data["dct_pprd"],2) . '&nbsp;</td>
			<td style="width:7%; height:20px; text-align:right;">' . number_format($data["dct_fic"],2) . '&nbsp;</td>
			<td style="width:16%; text-align:right;height:20px;">' . number_format($data["imp_tprd"],2) . '&nbsp;</td>
		</tr>';
	}
	$pHTMLTmp = $pHTMLTmp . '</tbody></table>
		<page_footer>
			<table style="width:100%; border-collapse: collapse;">
			  <tr>
				<td style="width:50%;">Observación<br>
				'.$Observa.'
				</td>
				<td style="width:50%;">
				
				<table style="width:100%; border-collapse: collapse;">
				  <tr>
					<td style="width:50%; text-align:right; height:20px;">Sub-Total:</td>
					<td style="width:50%; text-align:right; height:20px;">'.number_format( $XvSubTotal, 2 ) .'&nbsp;</td>
				  </tr>
				  <tr>
					<td style="width:50%; text-align:right; height:20px;">'.$XvPorTdCt.'% Descuento:</td>
					<td style="width:50%; text-align:right; height:20px;">'.number_format( $XvImpTdct, 2 ) .'&nbsp;</td>
				  </tr>
				  <tr>
					<td style="width:50%; text-align:right; height:20px;">IGV: ' . number_format( $XvPorTigv ) . '%</td>
					<td style="width:50%; text-align:right; height:20px;">' . number_format( $XvIGV, 2 ) . '&nbsp;</td>
				  </tr>
				  <tr>
					<td style="width:50%; text-align:right; height:20px;">Total:</td>
					<td style="width:50%; text-align:right; height:20px;">' . number_format( $XvTotPed, 2 ) . '&nbsp;</td>
				  </tr>
				</table>
				
				</td>
			  </tr>
			</table>
		</page_footer>				
						
		</page>';
}else{
	$pHTMLTmp = "El Documento: (". $nrodoc . ") no existe.";
}

pg_close($con);

require_once('html2fpdf/html2pdf.class.php');
try{
	$html2pdf = new HTML2PDF("P", "A4", 'es',  true, 'UTF-8',array(12, 8, 8, 8)); 
	$html2pdf->pdf->SetDisplayMode('fullpage');
	$html2pdf->writeHTML($pHTMLTmp);
	$html2pdf->Output($XvFileExportTmp, "I");
}catch(HTML2PDF_exception $e) {
	echo $e;
	exit;
}

/*
try {
    //ob_start();
    $html2pdf = new Html2Pdf('P', 'A4', 'fr');
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML($html);
    $html2pdf->output('example01.pdf');
} catch (Html2PdfException $e) {
    $html2pdf->clean();
    $formatter = new ExceptionFormatter($e);
    echo $formatter->getHtmlMessage();
}
*/
?>