<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
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>
<?php
 
include("DF30000.inc");
?>

<body>
<?php
 
$ayuda = 'DG4400A_PR.php';
include("DYAYUDA.php");


if(isset($_POST['Submit'])){//Si boton buscar
	if($_POST['s1']=="")
		$busqueda="SELECT * FROM dgcatven";
	else
		$busqueda="SELECT * FROM dgcatven where cod_ven = '".$_POST['s1']."'";
	$sq_catven = mysqli_query ($busqueda); 

}

?>
<div align="center">
  <form name="form1" method="post" action="">
    <table  align="center">
      <tr>
			<th>VENTAS X EJECUTIVO DE VENTAS </th>
      </tr>
      <tr>
        <td>
		    <div align="center" class="estiloFechas">
			  Del
			  <input name="t1" type="text" class="Estilo4" id="t1" size="15" 
			  <?php
 
				  $axc = date('d/m/Y');
				  if(isset($_POST['t1'])){
					echo 'value="'.$_POST['t1'].'"'; 
				  }else{
				  	echo 'value="'.$axc.'"'; 
				  }
			  ?>
			  >		      
		      <input name="reset2" id="bot1" value=" ... " type="image" src="logos/cal4.jpg"/>
		      al
		      <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "t1",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot1"      // ID of the button
    					}
  					);
				</script>

	          <input name="t2" type="text" class="Estilo4" id="t2" size="15"  
			  <?php
 
			  if(isset($_POST['t2'])){
			  	echo 'value="'.$_POST['t2'].'"'; 
			  }else{
			  	echo 'value="'.$axc.'"'; 
			  }
			  ?>
			  >
		      <input name="reset2" id="bot2" value=" ... " type="image" src="logos/cal4.jpg"/>
				<script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "t2",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot2"      // ID of the button
    					}
  					);
				</script>
            </div></td>
      </tr>
      <tr>
        <th>Ejecutivo de Ventas </th>
      </tr>
      <tr>
        <td>
		  <div align="center">
		    <select name="s1" id="s1">
			<option value="">-------------------- TODOS --------------------</option>
            <?php
 
			$sql = mysqli_query($con ,"SELECT * FROM dgcatven order by CONCAT(nom_ven,' ',ap_pat_ven,' ',ap_mat_ven) asc");
			while($reg=mysqli_fetch_array($sql)){
				if($reg['cod_ven'] == $_POST['s1']){
					echo '<option value="' . $reg['cod_ven'] . '" selected>' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] .'</option>';
				}else{
					echo '<option value="' . $reg['cod_ven'] . '">' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] . '</option>';
				}
			
			}
			?>
            </select>
		  </div>	    </td>
      </tr>
      <tr>
        <th>Tipo de Reporte  </th>
      </tr>
      <tr>
        <td>
		  <div align="center">
		    <select name="s2" id="s2">
			<?php

				if($_POST['s2']==1){
					echo '<option value="1" selected>RESUMEN</option>';
				}else{
					echo '<option value="1">RESUMEN</option>';
				}
				if($_POST['s2']==2){
					echo '<option value="2" selected>DETALLE</option>';
				}else{
					echo '<option value="2">DETALLE</option>';
				}
            ?>
			</select>
		  </div>	    </td>
      </tr>
      <tr>
        <td><div align="center">
          <input name="Submit" type="submit" id="Submit" value="Procesar" />
        </div></td>
      </tr>
    </table>
  </form>
</div>
<div align="center">
<form name="frm1" id="frm1" method="post" action="" >
  <input name="act" type="hidden" <?php
 if(isset($_POST['Submit'])) echo 'value=1'; ?>>
  <input name="t1" type="hidden" <?php
 echo 'value="'.$_POST['t1'].'"';?>>
  <input name="t2" type="hidden" <?php
 echo 'value="'.$_POST['t2'].'"';?>>

  <input name="s2" type="hidden" <?php
 echo 'value="'.$_POST['s2'].'"';?>>
  <input name="busqueda" type="hidden" <?php
 echo 'value="'.$busqueda.'"';?>>
    <br>
    <table width="449"  align="center">
      <tr>
        <th colspan="6" nowrap>
          <div align="center">
            <input type="button" name="bot4" value="Imprimir" onClick="openpage('DG44000_IM.php','_self')">       
            <input type="button" name="bot42" value="Exportar Excel" onClick="openpage('DG44000_IME.php','_self')">
          </div></th>
      </tr>
