<?php 
foreach($_GET  as $key => $value){${$key}=trim($value);} 
foreach($_POST as $key => $value){${$key}=trim($value);} 
include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);
if(empty($user_codigo)){include('user_login0.php');return;}

$enti_emis=$user0['user_cedula'].'001';
$emis0=data_maes('enti',$enti_emis);

$iva=$_SESSION['porc_iva'];

$taxi=1;
if($taxi==1)
{
     $tpos=2;
     $docu='FC10';     
     $item='9999999999';     
}

$modu=strtolower($modu);
$tabla=$modu;

if(!empty($idnum1))
{
     $sql_docu='select * from '.$tabla.'1 left join '.$tabla.'2 on idnum1=idnum2 where idnum1="'.$idnum1.'" order by iddet desc limit 1';
     $res_docu=msql_query($sql_docu,$link_data);
     $data_docu=msql_fetch_assoc($res_docu);
     foreach($data_docu as $campo => $valor){${$campo}=trim($valor);} 
}

$cant=1;

if(empty($ptov) and $tpos<=2){$ptov=$user0['user_ptov'];}
if(empty($vndd)){$vndd=$user0['user_cedula'];}
if(empty($modu)){$modu='vtas';}
if(empty($format)){$format=1;}
if(empty($tpos)){$tpos=2;}
if(empty($tpre)){$tpre=2;}
if(empty($ptov))
{
     $sql_ptov='select * from '.$modu.'1 where iuser="'.$user_codigo.'" order by idnum1 desc limit 1';
     $res_ptov=msql_query($sql_ptov,$link_data);
     $ptov=msql_result($res_ptov,0,'ptov');
}

if(empty($lpre))
{
     $lpre='puni1';
     if($cfg_empr=='imcp'){$lpre='puni3';}
}


if($format==1){$info=substr($modu,0,2).'det2';}
if($format==2){$info=substr($modu,0,2).'deta';}
if($format==3){$info=substr($modu,0,2).'blog';}

if($tpos==1){$info=substr($modu,0,2).'det1';}
if($tpos==2){$info=substr($modu,0,2).'det2';}
if($tpos==3){$info=substr($modu,0,2).'det2';}


$info_descri='VENTAS';

if(empty($docu)){$docu='FC10';}

$docu0=data_maes('docu',$docu);
$enti0=data_maes('enti',$enti);
$item0=data_maes('item',$item);
// $lsub0=data_maes('lsub',$lsub);
$ptov0=data_maes('ptov',$ptov);

$xtitulo='VENTAS';
$xtitle='REG.'.$xtitulo;
// if($tpos==2){$xtitle='PTO. VENTA';}
if($tpos==2){$xtitle='Factura TAXI';}
$xtopcenter=$xtitle;
if(!empty($docu)){$xtitle.=' /'.$docu;}
if(!empty($numero)){$xtitle.='-'.$numero;}
if(empty($fecha)){$fecha=date('Y-m-d');}

$fecha_min=date('Y-m-d');
$fecha_max=date('Y-m-d');

$idnum1='';


// echo '<br>P*//*'.$tpos;
// echo '<br>D*//*'.$docu;
// echo '<br>P*//*'.$ptov;
// echo '<br>N*//*'.$numero;
if($tpos!=0 and !empty($docu)  and !empty($ptov) and !empty($numero))
{
     include('sele_info.php');
}
// echo '<br>S*/*/*'.$sql_info;
if(!empty($sql_info))
{
     $sql_info1=$sql_info;
     $sql_info1=str_replace('group by idnum1,iddet','group by idnum1',$sql_info1);
     $res_info1=msql_query($sql_info1,$link_data);
     
     $subt_iva=msql_result($res_info1,0,'subt_iva_hide');
     $subt_iva0=msql_result($res_info1,0,'subt_iva0_hide');
     $tiva=msql_result($res_info1,0,'iva');
     $total=msql_result($res_info1,0,'total');
     
     
     $claveacc=msql_result($res_info1,0,'claveacc_hide');
     $iuser=msql_result($res_info1,0,'iuser_hide');
     
     if(msql_num_rows($res_info1)!=0)
     {
          $subt_iva0=number_format($subt_iva0,2);
          $subt_iva=number_format($subt_iva,2);
          $tiva=number_format($tiva,2);
          $total=number_format($total,2);
     }
     
     $res_info=msql_query($sql_info,$link_data);
     if(!empty($numero))
     {
          $idnum1=msql_result($res_info,0,'idnum1_hide');
          $edit=msql_result($res_info,0,'b_o');

          $data_head=data_docu($docu,$idnum1);
          foreach($data_head as $key => $value){${$key}=trim($value);} 
     }
     $enti0=data_maes('enti',$enti);          
     
}




