<?php
declare(strict_types=0);
// function gen_rbnd0($fecha1=date('Y-m-d'),$fecha2=date('Y-m-d'))
function gen_rbnd0($fecha1,$fecha2)
{
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     
     if(empty($fecha1)){$fecha1=date('Y-m-d');}
     if(empty($fecha2)){$fecha2=date('Y-m-d');}
     
     $sql_rbco='select * from retc1 where docu like "RB%" and fecha>="'.$fecha1.'" and fecha<="'.$fecha2.'" order by fecha';
     $res_rbco=msql_query($sql_rbco,$link_data);
     
     while($aso_rbco=msql_fetch_assoc($res_rbco))
     {gen_rbnd($aso_rbco['docu'],$aso_rbco['idnum1']);}

     
}

function gen_rbnd($docu,$idnum)
{
//      return;
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}

     if(substr($docu,0,2)!='RB'){return;}

     $sql_rbco='select * from retc1 where docu="'.$docu.'" and idnum1="'.$idnum.'"';
     $res_rbco=msql_query($sql_rbco,$link_data);
     
     if(msql_num_rows($res_rbco)==0){return;}     
     if(msql_result($res_rbco,0,'fecha')<='2021-12-31'){return;}

//      echo '*/*/*'.$idnum;
     
     $docu2='ND17';
     $newid=0;
//      $sql_bcos='select * from bcos1 where docu="'.$docu2.'" and idfrom="'.$idnum.'"';
     $sql_bcos='select * from bcos1 where docu like "'.substr($docu2,0,2).'%" and idfrom="'.$idnum.'"';
     
     $res_bcos=msql_query($sql_bcos,$link_data);
     if(msql_num_rows($res_bcos)==0)
     {$newid=clon_docu($docu,$idnum,$docu2);}
     
     if(msql_num_rows($res_bcos)==1)
     {$newid=msql_result($res_bcos,0,'idnum1');}     

     $sql_updt='update bcos1 set docu="'.$docu2.'", status="'.msql_result($res_rbco,0,'status').'" where idnum1="'.$newid.'"';
     $res_updt=msql_query($sql_updt,$link_data);
//      echo '<br>*/*/*'.$sql_updt;
     
     
}




?>