<?php
declare(strict_types=0);
// session_start();
// echo '<meta http-equiv="refresh" content="5">';
foreach($_GET  as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}  
foreach($_POST as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}

// if($tkn!='1234567890'){die('Invalid Token');}
include_once('lib_main.php');

if($docu=='DA16'){$bode=1;}
if($docu=='DA16'){$cost='true';}

if($docu=='DA16')
{if(empty($pgsize)){$pgsize='A4H';}}



// if(date('Y-m-d')<='2025-04-25')
// {
//      if($_SESSION['user_codigo']==2){$cont=3;}
// }

// if(empty($pgsize))
// {if(getPlatform()=='Android'){$pgsize='P';}}
// {if(getPlatform()=='Linux'){$pgsize='P';}}

if(empty($pgsize)){if($_SESSION['screeno']==1){$pgsize='P';}}

if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}

if(substr($docu,0,2)=='RB'){$rbco=1;}

$pgsize=strtoupper($pgsize);
$tkwidth=0;
if(empty($pgsize)){$pgsize='A4V';}
if(substr($pgsize,0,1)=='P'){$tkwidth=(int)substr($pgsize,-2,2); $pgsize='POST'; $post=1;   }

if(substr($pgsize,0,1)=='T')
{
     $tkwidth=(int)(substr($pgsize,-2,2));     
     $pgsize='TCKT'; $post=1; $tckt=1;
}

if($mailsend==1)
{
     $pgsize='A4V';
     $post=0;
}


if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];

if(strlen($clave)==48){$clave.=val_mod11($clave);}

if(val_clac($clave))
{
     $tabla='';
     $tipodoc=substr($clave,8,2);
     
     $sql_docu='select * from docu0 where docu_tice="'.$tipodoc.'"';
     $res_docu=msql_query($sql_docu,$link_data);
     $tabla=msql_result($res_docu,0,'docu_tabla');
     
     $sql_docu='select * from '.$tabla.'1 where claveacc="'.$clave.'"';
     $sql_docu.='and ifnull(status,"")not in ("X")';
     $res_docu=msql_query($sql_docu,$link_data);
     $docu=msql_result($res_docu,0,'docu');
     $idnum=msql_result($res_docu,0,'idnum1');
//      echo '==='.$sql_docu ;
//      echo '==='.msql_num_rows($res_docu);
//      echo '==='.$tabla;
//      echo '==='.$idnum;
     $clave_print_ok=1;
}
else{$clave='';}

if(empty($user_codigo) and empty($clave)){include('user_login0.php');return;}
// if(empty($user_codigo) and !empty($clave)){$clave_ok=1;}


if(empty($idnum)){die('NO HAY DATOS POR CONSULTAR');}
// if(empty($idnum) and empty($clave)){die('NO HAY DATOS POR CONSULTAR');}

if(empty($user_codigo)){$hideusr=1;}
if(empty($user_codigo)){$cont=0; $xtra=0; $abon=0; $bode=0;}

if($cont!=1)
if(!empty($idnum))
{
     $clave=gen_clacc($docu,$idnum);
//      gen_cexml($docu,$idnum);
     gen_cexml($clave);
     set_cant123($docu,$idnum);
}


$user0=data_maes('user',$user_codigo);
// $empr0=data_maes('empr',$cfg_empr);
$empr0=data_maes('empr',$_SESSION['empr_codigo']);
$docu0=data_maes('docu',$docu);
$modu0=data_maes('modu',$modu);
$docu1=data_docu($docu,$idnum);
$numero=$docu1['numero'];
$ptoe=$docu1['ptoe'];

vali_docu($docu,$idnum);

{gen_fc10cob($docu,$idnum);}

pcs1_calc1($docu,$idnum);


if(empty($clave)){$clave=$docu1['claveacc'];}

$xtitle= $docu.' - '.(int)$numero.' /'.$docu1['enti'];

