<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include("DF30000.inc");
include('DF80000.php');
?>
<?php
 
$tipmov='P';
$fec1=chfecha($_POST['fecha'],'d/m/Y','Y-m-d');
$dt_parame=mysqli_fetch_array(mysqli_query($con ,"select * from dvparame"));

if(isset($_POST['bo_bus'])){//Inicia Si buscar
	switch($_POST['sedoc']){//'V':Vencidos, 'XV': x vencer, 'A':ambos
		case 'V': $st_bus="select * from dvredocl where tip_mov='$tipmov' and tot_sal<>0 and fec_ven < '$fec1' order by codigo,fec_doc ";
		break;
		case 'XV': $st_bus="select * from dvredocl where tip_mov='$tipmov' and tot_sal<>0 and fec_ven >= '$fec1' order by codigo,fec_doc";
		break;
		case 'A': $st_bus="select * from dvredocl where tip_mov='$tipmov' and tot_sal<>0 order by codigo,fec_doc";
		break;
	}
	$sq_redocl=mysqli_query($con ,$st_bus);
}//Termina Si buscar
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>
<body>
<?php
 
$ayuda = 'DV4200A_PR.php';
include("DYAYUDA.php");
?>


<form name="form1" method="post" action="DW32000_PR.php">
  <div align="center">
  <table >
    <tr>
      <th colspan="2" >VENCIMIENTOS X FECHA </th>
      </tr>
    <tr>
      <td class="tabagre">Fecha corte: </td>
      <td class="inputcodigo"> <input name="fecha" type="text" class="emplea" id="fecha" size="15" 
					value="<?php
 if(!isset($_POST['sedoc'])){
									echo date("d/m/Y");
								}
								else{
									echo $_POST['fecha'];
								}
					?>"> 
				<input name="reset2" id="bot1" value=" ... " type="image" src="logos/cal4.jpg"/>
                <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fecha",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot1"      // ID of the button
    					}
  					);
				</script>&nbsp;</td>
    </tr>
    <tr>
      <td class="tabagre">Documentos:</td>
      <td class="inputcodigo"> 
        <select name="sedoc" id="sedoc">
          <option value="V" <?php
 if($_POST['sedoc']=='V') echo 'selected';?>>VENCIDOS</option>
          <option value="XV" <?php
 if($_POST['sedoc']=='XV') echo 'selected';?>>X VENCER</option>
          <option value="A" <?php
 if($_POST['sedoc']=='A') echo 'selected';?>>AMBOS</option>
        </select>
        </td>
    </tr>
    <tr>
      <td colspan="2"> 
        <div align="center">
          <input name="bo_bus" type="submit" id="bo_bus" value="Buscar">
          </div>
      </td>
      </tr>
  </table>
  </div>
</form>
<form name="frm1" method="post" action="">
<input name="fecha" type="hidden" id="fecha" value="<?php
 echo $_POST['fecha'];?>">
<input name="sedoc" type="hidden" id="sedoc" value="<?php
 echo $_POST['sedoc'];?>">
<input name="busqueda" type="hidden" id="busqueda" value="<?php
 echo $st_bus;?>">
