<?php 
session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

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);

$docu1=$docu;
$docu2=substr($docu2,0,2).'10';

$docu10=data_maes('docu',$docu1);
$docu20=data_maes('docu',$docu2);

$xtitulo = $docu20['docu_descri'];
$xtitle = $xtitulo;
$xtopcenter = $xtitulo;


// function clon_docu($docu1,$idnum,$docu2)
{

     if(empty($enti))
     {$enti=$user0['user_cedula'];$enti0=data_maes('enti',$enti);}
     
     if(!empty($enti))
     {
          $sql_user='update user0 set user_cedula="'.$enti.'" where user_tipo not in("sist","admi") and ifnull(user_cedula,"")="" and user_codigo="'.$user_codigo.'"';
          $res_user=msql_query($sql_user,$link_data);
     }
     
     $tablao=$docu10['docu_tabla'];
     $tablad=$docu20['docu_tabla'];
		
	$sql_docu1='select * from '.$tablao.'1 where idnum1="'.$idnum1.'"' ;
	$res_docu1=msql_query($sql_docu1,$link_data);
  
	$sql_docu2='select * from '.$tablao.'2 where idnum2="'.$idnum1.'"' ;
	$res_docu2=msql_query($sql_docu2,$link_data);
     
     $ptoe=msql_result($res_docu1,0,'ptoe');
     $status=msql_result($res_docu1,0,'status');
     $concepto=msql_result($res_docu1,0,'concepto');
     $numero=msql_result($res_docu1,0,'numero');

     if(strlen(trim($enti))>=10)
     {
          $sql_updt ='update enti0 set ';
          if(!empty($email))
          {$sql_updt.='enti_email="'.$email.'", ';}
          if(!empty($wsapp))
          {$sql_updt.='enti_wsapp="'.$wsapp.'", ';}
          $sql_updt.='enti_status=enti_status ';
          $sql_updt.='where enti_codigo like "'.substr($enti,0,10).'%" ';
          $res_updt=msql_query($sql_updt,$link_data);
     }
          
     $concepto2=trim($concepto);
     
     if(msql_num_rows($res_docu2)!=0)
     {
     
          if($docu1!=$docu2)
          {$concepto2.=' /'.$docu1.'-'.$numero;}

          if($docu1!=$docu2 and false)
          {
          $sql_docud='select * from '.$tablad.'1 ' ;
          $sql_docud.='where status!="X" ' ;
          $sql_docud.='and docu="'.$docu2.'" ' ;
          $sql_docud.='and ptoe!="" ' ;
          $sql_docud.='and ptoe is not null ' ;
            if(mb_stristr('vtas',$tablad))
            {$sql_docud.='and ptoe="'.$ptoe.'" ' ;}
            if(mb_stristr('CH',substr($docu2,0,2)))
            {$sql_docud.='and ptoe="'.$ptoe.'" ' ;}
          $sql_docud.='order by fecha desc, idnum1 desc limit 1' ;
          $res_docud=msql_query($sql_docud,$link_data);
          $ptoed=@msql_result($res_docud,0,'ptoe');
          if($ptoed!=''){$ptoe=$ptoed;}
          }


          $ptoed='001101';
          if($cfg_empr=='imcp'){$ptoed='003101';}
          $_PARAMS['docu']=$docu2;
          $_PARAMS['ptoe']=$ptoed;
          $_PARAMS['ptov']=$ptoed;
          $new_numero=new_numero($_PARAMS);

          $sql_ndocu='insert into '.$tablad.'1(docu,iuser,idate)value("'.$docu2.'","'.$user_codigo.'",now())' ;
          $res_ndocu=msql_query($sql_ndocu,$link_data);
          $idnum2=msql_insert_id($link_data);

          $sql_stru1='describe '.$tablao.'1';
          $res_stru1=msql_query($sql_stru1,$link_data);
          while ($aso_stru1=msql_fetch_assoc($res_stru1))
          {
               if(!mb_stristr('idnum1.iuser.idate',trim($aso_stru1['Field'])))
               {
                $sql_updt ='update '.$tablad.'1 set '.$aso_stru1['Field'].'="'.msql_result($res_docu1,0,$aso_stru1['Field']).'" ';
                $sql_updt.='where idnum1="'.$idnum2.'"';
                $res_updt=msql_query($sql_updt,$link_data);
               }
          }

          $sql_docu2='select * from '.$tablao.'2 where idnum2="'.$idnum1.'" order by iddet' ;
          $res_docu2=msql_query($sql_docu2,$link_data);

          $crow=0;
          while ($aso_docu2=msql_fetch_assoc($res_docu2))
          {
               $crow++;
               if($crow>1000){echo '************* BREAK'; die();}
               $sql_ndocu='insert into '.$tablad.'2(idnum2,iddet)value("'.$idnum2.'",'.$aso_docu2['iddet'].')' ;
               $res_ndocu=msql_query($sql_ndocu,$link_data);
               $sql_stru2='describe '.$tablao.'2';
               $res_stru2=msql_query($sql_stru2,$link_data);

               while ($aso_stru2=msql_fetch_assoc($res_stru2))
               {
                    if($aso_stru2['Field']!='idnum2')
                    {
                         $sql_updt ='update '.$tablad.'2 set '.$aso_stru2['Field'].'="'.$aso_docu2[$aso_stru2['Field']].'" ';
                         $sql_updt.='where idnum2="'.$idnum2.'" and iddet='.$aso_docu2['iddet'].'';
                         $res_updt=msql_query($sql_updt,$link_data);
                    }
               }
          }

          $vndd=$enti0['enti_vndd'];
          if(empty($vndd))
          {
               $iuser0=data_maes('user',$user_codigo);
               $vndd=$iuser0['user_cedula'];
          }
          
          $sql_docu2 ='update '.$tablad.'1 set ' ;
          $sql_docu2.='docu="'.$docu2.'",' ;
          $sql_docu2.='concepto="'.$concepto2.'",' ;
          $sql_docu2.='vndd="'.$vndd.'",' ;
          $sql_docu2.='iuser="'.$user_codigo.'",' ;
          $sql_docu2.='idate=now(),' ;
          $sql_docu2.='idfrom="'.$idnum.'",' ;
          $sql_docu2.='mdfrom="'.$tablao.'",' ;
          if(mb_stristr('sist.supe.admi',$user0['user_tipo']))
          {$sql_docu2.='stfin="0",' ;}
          if(!mb_stristr('sist.supe.admi',$user0['user_tipo']))
          {$sql_docu2.='stfin="1",' ;}
          $sql_docu2.='status="-"' ;
          $sql_docu2.='where idnum1="'.$idnum2.'"' ;
          $res_docu2=msql_query($sql_docu2,$link_data);


          //   if($docu1==$docu2)
          {
               $sql_docu2 ='update '.$tablad.'1 set ' ;
               $sql_docu2.='fecha=now(),' ;
               $sql_docu2.='enti="'.$enti.'",' ;
               $sql_docu2.='ptoe="'.$ptoed.'",' ;
               $sql_docu2.='ptov="'.$ptoed.'",' ;
               $sql_docu2.='numero="'.$new_numero.'",' ;
               $sql_docu2.='status="-" ' ;
               $sql_docu2.='where idnum1="'.$idnum2.'"' ;
               $res_docu2=msql_query($sql_docu2,$link_data);
          }  


          // limpia carrito
          if(!mb_stristr('sist.supe.admi',$user0['user_tipo']))
          {
               $sql_docu2='delete from '.$tablao.'2 where idnum2="'.$idnum1.'" ' ;
               $res_docu2=msql_query($sql_docu2,$link_data);
          }
          
     }
          
          
}