if($empr0['empr_cxco']==0){$abon=0;}
if($empr0['empr_inve']==0){$bode=0;}
if($empr0['empr_cont']==0){$cont=0;}
if(!mb_stristr2('.vtas.comp.prov.prod.opro',$docu0['docu_tabla']))
{$bode=0; $abon=0;}


     
if($docu0['docu_tabla']=='cont'){$cont=0;}
     
// if($docu0['docu_tabla']!='cont')
if($cont==1)
{
     $sql_cont='select * from cont1 where docu="'.$docu.'" and idfrom="'.$idnum.'"';
     $res_cont=msql_query($sql_cont,$link_data);
     $idnum=msql_result($res_cont,0,'idnum1');
     $modu='cont';
//      $modu0=data_maes('modu',$modu);
}     

$info=substr($modu,0,2).'ride';
if($modu=='cont'){$info='ttride';}
// echo '===='.$info;
include('sele_info.php');

$date1=date('H:i:s');
$res_info1=msql_query($sql_info11,$link_data);
$date2=date('H:i:s');


$date1=date('H:i:s');
$res_info2=msql_query($sql_info12,$link_data);
$date2=date('H:i:s');

// echo '==='.$sql_info12;
     
if(mb_stristr2('.vtas.comp.clie.prov',$modu))
{
     $date3=date('H:i:s');
     $sql_info13=$sql_info12;     
     $sql_info13=str_replace('group by idnum1,iddet','group by idnum1,iva',$sql_info13);
     $res_info3=msql_query($sql_info13,$link_data);
     $date3=date('H:i:s');
}

if(mb_stristr2('.otbj',$modu))
{
     $date3=date('H:i:s');
     $sql_info13=$sql_info12;     
     $sql_info13=str_replace('group by idnum1,iddet','group by idnum1,fase',$sql_info13);
     $res_info3=msql_query($sql_info13,$link_data);
     $date3=date('H:i:s');
}



$xtitle= $docu.' - '.(int)$numero.' /'.msql_result($res_info1,0,'enti_descri');

if(empty($user0['user_tipo'])){$cost='false';}
if(empty($user0['user_tipo'])){$prec='false';}


// if(@!mb_stristr2('.sist.supe',$user0['user_tipo']) and $modu=='inve')
if(!mb_stristr2('.sist.supe',$user0['user_tipo']) and $modu=='inve')
{
     $sql_acce ='select docu,numero, ';
     $sql_acce.='case when docum="COST" and info=1 then 1 else 0 end as cost, ';
     $sql_acce.='case when docum="PREC" and info=1 then 1 else 0 end as prec, ';
     $sql_acce.='status ';
     $sql_acce.='from acce1 ';
     $sql_acce.='left join acce2 on idnum1=idnum2 ';
     $sql_acce.='left join docu0 on docum=docu_codigo ';
     $sql_acce.='where numero*1="'.(int)$user_codigo.'" ';
     $sql_acce.='group by idnum1 ';
     $res_acce=msql_query($sql_acce,$link_data);
     if(empty(msql_result($res_acce,0,'cost'))){$cost='false';}
     if(empty(msql_result($res_acce,0,'prec'))){$prec='false';}
}

if(mb_stristr2('.sist.supe',$user0['user_tipo']) and $modu=='inve')
{$cost='true';$prec='true';}

if($modu=='infi'){$cost='false';}