// datalist
{

$ztabla='enti';
{$zwhere='(left(enti_codigo,10) in(select distinct left(enti,10) from vtas1))';}
include('ele_datalist.php');

$ztabla='item';
$zlpre='pvp1';     
$zwhere='(left(item_codigo,2)!="99")';
include('ele_datalist.php');
     
$ztabla='docu';
$zwhere='(docu_tabla="'.$modu.'")';
include('ele_datalist.php');

echo '<datalist id="dlpuni"></datalist>';
echo '<datalist id="dlpvp"></datalist>';
echo '<datalist id="dldcto"></datalist>';

}



// PANTALLA FILTROS
// echo '<div id="filtros" noprint >';
echo '<div id="filtros"  >';
{
     
// OCULTOS
echo '<div hide>';   
{
     echo '<input id="ipip" value="'.$ipip.'">';
     echo '<input id="modu" value="'.$modu.'">';
     echo '<input id="info" value="'.$info.'">';
     echo '<input id="tpos" value="'.$tpos.'">';
     echo '<input id="idnum1" value="'.$idnum1.'">';
     echo '<input id="stfin" value="'.$stfin.'">';
     echo '<input id="taxi" value="'.$taxi.'">';
}     
echo '</div>';     
          
$zobj=2;     
include('ele_break.php');
     
$zid='btn_nuevo'; 
$zlabel='Nuevo';
$zalign='right';
$zonclick='{fn_nuevo();}';
$zsize=1.2;     
$zsize=1.8;     
include('ele_button.php');
         
$zid='fecha';
$ztype='date';
$zlabel='Fecha';
$zvalue=$fecha;
$zmin=$fecha_min;     
$zmax=$fecha_max;     
$zreado=2;
// if($taxi==1){$zhide=1;}
$zhide=1;     
$zsize=1.8;     
include('ele_input.php');  
     
     
// $zobj=1;     
// include('ele_break.php');

     
$zid='docu';
$zlabel='Docum.';
$zvalue=$docu;
$ztabla='docu';
$zwhere='(docu_tabla="'.$modu.'")';
if(!empty($docu)){$zreado=2;}
$zonblur='{location.href="?scri='.basename(__FILE__,'.php').'&tpos="+document.getElementById("tpos").value+"&docu="+document.getElementById("docu").value+"&ptov="+document.getElementById("ptov").value;}';
$zonchange=$zonblur;
if($tpos<=2){$zobj=1;}     
if($tpos<=2){$zreado=1;}     
if($tpos<=2){$zlist='nolist';}
$zhide=1;     
include('ele_select.php');

     
     
$zid='ptov';
$ztabla='ptov';
$zlabel='Pto.Vta.';
$zvalue=$ptov; 
$zhide=1;     
include('ele_select.php');

$zid='numero';
$zlabel='№ Factura';
// $zlabel='📝 Número';
$zvalue=$numero*1;
$zmin=1;
$zmax=9999999;     
$zclass='numeric-int';
$zreado=2;     
$zondblclick='{document.getElementById("'.$zid.'").style.width="'.round(7*2/3,2).'em";}';     
$zonblur='{if(document.getElementById("'.$zid.'").getAttribute("disabled")!=""){document.getElementById("idnum1").value="";fn_consulta();}}';
$zbackcolor='grey';
$zfontcolor='white';
$zsize=1.2;
$zsize=1.7;
include('ele_input.php');
     
$zid='btn_print'; 
$zlabel='Imprimir';
$zalign='right';
$href='?prnt=1&docu='.$docu.'&idnum='.$idnum1.'&formato=';  
$zonclick='{print_url("'.$href.'");}';
if(empty($idnum1)){$zhide=1;}     
$zsize=0.8;     
$zhide=1;     
include('ele_button.php');

     
$zid='btn_final'; 
$zlabel='Finalizar';
$zalign='right';
$zonclick='{fn_finalizar();}';
if(!empty($numero) and empty($edit)){$zhide=1;}     
if(empty($numero)){$zhide=1;}     
if($stfin==1){$zhide=1;}    
$zhide=1;     
$zsize=0.8;
include('ele_button.php');
     
$zid='btn_consul'; 
$zlabel='Consultar';
$zalign='right';
$zonclick='{fn_consulta(event);}';
$zhide=1;     
include('ele_button.php');

echo '<span style="float:right; white-space:nowrap;">';     
echo '</span>';     
     
if(!empty($ptov))
{
     
$zobj=1;     
include('ele_break.php');
     
//if(@mb_stristr('vtas',$modu))
{
     $zobj=2;
     $zobj=6;
     $zid='vndd';
     $ztabla='enti';
     $zvalue=$vndd;
     $zlist='dlvndd';
     $zalign='right';
     $zreado=1;
//      $zlabel='𐃏✇ Conductor';
     $zlabel='✇ Conductor';
     include('ele_input.php');
     
     $zid='enti';
//      $zlabel='⑉ Cliente';
     $zlabel='👤 Cliente';
     $zplace='cedula o ruc';
     $zvalue=$enti;
     $ztabla='enti';
     $zsize=1.7;
     $zexact=1;
     if(!empty($numero))
     {$zreado=1;}
     $zautoc='off';
     include('ele_input.php');
     
     
     $zobj=1;
     include('ele_break.php');
     
     if($tpos==2)
     {
     $zid='nombre';
     $zlabel='Nombre';
     $zvalue=$nombre;
     $zsize=0.9;
     $zsize=1.7;
     $zhide=1;
     include('ele_input.php');
     
     $zid='email';
     $zlabel='e-mail';
     $zvalue=$email;
     $zsize=0.9;
     $zsize=1.7;
     $zhide=1;
     include('ele_input.php');
     
          
     $zid='wsapp';
     $zlabel='WsApp';
     $zlabel='📱 Celular';
     $zplace='wsapp';
          
     $zvalue=$wsapp;
     if(empty($zvalue))
     {$zvalue=$enti0['enti_wsapp'];}
     $ztype='number';
     $zsize=0.9;
     $zsize=1.7;
     include('ele_input.php');
     }
          

     $zobj=2;
     $zobj=1;
     include('ele_break.php');
     
     $zobj=4;
     $zid='item';
     $zlabel='Item';
     $zvalue=$item;
     $ztabla='item';
     $zhide=1;
     include('ele_input.php');
     
     $cant=1;
     $zobj=4;
     $zid='cant';
     $zlabel='Cant.';
     $zvalue=$cant;
     $zdefa=1;
     $zwidth=5;
     $zclass='numeric-dec';
     $zhide=1;
     include('ele_input.php');


//      if($cost=='true')
     {
//      $zobj=4;
     $zid='totaliva';
     $zvalue=$totaliva;
     if($total>0)
     {$zvalue=$totaliva;}     
     $zmin=0;
     $zmax=9999.99;
     $zclass='numeric-dec';
     $zsize=1.7;
     $zlabel='💲 Total';
     if(!empty($numero))
     {$zreado=1;}
//      $zonchange='{parseFloat(document.getElementById("puni").value=(document.getElementById("totaliva").value / (1+(document.getElementById("iva").value)/100)).toFixed(6));}';
     $zonchange='{document.getElementById("puni").value=parseFloat((document.getElementById("totaliva").value / (1+(document.getElementById("iva").value)/100)).toFixed(6));}';
     $zonchange.='{document.getElementById("tiva").value=(document.getElementById("puni").value * document.getElementById("iva").value /100).toFixed(2);}';
     include('ele_input.php');
          
          
     $zobj=1;
     include('ele_break.php');
          
     $zobj=4;
     $zsize=0.8;     
     $zid='puni';
     $zvalue=$puni;
     if($total>0)
     {$zvalue=$total;}     
     $zmin=0;
     $zmax=999999.99;
     $zclass='numeric-dec';
     $zlabel='Sub-Total';
     {$zreado=1;}
//      $zhide=1;     
     include('ele_input.php');
          
     $zobj=4;
     $zsize=0.8;     
     $zid='iva';
     $zvalue=$iva;
     $zmin=0;
     $zmax=$_SESSION['porc_iva'];
     $zclass='numeric-dec';
     $zlabel='% IVA';
     {$zreado=1;}
     $zhide=1;     
     include('ele_input.php');
          
     $zobj=4;
     $zsize=0.8;     
     $zid='tiva';
     $zvalue=$tiva;
     $zmin=0;
     $zmax=999.99;
     $zlabel='IVA';
     {$zreado=1;}
//      $zhide=1;     
     include('ele_input.php');
          
          
          
     }

     $zobj=1;
     include('ele_break.php');
     
     $zid='concepto';
     $zlabel='Concepto';
     $zvalue=$concepto;
     $zwidth=25;
     $zrows=2;
     $zsize=1.5;
     
     include('ele_input.php');
     
     
     
}


     
     
          

$zid='btn_grabar'; 
$zlabel='Grabar';
$zalign='right';
$zonclick='{save_docu(event);}';
$zsize=1.7;
if(!empty($numero) and empty($edit)){$zhide=1;}  
if($format==3){$zhide=0;}     
$zsize=1.7;
$zbackcolor='black';     
if($total>0){$zhide=1;}
include('ele_button.php');
     

     
// $zobj=1;     
// include('ele_break.php');

     
$zid='btn_deleall'; 
$zlabel='Borrar';
$zalign='right';
$zonclick='{fn_deledeta(event);}';
//$zsize=1.7;
$zbackcolor='darkred';
$zfontcolor='white';     
if(!empty($numero) and empty($edit)){$zhide=1;}       
$zhide=1;     
include('ele_button.php');
     
     
     
     


     
$zobj=4;
$zid='detalle';
$zlabel='Notas';
$zwidth=25;    
$zrows=2;     
$zvalue=$detalle;
if($tpos==2){$zhide=1;}
     
// $zvalue=$data_docu[$zid];
     
include('ele_input.php');
     

     
     
     

$zobj=2;     
include('ele_break.php');

}
     
// TITULO     
     
     
}     



