<?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="JavaScripfec_ini.2" src="DSSCRIPT.js"></script>

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

$arrColRep=$_POST['tip_rep'];
$numDias=dayDiff($fecFin,$fecIni);
if(count($arrColRep)>0)
	$colRep=implode(",",$arrColRep);
?>

  <form name="frm1" id="frm1" method="post" action="">
    <table border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
			<th colspan="2" nowrap>VENTA, COSTO, UTILIDAD </th>
      </tr>
	  <tr class="estilotabla">
        <td nowrap><div align="center" class="tdeli">
        <span class="Estilo10">Tipo de Venta:</span></div></td>
	    <td nowrap><div align="center" class="tdeli">
	      <select name="tip_ven" id="tip_ven">
	        <option value="T">----- Todos ------</option>
	        <?php
 
			$sql = mysqli_query($con ,"select * from dgtipven");
			while ($reg = mysqli_fetch_array($sql)){
				if($reg['tip_ven'] == $_POST['tip_ven']){
					echo '<option value="' . $reg['tip_ven'] . '" selected>' . $reg['des_tipven'].'</option>';
				}else{
					echo '<option value="' . $reg['tip_ven'] . '">' . $reg['des_tipven'].'</option>';
				}
			}
			?>
	        </select>
       </div></td>
	  </tr>
      <tr>
        <td colspan="2" nowrap>
		    <div align="center" class="estilotabla Estilo3"><strong>

		    Del 
            <input name="fec_ini" type="text" class="Estilo4" id="fec_ini" onBlur="valFecha(this)" size="10"
			  <?php
 
			  if(isset($_POST['fec_ini'])){
			  	echo 'value="'.$_POST['fec_ini'].'"'; 
			  }else{
			  	echo 'value="'.date('d/m/Y').'"'; 
			  }
			  ?>
			  >
	         <input name="bot" id="bot1" type="image" value="Cal" src="logos/cal4.jpg" />
	        		          <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fec_ini",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot1"      // ID of the button
    					}
  					);
				                </script>
		       
	        al
	          <input name="fec_fin" type="text" class="Estilo4" id="fec_fin" onBlur="valFecha(this)" size="10"
				<?php
 
			  if(isset($_POST['fec_fin'])){
			  	echo 'value="'.$_POST['fec_fin'].'"'; 
			  }else{
			  	echo 'value="'.date('d/m/Y').'"'; 
			  }
			  ?>
				>
	          <input name="bot2" id="bot2" type="image" value="Cal" src="logos/cal4.jpg" />
			  <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fec_fin",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot2"      // ID of the button
    					}
  					);
				</script>
            </strong></div></td>
       </tr>
      <tr>
        <td colspan="2" nowrap style="#EBEBEB">
		  <div align="center">
		    <label>
		    <span class="style2">
		    <input name="tip_rep[]" type="checkbox" id="tip_rep[]" value="ven"<?php
if(strstr($colRep,'ven')) echo 'checked';?>>
		    Venta 
		    <input name="tip_rep[]" type="checkbox" id="tip_rep[]" value="cos"<?php
if(strstr($colRep,'cos')) echo 'checked';?>>
		    Costo
		    <input name="tip_rep[]" type="checkbox" id="tip_rep[]" value="uti"<?php
if(strstr($colRep,'uti')) echo 'checked';?>>
		    Utilidad		    </span></label>
	      </div></td>
      </tr>
      <tr>
        <td colspan="2" nowrap style="#EBEBEB">
          <div align="center">
            <select name="for_rep" id="for_rep">
              <option value="R" <?php
 if (!(strcmp("R", "$_REQUEST['for_rep']"))) {echo "selected=\"selected\"";} ?>>RESUMEN</option>
              <option value="D" <?php
 if (!(strcmp("D", "$_REQUEST['for_rep']"))) {echo "selected=\"selected\"";} ?>>DETALLE</option>
            </select>
          </div>
       </td>
      </tr>
      <tr>
        <td colspan="2" nowrap style="#EBEBEB"><div align="center">
          <input name="Submit" type="button" id="Submit" value="Procesar" onClick="openpage('DG43000_PR.php','_self')"/>
        </div></td>
      </tr>
    </table>

    <table  align="center">
      <tr>
        <th colspan="7">
        <input type="button" name="bot4" value="Imprimir" onClick="openpage('DG43000_IM.php','_self')">
        <input name="bot_exe" type="button" value="Exportar Excel" onClick="openpage('DG43000_IME.php','_self')">		</th>
      </tr>