// if(!empty($cont))     
if($cont!=1)
{
     if($cont==3){gen_contab($docu,$idnum);}
     $res_cont='';
     $sql_cont ='select * from cont1 ';
     $sql_cont.='left join cont2 on idnum1=idnum2 ';
     $sql_cont.='left join ctas0 on ctas=ctas_codigo ';
     $sql_cont.='left join csub0 on csub=csub_codigo ';
     $sql_cont.='left join enti0 on enti=enti_codigo ';
     $sql_cont.='left join item0 on item=item_codigo ';
     
     $sql_cont.='left join cair0 on cair=cair_codigo ';
     $sql_cont=calc_fields('cont',$sql_cont);
     $sql_cont.='where ifnull(status,"") not in("O","X") ';
     $sql_cont.='and mdfrom="'.$modu.'" and idfrom="'.$idnum.'" ';
     $sql_cont.='group by idnum1,iddet ';
     $sql_cont.='order by idnum1,ctas,iddet ';
     $res_cont=msql_query($sql_cont,$link_data);


     
//      $xfecha=msql_result($res_cont,0,'fecha');
//      if($cont!=3)
//      if($xfecha>=date('Y-m-01',strtotime('-1 month')))
//      {gen_contab($docu,$idnum);}     
}


if(!empty($user0['user_tipo']))
if(mb_stristr2('.sist.supe.admi.oper', $user0['user_tipo']))
{
     $res_inve='';
//      if($modu=='vtas' or $modu=='comp' or $modu=='prov')
     if(mb_stristr2('.vtas.comp.prov.prod.opro',$modu))
     {
          if($bode==1)
          if($empr0['empr_inve']==1)
          {
               $nume_fact=$docu1['numero'];
               $nume_impo=$docu1['impor'];
               
               $sql_inve='';
               $sql_inve.='create temporary table inve_fact ';     
               $sql_inve.='select * from inve1 ';     
               $sql_inve.='left join inve2 on idnum1=idnum2 ';     
               $sql_inve.='left join item0 on item=item_codigo ';     
               $sql_inve.='where ifnull(status,"")not in ("O","X") ';
               
               
//                $sql_inve.='and docu not like "%13" ';
               
               if(!empty($nume_impo))
               {$sql_inve.='and trim(ifnull(impor,""))="'.$nume_impo.'" ';}
               else
               {
               if(!empty($nume_fact))
               {$sql_inve.='and ifnull(numfac,0)*1="'.(int)$nume_fact.'" ';}
               }
               
               if($modu=='vtas')
               {$sql_inve.='and docu in("EB10","EB15","IB12","EB19") ';}
               
               if(mb_stristr2('.prod.opro',$modu))
               {$sql_inve.='and docu like "EB%" ';}
//                {$sql_inve.='and docu in("EB19") ';}
               
               if($modu=='comp')
               {$sql_inve.='and docu in("IB10","IB15","EB12") ';}
               
//                if($modu=='inve')
               {$sql_inve.='and ifnull(cant,0)!=0 ';}
               
//                $sql_inve.='and (numfac*1="'.(int)$nume_fact.'" or trim(impor)="'.trim($nume_impo).'") ';
               
               if($docu!='DA16')
               {$sql_inve.='and enti like "'.substr($docu1['enti'],0,10).'%" ';}
               
               $sql_inve.='order by fecha,idnum1,iddet ';
               $res_inve=msql_query($sql_inve,$link_data);
               
//                echo '*/*/*/'.$sql_inve;

               $sql_inve='select * from inve_fact ';
               $res_inve=msql_query($sql_inve,$link_data);

               if(msql_num_rows($res_inve)==0){$res_inve='';}
          }

     //      if($empr0['empr_cxco']==1)
//           if($abon==1)
          {
               $moduo=$modu;
               $res_abon='';
               $modu='';
               if($moduo=='vtas' and ($empr0['empr_cxco']==1 or $empr0['empr_caja']==1) ){$modu='cxco';}
               if($moduo=='comp' and ($empr0['empr_cxpa']==1 or $empr0['empr_rete']==1) ){$modu='cxpa';}

               if(!empty($modu))
               {
                    if($modu=='cxco'){$info='ccmovi';}
                    if($modu=='cxpa'){$info='cpmovi';}
                    
                    $enti=$docu1['enti'];
                    $fecha1='2000-01-01';
                    include('sele_info.php');
                    $sql_abon ='select * from ('.$sql_info.')as cxcp ';
                    $sql_abon.='where numer*1='.(int)$docu1['numero'].' ';
                    $sql_abon.='and docu!="'.$docu1['docu'].'" ';
                    $sql_abon.='and docum="'.$docu1['docu'].'" ';
                    $sql_abon.='and ptoem_hide="'.$docu1['ptoe'].'" ';
                    
//                     echo '<br>==== '.$sql_abon;
                    $res_abon=msql_query($sql_abon,$link_data);
                    if(msql_num_rows($res_abon)==0){$res_abon='';}
               }

               $modu=$moduo;
          }     
          
          
          if($docu=='FC10'){gen_invent($docu,$idnum);}
          
     }

     if($modu=='inve')
     {
          $fechar=date('Y-m-d');
          {$fechar=date('Y-m-d',strtotime('-1 week'));}

          // calcula costos
          {
          $sql_item ='select * from item0 ';
          $sql_item.='where item_codigo in ';
          $sql_item.='(select distinct item from inve1 left join inve2 on idnum1=idnum2 ';
          $sql_item.='where ifnull(puni,0)<=(1/1000000) and idnum2='.$idnum.' and idate>='.$fechar.')';
          $res_item=msql_query($sql_item,$link_data);

          while($aso_item=msql_fetch_assoc($res_item))
          {cost_prom($aso_item['item_codigo']);}

          }

          if($docu=='EB13'){gen_invent($docu,$idnum);}
          if($docu=='IB11'){gen_invent($docu,$idnum);}


     }

     if(mb_stristr2('.caja.bcos',$modu))
     {
          $idanti=gen_anticipo($docu,$idnum);
          gen_cajabc($docu,$idnum);
     }
     
}


