$AMENDDATE=$$UPDATECODE;
$CHECKSUM=$$serverchk;


// uninstall if exists
Query2Var("select count(*) as chk from conf_exchangerate where exdate like '$$MONTH' ");

if($chk>0){
  exec_query("delete from conf_exchangerate where exdate like '$$MONTH' ");
}
  
// insert data
$SQL1=<<<EOF
$$SQL1

EOF;


$SQLARR1=explode(";",$SQL1);
$Loop_i = count($SQLARR1);
for($i=0;$i<$Loop_i;$i++){
    $SQL1=trim($SQLARR1[$i]);
    if($SQL1 !=''){
        exec_query($SQL1);
    }
}

Query2Var("select count(*) as chk from conf_exchangerate where  amenddate='$AMENDDATE' ");

//================================================================================================//

// uninstall if exists
Query2Var("select count(*) as chk from conf_exchangerate_export where exdate like '$$MONTH' ");

if($chk>0){
  exec_query("delete from conf_exchangerate_export where exdate like '$$MONTH' ");
  
}
  

// insert data
$SQL2=<<<EOF
$$SQL2

EOF;


$SQLARR2=explode(";",$SQL2);
$Loop_i = count($SQLARR2);
for($i=0;$i<$Loop_i ;$i++){
    $SQL2=trim($SQLARR2[$i]);
    if($SQL2 !=''){
        exec_query($SQL2);
    }
}


Query2Var("select count(*) as chk from conf_exchangerate_export where  amenddate='$AMENDDATE' ");


if($chk == $CHECKSUM){
  // 2021/05 : add default code for get PHPVERSION, JAVAVERSION, ORGANIZE
  $sql = "SELECT value as 'org' FROM conf_global 
  where name='IMDECL_GATEWAY_ORG' 
  AND value<>''
  GROUP BY value
  ORDER BY value;";
  $_all = Query2ArrayVarPack($sql);

  for($i=0; $i<count($_all['org']); $i++){
    $organizeDataRES = array();
    $organizeDataRES['ORG'] = $_all['org'][$i];
    // find profileCode by 
    $sql = "SELECT profileCode FROM conf_global
    WHERE name='IMDECL_GATEWAY_ORG' AND value='{$_all['org'][$i]}' ;";
    $listProfileCode = Query2ArrayVarPack($sql);
    
    // find profile from profileCode
    $whereProfile = " AND profileCode in ('".implode("','", $listProfileCode['profileCode'])."') ";
    $sql = "SELECT value AS _PROFILE FROM conf_global 
    WHERE name='IMDECL_PROFILE' AND value<>'' {$whereProfile} GROUP BY value ;";
    $listProfile = Query2ArrayVarPack($sql);
    
    $organizeDataRES['PROFILE'] = $listProfile['_PROFILE'];

    // set data for update in SPNPatch
    $reStruct[]=$organizeDataRES;
    unset($organizeDataRES);
  }

  // get java version
  if(!empty($JAVA_PATH)){
    $JAVAVERSION=array();
    $javaCmd = $JAVA_PATH." -version 2>&1";
    exec($javaCmd, $JAVAVERSION);
    //echo $javaCmd;
  }


  //echo json_encode($reStruct);
  $INFO['PHPVERSION']= phpversion();
  $INFO['GETORGANIZE']= $reStruct;
  $INFO['JAVAVERSION']= $JAVAVERSION;
  $VERSION=$INFO; 
  // end 2021/05

$PATCH_STATUS="OK";
}else{
$PATCH_STATUS="FAILED";
}
