<?php include('casa/setup.casa.php');
include('inc_scripts.php');
$title = 'Resumen de Ventas realizadas';

$fecha = DataForms::GET('id');

echo "<title>".AppName.' - '.$title."</title>";
$SucCodigo = SucursalUsuario($USID);

function SucursalUsuario($us_codigo){
  $Usuario = new DB();
  $sqlUsuario = "SELECT * FROM rol_usuarios
  WHERE us_id = '$us_codigo';";
  $Usuario->dbSQL($sqlUsuario);
  $Usuario->next();
  $sucursal = $Usuario->dbField('ubica_codigo');

  return $sucursal;
}
?>
<style>
th{background:#156E10;
color:white;
text-align:center;
text-transform:uppercase;}	
</style>
</head>
<body>
	<div class="wrapper">		
	<?php include('inc_menu.php');?>
		<div class="main-panel">
			<div class="content">
				
			<div class="page-inner">
					
					<div class="row">						
						<div class="col-md-12">
							<div class="card">
								<div class="card-header">
									<div class="d-flex align-items-center">
										<h3 class="card-title"><?php echo $title?></h3>																																									
									</div>
								</div>
								
								<div class="card-body">		
																									
									<div class="table-responsive">	
									<table class="display table table-striped table-hover" >
											<thead>
												<tr>				
													<th width="1%">N&ordm;</th>													
													<th>item</th>												
													<th>descripci&Oacute;n</th>
													<!-- th>cajas</th>
													<th>unidades</th -->													
													<th>precio unitario</th>
													<th>subtotal</th>
												</tr>
											</thead>											
											<tbody>									
									<?php
									$Ventas = new DB();
									$Productos = new DB();

									$sql = "SELECT * FROM ventas_cajeros c, ventas_productos p
									WHERE c.cajeros_usid = '$USID'
									AND c.ven_codigo = p.ven_codigo
									AND c.ent_fechaentrega LIKE('".$fecha."%');";
									
									$Ventas->dbSQL($sql);
									$html='';
									$i=0;
									$suma = 0;
									while($Ventas->next()){
										$i++;
										$ven_precio = $Ventas->dbField('ven_precio');
										$prod_codigo = $Ventas->dbField('prod_codigo'); 
										$ven_tipo = $Ventas->dbField('ven_tipo'); 
										$ven_cantidad = $Ventas->dbField('ven_cantidad'); 
										$ven_precio = $Ventas->dbField('ven_precio'); 

										$sqlProd = "SELECT * FROM inventarios_productos
										WHERE prod_codigo = '$prod_codigo';";
										$Productos->dbSQL($sqlProd);
										$Productos->next();
										$prod_item = $Productos->dbField('prod_item');
										$prod_nombre = utf8_encode($Productos->dbField('prod_nombre'));

										$suma = $suma + $ven_precio;

										$html.='<tr>';										
										$html.='<td align=center>'.$i.'</td>';							
										$html.='<td align=center>'.$prod_item.'</td>';									
										$html.='<td>'.$prod_nombre.'</td>';							
										//$html.='<td>'.$ven_tipo.'</td>';							
										//$html.='<td>'.$ven_cantidad.'</td>';							
										$html.='<td align=center>'.number_format($ven_precio,2).'</td>';								
										$html.='<td align=right>'.number_format($ven_precio,2).'</td>';							
										$html.='</tr>';
									}
									$html.='<tr>
										<td colspan=3 align=right></td>
										<td align=center><h2>TOTAL</h2></td>
										<td align=right><h2>'.number_format($suma,2).'</h2></td></tr>';
									echo $html;
									?>	</tbody>																								
									</table>
									
									</div>
								
									<div class="d-flex align-items-center">
										<h3 class="card-title">Caja Chica y Gastos menores</h3>																																									
									</div>
																									
									<div class="table-responsive">	
									<table class="display table table-striped table-hover" >
									<thead>
												<tr>							
													<th>N&ordm;</th>
													<th>detalles</th>
													<th>fecha</th>
													<th>monto</th>
												</tr>
											</thead>											
											<tbody>
											<?php
											$sql = "SELECT * FROM inventarios_cajachica 
											WHERE caja_fecha LIKE '".$fecha."%'
											AND us_codigo = '$USID';";	
											$db= new DB();
											$db->dbSQL($sql);
											$html='';
											$i=0;
											$suma2=0;
											while($db->next()){						
											$i++;																																								
												$caja_detalle = $db->dbField('caja_detalle');
												$caja_importe = $db->dbField('caja_importe');
												$caja_fecha	 = $db->dbField('caja_fecha');

												$suma2 = $suma2 + $caja_importe; 
												$html.='<tr>';
													$html.='<td align=center>'.$i.'</td>';
													$html.='<td>'.$caja_detalle.'</td>';
													$html.='<td align=center>'.$caja_fecha.'</td>';
													$html.='<td align=right>'.number_format($caja_importe,2).'</td>';
												$html.='</tr>';											
												
											}//wend() 
											$html.='<tr>
											<td colspan=2 align=right></td>
											<td align=right><h2>TOTAL</h2></td>
											<td align=right><h2>'.number_format($suma2,2).'</h2></td></tr>';
											echo $html;
											?>	</tbody>																								
									</table>									
									</div>
									<div class="d-flex align-items-center">
										<h3 class="card-title">Apertura de Caja</h3>																																									
									</div>
																									
									<div class="table-responsive">	
									<table class="display table table-striped table-hover" >
									<thead>
												<tr>							
													<th>N&ordm;</th>
													<th>detalles</th>
													<th>monto</th>
												</tr>
											</thead>											
											<tbody>
											<?php
											$sql = "SELECT * FROM rol_usuarios_asistencia 
											WHERE asis_fecha = '$fecha'
											AND us_id = '$USID';";	
											$db= new DB();
											$db->dbSQL($sql);
											$html='';
											$i=0;
											$suma3=0;
											while($db->next()){						
											$i++;																																																				
												$asis_codigo = $db->dbField('asis_codigo');
												$asis_fecha = $db->dbField('asis_fecha');
												$asis_hora = $db->dbField('asis_hora');
												$asis_apertura = $db->dbField('asis_apertura');
												$asis_detalle = $db->dbField('asis_detalle');

												$suma3 = $suma3 + $asis_apertura; 
												if($asis_apertura!=0){
													$html.='<tr>';
														$html.='<td align=center>1</td>';
														$html.='<td align=center>'.$asis_detalle.'</td>';
														$html.='<td align=right>'.number_format($asis_apertura,2).'</td>';
													$html.='</tr>';																							
												}
												
											}//wend() 
											$html.='<tr>
											<td colspan=1 align=right></td>
											<td align=right><h2>TOTAL</h2></td>
											<td align=right><h2>'.number_format($suma3,2).'</h2></td></tr>';
											echo $html;
											?>	</tbody>																								
									</table>
								</div>

									<div class="d-flex align-items-center">
										<h3 class="card-title">Total Caja</h3>																																									
									</div>
																									
									<div class="table-responsive">	
									<table class="display table table-striped table-hover" >
									<thead>
												<tr>							
													<th>N&ordm;</th>
													<th>detalles</th>
													<th>monto</th>
												</tr>
											</thead>											
											<tbody>
											<?php											
												$html='<tr>';
													$html.='<td align=center>1</td>';
													$html.='<td align=left>Ventas</td>';
													$html.='<td align=right>'.number_format($suma,2).'</td>';
												$html.='</tr>';												
												$html.='<tr>';
													$html.='<td align=center>2</td>';
													$html.='<td align=left>Caja Chica y Gastos Menores</td>';
													$html.='<td align=right>'.number_format($suma2,2).'</td>';
												$html.='</tr>';												
												$html.='<tr>';
													$html.='<td align=center>3</td>';
													$html.='<td align=left>Monto Apertura de Caja</td>';
													$html.='<td align=right>'.number_format($suma3,2).'</td>';
												$html.='</tr>';																							

												
												$total = $suma + $suma3-$suma2;
											$html.='<tr>
											<td colspan=1 align=right></td>
											<td align=right><h2>TOTAL</h2></td>
											<td align=right><h2>'.number_format($total,2).'</h2></td></tr>';
											echo $html;
											
											?>	</tbody>																								
									</table><?php echo 'sumas: '.$suma .'<br>'. $suma3.'<br>'.$suma2;?>
								</div>
							</div>
						</div>
					</div>
				</div>
				
			</div>
			<?php include('inc_pie.php');?>
		</div>
		
		
	</div>
</body>
</html>