<?php
	 

	$fecIni=chfecha($_POST['fec_ini'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fec_fin'],'d/m/Y','Y-m-d');
	$tipVen=$_POST['tip_ven'];
	$forRep=trim($_POST['for_rep']);
	$arrColRep=$_POST['tip_rep'];
	$numDias=dayDiff($fecFin,$fecIni);
	if(count($arrColRep)>0)
		$colRep=implode(",",$arrColRep);
	else{
		echo "<table></form></div></body></html>";
		exit();
	}

	$arrDataColRep=array("ven"=>"VENTA","cos"=>"COSTO","uti"=>"UTILIDAD");
	$arrBus[]="dgfactur.con_cre<>'W'";
	$arrBus[]="dgfactud.fec_fac BETWEEN '$fecIni' AND '$fecFin'";
	if($tipVen!="T"){
		$arrBus[]="dgfactud.tip_ven='$tipVen'";
	}
	if($forRep=='R')
		$groupBy='YEAR(dgfactud.fec_fac) DESC, MONTH(dgfactud.fec_fac) DESC,';
	else
		$groupBy='dgfactud.fec_fac,';	
	$WHERE=implode(" AND ",$arrBus);	
	$sq_retrieve="SELECT
					DATE_FORMAT(dgfactud.fec_fac,'%d/%m/%Y') as fecFac, 
					dgfactud.tip_ven as idTipVen,
					dgtipven.des_tipven as desTipVen,
					dgfactud.almacen as idAlm, 
					dgalmacen.desc_almacen desAlm,
					SUM(dgfactud.cos_ref*dgfactud.can_ref) as cosTot,
					SUM(dgfactud.subtotal) as venTot,
					SUM(dgfactud.subtotal-dgfactud.cos_ref*dgfactud.can_ref) utiTot
				FROM 
					dgfactud
				INNER JOIN dgtipven ON(dgtipven.tip_ven=dgfactud.tip_ven)
				INNER JOIN dgalmacen ON(dgalmacen.almacen=dgfactud.almacen)
				INNER JOIN dgfactur ON(dgfactur.fol_fac=dgfactud.cons_fac)
				LEFT JOIN dgrefdve ON(dgfactur.tip_ven=dgrefdve.tip_ven AND CONCAT(dgfactur.tip_fac,dgfactur.num_fac)=dgrefdve.num_fac)
				LEFT JOIN dgrefdvd ON(dgrefdvd.cons_dev=dgrefdve.fol_recep AND dgfactud.nparte1=dgrefdvd.nparte1)
				WHERE
					$WHERE
				GROUP BY 
					$groupBy dgfactud.tip_ven 
				ORDER BY 
					dgfactud.fec_fac,dgfactud.tip_ven";
	//echo $sq_retrieve;
	$sq_factudMix=mysqli_query($con ,$sq_retrieve);
	$bIni=true;
	if(mysqli_num_rows($sq_factudMix)==0){
		echo "<table></form></div></body></html>";
		exit();
	}
	while($dt_factudMix=mysqli_fetch_array($sq_factudMix)){
		$matDataReport[$dt_factudMix['fecFac']][$dt_factudMix['idTipVen']]['totales']['cos']=$dt_factudMix['cosTot'];
		$matDataReport[$dt_factudMix['fecFac']][$dt_factudMix['idTipVen']]['totales']['ven']=$dt_factudMix['venTot'];
		$matDataReport[$dt_factudMix['fecFac']][$dt_factudMix['idTipVen']]['totales']['uti']=$dt_factudMix['utiTot'];
		$matDataReport[$dt_factudMix['fecFac']][$dt_factudMix['idTipVen']]['desTipVen']=$dt_factudMix['desTipVen'];
		$tipVens[$dt_factudMix['idTipVen']]=$dt_factudMix['desTipVen'];	//Tipos de Ventas	
	}

	foreach($matDataReport as $fecFac=>$valuesTipVen){
		if($bIni){
			$bIni=false;
			echo "<tr>";
			echo "<td class=\"estilotabla\" rowspan=\"2\"><div align=\"center\">FECHA</div></td>";
			foreach($tipVens as $idTipVenEnc=>$desTipVenEnc){
				echo "<td class=\"estilotabla\" colspan=\"".count($arrColRep)."\">$idTipVenEnc  $desTipVenEnc</td>";
			}
			echo "<td class=\"estilotabla\" colspan=\"".count($arrColRep)."\"><div align=\"center\">TOTALES</div></td>";
			echo "</tr>";
			echo "<tr>";
			for($k=0;$k<=count($tipVens);$k++){
				for($i=0;$i<count($arrColRep);$i++){
					echo "<td class=\"mentab\"><div align=\"center\">".$arrDataColRep[$arrColRep[$i]]."</div></td>";
				}
			}
			echo "</tr>";

		}
		echo "<tr>";
		echo "<td >$fecFac</td>";
		foreach($tipVens as $idTipVen=>$desTipVen){
			for($i=0;$i<count($arrColRep);$i++){
				echo "<td ><div align=\"right\">".number_format($valuesTipVen[$idTipVen]['totales'][$arrColRep[$i]],2,'.',',')."</div></td>";
				$totRow[$arrColRep[$i]] +=$valuesTipVen[$idTipVen]['totales'][$arrColRep[$i]];
				$totCol[$idTipVen][$arrColRep[$i]] +=$valuesTipVen[$idTipVen]['totales'][$arrColRep[$i]];
			}
			
		}
		for($i=0;$i<count($arrColRep);$i++){
				echo "<td ><div align=\"right\">".number_format($totRow[$arrColRep[$i]],2,'.',',')."</div></td>";
		}
		unset($totRow);
		echo "</tr>";
	}
	echo "<tr class=\"inputcodigo\">";
	echo "<td><div align=\"right\">Total:</div></td>";
	foreach($tipVens as $idTipVen=>$desTipVen){
		for($i=0;$i<count($arrColRep);$i++){
				echo "<td ><div align=\"right\">".number_format($totCol[$idTipVen][$arrColRep[$i]],2,'.',',')."</div></td>";
				$totTot[$arrColRep[$i]] +=$totCol[$idTipVen][$arrColRep[$i]];
		}	
		
	}
	for($i=0;$i<count($arrColRep);$i++){
			echo "<td ><div align=\"right\">".number_format($totTot[$arrColRep[$i]],2,'.',',')."</div></td>";
	}	
	echo "</tr>";
?>	  
    </table>
  </form>

</body>
</html>