OLDBOY/skin/board/moving_assets/write.skin.php
<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

// 스킨 내 스타일 정의
add_stylesheet('<link rel="stylesheet" href="'.$write_skin_url.'/style.css">', 0);
add_stylesheet('<link rel="stylesheet" href="'.$board_skin_url.'/style.write.css">', 0);

// 업비트 API 설정
require '/home/www/DB/key_upbit_trade.php';
$ACCESS_KEY = $UPBIT_ACCESS_KEY ?? '';
$SECRET_KEY = $UPBIT_SECRET_KEY ?? '';
$SERVER_URL = "https://api.upbit.com";

$b64url = function($d) { return rtrim(strtr(base64_encode($d), '+/', '-_'), '='); };
$make_jwt = function($ak, $sk, $query = []) use ($b64url) {
    $h = ['alg'=>'HS256','typ'=>'JWT'];
    $p = ['access_key'=>$ak,'nonce'=>uniqid('',true)];
    if (!empty($query)) {
        $qs = http_build_query($query);
        $p['query_hash'] = hash('sha512', $qs);
        $p['query_hash_alg'] = 'SHA512';
    }
    $hh = $b64url(json_encode($h));
    $pp = $b64url(json_encode($p));
    $ss = $b64url(hash_hmac('sha256', "$hh.$pp", $sk, true));
    return "$hh.$pp.$ss";
};

// 파라미터 받기
$param_type = $_GET['type'] ?? '';   // 입금 / 출금
$param_date = $_GET['date'] ?? '';   // 2026-03-19T11:09:06+09:00

$upbit_data = [];

if ($param_type && $param_date) {
    $endpoint = ($param_type === '입금') ? '/v1/deposits' : '/v1/withdraws';
    $params = ['limit' => 100];
    $jwt = $make_jwt($ACCESS_KEY, $SECRET_KEY, $params);

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $SERVER_URL.$endpoint.'?'.http_build_query($params),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ["Authorization: Bearer {$jwt}"],
        CURLOPT_TIMEOUT => 10,
    ]);
    $raw = json_decode(curl_exec($ch), true);
    curl_close($ch);

    // 날짜 매칭
    if (is_array($raw)) {
        foreach ($raw as $d) {
            if ($d['created_at'] === $param_date) {
                $upbit_data = $d;
                break;
            }
        }
    }
}

// 폼에 박을 값 세팅
$api_type            = $param_type;
$api_currency        = $upbit_data['currency']         ?? '';
$api_transaction_type = $upbit_data['transaction_type'] ?? '';
$api_amount          = $upbit_data['amount']            ?? '';
$api_fee             = $upbit_data['fee']               ?? '';
$api_state           = $upbit_data['state']             ?? '';
$api_uuid            = $upbit_data['uuid']              ?? '';
$api_date            = $param_date ? date('Y-m-d H:i:s', strtotime($param_date)) : '';
$api_address         = $upbit_data['blockchain_transaction_id'] ?? ($upbit_data['address'] ?? '');
$api_subject         = $api_date ? substr($api_date, 0, 10).' : '.$api_type : '';

// x2_fee_krw, x2_amount_krw 연산
$api_fee_krw    = '';
$api_amount_krw = '';

if ($api_currency === 'KRW') {
    // 원화: 그대로 박기
    $api_fee_krw    = $api_amount;
    $api_amount_krw = $api_amount;
} else {
    // 코인: 시세 환산
    $market = 'KRW-' . $api_currency;
    $api_date_hour = date('Y-m-d H:i:s', strtotime($api_date));

    $sql = "SELECT trade_price FROM daemon_upbit_coin_1h 
            WHERE market = '{$market}' 
            AND collected_at <= '{$api_date_hour}' 
            ORDER BY collected_at DESC LIMIT 1";
    $row = sql_fetch($sql);
    $trade_price = $row['trade_price'] ?? 0;

    $api_fee_krw    = $api_fee;
    $api_amount_krw = $trade_price > 0 ? (float)$api_amount * (float)$trade_price : '';
}

// 새글: 가장 최근 게시물에서 누적값
    $sql = "SELECT * FROM {$write_table} ORDER BY wr_datetime DESC LIMIT 1";
    $prev_write = sql_fetch($sql);





