<?php
 
session_start();
include("DMMYSQLG.php");
require_once("DF80000.php");
?>
<html>
<head>
<title>Regenera Saldos CxC y Cxp</title>
</head>
<body>
<h2>!!! Regenerando Saldos...!!!</h2>
<img src="GIF32.gif" width="123" height="6" id="imagen_espera" name="imagen_espera">
<?php

$timeIni=date('H:i:s');
//limpiar saldos
mysqli_query($con ,"update dxbascpt set sal_deu = 0,sal_acr = 0, acc_hor =CURTIME(), usuario = '".$_SESSION['num_usu']."'");
//regenera saldos
$sq_redocl=mysqli_query($con ,"select * from dvredocl");
//
mysqli_query($con ,"optimize table dvredocl");
mysqli_query($con ,"repair  table dvredocl");

mysqli_query($con ,"optimize table dvnotcre");
mysqli_query($con ,"repair  table dvnotcre");

mysqli_query($con ,"optimize table dvcobdoc");
mysqli_query($con ,"repair  table dvcobdoc");

mysqli_query($con ,"alter table dxbascpt add index(codigo)");
mysqli_query($con ,"optimize table dxbascpt");
mysqli_query($con ,"repair table dxbascpt");
while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
	$cod = $dt_redocl['codigo'];$sal_deu=0;$sal_acr=0;
	$sq_notcre=mysqli_query($con ,"select sum(tot_not) as sum_not from dvnotcre where num_doc = '".$dt_redocl['num_doc']."' and tip_mto ='".$dt_redocl['tip_mov']."' and codigo=$cod");
	$dt_notcre=mysqli_fetch_array($sq_notcre);
	//echo $dt_notcre['tot_not'].'<br>';
	$sq_cobdoc=mysqli_query($con ,"select sum(tot_cob) as sum_cob from dvcobdoc where num_doc = '".$dt_redocl['num_doc']."' and tip_mto ='".$dt_redocl['tip_mov']."' and codigo=$cod");
	$dt_cobdoc=mysqli_fetch_array($sq_cobdoc);
	//echo 'val:'.$dt_redocl['n_reg'];
    $dt_redocl['total'] = $dt_redocl['sub_tot'] + $dt_redocl['iva_tot'];
	$dt_redocl['tot_doc'] = $dt_redocl['total'] - $dt_redocl['_im'] - $dt_redocl['_im'];
	$tot_sal = $dt_redocl['tot_doc'] - $dt_cobdoc['sum_cob'] - $dt_notcre['sum_not'] + $dt_redocl['tot_car'] - $dt_redocl['tot_abo'];	
	//exit();
	$sq0_redocl=mysqli_query($con ,"update dvredocl set total = '".$dt_redocl['total']."',tot_doc = '".$dt_redocl['tot_doc']."',tot_pag = '".$dt_cobdoc['sum_cob']."', tot_not = '".$dt_notcre['sum_not']."', tot_sal = '$tot_sal', acc_hor =  '".date('H:i:s')."', usuario = '".$_SESSION['num_usu']."' where n_reg = '".$dt_redocl['n_reg']."'");
}
	//saldo deudor
$sq_redoc0=mysqli_query($con ,"select * from dvredocl where tot_sal <> 0");
while(@$dt_redoc0=mysqli_fetch_array($sq_redoc0)){
//	$sq_bascpt=mysqli_query($con ,"select * from dxbascpt where codigo = '".$dt_redocl['codigo']."'");
//	$dt_bascpt=mysqli_fetch_array($sq_bascpt);
	$sde =0;$sac=0;
	if($dt_redoc0['tip_mov']=='C'){
	   $sde = $dt_redoc0['tot_sal'];
	}else{
	   $sac = $dt_redoc0['tot_sal'];
	}
	mysqli_query($con ,"update dxbascpt set sal_deu = sal_deu + $sde , sal_acr = sal_acr + $sac ,acc_hor =  '".date('H:i:s')."', usuario = '".$_SESSION['num_usu']."' where codigo = '".$dt_redoc0['codigo']."'");
}
$timeFin=date('H:i:s');
/*mysqli_query($con ,"alter table dxbascpt drop index codigo");*/
mysqli_query($con ,"optimize table dxbascpt");
mysqli_query($con ,"repair table dxbascpt");
echo '<h2>FELICIDADES SALDOS REGENERADOS CXC Y CXP EN: '.difTime($timeIni,$timeFin).' (H:m:s)</h2>' ;
//echo '<meta content="3; url=DV64000_PR.php" HTTP-EQUIV="REFRESH">';
?>
</body>
</html>