if(!empty($fdoc)){$res_fdoc=$res_info2;}


if(empty($espe))
{include('ele_docu.php');}

if(!empty($espe))
{include('ele_docu_'.$espe.'.php'); return;}


$psize =substr($pgsize,0,2).' ';
if(substr($pgsize,-1,1)=='V'){$psize.='portrait';}else{$psize.='landscape';}

?>

<!-- <style> -->
<style type="text/css">
     td{vertical-align:top;}
</style>

<style type="text/css" media="print">
/* a {text-decoration: none; color: inherit;} */
     
@page
{
     size: <?php echo $psize; ?>;   /* auto */
     <?php if(substr($pgsize,0,1)=='A') { ?>
     margin: 10mm 10mm 10mm 10mm; /*	top right bottom left */
     <?php } ?>
     
     <?php if(substr($pgsize,0,1)=='P') { ?>
     margin: 0mm 0mm 0mm 0mm; /*	top right bottom left */
     <?php } ?>
     
     <?php if(substr($pgsize,0,1)=='T') { ?>
     margin: 0mm 0mm 0mm 0mm; /*	top right bottom left */
     <?php } ?>
     
     
/*      -webkit-print-color-adjust: exact;      */
}        
</style>  


<!-- @media print {
  a {text-decoration: none;}
 -->

<style>
/* tr {height:1px;}      */
</style>


<script>document.title="<?php echo $xtitle;?>"</script>

<script>
function printFunction()
{
//      var divContents = document.getElementById("DIV_ID").innerHTML;
     var divContents = document.getElementById("main_body").innerHTML;
     var divContents = document.getElementById("cele").innerHTML;
     var prnt = window.open('', '', 'height=800, width=600');
     prnt .document.write(divContents);
     prnt .document.close();
     prnt .print();
}
</script>


<script>
// document.addEventListener("DOMContentLoaded", () => {
//     let boton = document.getElementById("crearpdf");
//     let container = document.getElementById("contenedor");
 
//     boton.addEventListener("click", event => {
//         event.preventDefault();
//         boton.style.display = "none";
//         window.print();
//     }, false);
 
//     container.addEventListener("click", event => {
//         boton.style.display = "initial";
//     }, false);
 
// }, false);     
</script>     
     