// RESULTADO
$sty_resu='float:right;';
$sty_resu='margin:0 auto;';
echo '<span id="main2" style="'.$sty_resu.'"  >';
{
     if(gettype($res_info)!='NULL')
     {
          $zreso=$res_info;
          $zinfo=$info;
          $zname=$zname;
          $zhead=0;
          $zgfsize=0.9;
          $zheightrow='2';
          $zblockidnum=1;
          $zhidebtns=1;
          include('ele_grid.php');     
     }
}     
echo '</span>';     


echo '</div>';


$zid='btn_envia'; 
$zlabel='WsApp';
$zalign='right';
if(!empty($numero))
{$zonclick='{send_wsapp(document.getElementById("wsapp").value);}';}
if(empty($claveacc))
{$zonclick='{location.reload();';}
$zsize=1.7;
if(empty($numero)){$zhide=1;}  
$zsize=1.7;
include('ele_button.php');
     

echo '<div align="center" height="auto" id="divstock" unselectable="on" onmousedown="return false;" onselectstart="return false;">';
echo '</div>';
echo '<div style="clear:both;">&#127380; : '.$idnum1.'</div>';


?>
<script>
function fn_consulta(event)
{
     var xhref='',dataget;
     xhref=xhref+'?scri=<?php echo basename(__FILE__,'.php');?>';
     
     dataget=new_FormData('filtros','get');
     xhref=xhref+dataget;
     
     document.getElementById("main2").innerHTML='Espere...';
     location.href=xhref;
}
</script>

