<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include("DF30000.inc");
include("DF80000.php");
?>
<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 = 'DH74000_PR.php';
include("DYAYUDA.php");
?>
<div align="center">
  <form name="form1" id="form1" method="post" action="">
    <table width="380"  align="center">
      <tr>
			<th colspan="2">LISTADO ORDENES DE COMPRAS  </th>
      </tr>
      <tr>
        <th colspan="2">Tipo de Compra </th>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
            <select name="setipcom" id="setipcom">
              <option value="TODAS">------------ Todos ------------</option>
              <?php
 
			  $sq_tipcom=mysqli_query($con ,"Select * from dgtipcom");
			while ($reg = mysqli_fetch_array($sq_tipcom)){
				if($reg['tip_com'] == $_POST['setipcom']){
					echo '<option value="' . $reg['tip_com'] . '" selected>' . $reg['des_tipcom'].'</option>';
				}else{
					echo '<option value="' . $reg['tip_com'] . '">' . $reg['des_tipcom'].'</option>';
				}
			}
			?>
            </select>
        </div></td>
      </tr>
      
      <tr>
        <th width="297">Fecha Inicial</th>
        <th width="297">Fecha Final </th>
      </tr>
      <tr>
        <td width="297">
		    <div align="center">
		      <p>
			  <input name="fecini" type="text" class="emplea" id="fecini" size="15" 
			  <?php
 
				  $axc = date('d/m/Y');
				  if(isset($_POST['fecini'])){
					echo 'value="'.$_POST['fecini'].'"'; 
				  }else{
					echo 'value="'.$axc.'"'; 
				  }
			  ?>
			  >		      
		      <input name="reset2" id="bot1" value=" ... " type="image" src="logos/cal4.jpg"/>
		        <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fecini",     // ID of the input field
      						ifFormat    : "%d/%m/%Y",   // the date format
      						button      : "bot1"      	// ID of the button
    					}
  					);
				</script>
		      </p>
          </div></td>
        <td width="297">
		    <div align="center">
			  <p>
	          <input name="fecfin" type="text" class="emplea" id="fecfin" size="15" 
			  <?php
 
			  if(isset($_POST['fecfin'])){
			  	echo 'value="'.$_POST['fecfin'].'"'; 
			  }else{
			  	echo 'value="'.$axc.'"'; 
			  }
			  ?>
			  >
		      <input name="reset2" id="bot2" value=" ... " type="image" 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      : "bot2"      // ID of the button
    					}
  					);
				</script>
		      </p>
          </div></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
          <select name="setipdoc" id="setipdoc">
            <option value="RESUMEN" <?php
 if($_POST['setipdoc']=='RESUMEN') echo 'selected';?>>RESUMEN</option>
            <option value="DETALLE" <?php
 if($_POST['setipdoc']=='DETALLE') echo 'selected';?>>DETALLE</option>
          </select>
        </div></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
          <input name="boproc" type="submit" id="boproc" value="Procesar" />
        </div></td>
      </tr>
    </table>
  </form>
</div>
<div align="center">
  <form name="frm1" id="frm1" method="post" action="" >
  <input name="fecini" type="hidden" value="<?php
 echo $_POST['fecini'];?>">
  <input name="fecfin" type="hidden" value="<?php
 echo $_POST['fecfin'];?>">
   <input name="setipdoc" type="hidden" value="<?php
 echo $_POST['setipdoc'];?>">
    <input name="setipcom" type="hidden" value="<?php
 echo $_POST['setipcom'];?>">
  <table >
  	<tr>
        <th colspan="7">
		<div align="left">
            <input type="submit" name="bot4" value="Imprimir" onClick="openpage('DH74000_IM.php','_self')">
            <input name="toExcel" type="button" id="toExcel" value="Exportar Excel" onClick="openpage('DH74000_IME.php','_self')">
		</div>		</th>
    </tr>
	<tr>
		<td class="tabagre"><div align="center">No Factura</div></td>
		<td class="tabagre"><div align="center">Producto</div></td>
		<td class="tabagre"><div align="center">Cantidad</div></td>
		<td class="tabagre"><div align="center">Precio Unitario</div></td>
		<td class="tabagre"><div align="center">Importe</div></td>
		<td class="tabagre"><div align="center">I.V.A</div></td>
		<td class="tabagre"><div align="center">Total Factura</div></td>
	  </tr>
