<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include('DF30000.inc');
require_once("DF80000.php");
// 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){

	global $er;
	$er ='';
	if($dt_catcta['cta_con']<>$cta)$er ="s";
	if($dt_catcta['tipo']<>'D')$er ="s";
}

function getCtaCon($cta){
	$sq_catcta=mysqli_query($con ,"select * from dycatcta where cta_con = '$cta'");
	if(mysqli_num_rows($sq_catcta)==0)
		return 0;
	else{
		$dt_catcta=mysqli_fetch_array($sq_catcta); // obtiene cta contable	
		return $dt_catcta;
	}
}

// -----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>


<form name="frm1" method="post" action="">
  <table width="730"  align="center">
    <tr>
      <th width="720" colspan="4">GENERA POLIZAS COMPRAS </th>
    </tr>
    <tr>
      <td>A&ntilde;o Proceso </td>
      <td><select name="axo" id="axo">
	  		<option value="2009">2009</option>
          <?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>Mes Proceso </td>
      <td><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_parameVen=getParGenVen(); // parametros generales
	$dt_parameCXC=getParGenCXC(); // parametros generales
	$fec_ini = $fec_imes;
	$fec_fin = $fec_fmes;
	$tpol = $dt_parameVen['tip_pol_com']; // tipo de poliza
	$ori = "DH"; // COMPRAS
	if(strcmp($tpol,"")==0){
		echo "<h2>Es Necesario Tipo de Poliza, Verifique Parametros de Compras !!!</h2>";
		exit();
	}
	$npol=getLastNumPol($tpol,$_POST['axo'],$_POST['mes'])+1;
	$fpol='';

	//Graba Movtos si no hay Errores 
	/*********************************POLIZAS VENTAS*********************************/
	$tpol = $dt_parameVen['tip_pol_ven']; // tipo de poliza
	$npol=getLastNumPol($tpol,$_POST['axo'],$_POST['mes'])+1;	
	$fpol='';
	$sq="SELECT dgrefcom.fec_doc as fecDoc,
				dgrefcom.num_doc as numDoc,
				dglineas.linea as idLin,
				dglineas.des_linea as desLin,
				dgtipcom.des_tipcom as desTipCom,
				dgtipcom.cta_con_cos as ctaComPadre,
				dgtipcom.cta_des_bon as ctaDevComPadre,
				dgtipcom.cta_con_inv as ctaComHijo,
				dglineas.cta_dev_vta as ctaDevVtaHijo,
				dglineas.cta_con_costo as ctaCosHijo,
				dglineas.cta_con_inven as ctaInvHijo,
				dglineas.cta_des_bon as ctaDesBonHijo,
				SUM(dgrefcom.sub_tot) as subTotLin,
				SUM(dgrefcom.iva) as totIvaLin
		 FROM
			dgrefcod
			INNER JOIN dgrefcom ON (dgrefcom.fol_recep=dgrefcod.cons_ord)
			INNER JOIN dginvent ON (dgrefcod.nparte1 =dginvent.nparte1)
			INNER JOIN dgtipcom ON (dgtipcom.tip_com =dgrefcom.tip_com)
			LEFT  JOIN dglineas ON (dginvent.linea   =dglineas.linea)			
		WHERE
			dgrefcom.fec_doc like '$_POST['axo']-$_POST['mes']%' 
		GROUP BY 
			dgrefcom.fec_doc,dglineas.des_linea,dgtipcom.tip_com
		ORDER BY 
			dgrefcom.fec_doc,dglineas.des_linea,dgtipcom.tip_com
		";
	$sq_pememd=mysqli_query($con ,$sq);
	echo mysqli_error($con);
	echo $sq."<br>";
	exit();
	$totImp=0;
	$totIva=0;
	echo "<h2>POLIZAS COMPRAS</h2>";
	$dt_tasiva=mysqli_fetch_array(mysqli_query($con ,"SELECT  cta_iva_acr_np as ctaIvaPpc, cta_iva_acr as ctaIvaCob FROM dxtasiva WHERE cve_iva='1'"));
	while($dt_pememd=mysqli_fetch_array($sq_pememd)){
		//Si Cambio de Fechas Inserta Encabezado Poliza
		if(strcmp($fpol,$dt_pememd['fecDoc'])!=0){
			//Si fecha no es vacia Insertamos Detalles de Iva de poliza
			if(strcmp($fpol,"")!=0){
				fgrab_dpol($tpol,$fpol,$npol,$idEncPol,$dt_tasiva['ctaIvaPpc'],"IVA PENDIENTE POR COBRAR","0.00",$totIva);
				$rs=mysqli_query($con ,"update dcepoliz set imp_total='".($totIva + $totImp)."',cuadrada='S' where id_dcepol ='$idEncPol'");
				$totIva=0;
				$totImp=0;
			}
			$fpol=$dt_pememd['fecDoc'];
			$dpol='Compras Del Dia: '.chfecha($fpol,'Y-m-d','d/m/Y');
			echo $dpol."<br>";
			@$rs=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', CURDATE(),CURTIME(), '".$_SESSION['num_usu']."','$ori','0')");
			$idEncPol=mysqli_insert_id($con);
		}
		$datCtaCon=getCtaCon($dt_pememd['ctaComPadre'].$dt_pememd['ctaComHijo']);
		if(is_array($datCtaCon)){
			fgrab_dpol($tpol,$fpol,$npol,$idEncPol,$datCtaCon['cta_con'],$datCtaCon['descripcion'],"0.00",$dt_pememd['subTotLin']);
			$totIva +=$dt_pememd['totIvaLin'];
			$totImp +=$dt_pememd['subTotLin'];			
		}else{
			echo "<h2>ERROR:Cuenta Contable No Valida, Tip.Pol.:$tpol Poliza:$npol, $dpol</h2>";
		}
		unset($datCtaCon);	
	}
	fgrab_dpol($tpol,$fpol,$npol,$idEncPol,$dt_tasiva['ctaIvaPpc'],"IVA PENDIENTE POR COBRAR","0.00",$totIva);
	$rs=mysqli_query($con ,"update dcepoliz set imp_total='".($totIva + $totImp)."',cuadrada='S' where id_dcepol ='$idEncPol'");
	/********************************************************TERMINA POLIZAS VENTAS***********************************************/
	
		
}// Termina Isset Calculo

//Obtiene Datos de Ultima Poliza por tipo a&ntilde;o y mes
function getLastNumPol($tipPol,$axo,$mes){
	$sq_epoliz=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dcepoliz WHERE tip_pol='$tipPol' AND YEAR(fec_pol)='$axo' AND MONTH(fec_pol)='$mes' order by num_pol desc limit 1"));
	return $dt_epoliz['num_pol'];
}

//Obtiene Datos de Parametros Generales de Ventas
function getParGenVen(){
	$sq_parame=mysqli_query($con ,"SELECT * from dgparame");
	return mysqli_fetch_array($sq_parame);
}

function getParGenCXC(){
	$sq_parame=mysqli_query($con ,"SELECT * from dvparame");
	return mysqli_fetch_array($sq_parame);
}
?>
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>
