<?php
	
session_cache_limiter('private');
session_start();

$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
 include('DF80000.php');
$name="Vencimiento X Fecha.xls"; 
if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	header('Content-Type: application/force-download');
else
	header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$name.'"');

$tipmov='C';
$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
	if(trim($_POST['tetipser'])!=""){
		$andTipSer=" AND dvredocl.num_doc like '".strtoupper(trim($_POST['tetipser']))."%'";
	}
	if(trim($_POST['seclipro'])!="" && trim($_POST['seclipro'])!="TODOS"){
		$andCli=" AND codigo='".$_POST['seclipro']."'";
	}
	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' $andTipSer $andCli 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' $andTipSer $andCli order by codigo,fec_doc";
		break;
		case 'A': $st_bus="select * from dvredocl where tip_mov='$tipmov' and tot_sal<>0 $andTipSer $andCli order by codigo,fec_doc";
		break;
	}
	$sq_redocl=mysqli_query($con ,$st_bus);
	//echo $st_bus;
	//exit();
//}//Termina Si buscar
?>

<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;?>">
  <table border="0">
    <tr>
      <th colspan="11" >
        <div align="left">
          <input name="bo_imp" type="button" id="bo_imp" value="Imprimir"  onClick="openpage('DV42000_IM.php','_self')">
          <input name="toExcel" type="button" id="toExcel" value="Exportar Excel" onClick="openpage('DV42000_IME.php','_self')">
        </div>
     </th>
    </tr>
    <tr>
      <td nowrap class="rowHead">Fec. Doc. </td>
      <td nowrap class="rowHead">N&uacute;m. Doc. </td>
      <td nowrap class="rowHead">Fec. Ven. </td>
      <td nowrap class="rowHead">Dias Doc. </td>
      <td nowrap class="rowHead">Doc. Ven. </td>
      <td nowrap class="rowHead">Doc. a <?php
 echo "$dt_parame['dia_ven_cc1']";?> dias </td>
      <td nowrap class="rowHead">Doc. a <?php
 echo "$dt_parame['dia_ven_cc2']";?> dias </td>
      <td nowrap class="rowHead">Doc. a <?php
 echo "$dt_parame['dia_ven_cc3']";?> dias </td>
      <td nowrap class="rowHead">Doc. a <?php
 echo "$dt_parame['dia_ven_cc4']";?> dias </td>
      <td nowrap class="rowHead">Doc. a <?php
 echo "$dt_parame['dia_ven_cc5']";?> dias </td>
      <td nowrap class="rowHead">Doc. 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="rowNueCli">'.$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'].'</td>';
				$z=$dt_redocl['codigo'];
			}
			elseif($z!=$dt_redocl['codigo']){
					imptotales("Saldo Total Documentos: ",$totales);
					echo utf8_encode('<td colspan="11" class="rowNueCli">'.$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'].'</td>');
					$z=$dt_redocl['codigo'];
					$totales=NULL;
			}
			echo ' <tr>';
      		echo '<td class="rowCad">'.chfecha($dt_redocl["fec_doc"],'Y-m-d','d/m/Y').'&nbsp;</td>';
      		echo '<td class="rowCad">'.$dt_redocl["num_doc"].'&nbsp;</td>';
      		echo '<td class="rowCad">'.chfecha($dt_redocl["fec_ven"],'Y-m-d','d/m/Y').'&nbsp;</td>';
			$b=0;
			if($_POST['sedoc']=='A'){
				echo '<td nowrap   class="rowTot">'.$diferencia.'&nbsp;</td>';
				if($diferencia<0){
					echo '<td nowrap  class="rowTot">'.number_format($dt_redocl["tot_sal"],2,'.',',').'</td>';
					for($i=1;$i<=6;$i++) echo '<td nowrap  class="rowTot">'.'0.00'.'</td>';
					$totales[0]=$totales[0]+$dt_redocl["tot_sal"]*$tipcambio;
					$totgeneral[0]=$totgeneral[0]+$dt_redocl["tot_sal"]*$tipcambio;
				}
				else{
					echo '<td nowrap   class="rowTot">0.00</td>';
					acdat($b,$totales,$totgeneral,$diferencia,$dt_redocl,$dt_parame,$tipcambio);
				}
			}
			else{
				echo '<td nowrap  class="rowTot">'.abs($diferencia).'</td>';
				echo '<td nowrap  class="rowTot">0.00</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 class="rowTot">'.number_format($dt_redocl["tot_sal"]*$tipcambio,2,'.',',').'</td>';
			}
			else
				echo '<td nowrap class="rowTot">0.00</td>';
		}
	}
	
	function imptotales($leyenda,$totales){
		for($i=0;$i<=6;$i++) $tottot=$tottot+$totales[$i];
		echo '<tr>';
		echo '<td colspan="2" nowrap class="rowCadPar">'.$leyenda.'&nbsp;</td>';
		echo '<td colspan="2" nowrap class="rowTotPar">'.number_format($tottot,2,'.',',').'</td>';
		for($i=0;$i<=6;$i++) echo '<td nowrap class="rowTotPar">'.number_format($totales[$i],2,'.',',').'</td>';
		echo '</tr>';
	}
	?>
   
  </table>

 