<?php

$tipcom = $_POST['setipcom'];
$fec_ini=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
$fec_fin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d'); 
if(isset($_POST['boproc'])){
	if($tipcom =='TODAS'){
		$sbus="SELECT * FROM dgreford where fec_ord between '$fec_ini' and '$fec_fin' order by fec_ord,tip_com";
	}else{
		$sbus="SELECT * FROM dgreford where tip_com = '$tipcom' and fec_ord between '$fec_ini' and '$fec_fin' order by fec_ord,tip_com";
	}
	echo ' <input name="sbus" type="hidden" value="'.$sbus.'">';
	
	$sq_refcom=mysqli_query($con ,$sbus);
	while(@$dt_refcom = mysqli_fetch_array($sq_refcom)){
		if($dt_refcom["tip_mon"]!='1') $tipoCambio=$dt_refcom["tip_cam"];
		else
			$tipoCambio=1;
		if($ven_cor == '' || $ven_cor != $dt_refcom["tip_com"]){
			$ven_cor = $dt_refcom["tip_com"];
			@$dt_tipcom = mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgtipcom where tip_com = '$ven_cor'"));
		}
		if($fec_cor == '' || $fec_cor != $dt_refcom["tip_com"].$dt_refcom["fec_ord"]){
			$fec_cor = $dt_refcom["tip_com"].$dt_refcom["fec_ord"];		
			$fecone=chfecha($dt_refcom["fec_ord"],'Y-m-d','d/m/Y');
			echo '<tr><td  class="typeagre" colspan="9"><div align="center">'.$dt_tipcom["des_tipcom"].' DEL : '. $fecone.'&nbsp</div></th></tr>';
		}
		
		//Si Detalle
		
			@$sql_fade=mysqli_query($con ,"SELECT * FROM dgrefodd where tip_com = '".$dt_refcom['tip_com']."' and num_ord = '".$dt_refcom['num_ord']."'");
			while(@$reg_fade = mysqli_fetch_array($sql_fade)){
				$cos_tot += $reg_fade["total"];
				if($_POST['setipdoc']=='DETALLE'){
					$sql_dapo=mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '".$reg_fade['nparte1']."'");
					$reg_dapo=mysqli_fetch_array($sql_dapo);
					echo '<tr>';
					echo '<td><div align="center">'.$dt_refcom["num_ord"].'&nbsp</div></td>';
					echo '<td><div align="left">'.$reg_dapo["nparte1"].'  '.$reg_dapo["np_desc1"].'&nbsp</div></td>';
					echo '<td><div align="right">'.$reg_fade["can_ord"].'&nbsp</div></td>';
					echo '<td><div align="right">'.number_format($reg_fade["total_cd"]*$tipoCambio, 4, '.', ',') .'&nbsp</div></td>';
					echo '<td><div align="right">'.number_format($reg_fade["subtotal"]*$tipoCambio, 2, '.', ',').'&nbsp</div></td>';
					echo '<td><div align="right">'.number_format($reg_fade["iva_tot"]*$tipoCambio, 2, '.', ',').'&nbsp</div></td>';
					echo '<td><div align="right">'.number_format($reg_fade["total_fin"]*$tipoCambio, 2, '.', ',').'&nbsp</div></td>';
					echo '</tr>';
				}
			}
		
		
		if ($dt_refcom['sub_ord']==0){
			$nom_cli = ' C   A   N   C   E   L   A   D   A ';
		}
		else{
			$sql_cli=mysqli_query($con ,"SELECT * FROM dgcatpro where cod_pro = '".$dt_refcom['codigo']."'");
			$reg_cli=mysqli_fetch_array($sql_cli);
			$nom_cli = $reg_cli['nom_pro'].' '.$reg_cli['ap_pat_pro'].' '.$reg_cli['ap_mat_pro'];
		}
		echo '<tr class="inputcodigo">';
		echo '<td colspan="4"><div align="left">'.' '.$dt_refcom["tip_fac"].$dt_refcom["num_ord"].'  '.$nom_cli.'&nbsp</div></td>';
		echo '<td><div align="right">'.number_format($dt_refcom["sub_ord"]*$tipoCambio, 2, '.', ',') . '&nbsp</div></td>';
		echo '<td><div align="right">'.number_format($dt_refcom["iva_ord"]*$tipoCambio, 2, '.', ',') . '&nbsp</div></td>';
		echo '<td><div align="right">'.number_format($dt_refcom["total_ord"]*$tipoCambio, 2, '.', ',') . '&nbsp</div></td>';
		echo '</tr>';
		echo '<tr><td colspan="9"></tr>';
		llenaTotalGeneral($dt_refcom,$sumTotGen,$cos_tot,$dt_tipcom);
		$cos_tot=0;
	}
	imp_totalGeneral($sumTotGen);
}

