<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");

include("DF30000.inc");
include('DF80000.php');
require_once("DFMOVCAP.inc");
require_once("DFJQUERY.inc");
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>
<script language="javascript">
$(document).ready(function(){
	$('#bus_prov').click(function(){
		loadOnLayer(
			{
				action:'DYPROBASCPS.php',
				idLayer:'loadProv',
				width:'auto',
				height:'300px'
			},
			{	textBus:document.getElementById('tebusclipro').value,
				idRetObj:'seclipro',
				nomRetObj:'tebusclipro',
				tipBusCat:'nom'
			});
	});
	$('#tod_prov').bind('click',function(){
		seTodPro();
	});
	seTodPro();
});


function seTodPro(){
	var objTodProv=document.getElementById('tod_prov');
	var objSeCliPro=document.getElementById('seclipro');
	var objBusProv=document.getElementById('bus_prov');
	if(objTodProv.checked){
		objBusProv.disabled=true;
		objSeCliPro.value=objTodProv.value;
	}
	else{
		objBusProv.disabled=false;
	}
}

</script>

<body  onLoad="movCap({idObj:'loadProv'});">
<?php
 
$ayuda = 'DV4100A_PR.php';
include("DYAYUDA.php");
$tipmov='P';
$fecini=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
$fecfin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
$sq_bascps=mysqli_query($con ,"select * from dxbascps where act2<>'0' order by nombre ");//busca proveedores
if(isset($_POST['bobus'])){
	$codigo=explode('|',$_POST['seclipro']);
	if($codigo[0]=="TODOS"){
		switch($_POST['setipdoc']){
			case "TODO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' and fec_doc between '$fecini' and '$fecfin' order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and fec_doc between '$fecini' and '$fecfin' and num_doc like '$_POST['tefac']%' order by codigo,fec_doc";
			break;
			case "PAGADO": if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' and  fec_doc between '$fecini' and '$fecfin' and tot_sal=0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc";
			break;
			case "SALDO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' and  tot_sal<>0 order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' and  tot_sal<>0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc";
			break;
		}
		
	}
	
	elseif($codigo[0]!="TODOS"){
		switch($_POST['setipdoc']){
			case "TODO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and num_doc like '$_POST['tefac']%' order by codigo,fec_doc";
			break;
			case "PAGADO": if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc";
			break;
			case "SALDO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and tot_sal<>0 order by codigo,fec_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and tot_sal<>0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc";
			break;
		}
	}
	$sq_redocl=mysqli_query($con ,$busqueda);
	echo  mysqli_error($con);
}
?>
<div id="loadProv"></div>
<div align="center">
  <form name="frm1" id="frm1" method="post" action="DW31000_PR.php">
    <table  align="center">
      <tr>
			<th colspan="3">DOCUMENTOS X PAGAR </th>
      </tr>
      <tr>
        <th colspan="3"> Proveedor</th>
      </tr>
      <tr>        
        <td colspan="2" nowrap>
		  <div align="center">&nbsp;&nbsp;
		    <input name="seclipro" type="text" id="seclipro" size="10" value="<?php
echo $_POST['seclipro'];?>" readonly>
		    <label></label>
		    &nbsp;
		     
		    <input name="tebusclipro" type="text" id="tebusclipro" size="50" value="<?php
echo $_POST['tebusclipro'];?>">
		     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		     <label>
		     <input name="bus_prov" type="button" id="bus_prov" value="Buscar">
		     </label>
&nbsp;	    </div>	    </td>
        <td nowrap>&nbsp;&nbsp;
          <label>
          <input name="tod_prov" type="checkbox" id="tod_prov" value="TODOS"<?php
if($_POST['tod_prov']=='TODOS') echo 'checked';?>>
          <span class="Estilo2"> Todos</span></label>
&nbsp;</td>
      </tr>
      <tr>
        <th colspan="3">Tipo</th>
      </tr>
      <tr>
        <td colspan="3" nowrap><div align="center">
            <select name="setipdoc" id="setipdoc" onChange="de_fe(this.value)">
			<option value="TODO" <?php
 if($_POST['setipdoc']=='TODO') echo 'selected';?>>------ TODO ------</option>
		    <option value="PAGADO"  <?php
 if($_POST['setipdoc']=='PAGADO') echo 'selected';?>>PAGADO</option>
		    <option value="SALDO"  <?php
 if($_POST['setipdoc']=='SALDO') echo 'selected';?>>SALDO</option>
            </select>
        </div></td>
      </tr>
      
      <tr>
        <th colspan="3"><div id="divTitFec">Fechas</div></th>
      </tr>
      <tr>        
        <td colspan="3" nowrap>
		  <div align="center" id="divFechas">
		     
		     
		  Del:
           
          <input name="fecini" type="text" class="emplea" id="fecini" size="20" maxlength="20" 
			<?php

				if(isset($_POST['fecini'])){ //este es el truco
					echo 'value="' . $_POST['fecini'] .  '"';
				}else{
					echo 'value="' . date("d/m/Y") .  '"';
				}
			?>onkeypress="return soloNumeros(event)"//solo fechas
		/>
          <input name="bot" id="bot" type="button" value="..." />
          <script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "fecini",        // ID of the input field
      ifFormat    : "%d/%m/%Y",    // the date format
      button      : "bot"      // ID of the button
    }
  );
            </script>