if ($param_type === '입금') { 
    ### 입금 주소 

    // 이자 누적 계산
    $prev_cum_interest = $prev_write['x2_cum_interest'] ?? 0;

    if ($api_transaction_type === 'internal') {
        $api_cum_interest = $prev_cum_interest + (float)$api_amount;
    } else {
        $api_cum_interest = $prev_cum_interest;
    }


    // 원화 입금 누적
    $prev_cum_bank_in = $prev_write['x2_cum_bank_in'] ?? 0;

    if ($api_currency === 'KRW') {

        $api_cum_bank_in = $prev_cum_bank_in + (float)$api_amount;

    } else {

        $api_cum_bank_in = $prev_cum_bank_in;

    }

    // 순수 은행 입금 누적
    $prev_cum_bank_in_pure = $prev_write['x2_cum_bank_in_pure'] ?? 0;

    if ($api_currency === 'KRW' && $api_transaction_type === 'default') {
        $api_cum_bank_in_pure = $prev_cum_bank_in_pure + (float)$api_amount;
    } else {
        $api_cum_bank_in_pure = $prev_cum_bank_in_pure;
    }

    // 거래소 입금 누적
    $prev_cum_exchange_in = $prev_write['x2_cum_exchange_in'] ?? 0;

    if ($api_currency !== 'KRW') {
        $api_cum_exchange_in = $prev_cum_exchange_in + (float)$api_amount_krw;
    } else {
        $api_cum_exchange_in = $prev_cum_exchange_in;
    }

    // 전체 입금 누적
    $prev_total_bank_in = $prev_write['x2_total_bank_in'] ?? 0;
    $api_total_bank_in = $prev_total_bank_in + (float)$api_amount_krw;

    // 전체 출금 누적은 그대로
    $api_total_bank_out = $prev_write['x2_total_bank_out'] ?? 0;

    // 원화 출금 누적은 그대로
    $api_cum_bank_out = $prev_write['x2_cum_bank_out'] ?? 0;

    // 순수 은행 출금 누적은 그대로
    $api_cum_bank_out_pure = $prev_write['x2_cum_bank_out_pure'] ?? 0;

    // 거래소 출금 누적은 그대로
    $api_cum_exchange_out = $prev_write['x2_cum_exchange_out'] ?? 0;

    // 전체 출금 누적 합산은 그대로
    $api_cum_bank_total = (float)$api_cum_bank_in - (float)$api_cum_bank_out;

    // 거래소 입/출금 누적 합산 계산
    $api_cum_exchange_total = (float)$api_cum_exchange_out - (float)$api_cum_exchange_in;

} else { 
    ### 출금 주소

}

?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">