<script>
function fn_nuevo(event)
{
     var enti;
     swal.fire({
          icon:'warning',
          title:'Nuevo Documento',
          text:'Continuar ?',
          confirmButtonText:'Nuevo',
          confirmButtonColor:color1,
          focusCancel:true,
          showCancelButton:true,
          cancelButtonColor: 'darkred',
          timer:60000
     }).then((result) => 
          {
               if(result.isConfirmed)
               {
                    document.getElementById("fecha").value="";
                    document.getElementById("numero").value="";
                    document.getElementById("idnum1").value="";
                    enti=document.getElementById("enti").value;
     //                if(enti.indexOf("9999999999")<0)
                    {document.getElementById("enti").value="";}
                    fn_consulta(event);
               }
          })
}     
</script>


<script>
function fn_deledeta(event)
{
     anul_docu(document.getElementById('docu').value,document.getElementById('idnum1').value,'ALL');
     return;
}          
</script>

<script>
function fn_finalizar(event)
{
     swal.fire({
          icon:'warning',
          title:'Finalizar Documento',
          text:'Continuar ?',
          confirmButtonText:'Finalizar',
          confirmButtonColor:color1,
          focusCancel:true,
          showCancelButton:true,
          cancelButtonColor: 'darkred',
          timer:10000
     }).then((result) => 
          {
               if(result.isConfirmed)
               {
               document.getElementById("stfin").value="1";
               save_docu(event);
               }
          })
}
</script>

