<?php
session_start();

function f104xml1($xanio,$xmes)
{
header('Content-disposition: attachment; filename=F104_'.$xanio.$xmes.'_'.$_SESSION['user_vcrx_empr'].'.xml');
header ("Content-Type:text/xml"); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> '; 

require('../cfg/res.php');

$sql_info='select * from empr1 ';
$res_info=mysql_query($sql_info,$link_vcrx);

$sql_vtas='update vtas1 set suco="01" where docu like "FC%" and (suco="" or suco is null)';
$res_vtas=mysql_query($sql_vtas,$link_vcrx);

$sql_comp='update comp1 set suco="01" where docu like "FP%" and (suco="" or suco is null)';
$res_comp=mysql_query($sql_vtas,$link_vcrx);
  
$sql_vtas='select * from f104_v where fanio="'.$xanio .'" and fmes="'.$xmes.'"';
$res_vtas=mysql_query($sql_vtas,$link_vcrx);
  
$sql_comp='select * from f104_c where fanio="'.$xanio .'" and fmes="'.$xmes.'"';
$res_comp=mysql_query($sql_comp,$link_vcrx);

$sql_retc='select * from f104_rc where fanio="'.$xanio .'" and fmes="'.$xmes.'"';
$res_retc=mysql_query($sql_retc,$link_vcrx);

$sql_retp='select * from f104_rp where fanio="'.$xanio .'" and fmes="'.$xmes.'"';
$res_retp=mysql_query($sql_retp,$link_vcrx);

$contab=mysql_result($res_info,0,'enti_pcont');
$enti_descri=mysql_result($res_info,0,'enti_descri');
$enti_descri=str_replace('&','&amp;',$enti_descri);	
$enti_descri=str_replace('"','&quot;',$enti_descri);	
	
$c111=0;
$c113=0;

$c401=0;
$c402=0;
$c403=0;
$c404=0;
$c405=0;
$c406=0;
$c409=0;

$c411=0;
$c412=0;
$c413=0;
$c414=0;
$c415=0;
$c416=0;
$c419=0;
$c421=0;
$c429=0;
$c482=0;
$c483=0;
$c484=0;
$c485=0;
$c499=0;

$c500=0;
$c501=0;
$c502=0;
$c503=0;
$c504=0;
$c505=0;
$c506=0;
$c507=0;
$c508=0;
$c509=0;

$c510=0;
$c511=0;
$c512=0;
$c513=0;
$c514=0;
$c515=0;
$c516=0;
$c517=0;
$c518=0;
$c519=0;

$c520=0;
$c521=0;
$c522=0;
$c523=0;
$c524=0;
$c525=0;
$c526=0;
$c529=0;

$c563=0;
$c564=0;
	
$c601=0;	
$c602=0;	
	
$c721=0;
$c723=0;
$c725=0;
$c727=0;
$c729=0;
$c731=0;
$c799=0;
	
if(mysql_num_rows($res_vtas)!=0 ){
$c111=mysql_result($res_vtas,0,'c111');
$c113=mysql_result($res_vtas,0,'c113');

$c401=mysql_result($res_vtas,0,'c401');
$c411=mysql_result($res_vtas,0,'c411');
$c421=mysql_result($res_vtas,0,'c421');

$c402=mysql_result($res_vtas,0,'c402');
$c412=mysql_result($res_vtas,0,'c412');
$c422=mysql_result($res_vtas,0,'c422');

$c403=mysql_result($res_vtas,0,'c403');
$c413=mysql_result($res_vtas,0,'c413');
	
$c404=mysql_result($res_vtas,0,'c404');
$c414=mysql_result($res_vtas,0,'c414');
	
$c405=mysql_result($res_vtas,0,'c405');
$c415=mysql_result($res_vtas,0,'c415');
	
$c406=mysql_result($res_vtas,0,'c406');
$c416=mysql_result($res_vtas,0,'c416');
	
// aumentar 7 y 8 para exportaciones	
$c409=$c401+$c402+$c403+$c404+$c405+$c406;
$c419=$c411+$c412+$c413+$c414+$c415+$c416;
$c429=$c421+$c422;
$c482=$c429;	
$c485=$c482-$c484;	
$c499=$c482+$c484;	
	
} 
	
if(mysql_num_rows($res_comp)!=0 ){ 
$c500=mysql_result($res_comp,0,'c500');
$c520=mysql_result($res_comp,0,'c520');
$c510=mysql_result($res_comp,0,'c510');
$c521=mysql_result($res_comp,0,'c521');
$c511=mysql_result($res_comp,0,'c511');
$c501=mysql_result($res_comp,0,'c501');
$c502=mysql_result($res_comp,0,'c502');
$c522=mysql_result($res_comp,0,'c522');
$c512=mysql_result($res_comp,0,'c512');
// 3,4,5,6 importaciones
$c517=mysql_result($res_comp,0,'c517');
$c507=mysql_result($res_comp,0,'c507');
$c508=mysql_result($res_comp,0,'c508');
$c509=$c501+$c502+$c503+$c504+$c505+$c506+$c507+$c508;
$c519=$c511+$c512+$c513+$c514+$c515+$c516+$c517+$c518;
$c529=$c521+$c522+$c523+$c524+$c525+$c526;

if($c419>0){$c563=($c411+$c412+$c415+$c416)/$c419;}
if($c563>0){$c564=($c520+$c521+$c523+$c524+$c525+$c526)/$c563;}

$impto=$c499-$c564;
if($impto>0){$c601=$impto;}	
if($impto<0){$c602=$impto*-1;}	
 } 

if(mysql_num_rows($res_retc)!=0 )
{ 
$c609=mysql_result($res_retc,0,'c609');
} 
	
	
if(mysql_num_rows($res_retp)!=0 ){ 
$c721=mysql_result($res_retp,0,'c721');
$c723=mysql_result($res_retp,0,'c723');
$c725=mysql_result($res_retp,0,'c725');
$c727=mysql_result($res_retp,0,'c727');
$c729=mysql_result($res_retp,0,'c729');
$c731=mysql_result($res_retp,0,'c731');
$c799=$c721+$c723+$c725+$c727+$c729+$c731;
} ?>


<?php if($contab==1) { ?>
<formulario version="0.2">
<cabecera>
<codigo_version_formulario>04201701</codigo_version_formulario>
<ruc><?php echo mysql_result($res_info,0,'enti_codigo');?></ruc>
<codigo_moneda>1</codigo_moneda>
</cabecera>

<detalle>

<campo numero="102"><?php echo $xanio;?></campo>
<campo numero="101"><?php echo $xmes;?></campo>
<campo numero="31">O</campo>
<campo numero="104"></campo>
<campo numero="202"><?php echo $enti_descri;?></campo>
<campo numero="201"><?php echo mysql_result($res_info,0,'enti_codigo');?></campo>
<?php if(mysql_num_rows($res_vtas)!=0){ ?>
<campo numero="411"><?php echo number_format($c411,2,'.','');?></campo>
<campo numero="401"><?php echo number_format($c401,2,'.','');?></campo>
<campo numero="421"><?php echo number_format($c421,2,'.','');?></campo>
<campo numero="422"><?php echo number_format($c422,2,'.','');?></campo>
<campo numero="412"><?php echo number_format($c412,2,'.','');?></campo>
<campo numero="402"><?php echo number_format($c402,2,'.','');?></campo>
<campo numero="413"><?php echo number_format($c413,2,'.','');?></campo>
<campo numero="403"><?php echo number_format($c403,2,'.','');?></campo>
<campo numero="404"><?php echo number_format($c404,2,'.','');?></campo>
<campo numero="414"><?php echo number_format($c414,2,'.','');?></campo>
<campo numero="405"><?php echo number_format($c405,2,'.','');?></campo>
<campo numero="415"><?php echo number_format($c415,2,'.','');?></campo>
<campo numero="406"><?php echo number_format($c406,2,'.','');?></campo>
<campo numero="416"><?php echo number_format($c416,2,'.','');?></campo>
<campo numero="417">0.00</campo>
<campo numero="407">0.00</campo>
<campo numero="418">0.00</campo>
<campo numero="408">0.00</campo>
<campo numero="419"><?php echo number_format($c419,2,'.','');?></campo>
<campo numero="409"><?php echo number_format($c409,2,'.','');?></campo>
<campo numero="429"><?php echo number_format($c429,2,'.','');?></campo>
<campo numero="431">0.00</campo>
<campo numero="441">0.00</campo>
<campo numero="442">0.00</campo>
<campo numero="443">0.00</campo>
<campo numero="453">0.00</campo>
<campo numero="434">0.00</campo>
<campo numero="444">0.00</campo>
<campo numero="454">0.00</campo>
<campo numero="499"><?php echo number_format($c499,2,'.','');?></campo>
<campo numero="485"><?php echo number_format($c485,2,'.','');?></campo>
<campo numero="484">0.00</campo>
<campo numero="483">0.00</campo>
<campo numero="482"><?php echo number_format($c482,2,'.','');?></campo>
<campo numero="481">0.00</campo>
<campo numero="480">0.00</campo>
<?php } ?>
	
<?php if(mysql_num_rows($res_comp)!=0){ ?>
<campo numero="510"><?php echo number_format($c510,2,'.','');?></campo>
<campo numero="500"><?php echo number_format($c500,2,'.','');?></campo>
<campo numero="520"><?php echo number_format($c520,2,'.','');?></campo>
<campo numero="511"><?php echo number_format($c511,2,'.','');?></campo>
<campo numero="521"><?php echo number_format($c521,2,'.','');?></campo>
<campo numero="501"><?php echo number_format($c501,2,'.','');?></campo>
<campo numero="512"><?php echo number_format($c512,2,'.','');?></campo>
<campo numero="502"><?php echo number_format($c502,2,'.','');?></campo>
<campo numero="522"><?php echo number_format($c522,2,'.','');?></campo>
<campo numero="523">0.00</campo>
<campo numero="503">0.00</campo>
<campo numero="513">0.00</campo>
<campo numero="514">0.00</campo>
<campo numero="504">0.00</campo>
<campo numero="524">0.00</campo>
<campo numero="525">0.00</campo>
<campo numero="505">0.00</campo>
<campo numero="515">0.00</campo>
<campo numero="506">0.00</campo>
<campo numero="516">0.00</campo>
<campo numero="517"><?php echo number_format($c517,2,'.','');?></campo>
<campo numero="507"><?php echo number_format($c507,2,'.','');?></campo>
<campo numero="508"><?php echo number_format($c508,2,'.','');?></campo>
<campo numero="518">0.00</campo>
<campo numero="529">0.00</campo>
<campo numero="519">0.00</campo>
<campo numero="509">0.00</campo>
<campo numero="541">0.00</campo>
<campo numero="531">0.00</campo>
<campo numero="532">0.00</campo>
<campo numero="542">0.00</campo>
<campo numero="543">0.00</campo>
<campo numero="544">0.00</campo>
<campo numero="554">0.00</campo>
<campo numero="555">0.00</campo>
<campo numero="545">0.00</campo>
<campo numero="535">0.00</campo>
<campo numero="563"><?php echo number_format($c563,2,'.','');?></campo>
<campo numero="564">0.00</campo>
<?php } ?>

<campo numero="601"><?php echo number_format($c601,2,'.','');?></campo>
<campo numero="602"><?php echo number_format($c602,2,'.','');?></campo>
<campo numero="605">0.00</campo>
<campo numero="607">0.00</campo>
<campo numero="609"><?php echo number_format($c609,2,'.','');?></campo>
<campo numero="611">0.00</campo>
<campo numero="612">0.00</campo>
<campo numero="613">0.00</campo>
<campo numero="615">0.00</campo>
<campo numero="617">0.00</campo>
<campo numero="619">0.00</campo>
<campo numero="621">0.00</campo>
<campo numero="699">0.00</campo>
<?php if(mysql_num_rows($res_retp)!=0){ ?>
<campo numero="721"><?php echo number_format($c721,2,'.','');?></campo>
<campo numero="723"><?php echo number_format($c723,2,'.','');?></campo>
<campo numero="725"><?php echo number_format($c725,2,'.','');?></campo>
<campo numero="727"><?php echo number_format($c727,2,'.','');?></campo>
<campo numero="729"><?php echo number_format($c729,2,'.','');?></campo>
<campo numero="731"><?php echo number_format($c731,2,'.','');?></campo>
<campo numero="799"><?php echo number_format($c799,2,'.','');?></campo>
<?php } ?>
<campo numero="800">0.00</campo>
<campo numero="801">0.00</campo>
<campo numero="859">0.00</campo>
<campo numero="890">0.00</campo>
<campo numero="897">0.00</campo>
<campo numero="898">0.00</campo>
<campo numero="899">0.00</campo>
<campo numero="880">0.00</campo>
<campo numero="902">0.00</campo>
<campo numero="903">0.00</campo>
<campo numero="904">0.00</campo>
<campo numero="999">0.00</campo>
<campo numero="905">0.00</campo>
<campo numero="906">0.00</campo>
<campo numero="907">0.00</campo>
<campo numero="925">0.00</campo>
<campo numero="912"></campo>
<campo numero="910"></campo>
<campo numero="908"></campo>
<campo numero="915">0.00</campo>
<campo numero="913">0.00</campo>
<campo numero="911">0.00</campo>
<campo numero="909">0.00</campo>
<campo numero="916"></campo>
<campo numero="918"></campo>
<campo numero="919">0.00</campo>
<campo numero="917">0.00</campo>
<campo numero="920">0.00</campo>
<campo numero="198"><?php echo mysql_result($res_info,0,'empr_ruclegal');?></campo>
<campo numero="199"><?php echo mysql_result($res_info,0,'empr_ruccontador');?></campo>
<campo numero="921">2</campo>	
<campo numero="922">16</campo>	
	
	
</detalle>

</formulario>

<?php }else{ ?>

<formulario version="0.2">
<cabecera>
<!-- <codigo_version_formulario>04<?php echo $xanio.$xmes;?></codigo_version_formulario> -->
<!-- <codigo_version_formulario>04201501</codigo_version_formulario> -->
<!-- <codigo_version_formulario>04201606</codigo_version_formulario> -->
<codigo_version_formulario>04201703</codigo_version_formulario>
<ruc><?php echo mysql_result($res_info,0,'enti_codigo');?></ruc>
<codigo_moneda>1</codigo_moneda>
</cabecera>

<detalle>

<campo numero="102"><?php echo $xanio;?></campo>
<campo numero="101"><?php echo $xmes;?></campo>
<campo numero="31">O</campo>
<campo numero="104"></campo>
<campo numero="202"><?php echo $enti_descri;?></campo>
<campo numero="201"><?php echo mysql_result($res_info,0,'enti_codigo');?></campo>


	
	
	
<?php if(mysql_num_rows($res_retp)!=0 or 1==1){ ?>
<campo numero="411"><?php echo number_format($c411,2,'.','');?></campo>
<campo numero="421"><?php echo number_format($c421,2,'.','');?></campo>
<campo numero="401"><?php echo number_format($c401,2,'.','');?></campo>
<campo numero="412">0.00</campo>
<campo numero="422">0.00</campo>
<campo numero="402">0.00</campo>
<campo numero="413">0.00</campo>
<campo numero="403">0.00</campo>
<campo numero="414">0.00</campo>
<campo numero="404">0.00</campo>
<campo numero="415"><?php echo number_format($c415,2,'.','');?></campo>
<campo numero="405"><?php echo number_format($c405,2,'.','');?></campo>
<campo numero="416">0.00</campo>
<campo numero="406">0.00</campo>
<!-- 
<campo numero="417">0.00</campo>
<campo numero="407">0.00</campo>
<campo numero="418">0.00</campo>
<campo numero="408">0.00</campo>
 -->
		
<campo numero="409"><?php echo number_format($c409,2,'.','');?></campo>
<campo numero="419"><?php echo number_format($c419,2,'.','');?></campo>
<campo numero="429"><?php echo number_format($c429,2,'.','');?></campo>
<campo numero="431">0.00</campo>
<campo numero="441">0.00</campo>
<campo numero="442">0.00</campo>
<campo numero="453">0.00</campo>
<campo numero="443">0.00</campo>
<campo numero="434">0.00</campo>
<campo numero="454">0.00</campo>
<campo numero="444">0.00</campo>
<campo numero="499"><?php echo number_format($c499,2,'.','');?></campo>
<campo numero="485"><?php echo number_format($c485,2,'.','');?></campo>
<campo numero="484">0.00</campo>
<campo numero="483">0.00</campo>
<campo numero="482"><?php echo number_format($c482,2,'.','');?></campo>
<campo numero="481">0.00</campo>
<campo numero="480">0.00</campo>
<?php } ?>
<campo numero="111"><?php echo number_format($c111,2,'.','');?></campo>
<campo numero="113"><?php echo number_format($c113,2,'.','');?></campo>

<campo numero="500"><?php echo number_format($c500,2,'.','');?></campo>
<campo numero="520"><?php echo number_format($c520,2,'.','');?></campo>
<campo numero="510"><?php echo number_format($c510,2,'.','');?></campo>
<campo numero="521"><?php echo number_format($c521,2,'.','');?></campo>
<campo numero="511"><?php echo number_format($c511,2,'.','');?></campo>
<campo numero="501"><?php echo number_format($c501,2,'.','');?></campo>
<campo numero="522"><?php echo number_format($c522,2,'.','');?></campo>
<campo numero="502"><?php echo number_format($c502,2,'.','');?></campo>
<campo numero="512"><?php echo number_format($c512,2,'.','');?></campo>
<!-- 
<campo numero="523">0.00</campo>
<campo numero="503">0.00</campo>
<campo numero="513">0.00</campo>
<campo numero="514">0.00</campo>
<campo numero="504">0.00</campo>
<campo numero="524">0.00</campo>
<campo numero="525">0.00</campo>
<campo numero="505">0.00</campo>
<campo numero="515">0.00</campo>
<campo numero="506">0.00</campo>
<campo numero="516">0.00</campo>
	 -->
<campo numero="517"><?php echo number_format($c517,2,'.','');?></campo>
<campo numero="507"><?php echo number_format($c507,2,'.','');?></campo>
<campo numero="508"><?php echo number_format($c508,2,'.','');?></campo>
<campo numero="518">0.00</campo>
<campo numero="509">0.00</campo>
<campo numero="519">0.00</campo>
<campo numero="529">0.00</campo>
<campo numero="531">0.00</campo>
<campo numero="541">0.00</campo>
<campo numero="532">0.00</campo>
<campo numero="542">0.00</campo>
<campo numero="543">0.00</campo>
<campo numero="544">0.00</campo>
<campo numero="554">0.00</campo>
<campo numero="555">0.00</campo>
<campo numero="545">0.00</campo>
<campo numero="535">0.00</campo>
<campo numero="563"><?php echo number_format($c563,2,'.','');?></campo>
<campo numero="564">0.00</campo>

	
<campo numero="115"></campo>
<campo numero="117"></campo>
<campo numero="119"></campo>

<campo numero="601"><?php echo number_format($c601,2,'.','');?></campo>
<campo numero="602"><?php echo number_format($c602,2,'.','');?></campo>
<campo numero="605">0.00</campo>
<campo numero="607">0.00</campo>
<campo numero="609"><?php echo number_format($c609,2,'.','');?></campo>
<campo numero="611">0.00</campo>
<campo numero="612">0.00</campo>
<campo numero="613">0.00</campo>
<campo numero="615">0.00</campo>
<campo numero="617">0.00</campo>
<campo numero="619">0.00</campo>
<campo numero="621">0.00</campo>
<campo numero="699">0.00</campo>
<?php if(mysql_num_rows($res_retp)!=0){ ?>
<campo numero="721"><?php echo number_format($c721,2,'.','');?></campo>
<campo numero="723"><?php echo number_format($c723,2,'.','');?></campo>
<campo numero="725"><?php echo number_format($c725,2,'.','');?></campo>
<campo numero="727"><?php echo number_format($c727,2,'.','');?></campo>
<campo numero="729"><?php echo number_format($c729,2,'.','');?></campo>
<campo numero="799"><?php echo number_format($c799,2,'.','');?></campo>
<?php } ?>
<!-- 	
<campo numero="800">0.00</campo>
<campo numero="801">0.00</campo>
<campo numero="859">0.00</campo>
	 -->
<campo numero="890">0.00</campo>
<campo numero="899">0.00</campo>
<campo numero="897">0.00</campo>
<campo numero="898">0.00</campo>
<!-- <campo numero="880">0.00</campo> -->
<campo numero="902">0.00</campo>
<campo numero="903">0.00</campo>
<campo numero="904">0.00</campo>
<campo numero="999">0.00</campo>
<campo numero="905">0.00</campo>
<campo numero="906">0.00</campo>
<campo numero="907">0.00</campo>
<campo numero="925">0.00</campo>
<campo numero="908"></campo>
<campo numero="910"></campo>
<campo numero="912"></campo>
<campo numero="909">0.00</campo>
<campo numero="911">0.00</campo>
<campo numero="913">0.00</campo>
<campo numero="915">0.00</campo>
<campo numero="918"></campo>
<campo numero="916"></campo>
<campo numero="919">0.00</campo>
<campo numero="917">0.00</campo>
<campo numero="920">0.00</campo>
<campo numero="198"><?php echo mysql_result($res_info,0,'empr_ruclegal');?></campo>
<!-- <campo numero="199"><?php echo mysql_result($res_info,0,'empr_ruccontador');?></campo> -->
<!-- <campo numero="921">2</campo>	 -->
<campo numero="922">16</campo>	
	
<campo numero="423">0.00</campo>
<campo numero="526">0.00</campo>
<campo numero="603">0.00</campo>
<campo numero="604">0.00</campo>
<campo numero="606">0.00</campo>
<campo numero="608">0.00</campo>
<campo numero="610">0.00</campo>
<campo numero="614">0.00</campo>
<campo numero="618">0.00</campo>
<!-- <campo numero="619">0.00</campo>	 -->
	
	
	
</detalle>

</formulario>	
<?php } ?>


<?php } ?>