<?php
 
session_start();
$com="M";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
include("DX25000_CL.php"); 
$flagReject=true;
if($_SESSION['modlab'] && $flagReject){
	require_once('DMODBCCON.php');
}
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>
<body>
<?php
 
	//cancela facturas
	$hisMovFac=new HisMovSis(array("mod"=>"DG33000_EC","tabAfe"=>"dgfactur"));
	$hisMovPem=new HisMovSis(array("mod"=>"DG33000_EC","tabAfe"=>"dgpemema"));	
	
	$arrLlaves = array_keys($_POST);
	$TotalRecibidos = count($arrLlaves);
	for ($i=0;$i<$TotalRecibidos;$i++){
		$let = substr($arrLlaves[$i],0,3);
		if($let == "sel"){
			 if($_POST[$arrLlaves[$i]] <> ""){
				// -- > echo 'cancela la numero'.$_POST[$arrLlaves[$i]].'<br>';
				// -- >busco el folio de la factura a eliminar 
				$folio_factura = $_POST[$arrLlaves[$i]];
				$sql = mysqli_query($con ,"select * from dgfactur where fol_fac = '$folio_factura'");
				$reg = mysqli_fetch_array($sql);
				$dt_catcli=mysqli_fetch_array(mysqli_query($con ,"SELECT codigo FROM dgcatcli WHERE cod_cli='".$reg['cod_cli']."'"));
				// -- >limpia detalle de la factura (dgfactur)
				mysqli_query($con ,"delete from dgfactud where cons_fac = '".$reg['fol_fac']."'");
				// -- >limpia los totales de la factura (dgfactud)
				mysqli_query($con ,"update dgfactur set sub_ven = '0', iva_ven = '0', tot_ven = '0', cancelado = '1', fec_can = '".date("Y-m-d")."' where fol_fac = '$folio_factura'");
				if($reg['con_cre']=='W') $esFac='S'; else $esFac='0';
				// -- >limpio el numero de la factura de los pedidos
				mysqli_query($con ,"update dgpemema set num_fac =fac_ant,fac_ant='0', facturado = '$esFac' where num_fac = '".$reg['num_fac']."' AND tip_ven='".$reg['tip_ven']."'");
				$hisMovPem->resetData(array(
					"regAfe"=>"num_fac = '".$reg['num_fac']."' AND tip_ven='".$reg['tip_ven']."'",
					"tipMov"=>"M",
					"obsMov"=>"update dgpemema set num_fac =fac_ant,fac_ant='0', facturado = '$esFac' where num_fac = '".$reg['num_fac']."' AND tip_ven='".$reg['tip_ven']."'"
				));
				$hisMovPem->agregar();				
				$num_fac = $reg['tip_fac'].$reg['num_fac'];
				//echo $num_fac.'<br>';
				
				//-------------------------------->ELIMINAR REGISTRO COBROS CXC---------------------------->
				$sq_cobdoc=mysqli_query($con ,"SELECT * FROM dvcobdoc WHERE tip_mto='C' AND codigo='".$dt_catcli['codigo']."' AND num_doc='$num_fac'");
				while($dt_cobdoc=mysqli_fetch_array($sq_cobdoc)){
					$actRedocl=false;
					$tm1 = "EFC";
					$tip_mto="C";
					$nnrr    = $dt_cobdoc['n_reg'];
					$fol_cob = $dt_cobdoc['fol_cob'];
					$num_doc = $dt_cobdoc['num_doc'];
					$codigo  = $dt_cobdoc['codigo'];
					$tm = "AS";//Tipo de movimiento AGRAGAR (A) MODIFICAR (M) ELIMINAR (E)
					$num_reg = 0;
					$pag_reg1 = "DG35000_PR.php";//A DONDE VA SI ES TODO OK
					$pag_reg2 = "DG35000_PR.php";//A DONDE VA SI ES ERROR
					$regresar = 0; 
					$tip_per = "C";
					include('DV35000_CL.php');				
				}
				//------------------------------------------------------------------------------------------>				
				// -- >limpia los totales de la factura (dvredocl)
				$regx = mysqli_fetch_array(mysqli_query($con ,"select * from dvredocl where tip_mov='C' AND num_doc = '$num_fac' AND codigo='".$dt_catcli['codigo']."'"));
				$tm = "M";//tipo de movimiento AGREGAR (A) MODIFICAR (M) ELIMINAR (E)
				$pag_reg1 = "DG33000_PR.php";//A DONDE VA SI ES TODO OK
				$pag_reg2 = "DG33000_PR.php";//A DONDE VA SI ES ERROR
				$num_doc = $num_fac;//numero de docuemnto 
				$codigo = $regx['codigo'];//cliente
				$tip_per = "C";//CLIENTE O PROVEEDOR
				$fec_doc = $regx['fec_doc'];//FECHA DE LA FACTURA
				$sub_tot=0;
				$iva_tot=0;
				$total=0;
				$cve_iva = $regx['cve_iva'];//clave del iva
				$ret1=$regx['ret1'];
				$ret1_po=$regx['_po'];
				$ret1_im=$regx['_im'];     
				$ret2=$regx['ret2'];
				$ret2_po=$regx['_po'];
				$ret2_im=$regx['_im'];
				$tot_doc=0;
				$moneda=$regx['moneda'];
				$cve_pai=$regx['cve_pai'];
				$tip_cam=$regx['tip_cam'];
				$dia_cre=$regx['dia_cre'];
				$con_cre=$regx['con_cre'];
				$tip_mot=$regx['tip_mot'];
				$fec_ven=$regx['fec_ven'];
				$des=$regx['des'];
				$regresar = 1; //determina si regresa1 = SI   2 = NO
				include("DYDOCXCP.php");
				mysqli_query($con ,"delete from dvredocd where num_doc = '$num_fac'");
				
				//---------------------------->Actualizar Saldos dvredocl y Cliente------------------------>
				$actRedocl=true;
				$tm1 = "";
				$tip_mto="C";
				$tip_per = "C";
				$num_doc =  $num_fac;
				$codigo  = $regx['codigo'];
				$tm = "AS";//Tipo de movimiento AGRAGAR (A) MODIFICAR (M) ELIMINAR (E)
				$regresar = 0; 
				include('DV35000_CL.php');				
				//------------------------------------------------------------------------------------------>

				 mysqli_query($con ,"DELETE FROM dgfactur WHERE fol_fac = '$folio_factura'");
				 mysqli_query($con ,"DELETE FROM dvredocl WHERE num_doc='$num_fac' AND tip_mov='C'");
				 mysqli_query($con ,"DELETE FROM dvredocd WHERE num_doc='$num_fac' AND tip_mov='C'");
				 
				$hisMovFac->resetData(array(
					"regAfe"=>"fol_fac = '$folio_factura'",
					"tipMov"=>"E",
					"obsMov"=>"DELETE FROM dgfactur WHERE fol_fac = '$folio_factura'"
				));
				$hisMovFac->agregar();
			 }
			 /********************************/
		}
	}
?>
</body>
</html>