<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include('DF30000.inc');

// Graba Detalle Poliza  
function fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo){
		$sql="insert into dcdpoliz (tip_pol, fec_pol, num_pol, num_reg, cta_con, desc_con, cargo, abono) values ('$tpol', '$fpol', '$npol','$nr', '$cta', '$ddpol','$car', '$abo')";
	   	$result = mysqli_query($con ,$sql);
}
// verifica cta contable  
function fver_cta($cta,$doc,$er){
	@$dt_catcta=mysqli_fetch_array(mysqli_query($con ,"select * from dycatcta where cta_con = $cta")); // obtiene cta contable
	global $er;
	$er ='';
	if($dt_catcta['cta_con']<>$cta)$er ="s";
	if($dt_catcta['tipo']<>'D')$er ="s";
}


// -----Empieza Formulario -----------------
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>

<body>
<div align="center">
<form name="form1" id="form1" method="post" action="">
  <table width="906"  align="center">
    <tr>
      <th colspan="4">GENERA POLIZAS DE CUENTAS X PAGAR </th>
    </tr>
    <tr>
      <td width="146">A&ntilde;o Proceso </td>
      <td width="113"><select name="axo" id="axo">
          <?php
 
			$sq_a = mysqli_query($con ,"select axo from dccalend group by axo DESC");
			while($re_a = mysqli_fetch_array($sq_a)){
				echo '<option value = "'.$re_a['axo'].'">'.$re_a['axo'].'</option>';
				}
			?>
      </select>      </td>
      <td width="144">Mes Proceso </td>
      <td width="411"><select name="mes" id="mes">
          <option value="01" selected>Enero</option>
          <option value="02">Febrero</option>
          <option value="03">Marzo</option>
          <option value="04">Abril</option>
          <option value="05">Mayo</option>
          <option value="06">Junio</option>
          <option value="07">Julio</option>
          <option value="08">Agosto</option>
          <option value="09">Septiembre</option>
          <option value="10">Octubre</option>
          <option value="11">Noviembre</option>
          <option value="12">Diciembre</option>
      </select></td>
    </tr>
          <tr>
          <td colspan="4" align="center" class="inputcodigo"> <input name="calcula" type="submit" id="calcula" value="Genera Polizas" /></td>
        </tr>
      <tr>
        <td colspan="4" class="typeagre">
		<div align="left">
<?php
 