<?php

$bIni=true;
if(isset($_POST['Submit'])){
	$fecini=chfecha($_POST['t1'],'d/m/Y','Y-m-d');
	$fecfin=chfecha($_POST['t2'],'d/m/Y','Y-m-d');
	echo '<tr><th colspan="6"><div align="center">Concepto - Ventas X Ejecutivo de Ventas</div></th></tr>';
	while ($row = mysqli_fetch_array($sq_catven)){ 
		if($bIni){
			echo '<tr><td colspan="6" ><div align="center">'.'&nbsp</div></td></tr>';
			echo '<tr><td colspan="6" class="tabagre"><div align="center">'.'Ejecutivo de Ventas'.'&nbsp</div></td></tr>';
			if($_POST['s2']!='1') echo '<tr><td class="mentab"><div align="center">'.'Fecha'.'&nbsp</div></td>';
			else  echo '<tr><td class="mentab"><div align="center">'.'A&ntilde;o-Mes'.'&nbsp</div></td>';
			echo '<td class="mentab"><div align="center">'.'Venta'.'&nbsp</div></td>';
			echo '<td class="mentab"><div align="center">'.'Costo'.'&nbsp</div></td>';
			echo '<td class="mentab"><div align="center">'.'Utilidad'.'&nbsp</div></td>';
			echo '<td class="mentab"><div align="center">'.'Num. Ventas'.'&nbsp</div></td>';
			echo '<td class="mentab"><div align="center">'.'Partidas'.'&nbsp</div></td></tr>';
			$bIni=false;
		}
		echo '<tr><td colspan="6"><div align="center" class="estilotabla">' . $row['nom_ven'] . ' ' . $row['ap_pat_ven'] . ' ' . $row['ap_mat_ven'] . '&nbsp</div></td></tr>';
		if($_POST['s2']=='1'){
			$trabus= "select sum(dgfactud.subtotal) as tven, sum(dgfactud.cos_ref*dgfactud.can_ref) as tcos, dgfactur.fec_fac,COUNT(*) as numPar,dgfactur.tip_ven from dgfactud INNER JOIN dgfactur ON(dgfactur.fol_fac=dgfactud.cons_fac) where dgfactur.cod_ven= '".$row['cod_ven']."' and dgfactur.fec_fac between '$fecini' and '$fecfin' group by  YEAR(dgfactur.fec_fac) asc, MONTH(dgfactur.fec_fac) asc ";
		}else{
			$trabus= "select sum(dgfactud.subtotal) as tven, sum(dgfactud.cos_ref*dgfactud.can_ref) as tcos, dgfactur.fec_fac,COUNT(*) as numPar,dgfactur.tip_ven from dgfactud INNER JOIN dgfactur ON(dgfactur.fol_fac=dgfactud.cons_fac) where dgfactur.cod_ven= '".$row['cod_ven']."' and dgfactur.fec_fac between '$fecini' and '$fecfin' group by  dgfactur.fec_fac asc";
		}	

		$sq_factur=mysqli_query($con ,$trabus);
		//echo mysqli_error($con);
		while($dt_factur=mysqli_fetch_array($sq_factur)){
			if($_POST['s2']=='1'){
				$auxFecVen=explode('-',$dt_factur['fec_fac']);
				$andFecVen=" AND fec_ven like '$auxFecVen[0]-$auxFecVen[1]-%'";
			}else{
				$andFecVen=" AND fec_ven ='".$dt_factur['fec_fac']."'";
			}
			$dt_pememaSum=mysqli_fetch_array(mysqli_query($con ,$sqnv="SELECT count(*) AS numVen From dgpemema WHERE tip_ven='".$dt_factur['tip_ven']."' $andFecVen AND cod_ven='".$row['cod_ven']."' "));
			//echo "$sqnv<br>";
			//echo mysqli_error($con);
			$totales[0] +=$dt_factur['tven'];
			$totales[1] +=$dt_factur['tcos'];
			$totales[2] +=$dt_factur['numPar'];
			$totales[3] +=$dt_pememaSum['numVen'];			
			$totalesgene[0] +=$dt_factur['tven'];
			$totalesgene[1] +=$dt_factur['tcos'];
			$totalesgene[2] +=$dt_factur['numPar'];
			$totalesgene[3] +=$dt_pememaSum['numVen'];						
			if($_POST['s2']!='1') echo '<tr><td><div align="left">'.chfecha($dt_factur['fec_fac'],'Y-m-d','d/m/Y').'&nbsp</div></td>';
			else echo '<tr><td><div align="left">'.chfecha($dt_factur['fec_fac'],'Y-m-d','Y').'-'.chfecha($dt_factur['fec_fac'],'Y-m-d','M').'&nbsp</div></td>';
			echo '<td><div align="left"> $' . number_format($dt_factur['tven'], 2, '.', ',') . '&nbsp</div></td>';
			echo '<td><div align="left"> $' . number_format($dt_factur['tcos'], 2, '.', ',') . '&nbsp</div></td>';
			echo '<td><div align="left"> $' . number_format(($dt_factur['tven']-$dt_factur['tcos']), 2, '.', ',') . '&nbsp</div></td>';	
			echo '<td><div align="left"> ' . $dt_pememaSum['numVen'] . '&nbsp</div></td>';	
			echo '<td><div align="left"> ' . number_format($dt_factur['numPar'], 2, '.', ',') . '&nbsp</div></td></tr>';	
		}
		echo '<tr><td class="inputcodigo"><div align="right">'.'TOTALES: '.'&nbsp</div></td>';
		echo '<td class="inputcodigo"><div align="left"> $' . number_format($totales[0], 2, '.', ',') . '&nbsp</div></td>';
		echo '<td class="inputcodigo"><div align="left"> $' . number_format($totales[1], 2, '.', ',') . '&nbsp</div></td>';
		echo '<td class="inputcodigo"><div align="left"> $' . number_format(($totales[0]-$totales[1]), 2, '.', ',') . '&nbsp</div></td>';
		echo '<td class="inputcodigo"><div align="left"> ' . number_format($totales[2], 2, '.', ',') . '&nbsp</div></td>';
		echo '<td class="inputcodigo"><div align="left"> ' . $totales[3]. '&nbsp</div></td></tr>';		
		
		$totales=NULL;
	}
	echo '<tr><td colspan="4"><div align="center">&nbsp</div></td>';
	echo '<tr><td class="inputcodigo"><div align="right">'.'TOTALES GENERALES:'.'&nbsp</div></td>';
	echo '<td class="inputcodigo"><div align="left"> $' . number_format($totalesgene[0], 2, '.', ',') . '&nbsp</div></td>';
	echo '<td class="inputcodigo"><div align="left"> $' . number_format($totalesgene[1], 2, '.', ',') . '&nbsp</div></td>';
	echo '<td class="inputcodigo"><div align="left"> $' . number_format(($totalesgene[0]-$totalesgene[1]), 2, '.', ',') . '&nbsp</div></td>';
	echo '<td class="inputcodigo"><div align="left"> ' . number_format($totalesgene[2], 2, '.', ',') . '&nbsp</div></td></tr>';	
	echo '<td class="inputcodigo"><div align="left"> ' . $totalesgene[3]. '&nbsp</div></td></tr>';		
}
?>	  
    </table>
  </form>
</div>
</body>
</html>