
<script language="javascript">

function pon_prefijo() {
	parent.document.getElementById("form_busqueda").submit();
	parent.document.getElementById('bloquea').style.display='none';
}



</script>

<?php
include ("../conexion.php");
include ("../funciones/fechas.php");

$consulta1 = "DELETE FROM cartera_cxp ";  ////// Limpia la tabla cartera_cxc
$mysqli->query($consulta1);


$fechainicio=$_GET["fechainicio"];
$fechainicio= implode("-", array_reverse(explode("-", $fechainicio))); 

	   $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $tazint =  $row_para["INTMOR"];

//////////////   seleciona la tabla cxc (2101) 
				 
				 
	$sel_resultado="SELECT * FROM web_2201 WHERE salact > 0 " ;

if ($resultado = $mysqli->query($sel_resultado)) 

 
    while ($fila = $resultado->fetch_assoc()) {

        $fecfac = $fila["fecfac"];
		$fecfactura     = implode("-", array_reverse(explode("-", $fecfac)));
		$fecven = $fila["fecven"];
		$fecvencimiento = implode("-", array_reverse(explode("-", $fecven)));
        $numfac = $fila["numfac"];
        $salact = $fila["salact"];
        $totfac = $fila["totfac"];
		$cdgpro = $fila["cdgpro"];
		$dscpro = $fila["dscpro"];			 
				 
				 $segundos=strtotime($fechainicio) - strtotime($fecven);  ///calcula los dias de vencimiento
				 $diaven=intval($segundos/60/60/24);		 
				 
	
		if 	($diaven  > 0 ) {         //////  calcula los intereses segun los dias de vencimiento 
		$taza=$tazint/100;
		$interes=$salact*$taza;
		$saldo=$interes/30;
		$intmor=$saldo*$diaven;
		} else {
		$intmor=0;

	}
				//////////////  Asigna el saldo pendiente segun la categoria de vencimiento //////// 
				
								//    cambio version   8.0.2 se igualan a 0 las variables
				 
		if($diaven <= 0 ){         
			$al_dia=$salact;
			$d0_15=0;
			$d15_30=0;
			$d30_60=0;
			$d60_90=0;
			$d90_mas=0;

			}
			elseif(($diaven > 0) && ($diaven <= 15)) {
			$al_dia=0;
			$d0_15=$salact;
			$d15_30=0;
			$d30_60=0;
			$d60_90=0;
			$d90_mas=0;
			}
			elseif(($diaven > 15) && ($diaven <= 30)) {
			$al_dia=0;
			$d0_15=0;
			$d15_30=$salact;
			$d30_60=0;
			$d60_90=0;
			$d90_mas=0;
			
			}
			elseif(($diaven > 30) && ($diaven <= 60)) {
			$al_dia=0;
			$d0_15=0;
			$d15_30=0;
			$d30_60=$salact;
			$d60_90=0;
			$d90_mas=0;
			}
			elseif(($diaven > 60) && ($diaven <= 90)) {
			$al_dia=0;
			$d0_15=0;
			$d15_30=0;
			$d30_60=0;
			$d60_90=$salact;
			$d90_mas=0;
	
		}else{
			$al_dia=0;
			$d0_15=0;
			$d15_30=0;
			$d30_60=0;
			$d60_90=0;
			$d90_mas=$salact;
		}
			$salven= $d0_15+ $d15_30 + $d30_60 + $d60_90 + $d90_mas	;
			 
			 
	   ///////////////////////  inserta en la tabla cartera_cxc los resultados ////////////
	   
	$sel_insertar="INSERT INTO cartera_cxp (numfac,dscpro,cdgpro,fecfac,fecven,salact,totnet,diaven, al_dia,salven, 0_15, 15_30, 30_60, 60_90, 90_mas, feccal, intmor) VALUES 
		('$numfac','$dscpro','$cdgpro','$fecfac','$fecven','$salact','$totfac','$diaven','$al_dia','$salven','$d0_15','$d15_30','$d30_60','$d60_90','$d90_mas','$fechainicio','$intmor')";
$mysqli->query($sel_insertar);		
	
	

	
			}	

		

	
	$consulta2="SELECT 	cdgpro,dscpro, sum(al_dia) as al_dia , sum(0_15) as 0_15, sum(15_30) as 15_30, sum(30_60) as 30_60 , sum(60_90) as 60_90  , sum(90_mas) as 90_mas , sum(intmor) as intmor, sum(salven) as salven FROM cartera_cxp   GROUP BY cdgpro ORDER BY dscpro ASC " ;


if ($resultado2 = $mysqli->query($consulta2)) 

 
    while ($fila = $resultado2->fetch_assoc()) {
        $al_dia  = $fila["al_dia"];
        $d0_15   = $fila["0_15"];
		$d15_30  = $fila["15_30"];
		$d30_60  = $fila["30_60"];
        $d60_90  = $fila["60_90"];
        $d90_mas = $fila["90_mas"];
        $salven  = $fila["salven"];
        $intmor  = $fila["intmor"];
		$salpag  = $salven+$al_dia+$intmor;
	    $salact  = $salven+$al_dia;
		$cdgpro  = $fila["cdgpro"];
		$cdgpro  = sprintf("%06d",$cdgpro); 
		$dscpro  = $fila["dscpro"];
		$fecfac = $fila["fecfac"];
		$fecfactura     = implode("-", array_reverse(explode("-", $fecfac)));
		$fecven = $fila["fecven"];
		$fecvencimiento = implode("-", array_reverse(explode("-", $fecven)));
        $numfac = $fila["numfac"];
        $salact = $fila["salact"];
        $totfac = $fila["totfac"];
		
		
				 	
				
				$consulta2 = "DELETE FROM cartera_cxp WHERE cdgpro =  $cdgpro ";  ////// Limpia la tabla cartera_cxc
				$mysqli->query($consulta2);	
			
			
	$sel_insertar="INSERT INTO cartera_cxp (numfac,dscpro,cdgpro,fecfac,fecven,salact,totnet,diaven, al_dia,salven, 0_15, 15_30, 30_60, 60_90, 90_mas, feccal, intmor) VALUES 
		('$numfac','$dscpro','$cdgpro','$fecfac','$fecven','$salact','$totfac','$diaven','$al_dia','$salven','$d0_15','$d15_30','$d30_60','$d60_90','$d90_mas','$fechainicio','$intmor')";
$mysqli->query($sel_insertar);		
		
		
			
			
						 $contador++;
							}

?>
	<script>
		parent.document.getElementById("form_busqueda").submit();
		parent.document.getElementById('bloquea').style.display='none';
   </script> 


