<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Comparativo Ing. Inv. vs Conta.</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 "../../general.php";
include "../../token.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$itemini=$_POST["itemini"];
$itemfin=$_POST["itemfin"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$almacen=$_POST["almacen"];
$cuenta_almacen=$_POST["cuenta_almacen"];
$orden=$_POST["orden"];

$nro_col = 8;

?>
<body>
<?php

if ($almacen == 0) {
	$nom_almacen = 'Todos';
	$valor_almacen = '';
} else { 
	$resultalm = mysqli_query($link, "select fldName from invWarehouse where pkyWarehouse='$almacen';");
	while ($row = mysqli_fetch_array($resultalm)) {
		$nom_almacen = $row['fldName'];
	}
	mysqli_free_result($resultalm);
	$valor_almacen = ' d.pkyWarehouse = '. $almacen. ' and ';
}

if ($itemini == 0) { // if 2.0
	$nom_item_ini = 'Todos';
	$valor_item_ini = ' ';
	$nom_item_fin = 'Todos';
	$valor_item_fin = ' ';
} else {
	//nombre item
	$result2 = mysqli_query($link, "select pkyItem as cod_item, fldName as item, fldFactoryKey as cod_fabrica
							from
							invItem 
							where
							pkyItem = '$itemini' and
							sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($result2)) {
		$nom_item_ini = $itemini.' - '.$row['cod_fabrica'].' - '.$row['item'];
	}
	mysqli_free_result($result2);
	$valor_item_ini = ' g.pkyItem >= ' .$itemini. ' and ';
} //fin if 2.0

if ($itemfin == 0) { // if 2.1
	$nom_item_fin = 'Todos';
	$valor_item_fin = ' ';
} else {
	//nombre item
	$result2 = mysqli_query($link, "select pkyItem as cod_item, fldName as item, fldFactoryKey as cod_fabrica
							from
							invItem 
							where
							pkyItem = '$itemfin' and
							sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($result2)) {
		$nom_item_fin = $itemfin.' - '.$row['cod_fabrica'].' - '.$row['item'];
	}
	mysqli_free_result($result2);
	$valor_item_fin = ' g.pkyItem <= ' .$itemfin. ' and ';
} //fin if 2.1

if ($cuenta_almacen == 0) { // if 2.3.1
	$nom_cuenta_almacen = 'Todos';
	$valor_cuenta_almacen = ' ';
} else {
	//nombre de la cuenta contable del almacen
	$result2 = mysqli_query($link, "select pkyAccount, fldName 
							from actAccount 
							where 
							pkyAccount = '$cuenta_almacen' and
							sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($result2)) {
		$nom_cuenta_almacen = $row['fldName'];
	}
	mysqli_free_result($result2);
	$valor_cuenta_almacen = ' f.pkyAccount = ' .$cuenta_almacen. ' and ';
} //fin if 2.3.1

if ($orden == 0) { 
	$nom_orden = 'Fecha';
	$valor_orden = ' order by fecha ';
}
if ($orden == 1) {
	$nom_orden = 'Item';
	$valor_orden = ' order by item ';
} 
if ($orden == 2) {
	$nom_orden = 'Almacén';
	$valor_orden = ' order by almacen ';
} 
if ($orden == 3) {
	$nom_orden = 'Cuenta contable';
	$valor_orden = ' order by cuenta ';
}



//Inicio reporte 
if($rep == 9 && $tipo == 1) { // if 2

	$result2 = mysqli_query($link, "SELECT a.pkyProductionOrder AS cod_op, a.fldDocumentNumber AS nro_doc, a.fldDate AS fecha, a.codItemMovementOut AS cod_mov_out, a.codItemMovementIn AS cod_mov_in, a.codAccountEntry AS cod_asiento, b.codItem AS cod_item, b.fldQuantity AS cantidad, b.fldCost AS costo_u, b.fldExtraCost AS costo_extra, b.fldTotaCost AS costo_total, c.codTargetWarehouse AS cod_almacen, d.fldName AS almacen, e.codAccount AS cod_cuenta,f.fldName AS cuenta, e.fldDebit AS debe, g.fldName AS item, e.fldDebit - b.fldTotaCost AS diferencia
	FROM prdProductionOrder a, invItemMovementDetail b, invItemMovement c, invWarehouse d, actAccountEntryDetail e, actAccount f, invItem g
	WHERE 
	$valor_item_ini 
	$valor_item_fin 
	$valor_almacen 
	$valor_cuenta_almacen 
	a.fldDate >= '$fechaini' AND 
	a.fldDate <= '$fechafin' AND
	a.codItemMovementIn = b.codItemMovement AND 
	b.codItemMovement = c.pkyItemMovement AND 
	c.codTargetWarehouse = d.pkyWarehouse AND 
	a.codAccountEntry = e.codAccountEntry AND 
	e.codAccount = f.pkyAccount AND 
	d.codAccount = e.codAccount AND 
	b.codItem = g.pkyItem AND 
	b.fldTotaCost <> e.fldDebit AND 
	e.fldDebit <> 0 AND 
	a.sysEnabled = 1 AND a.sysAuthorized = 1 AND 
	b.sysEnabled = 1 AND b.sysAuthorized = 1 AND 
	e.sysEnabled = 1 AND e.sysAuthorized = 1
	$valor_orden;");
				
	?>
	<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>Comparativo Ing. Inv. vs Conta. </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="40%"><b>Item inicial: </b><?php echo $nom_item_ini; ?></TD>
			<TD width="30%"><b>Item final: </b><?php echo $nom_item_fin; ?></TD>
			<TD width="30%"><b>Almac&eacute;n: </b><?php echo $nom_almacen; ?></TD>
			</TR>
			<TR>
			<TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
			<TD><b>Cuenta contable: </b><?php echo $nom_cuenta_almacen; ?></TD>
			<TD><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
			</TR>
		</table>
	</td>

	</tr>
	</table>
	<br /> 
	<table border="1" align="center" cellpadding="2" width="100%">
		<thead>
			<TR> 
			<TD align="center"><b>Cod. Ord. Prod.</b></TD>
			<TD align="center"><b>Nro. Doc.</b></TD>
			<TD align="center"><b>Fecha</b></TD>
			<TD align="center"><b>Item</b></TD>
			<TD align="center"><b>Almacén</b></TD>
			<TD align="center"><b>Cuenta contable</b></TD>
			<TD align="center"><b>Cod. Mov. Ing.</b></TD>
			<TD align="center"><b>Cantidad</b></TD>
			<TD align="center"><b>Costo Ing. Alm.</b></TD>
			<TD align="center"><b>Cod. Asiento</b></TD>
			<TD align="center"><b>Monto Asiento</b></TD>
			<TD align="center"><b>Diferencia</b></TD>
			</TR>
		</thead>
		<tbody class="marcafila">
			<?php
			$color = 2;
			$suma_total_ing_inv = 0;
			$suma_total_conta = 0;
			$suma_total_dif = 0;			
			while ($row = mysqli_fetch_array($result2)) { //while
				$cod_op = $row['cod_op'];
				$cod_mov_in = $row['cod_mov_in'];
				$cod_asiento = $row['cod_asiento'];
				if($color % 2 == 0) { //if color
					printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleProduction/VoucherProductionOrder/index.php?pkyProductionOrder=$cod_op&user=$usuario&token=$token' title='Nota de Orden de Producci&oacute;n'>%s</a></td>", $row['cod_op']);//cod op
				} else {
					printf("<tr><td align='center'><a href='../../ModuleProduction/VoucherProductionOrder/index.php?pkyProductionOrder=$cod_op&user=$usuario&token=$token' title='Nota de Orden de Producci&oacute;n'>%s</a></td>", $row['cod_op']);//cod op
				} //cierra if color
				
				printf("<td align='left'>%s</td>", $row['nro_doc']);
				printf("<td align='center'>%s</td>", $row['fecha']);
				printf("<td align='left'>%s - %s</td>", $row['cod_item'], $row['item']);
				printf("<td align='left'>%s - %s</td>", $row['cod_almacen'], $row['almacen']);
				printf("<td align='left'>%s - %s</td>", $row['cod_cuenta'], $row['cuenta']);
				printf("<td align='center'><a href='../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement=$cod_mov_in&showBatch=0&printAccountEntry=0&user=$usuario&token=$token' title='Nota de Movimiento de Mercader&iacute;a'>%s</a></td>", $row['cod_mov_in']);
				printf("<td align='right'>%s</td>", number_format($row['cantidad'],$n_cantidad));
				printf("<td align='right'>%s</td>", number_format($row['costo_total'],10));
				printf("<td align='center'><a href='../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=$cod_asiento&showCC=0&user=$usuario&token=$token' title='Asiento Contable'>%s</a></td>", $row['cod_asiento']);
				printf("<td align='right'>%s</td>", number_format($row['debe'], 10));
				printf("<td align='right'>%s</td>", number_format($row['diferencia'], 10));
				echo '</tr>'; 
				
				$suma_total_ing_inv = $suma_total_ing_inv + $row['costo_total'];
				$suma_total_conta = $suma_total_conta + $row['debe'];
				$suma_total_dif = $suma_total_dif + $row['diferencia'];
				$color++;

			} //cierra while
							
			?>
			<tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_col; ?>"><b>Total</b></td>
			<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_ing_inv, 10)); ?></td>
			<td>&nbsp;</td>
			<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_conta, 10)); ?></td>
			<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_dif, 10)); ?></td>
			</tr>
		</tbody>
	</table>

	<?php

} //fin if 3
?>

</body>
</html>