<div align="center">
  <table >
    <tr>
      <th colspan="11" >
        <div align="left">
          <input name="bo_imp" type="button" id="bo_imp" value="Imprimir"  onClick="openpage('DW32000_IM.php','_self')">
          <input name="toExcel" type="button" id="toExcel" value="Exportar Excel" onClick="openpage('DW32000_IME.php','_self')">
        </div>     </th>
    </tr>
    <tr>
      <td nowrap class="tdagre1">Fecha Documento </td>
      <td nowrap class="tdagre1">N&uacute;mero Documento </td>
      <td nowrap class="tdagre1">Fecha Vencimiento </td>
      <td nowrap class="tdagre1">Dias Docto </td>
      <td nowrap class="tdagre1">Doctos Vencidos </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc1']";?> dias </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc2']";?> dias </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc3']";?> dias </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc4']";?> dias </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc5']";?> dias </td>
      <td nowrap class="tdagre1">Doctos a <?php
 echo "$dt_parame['dia_ven_cc6']";?> o m&aacute;s dias </td>
    </tr>
	<?php
 
	if(isset($_POST['bo_bus'])){
		$z=-1;
		$totales=NULL;
		$totgeneral=NULL;
		$diferencia=0;
		$b=0;
		
		while($dt_redocl=mysqli_fetch_array($sq_redocl)){
			if ($dt_redocl['tip_cam'] > 0 && $dt_redocl['moneda']=='2') $tipcambio=$dt_redocl['tip_cam'];
			else $tipcambio=1;
			$diferencia=daydiff($dt_redocl["fec_ven"],chfecha($_POST['fecha'],'d/m/Y','Y-m-d'),'');
			$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
			if($z==-1){
				echo '<td colspan="11" class="typeagre"><div align="center">'.$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'].'&nbsp;</div></td>';
				$z=$dt_redocl['codigo'];
			}
			elseif($z!=$dt_redocl['codigo']){
					imptotales("Saldo Total Documentos: ",$totales);
					echo '<td colspan="11" class="typeagre"><div align="center">'.$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'].'&nbsp;</div></td>';
					$z=$dt_redocl['codigo'];
					$totales=NULL;
			}
			echo ' <tr>';
      		echo '<td>'.chfecha($dt_redocl["fec_doc"],'Y-m-d','d/m/Y').'&nbsp;</td>';
      		echo '<td>'.$dt_redocl["num_doc"].'&nbsp;</td>';
      		echo '<td>'.chfecha($dt_redocl["fec_ven"],'Y-m-d','d/m/Y').'&nbsp;</td>';
			$b=0;
			if($_POST['sedoc']=='A'){
				echo '<td nowrap>'.$diferencia.'&nbsp;</td>';
				if($diferencia<0){
					echo '<td nowrap>'.number_format($dt_redocl["tot_sal"],2,'.',',').'&nbsp;</td>';
					for($i=1;$i<=6;$i++) echo '<td nowrap>'.'0.00'.'&nbsp;</td>';
					$totales[0]=$totales[0]+$dt_redocl["tot_sal"]*$tipcambio;
					$totgeneral[0]=$totgeneral[0]+$dt_redocl["tot_sal"]*$tipcambio;
				}
				else{
					echo '<td nowrap><div align="left">0.00&nbsp;</div></td>';
					acdat($b,$totales,$totgeneral,$diferencia,$dt_redocl,$dt_parame,$tipcambio);
				}
			}
			else{
				echo '<td nowrap>'.abs($diferencia).'&nbsp;</td>';
				echo '<td nowrap><div align="left">0.00&nbsp;</div></td>';
				acdat($b,$totales,$totgeneral,$diferencia,$dt_redocl,$dt_parame,$tipcambio);
			}
			echo '</tr>';
		}
		imptotales("Saldo Total Documentos: ",$totales);
		imptotales("Saldo General Documentos: ",$totgeneral);
	}
	
	function acdat(&$b,&$totales,&$totgeneral,$diferencia,$dt_redocl,$dt_parame,$tipcambio){
		for($k=1;$k<=6;$k++){
			if((abs($diferencia)<=$dt_parame['dia_ven_cc'.$k] || $k==6) && $b==0){
				$b=1;
				$totales[$k]=$totales[$k]+$dt_redocl["tot_sal"]*$tipcambio;
				$totgeneral[$k]=$totgeneral[$k]+$dt_redocl["tot_sal"]*$tipcambio;
				echo '<td>'.number_format($dt_redocl["tot_sal"]*$tipcambio,2,'.',',').'&nbsp;</td>';
			}
			else
				echo '<td nowrap><div align="left">0.00&nbsp;</div></td>';
		}
	}
	
	function imptotales($leyenda,$totales){
		for($i=0;$i<=6;$i++) $tottot=$tottot+$totales[$i];
		echo '<tr>';
		echo '<td colspan="2" nowrap>'.$leyenda.'&nbsp;</td>';
		echo '<td colspan="2" nowrap><div align="center">'.number_format($tottot,2,'.',',').'&nbsp;</div></td>';
		for($i=0;$i<=6;$i++) echo '<td nowrap>'.number_format($totales[$i],2,'.',',').'&nbsp;</td>';
		echo '</tr>';
	}
	?>
  </table>
  </div>
</form>
</body>
</html>