<?php
include "procesos/creditos_formulas.php";
include("conexion.php");


//session_start();
//include "verifica.php";


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Reporte de compras</title>

<script src="assets/js/funciones.js"></script>
<script src="assets/js/numerosletras2.js"></script>

<style type="text/css">
<!--
.Estilo6 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	letter-spacing: 0px;
}
.Estilo8 {font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; letter-spacing: -1px; }

.Estilo10 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; letter-spacing: -1px; }

.Estilo11 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; letter-spacing: 0px; }

.Estilo9 {
	font-size: 16px;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
}
.Estilo16 {	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.Estilo17 {font-size: 11px; letter-spacing: -1px; font-family: Arial, Helvetica, sans-serif;}




-->
</style>
</head>

<body>

<table width="779" border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td width="771"><table width="915" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><div align="right">
          <div align="right"><span class="Estilo8"><a href="#" onclick="imprimir_reporte(); return false;">Imprimir</a> </span></div>
        </div></td>
      </tr>
      <tr>
        <td width="360" valign="bottom"><div align="left"><span class="Estilo9">REPORTE DE COMPRAS </span></div></td>
        <td width="294"><div align="left"></div></td>
        <td width="261">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><div align="left">
      <table width="899" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td width="105"><div align="left"><span class="Estilo8">FECHA DE REPORTE : </span></div></td>
          <td width="12"><div align="left"></div></td>
          <td width="116"><div  class="Estilo6" align="left"><?php echo date("d/m/Y");?></div></td>
          <td width="20">&nbsp;</td>
          <td width="122">&nbsp;</td>
          <td width="88">&nbsp;</td>
          <td width="111">&nbsp;</td>
          <td width="319">&nbsp;</td>
        </tr>
      </table>
    </div></td>
  </tr>
  
  <?php
  
  
  /*
  
 SELECT o.*, pt.*, dp.*, e.*, SUM(ordpeddet_precio * ordpeddet_cantidad) AS monto,  SUM(ordpeddet_cantidad) AS cantidad_total
FROM orden_pedido o
INNER JOIN pago_tipo pt ON o.pagtip_codi_fk=pt.pagtip_codi
INNER JOIN orden_pedido_detalle opd ON opd.ordped_codi_fk=o.ordped_codi
INNER JOIN documento_pago dp ON dp.docpag_codi=o.docpag_codi_fk
INNER JOIN empresas e ON o.emp_codi_fk=e.emp_codi
WHERE o.ordped_fecha<='2017-06-08' and o.ordped_fecha>='2017-01-01'
AND o.ordped_estado=1
GROUP BY o.ordped_fecha
ORDER BY o.ordped_fecha DESC, ordped_codi DESC; 
  
  */
  
  
  	@$xfecha_inicio = $_REQUEST['xfecha_inicio'];
  	@$xfecha_fin = $_REQUEST['xfecha_fin'];	

	@$xactivar_doc = $_REQUEST['xactivar_doc'];
	@$xpagodocumento_criterio = $_REQUEST['xpagodocumento_criterio'];
	
	@$xemp_nom = $_REQUEST['xemp_nom'];	
	

	@$xactivar_tc = $_REQUEST['xactivar_tc'];
	@$xtc_criterio = $_REQUEST['xtc_criterio'];
	@$xactivar_loc = $_REQUEST['xactivar_loc'];
	@$xciudad = $_REQUEST['xciudad'];
	@$xdistrito = $_REQUEST['xdistrito'];
	@$xactivar_emp = $_REQUEST['xactivar_emp'];
	@$xemp_criterio = $_REQUEST['xemp_criterio'];
	
	

	$xemp_nom ="";
	
	if($xemp_criterio!=NULL and trim($xemp_criterio!="")){
		$r_empresa = mysqli_query($conexion, "SELECT emp_nom FROM empresas e WHERE emp_codi=$xemp_criterio;");
		while($row_empresa = mysqli_fetch_array($r_empresa)){
			$xemp_nom = $row_empresa['emp_nom'];
	
		}
	}	

	if($xactivar_emp == 1 and trim($xemp_criterio)!=""){
		//$xcretip_nombre ="";		
	}else{
		$xemp_nom ="TODOS";
	}
	

	@$xs_empresa=$_SESSION['s_empresa'];																									
	if(@$xactivar_emp==1){
														
		if(trim($xemp_criterio)==""){
			$xsql_empresa = " ";
		}else{	
			$xsql_empresa = " AND o.emp_codi_fk=$xemp_criterio ";
		}	

	}else{
	
		$xsql_empresa = " AND o.emp_codi_fk=$xs_empresa ";
							
	}
																							




	if($xactivar_doc == 1 and trim($xpagodocumento_criterio)!=""){
		//$xcretip_nombre ="";		
	}else{
		$xdoc_nom ="TODOS";
	}
	

	$xdoc_nom ="";
	
	if($xpagodocumento_criterio!=NULL and trim($xpagodocumento_criterio!="")){
		$r_documento = mysqli_query($conexion, "select docpag_nombre from documento_pago WHERE docpag_codi=$xpagodocumento_criterio;");
		while($row_documento = mysqli_fetch_array($r_documento)){
			$xdoc_nom = $row_documento['docpag_nombre'];
	
		}
	}	

	if($xactivar_doc == 1 and trim($xpagodocumento_criterio)!=""){
		//$xcretip_nombre ="";		
	}else{
		$xdoc_nom ="TODOS";
	}
	

	if(@$xactivar_doc==1){
														
		if(trim($xpagodocumento_criterio)==""){
			$xsql_documento = " ";
		}else{	
			$xsql_documento = " AND o.docpag_codi_fk=$xpagodocumento_criterio ";
		}	

	}else{
	
		$xsql_documento = " ";
							
	}
																							
												
	
												
	
	
	
	//SELECT * FROM country c INNER JOIN city ct ON c.country_id = ct.country_id WHERE ct.city_id=13;
	//SELECT * FROM empresas e WHERE emp_codi=1;
  
  
  ?>
  
  <tr>
    <td><div align="left">
      <table width="909" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td><div align="left">
            <div align="right">
              <div align="left"><span class="Estilo8">CRITERIO DE FILTRO : </span></div>
            </div>
          </div></td>
          <td><div align="right" class="Estilo17">
            <div align="left"><span class="Estilo6">Rango de fechas  </span></div>
          </div></td>
          <td><div align="right" class="Estilo17">
            <div align="left"><span class="Estilo6">: </span></div>
          </div></td>
          <td><span class="Estilo6">Desde <?php echo date("d/m/Y",strtotime($xfecha_inicio));?> hasta <?php echo date("d/m/Y",strtotime($xfecha_fin));?></span></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="113">&nbsp;</td>
          <td width="127"><div align="right" class="Estilo17">
            <div align="left"><span class="Estilo6">Tipo de documento </span></div>
          </div></td>
          <td width="12"><div align="right" class="Estilo17">
            <div align="left"><span class="Estilo6">: </span></div>
          </div></td>
          <td width="415"><div  class="Estilo6" align="left"><?php echo $xdoc_nom;?></div></td>
          <td width="19">&nbsp;</td>
          <td width="30">&nbsp;</td>
          <td width="50">&nbsp;</td>
          <td width="134">&nbsp;</td>
        </tr>
        
        <tr>
          <td>&nbsp;</td>
          <td><div align="right" class="Estilo6">
            <div align="left">Empresa (sucursal) </div>
          </div></td>
          <td><div align="right" class="Estilo17">
            <div align="left"><span class="Estilo6">: </span></div>
          </div></td>
          <td><div align="left">
            <div  class="Estilo6" align="left"><?php echo $xemp_nom;?></div>
          </div></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </div></td>
  </tr>
  <tr>
    <td><div align="left"></div></td>
  </tr>



<?php
$xtotal_creditos = 0;




//$fechainicial = '2016-07-01';
//$fechafinal = '2017-06-08';

$fechainicial = $xfecha_inicio;
$fechafinal = $xfecha_fin;

/*
echo "FECHA INICIAL: ".$fechainicial;
echo "<br>";
echo "FECHA FINAL: ".$fechafinal;
echo "<br>";
*/

$anoinicial = date('Y',strtotime($fechainicial));
$anofinal = date('Y',strtotime($fechafinal));

$mesinicial = date('n',strtotime($fechainicial));
$mesfinal = date('n',strtotime($fechafinal));

/*
echo "MES INICIAL: ".$mesinicial;
echo "<br>";
echo "MES FINAL: ".$mesfinal;
echo "<br>";
*/

for($xn=$anoinicial; $xn<=$anofinal; $xn++){
	//echo "AŅO:".$xn;

?>

  <tr style="display:block; padding-top:20px;">
    <td height="23">
	<div >	
		<div align="left" style="float:left; " ><span class="Estilo9" style="font-size:20px;">A&Ntilde;O:<?php echo $xn; ?> </span></div>  
		<div style="float:left; padding-left:10px;"><span class="Estilo16"> <div id="d_cant_cobranzas" >  </div> </span></div>
	</div>	
	</td>
  </tr>
  
   
  <tr>
    <td>========================================================================================</td>
  </tr>
   
<?
	$xm_inicio=1;
	$xm_fin=12;
	if($xn==$anoinicial){
		$xm_inicio=$mesinicial;
	}
	if($xn==$anofinal){
		$xm_fin=$mesfinal;
	}
	
	for($xm=$xm_inicio; $xm<=$xm_fin; $xm++){
	
		$xmm = trim("0".$xm);
		$xmm = substr($xmm, strlen($xmm)-2, 2);
	
		$month = $xn.'-'.$xmm;
		$aux = date('Y-m-d', strtotime("{$month} + 1 month"));
		$last_day = date('Y-m-d', strtotime("{$aux} - 1 day"));		
		
		if($xn==$anoinicial and $xm==$xm_inicio){
			$first_day = $fechainicial;
		}else{
			$first_day = $xn.'-'.$xmm.'-01';			
		}
		if($xn==$anofinal and $xm==$xm_fin){
			$last_day = $fechafinal;
		}else{
			$last_day = $last_day;
		}
		
		
		switch ($xm) {
			case 1:
				$cmes = "ENERO";
				break;
			case 2:
				$cmes = "FEBRERO";
				break;
			case 3:
				$cmes = "MARZO";
				break;
			case 4:
				$cmes = "ABRIL";
				break;
			case 5:
				$cmes = "MAYO";
				break;
			case 6:
				$cmes = "JUNIO";
				break;
			case 7:
				$cmes = "JULIO";
				break;
			case 8:
				$cmes = "AGOSTO";
				break;
			case 9:
				$cmes = "SETIEMBRE";
				break;
			case 10:
				$cmes = "OCTUBRE";
				break;
			case 11:
				$cmes = "NOVIEMBRE";
				break;
			case 12:
				$cmes = "DICIEMBRE";
				break;				
				
		}
					
		//echo "...MES:".$xm. "(desde ".$first_day."  hasta ".$last_day .")";
		//echo "<br>";

?>
  <tr style="display:block; padding-top:10px;">
    <td>
	<div >	
		<div align="left" style="float:left; " ><span class="Estilo9"><?php echo $cmes." - ".$xn; ?> </span></div>  
		<div style="float:left; padding-left:10px;"><span class="Estilo16"> <div id="d_cant_cobranzas" >  </div> </span></div>
	</div>	
	</td>
  </tr>
  
   
  <tr>
    <td><div align="left">
      <div align="left">
        <table width="397" border="1" cellpadding="0" cellspacing="0" bordercolor="#397AA8">
          <tr>
            <td width="393"><table width="800" border="0" cellpadding="1" cellspacing="1">
                <tr>
                  <td width="79"><div align="left"><span class="Estilo8">FECHA</span></div></td>
                  <td width="137"><div align="left"><span class="Estilo8">TIPO DE DOCUMENTO</span></div></td>
                  <td width="203"><div align="left"><span class="Estilo8">EMPRESA</span></div></td>
                  <td width="119"><div align="right"><span class="Estilo8">PENDIENTE</span></div></td>
                  <td width="116"><div align="right"><span class="Estilo8">AMORTIZADO</span></div></td>
                  <td width="127"><div align="right"><span class="Estilo8">MONTO</span></div></td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td><table width="800" border="0" cellpadding="1" cellspacing="1">
                <?php 

				$r_ventas = mysqli_query($conexion,"SELECT o.ordcom_fecha, pt.pagtip_nombre, dp.docpag_nombre, e.emp_nom,
												SUM( ROUND((ordcomdet_precio - ordcomdet_precio_descuento) * (ordcomdet_cantidad - ordcomdet_cantidad_devolver),2)) AS monto, 
												SUM(ordcomdet_cantidad - ordcomdet_cantidad_devolver) AS cantidad_total
												FROM orden_compra o
												INNER JOIN pago_tipo pt ON o.pagtip_codi_fk=pt.pagtip_codi
												INNER JOIN orden_compra_detalle ocd ON ocd.ordcom_codi_fk=o.ordcom_codi
												INNER JOIN documento_pago dp ON dp.docpag_codi=o.docpag_codi_fk
												INNER JOIN empresas e ON o.emp_codi_fk=e.emp_codi
												WHERE o.ordcom_fecha<='$last_day' and o.ordcom_fecha>='$first_day'
												AND o.ordcom_estado=1 "
												.$xsql_empresa.$xsql_documento.
												"GROUP BY o.ordcom_fecha
												ORDER BY o.ordcom_fecha;");
				
				$xamortizacion = 0;
				$xmonto_total_mes = 0;
				$xamortizacion_total_mes = 0;
				$xpendiente_total_mes = 0;
				$nr = 0;
				
				while($row_ventas = mysqli_fetch_array($r_ventas)){
					
					$xamortizacion = 0;
					
					$xfecha = $row_ventas['ordcom_fecha'];
					//$xemp_nom = $row_ventas['emp_nom'];
					$xmonto = number_format($row_ventas['monto'],2,'.','');
					
					$xmonto_total_mes = $xmonto_total_mes + $xmonto;
																
					$xamortizacion = $creform->calcular_amortizacion_compra($conexion, $xfecha, $xsql_empresa, $xsql_documento);
					$xamortizacion = number_format($xamortizacion,2,'.','');
					$xamortizacion_total_mes = $xamortizacion_total_mes + $xamortizacion;
					
					$xpendiente = $xmonto - $xamortizacion;					
					$xpendiente_total_mes = $xpendiente_total_mes + $xpendiente;
					
					$nr = $nr + 1;
					
					if($nr % 2 == 1){
						$xcolor_linea = "#FFFFFF";
					}else{
						$xcolor_linea = "#E9E9E9";					
					}
						
				?>
                <tr style="background-color:<?php echo $xcolor_linea;?>;">
                  <td width="80"><div align="left"><span class="Estilo16"><?php echo date("d/m/Y",strtotime($xfecha));?></span></div></td>
                  <td width="137"><div align="center">
                      <div align="left"><span class="Estilo16"><?php echo $xdoc_nom;?></span></div>
                  </div></td>
                  <td width="202"><div align="center">
                      <div align="center">
                        <div align="left"><span class="Estilo16"><?php echo $xemp_nom;?></span></div>
                      </div>
                  </div></td>
                  <td width="120"><div align="center">
                      <div align="center">
                        <div align="right"><span class="Estilo16"><?php echo number_format($xpendiente,2,'.',',');?></span></div>
                      </div>
                  </div></td>
                  <td width="116"><div align="center">
                      <div align="center">
                        <div align="right"><span class="Estilo16"><?php echo number_format($xamortizacion,2,'.',',');?></span></div>
                      </div>
                  </div></td>
                  <td width="126"><div align="right"><span class="Estilo16"><?php echo number_format($xmonto,2,'.',',');?></span></div></td>
                </tr>

				<?php
				}
				?>

                <tr>
                  <td colspan="6">========================================================================================</td>
                  </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td><div align="right"><span class="Estilo8">TOTAL: </span></div></td>
                  <td><div align="right"><span class="Estilo16"><?php echo number_format($xpendiente_total_mes,2,'.',',');?></span></div></td>
                  <td><div align="right"><span class="Estilo16"><?php echo number_format($xamortizacion_total_mes,2,'.',',');?></span></div></td>
                  <td><div align="right"><span class="Estilo16"><?php echo number_format($xmonto_total_mes,2,'.',',');?></span></div></td>
                </tr>
				
				
				
            </table></td>
          </tr>
        </table>
      </div>
    </div></td>
  </tr>
  
<?php

	}
}
?>
  
  
  
  <tr>
    <td>&nbsp;</td>
  </tr>
  
  

	<?php

	?>
  <tr>
    <td>&nbsp;</td>
  </tr>
  

  
  <tr>
    <td>========================================================================================</td>
  </tr>
   

  <tr>
    <td><div align="left"><span class="Estilo6">USUARIO: <?php //echo $xcre_usu_crea;?></span></div></td>
  </tr>
</table>

<script language="javascript">
covertirNumLetras("<?php echo $xtotal_venta;?>","tnumeros");
</script>	


</body>
</html>
