<?php
 
if(!isset($num_doc) || strcmp(trim($num_doc),"")==0){
	echo '<h2>&iexcl;&iexcl;&iexcl; ESCRIBE EL NUMERO DE DOCUMENTO !!!</h2>';
	echo '<meta content="3; url='.$pag_reg1.'?bot='.$num_reg.'" HTTP-EQUIV="REFRESH">';
	exit();
}
//buscar si existe la cuen ta contable
@$sql = mysqli_query($con ,"select * from dycatcta where cta_con = '$cve_mto'");
@$res=mysqli_num_rows($sql); 
if($res==0){
	//echo '<h2>&iexcl;&iexcl;&iexcl;LA CUENTA CONTABLE NO EXISTE!!!</h2>';
	//if($regresar == 1){
		//echo '<meta content="2; url='.$pag_reg1.'?bot='.$num_reg.'" HTTP-EQUIV="REFRESH">';
	//}
	//exit();
}	
if($tm == "A"){//agregar
	@$dtNumRows = mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows FROM dvredocl WHERE tip_mov='$tip_per' AND num_doc = '$num_doc' and codigo = '$cli_pro' "));
	if($dtNumRows['numRows']>0){//no se puede agregar registro error y salir
		echo '<h2>!!! EL REGISTRO YA EXISTE...!!!'.$cli_pro.'-'.$num_doc.'</h2>';
		if($regresar == 1) echo '<meta content="3; url='.$pag_reg1.'?bot='.$num_reg.$ArgsPag_reg.'" HTTP-EQUIV="REFRESH">';
	}else{//si se puede agregar registro
	   	@$resulty = mysqli_query($con ,"insert into dvredocl (codigo, tip_mov, fec_doc, fec_rev, num_doc, cve_mto, sub_tot, iva_tot, total, cve_iva, ret1, ret1_po, ret1_im, ret2, ret2_po, ret2_im, tot_doc, tot_pag, tot_not, tot_car, tot_abo, tot_sal, moneda, cve_pai, tip_cam, dia_cre, con_cre, tip_mot, fec_ven, des, ori_doc,acc_fec, acc_hor, usuario, afe_pol,afe_ietu,cve_mon,afe_caj,num_fac_ant) values ('$cli_pro', '$tip_per', '$fec_doc', '$fec_rev', '$num_doc', '$cve_mto','$sub_tot', '$iva_tot', '$total', '$cve_iva','$ret1', '$ret1_po', '$ret1_im', '$ret2','$ret2_po', '$ret2_im', '$tot_doc', '$tot_pag','$tot_not', '$tot_car', '$tot_abo', '$tot_sal','$moneda', '$cve_pai', '$tip_cam', '$dia_cre','$con_cre', '$tip_mot', '$fec_ven','$des','$ori_doc', CURDATE(), CURTIME(),'".$_SESSION['num_usu']."', '$afe_pol','$afe_ietu','$cve_mon','$afe_caj','$num_fac_ant')") or die(mysqli_error($con));
		@$num_id = mysqli_insert_id();//numero de registro del documento agregado
		if($num_id <> ""){//si se agrego registro
			//Si se Dispone de Array de Detalle Insertarlo
			$totTotDetOcl=0;
			if(isset($arr_detdoc) && count($arr_detdoc)>0){
				for($conDetDocOcl=0;$conDetDocOcl<count($arr_detdoc);$conDetDocOcl++){
					@$ope = mysqli_query($con ,"insert into dvredocd (fol_ope, codigo, tip_mov, fec_doc, num_doc, cve_mto, sub_tot, acc_fec, acc_hor, usuario) values ('$num_id', '$cli_pro', '$tip_per', '$fec_doc', '$num_doc', '".$arr_detdoc[$conDetDocOcl]['cve_mto']."', '".$arr_detdoc[$conDetDocOcl]['sub_det']."', CURDATE(), CURTIME(),'".$_SESSION['num_usu']."')");
					$totTotDetOcl +=$arr_detdoc[$conDetDocOcl]['tot_det'];
				}
				if(round($totTotDetOcl,2)!=round('$sub_tot') && $verDetErr){
					echo "<h2>EL SUBTOTAL DEL DETALLE NO COINCIDE CON EL DE DOCUMENTO</h2>";
				}
			}
			else{
				//en caso de cuenta contable de cuentas x cobrar o por pagat
				if ($cuenta_para)  @$ope = mysqli_query($con ,"insert into dvredocd (fol_ope, codigo, tip_mov, fec_doc, num_doc, cve_mto, sub_tot, acc_fec, acc_hor, usuario) values ('$num_id', '$cli_pro', '$tip_per', '$fec_doc', '$num_doc', '$cuenta_para', '$sub_tot', '".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."')");
				else @$ope = mysqli_query($con ,"insert into dvredocd (fol_ope, codigo, tip_mov, fec_doc, num_doc, cve_mto, sub_tot, acc_fec, acc_hor, usuario) values ('$num_id', '$cli_pro', '$tip_per', '$fec_doc', '$num_doc', '$cve_mto', '$sub_tot', '".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."')");
			}
		}else{
			echo '<h2>!!! NO SE HA PODIDO AGREGAR EL REGISTRO... ACTUALIZANDO !!!</h2>';
			if($regresar == 1)	echo '<meta content="3; url='.$pag_reg2.'?bot='.$num_id.$ArgsPag_reg.'" HTTP-EQUIV="REFRESH">';
		}  
	}
}elseif($tm == "M"){//Modificar
	@$resulty = mysqli_query($con ,"UPDATE dvredocl set codigo='$codigo', tip_mov='$tip_per', fec_rev='$fec_rev',fec_doc='$fec_doc', num_doc='$num_doc', cve_mto='$cve_mto', sub_tot='$sub_tot', iva_tot='$iva_tot', total='$total', cve_iva='$cve_iva', ret1='$ret1', ret1_po='$ret1_po', ret1_im='$ret1_im', ret2='$ret2', ret2_po='$ret2_po', ret2_im='$ret2_im', tot_doc='$tot_doc', tot_pag='$tot_pag', tot_not='$tot_not', tot_car='$tot_car', tot_abo='$tot_abo', tot_sal='$tot_sal', moneda='$moneda', cve_pai='$cve_pai', tip_cam='$tip_cam', dia_cre='$dia_cre', con_cre='$con_cre', tip_mot='$tip_mot', fec_ven='$fec_ven', des='$des',ori_doc ='$ori_doc',acc_fec='".date('Y-m-d')."', acc_hor='".date('H:i:s')."', usuario='".$_SESSION['num_usu']."', afe_pol='$afe_pol',afe_ietu='$afe_ietu', afe_caj='$afe_caj' WHERE tip_mov='$tip_per' and codigo = '$codigo' and num_doc = '$num_doc'");
}
elseif($tm == "E"){//Eliminar
	@$dt_redocl=mysqli_fetch_array(mysqli_query($con ,"SELECT * from dvredocl where n_reg='$nrr'"));
	if($dt_redocl['tot_pag']==0 && $dt_redocl['tot_abo']==0 && $dt_redocl['tot_car']==0 && $dt_redocl['tot_not']==0){
		@mysqli_query($con ,"delete from dvredocd where fol_ope = '$nrr'");
		@mysqli_query($con ,"delete from dvredocl where n_reg = '$nrr'");
	}else{
		echo '<h2>IMPOSIBLE ELIMINAR,MOVIMIENTOS EXISTENTES EN CUENTA</h2>';
	}
}elseif($tm == "C"){//Consultar
	
}

/**************************************************ACTUALIZAR BASCPT************************************************************/
($tip_per=='C')?($fieldAct='sal_deu'):($fieldAct='sal_acr');
$sq_bas="
UPDATE 
	dxbascpt 
SET 
	$fieldAct=(SELECT SUM(tot_sal) FROM dvredocl WHERE tip_mov='$tip_per' AND codigo='$codigo' GROUP BY tip_mov)
WHERE
	codigo='$codigo'";
//echo $sq_bas;
//exit();	
$rsCli=mysqli_query($con ,$sq_bas);
/********************************************************************************************************************************/
if($regresar == 1) 	echo '<meta content="1; url='.$pag_reg2.'?bot='.$num_id.$ArgsPag_reg.'"  HTTP-EQUIV="REFRESH">';
?>