Al:
<input name="fecfin" type="text" class="emplea" id="fecfin" size="20" maxlength="20" 
			<?php

				if(isset($_POST['fecfin'])){ //este es el truco
					echo 'value="' . $_POST['fecfin'] .  '"';
				}else{
					echo 'value="' . date("d/m/Y") .  '"';
				}
			?>onkeypress="return soloNumeros(event)"//solo fechas
		/>
<input name="bot1" id="bot1" type="button" value="..." src="logos/cal4.jpg" />
<script type="text/javascript">
  Calendar.setup(
    {
      inputField  : "fecfin",        // ID of the input field
      ifFormat    : "%d/%m/%Y",    // the date format
      button      : "bot1"      // ID of the button
    }
  );
            </script>
	    </div>	    </td>
      </tr>
      <tr>
        <th colspan="1">Facturas</th>
        <th colspan="1">Tipo de Reporte </th>
        <th colspan="1">Corte de Hoja </th>
      </tr>
      <tr>
        <td colspan="1" nowrap>
		  <div align="center">
		     
		    <input name="tefac" type="text" id="tefac" value="<?php
 echo $_POST['tefac'];?>">
	    </div>	    </td>
        <td colspan="1">
		  <div align="center">
		     
		    <select name="s4" id="s4">
		      <option value="G" <?php
 if($_POST['s4']=='G') echo 'selected';?>>GENERAL</option>
		      <option value="D" <?php
 if($_POST['s4']=='D') echo 'selected'?>>DETALLE</option>
	        </select>
		  </div>	    </td>
        <td colspan="1">
		  <div align="center">
		     
		    <select name="s5" id="s5">
		      <option value="1"  <?php
 if($_POST['s5']=='1') echo 'selected';?>>SI</option>
		      <option value="2"  <?php
 if($_POST['s5']=='2') echo 'selected';?>>NO</option>
	        </select>
		  </div>	    </td>
      </tr>

      <tr>
        <td colspan="3" nowrap><div align="center">
          <input name="bobus" type="submit" id="bobus" value="Buscar" />
        </div></td>
      </tr>
    </table>

    <br>
  </form>
  <form  name="form1" id="form1" method="post" action=""> 
   <input name="busqueda" type="hidden" id="busqueda" value="<?php
 echo $busqueda;?>">
   <input name="setipdoc" type="hidden" id="setipdoc" value="<?php
 echo $_POST['setipdoc'];?>">
   <input name="fecini" type="hidden" id="fecini" value="<?php
 echo $_POST['fecini'];?>">
   <input name="fecfin" type="hidden" id="fecfin" value="<?php
 echo $_POST['fecfin'];?>">
    <input name="s5" type="hidden" id="s5" value="<?php
 echo $_POST['s5'];?>">
	 <input name="s4" type="hidden" id="s4" value="<?php
 echo $_POST['s4'];?>">
   <script>
  function enviarFormulario(pag, ta){ 
		document.form1.action = pag; 
		document.form1.target = ta;
		document.form1.submit();
	}
   </script>
  <table >
    <tr>
      <th colspan="11" >
        <div align="left">
          <input name="bo_imp" type="button" id="bo_imp" value="Imprimir" onClick="enviarFormulario('DW31000_IM.php','_self')">
          <input name="toExcel" type="button" id="toExcel" value="Exportar Excel" onClick="enviarFormulario('DW31000_IME.php','_self')">
        </div>      </th>
    </tr>
    <tr>
      <td nowrap class="tdagre1">Fec. Mov. </td>
      <td nowrap class="tdagre1">Num. Doc. </td>
      <td nowrap class="tdagre1">Descrip.</td>
      <td nowrap class="tdagre1">Subtotal</td>
      <td nowrap class="tdagre1">IVA</td>
      <td nowrap class="tdagre1">Total Documento </td>
      <td nowrap class="tdagre1">Importe Pagado </td>
      <td nowrap class="tdagre1">Notas Credito </td>
      <td nowrap class="tdagre1">Otros Car. </td>
      <td nowrap class="tdagre1">Otros Abo </td>
      <td nowrap class="tdagre1">Saldo Documento</td>
    </tr>
	<?php
 
	if(isset($_POST['bobus'])){
		$z=-1;
		$totales=NULL;
		$totgeneral=NULL;
		while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
		
		/*	if ($dt_redocl['tip_cam'] > 0){
				$dt_redocl['sub_tot']= $dt_redocl['sub_tot'] * $dt_redocl['tip_cam'];
				$dt_redocl['iva_tot']= $dt_redocl['iva_tot'] * $dt_redocl['tip_cam'];
				$dt_redocl['total']= $dt_redocl['total'] * $dt_redocl['tip_cam'];
				$dt_redocl['tot_pag']= $dt_redocl['tot_pag'] * $dt_redocl['tip_cam'];
				$dt_redocl['tot_not']= $dt_redocl['tot_not'] * $dt_redocl['tip_cam'];
				$dt_redocl['tot_abo']= $dt_redocl['tot_abo'] * $dt_redocl['tip_cam'];
				$dt_redocl['tot_car']= $dt_redocl['tot_car'] * $dt_redocl['tip_cam'];
				$dt_redocl['tot_sal']= $dt_redocl['tot_sal'] * $dt_redocl['tip_cam'];
			}*/
			
			$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
			if($z==-1){
				echo '<tr><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></tr>';
				$z=$dt_redocl['codigo'];
			}elseif($z!=$dt_redocl['codigo']){
				imptotales("Total x dia: ",$totales);
				echo '<tr><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></tr>';
				$z=$dt_redocl['codigo'];
				$totales=NULL;
			}
			llenaTotales($totales,$dt_redocl);
			llenaTotales($totgeneral,$dt_redocl);
			echo '<tr>';
			echo '<td nowrap colspan="1">'.chfecha($dt_redocl['fec_doc'],'Y-m-d','d/m/Y').'&nbsp;</td>';
			echo '<td nowrap colspan="1">'.$dt_redocl['num_doc'].'&nbsp;</td>';
			echo '<td nowrap colspan="1">'.$dt_redocl['des'].'&nbsp;</td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['sub_tot'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['iva_tot'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['total'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['tot_pag'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['tot_not'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format(($dt_redocl['tot_abo']),2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['tot_car'],2,'.',',').'&nbsp;</div></td>';
			echo '<td nowrap colspan="1"><div align="right">'.number_format($dt_redocl['tot_sal'],2,'.',',').'&nbsp;</div></td>';
			echo '</tr>';
			if($_POST['s4']=='D'){
				imp_detalle($dt_redocl);
			}
		}
		imptotales("Total x dia: ",$totales);
		echo '<tr><td nowrap colspan="11">&nbsp;</td></tr>';
		imptotales("Total General: ",$totgeneral);
		//imptotales("Saldo General Documentos: ",$totgeneral);
	}
	
	
	function imp_detalle($dt_redocl){
		if ($dt_redocl['tip_cam'] > 0 && $dt_redocl['moneda']=='2') $tipcambio=$dt_redocl['tip_cam'];
		else $tipcambio=1;
		$sq_redocd=mysqli_query($con ,"select * from dvredocd where codigo='".$dt_redocl['codigo']."' and fec_doc='".$dt_redocl['fec_doc']."' and num_doc='".$dt_redocl['num_doc']."'");
		while($dt_redocd=mysqli_fetch_array($sq_redocd)){
			echo '<tr class="trcoti">';
			echo '<td colspan="1">Mov: '.$dt_redocd['tip_mov'].'&nbsp;</td>';
			echo '<td colspan="2"> Fecha: '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d/m/Y').'&nbsp;</td>';
			echo '<td colspan="1"> Ref.:'.$dt_redocd['fol_ope'].'&nbsp;</td>';
			echo '<td colspan="1"> Importe: &nbsp;</td>';
			echo '<td colspan="1"><div align="right">'.number_format($dt_redocd['sub_tot']*$tipcambio,2,'.',',').'&nbsp;</div></td>';
			echo '<td colspan="1"><div align="right">'.number_format($dt_redocd['can_pro']*$tipcambio,2,'.',',').'&nbsp;</div></td>';
			echo '<td colspan="2"> Movto.:'.$dt_redocd['cve_mto'].'&nbsp;</td>';
			echo '<td colspan="2"> Cen/Vta: '.$dt_redocd['nom_pac'].'&nbsp;</td>';
			echo '</tr>';
		}
	}
	function llenaTotales(&$totales, $dat){
		if ($dat['tip_cam'] > 0 && $dat['tip_mot']!='N') $tipcambio=$dat['tip_cam'];
		else $tipcambio=1;
		$totales[0]+=$dat["sub_tot"]*$tipcambio;
		$totales[1]+=$dat["iva_tot"]*$tipcambio;
		$totales[2]+=$dat["total"]*$tipcambio;
		$totales[3]+=$dat["tot_pag"]*$tipcambio;
		$totales[4]+=$dat["tot_not"]*$tipcambio;
		$totales[6]+=$dat["tot_car"]*$tipcambio;
		$totales[5]+=$dat["tot_abo"]*$tipcambio;
		$totales[7]+=$dat["tot_sal"]*$tipcambio;
	
	}
	
	function imptotales($leyenda,$totales){
		echo '<tr class="inputcodigo">';
		echo '<td colspan="3"><div align="right">'.$leyenda.'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[0],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[1],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[2],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[3],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[4],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[5],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[6],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="1"><div align="right">'.number_format($totales[7],2,'.',',').'&nbsp;</div></td>';
		echo '</tr>';
	}
	?>
  </table>
  </form>
</div>
</body>
</html>