<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Nota de Orden de Compra</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php
include "../../conn.php";
include "../../class.nroenletraver2.php";
include "../../general.php";

//index.php?pkyPurchaseOrder=46&user=Luigi
$rep=$_GET["rep"];
$tipo=$_GET["tipo"];

$cod_orden = $_GET["pkyPurchaseOrder"];
//$mostrar_asiento_cc = $_GET["showCC"];
$usuario = $_GET["user"];

//$n_decimales = 6;
?>
<body>

<?php

// Inicio impresion Orden de Compra

$result = mysqli_query($link, "select a.pkyPurchaseOrder as cod_orden, a.fldDate as fecha, 
a.fldNumber as nro_doc, b.fldName as estado, 
a.codSupplier as cod_prove, c.fldName as proveedor, 
a.codAddress as cod_dir, a.codFixedContactPhone as cod_tel, 
a.codMovilContactPhone as cod_movil, d.fldName as almacen, 
a.codCurrency as cod_moneda, e.fldName as moneda, 
a.codExchangeRate as cod_tc, a.fldGenAccountPayable as gen_cxp, 
a.fldGrossTotal as total_bruto, a.fldTotalDiscountPercent as total_desc_porciento, 
a.fldGlobalDiscountPercent as total_desc_porciento_global, 
a.fldNetTotal as total_neto, a.codAccountPayable as cod_cxp, 
a.codAccountEntry as cod_asiento, f.fldName as dcto, 
a.txtDescription as glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto
from
pchPurchaseOrder a, pchPurchaseOrderStatus b, 
agnCompany c, invWarehouse d, actCurrency e, 
txsDocumentType f, accGroup g, srvContext s 
where
a.pkyPurchaseOrder = '$cod_orden' and 
a.codStatus = b.pkyPurchaseOrderStatus and 
a.codSupplier = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codCurrency = e.pkyCurrency and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.sysContext = s.pkyContext and 
a.codDocumentType = f.pkyDocumentType;");
	while ($row = mysqli_fetch_array($result)) {
		$cod_orden = $row['cod_orden'];
		$fecha_orden = $row['fecha'];
		$nro_doc = $row['nro_doc'];
		$estado = $row['estado'];
		$cod_prove = $row['cod_prove'];
		$proveedor = $row['proveedor'];
		$cod_dir = $row['cod_dir'];
		$cod_tel = $row['cod_tel'];
		$cod_movil = $row['cod_movil'];
		$almacen = $row['almacen'];
		$cod_moneda = $row['cod_moneda'];
		$moneda = $row['moneda'];
		$cod_tc = $row['cod_tc'];
		$gen_cxp = $row['gen_cxp'];
		$total_bruto = $row['total_bruto'];
		$total_des_porciento = $row['total_desc_porciento'];
		$total_des_porciento_global = $row['total_desc_porciento_global'];
		$total_neto = $row['total_neto'];
		$cod_cxp = $row['cod_cxp'];
		$cod_asiento = $row['cod_asiento'];
		$documento = $row['dcto'];
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$usuario_reg = $row['usuario_reg'];
		$enabled = $row['enabled'];
		$authorized = $row['authorized'];
		$cod_contexto = $row['cod_contexto'];
		$contexto = $row['contexto'];
	}
	mysqli_free_result($result);
	
$resultdir = mysqli_query($link, "select a.fldAddress as direccion, b.fldName as tipo_dir
from
agnCompanyAddress a, agnAddressType b
where
a.pkyCompanyAddress = '$cod_dir' and 
a.codType = b.pkyAddressType;");
while ($row = mysqli_fetch_array($resultdir)) {
	$direccion = $row['direccion'];
	$tipo_dir = $row['tipo_dir'];
}
mysqli_free_result($resultdir);

$resulttel = mysqli_query($link, "select a.fldPhone as telefono, b.fldName as tipo_tel
from
agnCompanyPhone a, agnPhoneType b
where
a.pkyCompanyPhone = '$cod_tel' and 
a.codType = b.pkyPhoneType;");
while ($row = mysqli_fetch_array($resulttel)) {
	$telef = $row['telefono'];
	$tipo_tel = $row['tipo_tel'];
}
mysqli_free_result($resulttel);

$resultmov = mysqli_query($link, "select a.fldPhone as telefono, b.fldName as tipo_tel
from
agnCompanyPhone a, agnPhoneType b
where
a.pkyCompanyPhone = '$cod_movil' and 
a.codType = b.pkyPhoneType;");
while ($row = mysqli_fetch_array($resultmov)) {
	$celu = $row['telefono'];
	$tipo_cel = $row['tipo_tel'];
}
mysqli_free_result($resultmov);

$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$cod_tc';");
while ($row = mysqli_fetch_array($resulttc)) {
	$tc_fecha = $row['fecha'];
	$tc_hora = $row['hora'];
}
mysqli_free_result($resulttc);

$resulttcdet = mysqli_query($link, "select a.pkyExchangeRateDetail as cod_tc_det, a.codBaseCurrency as cod_mon_target, b.fldName as mon_target, b.txtDescription as nom_mon_target, a.codTargetCurrency as cod_mon_base, c.fldName as mon_base, c.txtDescription as nom_mon_base, a.fldRate as tasa
from
actExchangeRateDetail a, actCurrency b, actCurrency c
where
a.codExchangeRate = '$cod_tc' and 
a.codBaseCurrency = '$cod_moneda_principal' and 
a.codBaseCurrency = b.pkyCurrency and 
a.codTargetCurrency = c.pkyCurrency 
order by cod_mon_target;");
$ma_tc = array();
$m = 1;
while ($row = mysqli_fetch_array($resulttcdet)) {
	$ma_tc [$m][1] = $row['mon_base'];
	$ma_tc [$m][2] = $row['mon_target'];
	$ma_tc [$m][3] = $row['tasa'];
	$m++;
}
mysqli_free_result($resulttcdet);
$fin_m = $m - 1;

if($gen_cxp == 0) { $gen_cxp_sn = 'No';}
if($gen_cxp == 1) { $gen_cxp_sn = 'Si';}

	$result2 = mysqli_query($link, "select a.pkyPurchaseOrderDetail as cod_orden_det, 
a.codItem as cod_item, b.fldName as item, a.fldQuantity as cantidad, a.fldPrice as precio, 
a.fldDiscount as descuento, a.fldTotalNetPrice as total, c.fldName as u_mayor, a.fldIsBonus as bono, a.fldStorageUnitCoefficient as coeficiente, a.fldPackagedItemWeight as peso_emb, a.fldPackagedItemVolume as vol_emb, b.fldFactoryKey as cod_fabrica
from
pchPurchaseOrderDetail a, invItem b, invStorageUnit c
where
a.codMaximumStorageUnit = c.pkyStorageUnit and 
a.codPurchaseOrder = '$cod_orden' and 
a.codItem = b.pkyItem 
order by item;");
	

?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Nota de Orden de Compra</b></font></center></p>

<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="25%"><b>Cod. Reg.: </b><?php echo $cod_orden; ?></TD>
        <TD width="25%"><b>Nro. Doc.: </b><?php echo $nro_doc; ?></TD>
        <TD width="25%"><b>Moneda: </b><?php echo $moneda; ?></TD>
        <TD width="25%"><b>Fecha de Reg.: </b><?php echo $fecha_orden; ?></TD>
        </TR>
        <TR>
        <TD><b>Proveedor: </b><?php echo $proveedor; ?></TD>
        <TD><b>Direcci&oacute;n: </b><?php echo $direccion; ?> (<?php echo $tipo_dir; ?>)</TD>
        <TD><b>Tel&eacute;fono: </b><?php echo $telef; ?> (<?php echo $tipo_tel; ?>)</TD>
        <TD><b>Celular: </b><?php echo $celu; ?> (<?php echo $tipo_cel; ?>)</TD>
        </TR>
        <TR>
        <TD><b>Asiento: </b><a href="../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=<?php echo $cod_asiento; ?>&showCC=<?php echo $mostrar_asiento_cc; ?>&user=<?php echo $usuario; ?>" title="Asiento Contable con Centro de Costo"><?php echo $cod_asiento; ?></a></TD>
        <!--<TD><b>Genera CxP: </b><?php echo $gen_cxp_sn; ?></TD>-->
        <TD><b>Cuenta x Pagar (<?php echo $gen_cxp_sn; ?>): </b><a href="../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=<?php echo $cod_cxp; ?>&user=<?php echo $usuario; ?>" title="Nota de Cuenta por Pagar"><?php echo $cod_cxp; ?></a></TD>
        <TD><b>Documento: </b><?php echo $documento; ?></TD>
        <TD><b>Estado: </b><?php echo $estado; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>Almac&eacute;n: </b><?php echo $almacen; ?></TD>
        <TD colspan="2"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
        </TR>
        <TR>
        <TD><b>Desc. Parcial (%): </b><?php echo number_format($total_des_porciento,$n_descuento); ?>%</TD>
        <TD><b>Desc. Parcial: </b><?php echo number_format($total_bruto * $total_des_porciento / 100,$n_descuento); ?></TD>
        <TD><b>Desc. Global (%): </b><?php echo number_format($total_des_porciento_global,$n_descuento); ?>%</TD>
        <TD><b>Desc. Global: </b><?php echo number_format($total_bruto * $total_des_porciento_global / 100,$n_descuento); ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>Total Bruto: </b><?php echo number_format($total_bruto, $n_decimales); ?></TD>
        <TD colspan="2"><b>Total Neto: </b><?php echo number_format($total_neto, $n_decimales); ?></TD>
        </TR>
        <TR>
        <TD colspan="4"><b>Glosa: </b><?php echo $glosa; ?></TD>
        </tr>
    </table>

</td>
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $cod_tc; ?>&user=<?php echo $usuario; ?>" title="Tipo de Cambio"><?php echo $cod_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
</tr>
</table>
<br />
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
    <TD align="center"><b>Cod. Item</b></TD>
    <TD align="center"><b>Cod. Fab.</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Bono</b></TD>
    <TD align="center"><b>Uni. May.</b></TD>
    <TD align="center"><b>Coef.</b></TD>
    <TD align="center"><b>Peso Emb.</b></TD>
    <TD align="center"><b>Vol. Emb.</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Precio</b></TD>
    <TD align="center"><b>Desc. %</b></TD>
    <TD align="center"><b>Desc.</b></TD>
    <TD align="center"><b>Total</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_cant = 0;
	$suma_desc = 0;
	$suma_desc_efec = 0;
	$suma_total = 0;
	while ($row = mysqli_fetch_array($result2)) { //while
	
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['cod_item']);//cod item
		} else {
		printf("<tr><td align='center'>%s</td>", $row['cod_item']);//cod item
		} //cierra if color
		printf("<td align='center'>%s</td>", $row['cod_fabrica']);//cod fabrica
		printf("<td>%s</td>", $row['item']);//nombre item
		if($row['bono'] == 0) { //if bono
			printf("<td align='center'>%s</td>", 'NO');//es bono?
		} else {
			printf("<td align='center'>%s</td>", 'SI');//es bono?
		}//fin if bono
		printf("<td align='left'>%s</td>", $row['u_mayor']);//uni may
		printf("<td align='right'>%s</td>", number_format($row['coeficiente'],0));//coeficiente
		printf("<td align='right'>%s</td>", number_format($row['peso_emb'],$n_pesovol));//peso
		printf("<td align='right'>%s</td>", number_format($row['vol_emb'],$n_pesovol));//volumen
		printf("<td align='right'>%s</td>", number_format($row['cantidad'],$n_cantidad));//cant
		printf("<td align='right'>%s</td>", number_format($row['precio'], $n_decimales));//precio
		printf("<td align='right'>%s</td>", number_format($row['descuento'],$n_descuento));//descuento
		$descuento_efectivo = $row['cantidad'] * $row['precio'] * $row['descuento'] / 100;
		printf("<td align='right'>%s</td>", number_format($descuento_efectivo,$n_descuento));//descuento efectivo
		printf("<td align='right'>%s</td></tr>", number_format($row['total'], $n_decimales));//total
		
		$suma_cant = $suma_cant + $row['cantidad'];
		$suma_desc = $suma_desc + $row['descuento'];
		$suma_desc_efec = $suma_desc_efec + $descuento_efectivo;
		$suma_total = $suma_total + $row['total'];
		$color++;
	}//fin while
	mysqli_free_result($result2);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_total,2);
	
	$mostrar_letras = ucfirst($letras->getLetra());
	
	$busca_espacio = substr($mostrar_letras,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras = substr($mostrar_letras,-5);
		$mostrar_letras = substr($mostrar_letras,0,-5);
		$mostrar_letras = $mostrar_letras .'0'. $fin_letras;
	}
	$resultmoneda = mysqli_query($link, "select txtDescription as nom_moneda
	from actCurrency
	where
	pkyCurrency = '$cod_moneda';");
	while ($row = mysqli_fetch_array($resultmoneda)) {
		$nom_moneda_letras_tot = $row['nom_moneda'];
	}
	mysqli_free_result($resultmoneda);
	?>
	<tr BGCOLOR='#ffcccc'><td colspan="8"><b>Total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras; ?> <?php echo $nom_moneda_letras_tot; ?></b></td>
    <td align="right"><b><?php echo number_format($suma_cant,$n_cantidad); ?></b></td>
    <td>&nbsp;</td>
    <td align="right"><b><?php echo number_format($suma_desc,$n_descuento); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_desc_efec,$n_descuento); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_total, $n_decimales); ?></b></td>
    </tr>
</tbody>    
</table>
<br />
<?php
    if($enabled == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO ANULADO</font></td></tr></table>");
			}
	if($authorized == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}

?>

<br /><br /><br /><br /><br />
<table border="0" align="center" cellpadding="2" width="90%">
<tr>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
</tr><tr>
<td align="center">Elaborado por: <?php echo $usuario_reg; ?></td>
<td align="center"><?php echo $firma_contabilidad; ?></td>
<td align="center"><?php echo $firma_gerencia; ?></td>
<td align="center"><?php echo $firma_interesado; ?></td>
</tr><tr>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
</tr><tr>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
</tr>
</table>

<?php


?>
</body>
</html>