// echo '<br><a href="https://'.$cfg_empr.'.vcrxol.com/?prnt=1&docu='.$docu2.'&idnum='.$idnum2.'&formato=" target="_blank">VISUALIZAR</a>';
// https://demo.vcrxol.com/?prnt=1&docu=CAR1&idnum=3&formato=

$info='';
$info=substr($tablad,0,2).'det2';

$docu=$docu2;
$idnum=$idnum2;
$numero=$new_numero;
$enti0=data_maes('enti',$enti);

include('sele_info.php');

// echo '/*/*/*'.$sql_info;

if(!empty($sql_info))
{
     $sql_info1=$sql_info;
     $sql_info1=str_replace('group by idnum1,iddet','group by idnum1',$sql_info1);
     $sql_info1=str_replace('group by idnum1,iddet','group by idnum1',$sql_info1);
     $res_info1=msql_query($sql_info1,$link_data);
     $total=msql_result($res_info1,0,'total');
     
//      $total=msql_result($res_info1,0,'total');
     if(msql_num_rows($res_info1)!=0)
     {
          $total=number_format($total,2);
     }
     include('ele_break.php');          
     
     $zid='docu';
     $zlabel='Documento';
     $zvalue=$docu20['docu_descri'];
     $zobj=1;
     $zreado=1;
     $zsize=1.2;
     include('ele_input.php');          
     
     $zid='numero';
     $zlabel='Número';
     $zlabel='#';
     $zvalue=$new_numero;
     $zreado=1;
//      $zobj=1;
     $zsize=1.2;
     include('ele_input.php');          

     $zobj=1;
     include('ele_break.php');          
     
     $zid='enti';
     $zlabel='Cliente';
     $zvalue=$enti0['enti_descri'];
     $zreado=1;
     $zsize=1.2;
     include('ele_input.php');          
     
     $zobj=1;
     include('ele_break.php');          

     $zid='adrs';
     $zlabel='Dirección';
//      $zvalue=$adrs0['adrs_descri'];
     $zvalue=$enti0['enti_direccion'];
     $zreado=1;
     $zsize=1.2;
     include('ele_input.php');          

     
     $zobj=1;
     include('ele_break.php');          
     
     
     $zid='total';
     $zlabel='Total';
     $zvalue='$ '.$total;
     $zreado=1;
     $zsize=1.2;
     include('ele_input.php');          

     
     $zid='btn_print'; 
     $zlabel='Imprimir';
     $zalign='right';
     $href='?prnt=1&docu='.$docu2.'&idnum='.$idnum2.'&formato=';  
     $href='?scri=info_docu3&prnt=1&docu='.$docu2.'&idnum='.$idnum2.'&formato=';  
     $zonclick='{print_url("'.$href.'");}';
     $zsize=1.2;
     include('ele_button.php');

     
     
     
     include('ele_break.php');          
     
     $res_info=msql_query($sql_info,$link_data);
          $zreso=$res_info;
          $zinfo=$info;
          $zname=$zname;
          $zhead=0;
//           $zgfsize=1.2;
//           $zblockrows=3;
//           $zheightrow='1.2em;';
          $zheightrow='2em;';
          $zblockidnum=1;
          $zhidebtns=1;
//           $zwidthtable='80%';
          include('ele_grid.php');     
     
     
}

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