<?php
ini_set('display_errors','0'); 
?>
!#/opt/php/bin/php -q
<?php
$root_path = "/var/www/html";
include $root_path."/common.php";
include $root_path."/lotto/lib/lotto.lib.php";

////API Url
$url = 'https://quotation-api-cdn.dunamu.com/v1/forex/recent?codes=FRX.KRWUSD';
$header = array();
$response = get_curl($root_path, $url, '_exchange_rate.log', $header);
if( !$response )
    exit;

$data = json_decode($response, true)[0];

$data['basePrice']        = round($data['basePrice']);
$data['cashBuyingPrice']  = round($data['cashBuyingPrice']);
$data['cashSellingPrice'] = round($data['cashSellingPrice']);
$data['ttSellingPrice']   = round($data['ttSellingPrice']);
$data['ttBuyingPrice']    = round($data['ttBuyingPrice']);

$pub_datetime = $data['date'].' '.$data['time'];

$sql = "insert into {$lo['exchange_rate_table']} set
            er_date         = '".G5_TIME_YMD."',
            er_base_rate    = '{$data['basePrice']}',
            er_cash_buy     = '{$data['cashBuyingPrice']}',
            er_cash_sell    = '{$data['cashSellingPrice']}',
            er_remit_buy    = '{$data['ttSellingPrice']}',
            er_remit_sell   = '{$data['ttBuyingPrice']}',
            er_pub_datetime = '{$pub_datetime}',
            er_datetime     = '".G5_TIME_YMDHIS."' ";
sql_query($sql);

/*
$history = sql_fetch("select * from {$lo['history_table']} where ht_draw_date = '".G5_TIME_YMD."' ");
if( $history['ht_id'] && !$history['ht_change_rate'] ) {
    if( $history['ht_exchange_type'] == '매매기준율' ) {
        $ht_exchange_rate = $data['basePrice'];
    } else if( $history['ht_exchange_type'] == '송금받을때' ) {
        $ht_exchange_rate = $data['ttBuyingPrice'];
    } else {
        exit;
    }

    $sql = "update {$lo['history_table']} set
                ht_exchange_rate = '{$ht_exchange_rate}'
            where ht_id = '{$history['ht_id']}' ";

    sql_query($sql);
}
*/