if(isset($_POST['calcula'])){
   	$f1 = $_POST['axo'].'-'.$_POST['mes'].'-01';
	$fr_fec = 'AMD';
    $fr_fecr = 'Ymd';
    $sep_fecr = '-';
	include('DF70000.php');

	$dt_parame=mysqli_fetch_array(mysqli_query($con ,"select * from dvparame where n_reg ='1'")); // parametros generales

	//$fec_ini = $fec_imes;
	$fec_ini = "2009-12-07";
	$fec_fin = $fec_fmes; 
	$tmov = "P"; // indica c x p
	$tpol = $dt_parame['tip_pol_gas']; // tipo de poliza
	$ori = "DW"; // indica ORIGEN c x p
	$hayerr = "N";
	echo 'Procesando Polizas del '.$fec_ini.' al '.$fec_fin.'<br>';
	
	//verfica proceso de polizas que no haya errores
	$sq_redocl=mysqli_query($con ,"select * from dvredocl where tip_mov='$tmov' and fec_doc between '$fec_ini' and '$fec_fin' group by fec_doc order by fec_doc");
	while($dt_redocl=mysqli_fetch_array($sq_redocl)){ //lee agrupado x rango de fecha
		echo 'Verificando y Procesando Fecha del '.$dt_redocl['fec_doc'].'<br>';
		$auxFecAct=explode('-',$dt_redocl['fec_doc']);
		$npol=$auxFecAct[2];
	   	$f1 = '".$dt_redocl['fec_doc']."';$fr_fec = 'AMD';$fr_fecr = 'Ymd';$sep_fecr = '/';
		include('DF70000.php');
		$fpol=$dt_redocl['fec_doc'];
		$ipol=0;

		$sq_redocl0=mysqli_query($con ,"select * from dvredocl where tip_mov='$tmov' and fec_doc = '".$dt_redocl['fec_doc']."' order by codigo,num_doc");
		while($dt_redocl0=mysqli_fetch_array($sq_redocl0)){ //lee doctos x fecha
			$tcar=0;$tabo=0;
			$dt_tasiva=mysqli_fetch_array(mysqli_query($con ,"select * from dxtasiva where cve_iva='".$dt_redocl0['cve_iva']."'")); // Obt Cta Iva
			$sq_redocd=mysqli_query($con ,"select * from dvredocd where tip_mov='$tmov' and codigo = '".$dt_redocl0['codigo']."' and num_doc = '".$dt_redocl0['num_doc']."' and fec_doc = '".$dt_redocl0['fec_doc']."'");
			$erc = 0; //deja contador errores cta en 0
			while($dt_redocd=mysqli_fetch_array($sq_redocd)){ //lee detalle doctos
//				echo '		Procesando Detalle ('.$dt_redocd['n_reg'].')'.$dt_redocd['num_doc'].' $'.$dt_redocd['sub_tot'].'<br>';
				$cta=$dt_redocd['cve_mto'];
				$car=$dt_redocd['sub_tot'];
				$abo='0.00'; 
				$ipol +=$car;$tcar +=$car;
				fver_cta($cta,$dt_redocl['num_doc'],$er);//verifica cta
				if($er=='s') $erc += 1;
			}//termina while detalle doctos
			if($dt_redocl0['iva_tot']<>0){
				$car=$dt_redocl0['iva_tot'];
				$abo='0.00'; 
				$ipol +=$car;$tcar +=$car;
			}
			if($dt_redocl0['_im']<>0){
				$abo=$dt_redocl0['_im'];$tabo +=$abo;
				$car='0.00'; 
			}
			if($dt_redocl0['_im']<>0){
				$abo=$dt_redocl0['_im'];$tabo +=$abo;
				$car='0.00'; 
			}
			$cta=$dt_redocl0['cve_mto'];
			$abo=$dt_redocl0['tot_doc'];$tabo +=$abo;
			$car='0.00'; 
			fver_cta($cta,$dt_redocl['num_doc'],$er);//verifica cta
			if($er=='s'){ 
				@$dt_catmov=mysqli_fetch_array(mysqli_query($con ,"select * from dvcatmov where cod_usu ='".$dt_redocl0['codigo']."' and tip_usu='p'"));
			}
			if($er=='s' and $dt_catmov['cod_usu']==$dt_redocl0['codigo']){
				$er='n';
				$dt_redocl0['cve_mto']=$dt_catmov['id_cta'];
				$cta=$dt_redocl0['cve_mto'];
				@$rs=mysqli_query($con ,"update dvredocl set cve_mto='$cta' where codigo='".$dt_redocl0['codigo']."' and num_doc ='".$dt_redocl0['num_doc']."'");
			}
			if($er=='s') $erc += 1;
			@$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"select * from dxbascps where codigo ='".$dt_redocl0['codigo']."'")); 
			if(number_format($tcar,2,'.',',')==number_format($tabo,2,'.',',')){
				$cua = "S";
			}else{
				$cua ="N";	
				echo 'Error Docto :'.$dt_redocl0['num_doc'].'/'.$dt_redocl0['fec_doc'].'/'.$dt_bascps['nombre'].' '.$dt_bascps['ap_paterno'].' Car:'.number_format($tcar,2,'.',',').' Abo:'.number_format($tabo,2,'.',',').'<br>';
				$hayerr = "S";
			}
			if($erc > 0){
				echo 'Error Docto :'.$dt_redocl0['num_doc'].'/'.$dt_redocl0['fec_doc'].'/'.$dt_bascps['nombre'].' '.$dt_bascps['ap_paterno'].' ctas invalidas en encabezado o detalle del docto'.'<br>';
				$hayerr = "S";
			}
		}//termina while documentos fecha especifica
	}// termina while fechas agrupadas

	//Graba Movtos si no hay Errores 
	if($hayerr=="N"){
	$sq_redocl=mysqli_query($con ,"select * from dvredocl where tip_mov='$tmov' and fec_doc between '$fec_ini' and '$fec_fin' group by fec_doc order by fec_doc");
	while($dt_redocl=mysqli_fetch_array($sq_redocl)){ //lee agrupado x rango de fecha
		$auxFecAct=explode('-',$dt_redocl['fec_doc']);
		$npol=$auxFecAct[2];
	   	$f1 = '".$dt_redocl['fec_doc']."';$fr_fec = 'AMD';$fr_fecr = 'Ymd';$sep_fecr = '/';
		include('DF70000.php');
		$dpol='Provision Gastos Dia '.$dt_redocl['fec_doc'];
		$fpol=$dt_redocl['fec_doc'];
		$ipol=0;

		@$sql=mysqli_query($con ,"insert into dcepoliz (tip_pol, num_pol, fec_pol, desc_pol, acc_fec, acc_hor, usuario, origen, actualiza) values ('$tpol', '$npol','$fpol', '$dpol', '".date('Y-m-d')."', '".date('H:i:s')."', '".$_SESSION['num_usu']."','$ori','0')");
		$nr =mysqli_insert_id($con); //id alta poliza encabezado
		$sq_redocl0=mysqli_query($con ,"select * from dvredocl where tip_mov='$tmov' and fec_doc = '".$dt_redocl['fec_doc']."' order by codigo,num_doc");
		while($dt_redocl0=mysqli_fetch_array($sq_redocl0)){ //lee doctos x fecha
			$tcar=0;$tabo=0;
//			echo '<br>';
//			echo '	Procesando Docto : ('.$dt_redocl0['codigo'].')'.$dt_redocl0['num_doc'].' $'.$dt_redocl0['sub_tot'].'<br>';
			$dt_tasiva=mysqli_fetch_array(mysqli_query($con ,"select * from dxtasiva where cve_iva='".$dt_redocl0['cve_iva']."'")); // obtiene registro de iva para ctas contables

			$sq_redocd=mysqli_query($con ,"select * from dvredocd where tip_mov='$tmov' and codigo = '".$dt_redocl0['codigo']."' and num_doc = '".$dt_redocl0['num_doc']."' and fec_doc = '".$dt_redocl0['fec_doc']."'");
			if($dt_redocl['moneda']!='1' && $dt_redocl['moneda']!='N')
				$tipCam=$dt_redocl['tip_cam'];
			else
				$tipCam=1;
			while($dt_redocd=mysqli_fetch_array($sq_redocd)){ //lee detalle doctos
//				echo '		Procesando Detalle ('.$dt_redocd['n_reg'].')'.$dt_redocd['num_doc'].' $'.$dt_redocd['sub_tot'].'<br>';
				$ddpol='Factura '.$dt_redocl0['num_doc'];
				$cta=$dt_redocd['cve_mto'];
				$car=$dt_redocd['sub_tot']*$tipCam;
				$abo='0.00'; 
				$ipol +=$car;$tcar +=$car;
				fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo);//graba detalle docto
			}//termina while detalle doctos
			if($dt_redocl0['iva_tot']<>0){
				$ddpol='Iva Factura '.$dt_redocl0['num_doc'];
				$cta=$dt_tasiva['cta_iva_acr_np'];
				$car=$dt_redocl0['iva_tot']*$tipCam;
				$abo='0.00'; 
				$ipol +=$car;$tcar +=$car;
				fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo);//graba iva docto
			}

			if($dt_redocl0['_im']<>0){
				$dt_catret=mysqli_fetch_array(mysqli_query($con ,"select * from dvcatret where cve_ret ='".$dt_redocl0['ret1']."'")); // obtiene retenciones
				$ddpol=$dt_catret['des_ret'].' Documento '.$dt_redocl0['num_doc'];
				$cta=$dt_catret['cta_con'];
				$abo=$dt_redocl0['_im']*$tipCam;$tabo +=$abo;
				$car='0.00'; 
				fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo);//graba retencion 1
			}
			if($dt_redocl0['_im']<>0){
				$dt_catret=mysqli_fetch_array(mysqli_query($con ,"select * from dvcatret where cve_ret ='".$dt_redocl0['ret2']."'")); // obtiene retenciones
				$ddpol=$dt_catret['des_ret'].' Documento '.$dt_redocl0['num_doc'];
				$cta=$dt_catret['cta_con'];
				$abo=$dt_redocl0['_im']*$tipCam;$tabo +=$abo;
				$car='0.00'; 
				fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo);//graba retencion 2
			}
			$ddpol='Factura '.$dt_redocl0['num_doc'];
			$cta=$dt_redocl0['cve_mto'];
			$abo=$dt_redocl0['tot_doc']*$tipCam;$tabo +=$abo;
			$car='0.00'; 
			fgrab_dpol($tpol,$fpol,$npol,$nr,$cta,$ddpol,$car,$abo);//graba total Factura

			if(number_format($tcar,2,'.',',')==number_format($tabo,2,'.',',')){
				$cua = "S";
			}else{
				$cua ="N";	
				$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"select * from dxbascps where codigo ='".$dt_redocl0['codigo']."'")); 
				echo 'Error Docto :'.$dt_redocl0['num_doc'].'/'.$dt_redocl0['fec_doc'].'/'.$dt_bascps['nombre'].' '.$dt_bascps['ap_paterno'].' Car:'.number_format($tcar,2,'.',',').' Abo:'.number_format($tabo,2,'.',',').'<br>';
			}
			$rs=mysqli_query($con ,"update dcepoliz set imp_total='$ipol',cuadrada='$cua' where id_dcepol ='$nr'");
		}//termina while documentos fecha especifica
	}// termina while fechas agrupadas
	}// termina if si procedia grabacion o no 
	if($hayerr=="S"){
		echo '<br>';
		echo 'No se grabaron polizas verifique errores '.'<br>';
	}else{
		echo '<br>';
		echo 'Fin Proceso Polizas Generadas Correctamente '.'<br>';
	}
}// Termina Isset Calculo
?>
</div></td></tr>
</table>
</form>
</body>
</html>