<script>
</script>

<script>document.title="<?php echo $xtitle;?>"</script>
<script>document.getElementById('topcenter').innerHTML="<?php echo $xtopcenter;?>"</script>
<!-- <script>calc_subtot()</script> -->
<script>document.getElementById('enti').focus();</script>

<script>
function send_wsapp(wsapp='')
{
     var url='',texto='',phone='',link='';
     link='https://<?php echo $_SERVER['SERVER_NAME']; ?>?scri=info_docu3&prnt=1';
     link=link+'&pgsize=P';
     link=link+'&clave='+'<?php echo $claveacc; ?>';
     link=encodeURIComponent(link);
     
     var eol='%0A';
     texto ='*=== FACTURA ELECTRONICA ===*';
     texto+=eol;
     texto+='Emisor : '+'<?php echo $enti_emis.' - '.$emis0['enti_descri']; ?>'+' ';
     texto+=eol;
     texto+='👤 Cliente : '+'<?php echo $enti.' - '.ucwords(strtolower($nombre)) ; ?>'+' ';
     texto+=eol;
     texto+='📆 Fecha : '+'<?php echo $fecha; ?>'+' ';
     texto+=eol;
     texto+='🎫 Número : *'+'<?php echo $ptoe.'-'.$numero; ?>'+'* ';
     texto+=eol;
     texto+='💲Valor  : $ '+'<?php echo number_format($total,2); ?>'+' ';
     texto+=eol;
     texto+='🔑 Clave : '+'<?php echo $claveacc; ?>'+' ';
     texto+=eol;
     texto+='🔗 Link : '+link;
//      texto=encodeURIComponent(texto);
     
     if(wsapp!=='')
     {
          wsapp.substring(wsapp.length-10);
          wsapp=String(wsapp).padStart(10, '0');
          phone='593';
          phone='593'+wsapp;
     }          
     url=url+'https://api.whatsapp.com/send/';
     url=url+'?phone='+phone;
     url=url+'&text='+texto;
     
     if(phone==='')
     {
          swal.fire({
               icon:'warning',
               title:'Falta Número Celular',
               text:'Seleccionar desde WhatsApp ?',
               confirmButtonText:'Continuar',
               confirmButtonColor:color1,
               focusCancel:true,
               showCancelButton:true,
               cancelButtonColor: 'darkred',
               timer:10000
          }).then((result) => 
               {if(result.isConfirmed){window.open(url,'_blank');}})
     }
     else{window.open(url,'_blank');}
     
     
     
}
</script>