function llenaTotalGeneral($dt_factudmix,&$sumTotGen,$cos_tot,$dt_tipcom){
	if($dt_factudmix["tip_mon"]!='1') $tipoCambio=$dt_factudmix["tip_cam"];
	else $tipoCambio=1;
	$tipventa=$dt_factudmix["tip_com"];
	$sumTotGen[$tipventa]["descripcion"] = $dt_tipcom["des_tipcom"];
	$sumTotGen[$tipventa]["compra"] +=$dt_factudmix["sub_ord"]*$tipoCambio;
	$sumTotGen[$tipventa]["iva"] +=$dt_factudmix["iva_ord"]*$tipoCambio;
}

function imp_totalGeneral($sumTotGen){
	echo '<tr class="typeagre">';
	echo '<td colspan = "1"><div align="center"> CVE. </div></td>';
	echo '<td colspan = "2"><div align="center"> Descripcion </div></td>';
	echo '<td colspan = "2"><div align="center"> Compra </div></td>';
	echo '<td colspan = "2"><div align="center"> I.V.A. </div></td>';
	echo '</tr>';
	//list($clave,$des) = each($sumTotGen);
	while(@list($clave,$des) = each($sumTotGen)){
		echo '<tr>';
			echo '<td colspan = "1"><div align="center">'.$clave.'&nbsp;</div></td>';
			echo '<td colspan = "2"><div align="center">'.$des["descripcion"].'&nbsp;</div></td>';
			echo '<td colspan = "2"><div align="center">'.number_format($des["compra"],2,'.',',').'&nbsp;</div></td>';
			echo '<td colspan = "2"><div align="center">'.number_format($des["iva"],2,'.',',').'&nbsp;</div></td>';
		echo '</tr>';
		$stot[0] +=$des["costo"];
		$stot[1] +=$des["compra"];
		$stot[2] +=$des["iva"];
		//list($clave,$des) = each($sumTotGen);
	}
	echo '<tr>';
	echo '<td colspan = "1">Total: &nbsp;</td>';
	echo '<td colspan = "2"><div align="center">'.number_format($stot[0],2,'.',',').'&nbsp;</div></td>';
	echo '<td colspan = "2"><div align="center">'.number_format($stot[1],2,'.',',').'&nbsp;</div></td>';
	echo '<td colspan = "2"><div align="center">'.number_format($stot[2],2,'.',',').'&nbsp;</div></td>';
	echo '</tr>';
}
mysqli_close($con);
?>
    </table>
  </form>
</div>
</body>
</html>