<?php
declare(strict_types=0);
function plan_atvd($idnum)
{
     if(empty($idnum)){return;}
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}

     $sql_plan='select * from plan2 where idnum2="'.$idnum.'" and asub like "%0000" order by iddet desc limit 1';
     $res_plan=msql_query($sql_plan,$link_data);
     
     if(msql_num_rows($res_plan)==1)
     {
          $sql_dele='delete from plan2 where idnum2="'.$idnum.'"';
          $res_dele=msql_query($sql_dele,$link_data);
          
          $asub=msql_result($res_plan,0,'asub');
          $sql_asub='select * from asub0 where asub_status not in ("X","I") and asub_codigo like "'.substr($asub,0,4).'%" order by asub_codigo';
          $res_asub=msql_query($sql_asub,$link_data);
          
          $iddet=1;
          while($aso_asub=msql_fetch_assoc($res_asub))
          {
               $data_inse=array();
               $data_inse['idnum2']=$idnum;
               $data_inse['iddet']=$iddet++;
               $data_inse['asub']=$aso_asub['asub_codigo'];
               $data_inse['cant']=1;
               $data_inse['puni']=1;
               $data_inse['idate2']=date('YmdHis');
               $data_inse['iuser2']=$_SESSION['user_codigo'];
               
               $columns = implode(", ",array_keys($data_inse));
//                $escaped_values = array_map('mysql_real_escape_string', array_values($data_inse));
//                $values  = implode("', '", $escaped_values);
               $values='"';
               foreach($data_inse as $key => $value){$values.=$value.'","';}
               $values=substr($values,0,-2);
               
               $sql_inse='insert into plan2('.$columns.')values('.$values.')';
               $res_inse=msql_query($sql_inse,$link_data);
          
          }
     
     }
}     
?>