<section id="bo_w">
    <div class="write-container">
        <form name="fwrite" id="fwrite" action="<?php echo $action_url ?>" onsubmit="return fwrite_submit(this);" method="post" autocomplete="off">
            <input type="hidden" name="w" value="<?php echo $w ?>">
            <input type="hidden" name="bo_table" value="<?php echo $bo_table ?>">
            <input type="hidden" name="wr_id" value="<?php echo $wr_id ?>">
            <input type="hidden" name="sca" value="<?php echo $sca ?>">
            <input type="hidden" name="page" value="<?php echo $page ?>">

            <!-- 0.1. 테이블 구조 -->
                <table cellpadding="0" cellspacing="0">
                <tr>
                    <td width="65%" valign="top">

                    <div class="write-card">
                        <div class="card-header">
                            <h2><i class="fa-solid fa-wallet"></i>&nbsp;Asset Transaction Management</h2>
                        </div>

                        <div class="card-body">
                            <div class="form-section">
                                <span class="section-title">01. Basic Information</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-4">
                                        <label class="frm-label">기록 제목 (Subject)</label>
                                        <input type="text" name="wr_subject" value="<?php echo $write['wr_subject'] ?: $api_subject ?>" required class="frm-input" placeholder="데이터를 식별할 제목을 입력하세요">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">자산 종류 (Asset Type)</label>
                                        <input type="text" name="x2_type" value="<?php echo $write['x2_type'] ?: $api_currency ?>" class="frm-input" placeholder="BTC, ETH, KRW...">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">거래 유형 (Transaction Type)</label>
                                        <input type="text" name="x2_transaction_type" value="<?php echo $write['x2_transaction_type'] ?: $api_transaction_type ?>" class="frm-input" placeholder="외부입금, 내부이체 등">
                                    </div>
                                </div>
                            </div>

                            <div class="form-section">
                                <span class="section-title">02. Financial Details</span>
                                <div class="frm-grid">
                                    <div class="frm-group">
                                        <label class="frm-label">구분 (Direction)</label>
                                        <input type="text" name="x2_direction" value="<?php echo $write['x2_direction'] ?: $api_type ?>" class="frm-input" placeholder="IN / OUT">
                                    </div>
                                    <div class="frm-group">
                                        <label class="frm-label">금액 (Amount)</label>
                                        <input type="text" name="x2_amount" value="<?php echo $write['x2_amount'] ?: $api_amount ?>" class="frm-input" placeholder="0.00000000">
                                    </div>
                                    <div class="frm-group">
                                        <label class="frm-label">수수료 (Fee)</label>
                                        <input type="text" name="x2_fee" value="<?php echo $write['x2_fee'] ?: $api_fee ?>" class="frm-input" placeholder="0.00">
                                    </div>
                                    <div class="frm-group">
                                        <label class="frm-label">상태 (Status)</label>
                                        <input type="text" name="x2_state" value="<?php echo $write['x2_state'] ?: $api_state ?>" class="frm-input" placeholder="Done / Pending">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">수수료 연산금</label>
                                        <input type="text" name="x2_fee_krw" value="<?php echo $write['x2_fee_krw'] ?: $api_fee_krw ?>" class="frm-input" placeholder="0.0000000000">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">최종 금액 (환산 금액)</label>
                                        <input type="text" name="x2_amount_krw" value="<?php echo $write['x2_amount_krw'] ?: $api_amount_krw ?>" class="frm-input" placeholder="0.0000000000">
                                    </div>
                                </div>
                            </div>

                            <div class="form-section">
                                <span class="section-title">03. Network & Tracking</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">거래 일시 (Transaction Time)</label>
                                        <input type="text" name="x2_time" value="<?php echo $write['x2_time'] ?: $api_date ?>" class="frm-input" placeholder="YYYY-MM-DD HH:MM:SS">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">지갑 주소 (Wallet Address)</label>
                                        <input type="text" name="x2_address" value="<?php echo $write['x2_address'] ?: $api_address ?>" class="frm-input" placeholder="0x...">
                                    </div>
                                    <div class="frm-group col-span-4">
                                        <label class="frm-label">거래 식별자 (UUID / TXID)</label>
                                        <input type="text" name="x2_uuid" value="<?php echo $write['x2_uuid'] ?: $api_uuid ?>" class="frm-input" placeholder="Unique Transaction Hash or UUID">
                                    </div>
                                </div>
                            </div>

                            <div class="form-section">
                                <span class="section-title">04. Data Time (wr_datetime)</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">데이터 일시 (Data Time)</label>
                                        <input type="text" name="wr_datetime" value="<?php echo $write['wr_datetime'] ?: $api_date ?>" class="frm-input" placeholder="게시판 글 작성 일시로 활용됩니다">
                                    </div>
                                </div>
                            </div>
                        </div>                        
                    </div>


                    </td>
                    <td valign="top">
                        <div class="write-card">
                            <div class="card-header">
                                <h2><i class="fa-solid fa-wallet"></i>&nbsp;Add Value</h2>
                            </div>

                            <div class="card-body">
                                <div class="form-section">
                                <span class="section-title">00. Total</span>
                                    <div class="frm-grid">
                                        <div class="frm-group col-span-2">
                                            <label class="frm-label">전체 입금 누적 (KRW in)</label>
                                            <input type="text" name="x2_total_bank_in" value="<?php echo $write['x2_total_bank_in'] ?: $api_total_bank_in ?>" class="frm-input" placeholder="Bank in">
                                        </div>

                                        <div class="frm-group col-span-2">
                                            <label class="frm-label">전체 출금 누적 (KRW out)</label>
                                            <input type="text" name="x2_total_bank_out" value="<?php echo $write['x2_total_bank_out'] ?: $api_total_bank_out ?>" class="frm-input" placeholder="Bank out">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-section">
                                <span class="section-title">01. Bank</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">원화 입금 누적 (KRW in)</label>
                                        <input type="text" name="x2_cum_bank_in" value="<?php echo $write['x2_cum_bank_in'] ?: $api_cum_bank_in ?>" class="frm-input" placeholder="Bank in">
                                    </div>

                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">순수 은행 원화 입금 누적 (Bank KRW in)</label>
                                        <input type="text" name="x2_cum_bank_in_pure" value="<?php echo $write['x2_cum_bank_in_pure'] ?: $api_cum_bank_in_pure ?>" class="frm-input" placeholder="Bank KRW in">
                                    </div>

                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">원화 출금 누적 (KRW out)</label>
                                        <input type="text" name="x2_cum_bank_out" value="<?php echo $write['x2_cum_bank_out'] ?: $api_cum_bank_out ?>" class="frm-input" placeholder="Bank out">
                                    </div>

                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">순수 은행 원화 출금 누적 (Bank KRW out)</label>
                                        <input type="text" name="x2_cum_bank_out_pure" value="<?php echo $write['x2_cum_bank_out_pure'] ?: $api_cum_bank_out_pure ?>" class="frm-input" placeholder="Bank KRW out">
                                    </div>

                                    <div class="frm-group col-span-4">
                                        <label class="frm-label">원화 입/출금 누적 합산 (KRW total)</label>
                                        <input type="text" name="x2_cum_bank_total" value="<?php echo $write['x2_cum_bank_total'] ?: $api_cum_bank_total ?>" class="frm-input" placeholder="Bank total">
                                    </div>
                                </div>

                                <div class="form-section">
                                <span class="section-title">03. Exchange</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">거래소 입금 누적 (Coin in)</label>
                                        <input type="text" name="x2_cum_exchange_in" value="<?php echo $write['x2_cum_exchange_in'] ?: $api_cum_exchange_in ?>" class="frm-input" placeholder="Coin in">
                                    </div>
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">거래소 출금 누적 (Coin out)</label>
                                        <input type="text" name="x2_cum_exchange_out" value="<?php echo $write['x2_cum_exchange_out'] ?: $api_cum_exchange_out ?>" class="frm-input" placeholder="Coin out">
                                    </div>
                                    <div class="frm-group col-span-4">
                                        <label class="frm-label">거래소 입/출금금 누적 합산 (Coin total)</label>
                                        <input type="text" name="x2_cum_exchange_total" value="<?php echo $write['x2_cum_exchange_total'] ?: $api_cum_exchange_total ?>" class="frm-input" placeholder="Coin total">
                                    </div>
                                </div>

                                <div class="form-section">
                                <span class="section-title">04. Add Other</span>
                                <div class="frm-grid">
                                    <div class="frm-group col-span-2">
                                        <label class="frm-label">이자 누적 (Coin Accumulated)</label>
                                        <input type="text" name="x2_cum_interest" value="<?php echo $write['x2_cum_interest'] ?: $api_cum_interest ?>" class="frm-input" placeholder="Coin Accumulated">
                                    </div>
                                </div>
                            </div>
                        </div>

                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <div class="card-footer">
                            <a href="<?php echo get_pretty_url($bo_table); ?>" class="btn-cancel">Cancel and Exit</a>
                            <button type="submit" id="btn_submit" class="btn-submit">Execute Storage Process</button>
                        </div>
                    </td>
                </tr>
            </table>

        </form>
    </div>
</section>

<script>
function fwrite_submit(f) {
    if (!f.wr_subject.value.trim()) {
        alert("기록 제목은 필수입니다.");
        f.wr_subject.focus();
        return false;
    }
    const btn = document.getElementById("btn_submit");
    btn.disabled = true;
    btn.innerHTML = "Processing...";
    btn.style.opacity = "0.6";
    return true;
}
</script>