<?php
// 이 파일은 include 파일이므로 common.php 파일을 불러올 필요가 없습니다.

// 현재 가용한도 1회, 1일, 월간, 연간한도 현재 상황에 맞게 재설정
// 현재 날짜와 시간
$current_date = date('Y-m-d H:i:s');
$current_day_start = date('Y-m-d 00:00:00');

// 포인트 충전 한도 초기화
$sql_charge = "SELECT * FROM {$g5['member_table']} WHERE mb_id = '{$member['mb_id']}'";
$row_charge = sql_fetch($sql_charge);

$row_charge_flag = false;

if ($row_charge) {
    $last_date = $row_charge['point_limit_charge_used_last_date'];

    // 연도 변경에 의한 초기화
    // 연간 한도 초기화
    if (date('Y', strtotime($last_date)) < date('Y', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_charge_1year_used = 0, point_limit_charge_1month_used = 0, point_limit_charge_1day_used = 0, point_limit_charge_1try_used = 0  WHERE mb_id = '{$member['mb_id']}'";
        sql_query($sql);
        $row_charge_flag = true;
    }

    // 월간 변경에 의한 초기화
    // 월간 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) < date('Ym', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_charge_1month_used = 0, point_limit_charge_1day_used = 0, point_limit_charge_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_charge_1year > point_limit_charge_1year_used";
        sql_query($sql);
        $row_charge_flag = true;
    }

    // 일간 변경에 의한 초기화
    // 일간 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) == date('Ym', strtotime($current_day_start)) && date('Ymd', strtotime($last_date)) < date('Ymd', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_charge_1day_used = 0, point_limit_charge_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_charge_1month > point_limit_charge_1month_used";
        sql_query($sql);
        $row_charge_flag = true;
    }

    // 1회 변경에 의한 초기화
    // 1회 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) == date('Ym', strtotime($current_day_start)) && date('Ymd', strtotime($last_date)) < date('Ymd', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_charge_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_charge_1try > 0";
        sql_query($sql);
        $row_charge_flag = true;
    }

    // 마지막 사용 날짜 업데이트
    if ($row_charge_flag) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_charge_used_last_date = '{$current_date}' WHERE mb_id = '{$member['mb_id']}'";
        sql_query($sql);
    }
}



// 포인트 출금 한도 초기화
$sql_withdraw = "SELECT * FROM {$g5['member_table']} WHERE mb_id = '{$member['mb_id']}'";
$row_withdraw = sql_fetch($sql_withdraw);

$row_withdraw_flag = false;

if ($row_withdraw) {
    $last_date = $row_withdraw['point_limit_withdraw_used_last_date'];

    // 연도 변경에 의한 초기화
    // 연간 한도 초기화
    if (date('Y', strtotime($last_date)) < date('Y', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_withdraw_1year_used = 0, point_limit_withdraw_1month_used = 0, point_limit_withdraw_1day_used = 0, point_limit_withdraw_1try_used = 0  WHERE mb_id = '{$member['mb_id']}'";
        sql_query($sql);
        $row_withdraw_flag = true;
    }

    // 월간 변경에 의한 초기화
    // 월간 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) < date('Ym', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_withdraw_1month_used = 0, point_limit_withdraw_1day_used = 0, point_limit_withdraw_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_withdraw_1year > point_limit_withdraw_1year_used";
        sql_query($sql);
        $row_withdraw_flag = true;
    }

    // 일간 변경에 의한 초기화
    // 일간 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) == date('Ym', strtotime($current_day_start)) && date('Ymd', strtotime($last_date)) < date('Ymd', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_withdraw_1day_used = 0, point_limit_withdraw_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_withdraw_1month > point_limit_withdraw_1month_used";
        sql_query($sql);
        $row_withdraw_flag = true;
    }

    // 1회 변경에 의한 초기화
    // 1회 한도 초기화
    if (date('Y', strtotime($last_date)) == date('Y', strtotime($current_day_start)) && date('Ym', strtotime($last_date)) == date('Ym', strtotime($current_day_start)) && date('Ymd', strtotime($last_date)) < date('Ymd', strtotime($current_day_start))) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_withdraw_1try_used = 0  WHERE mb_id = '{$member['mb_id']}' AND point_limit_withdraw_1try > 0";
        sql_query($sql);
        $row_withdraw_flag = true;
    }

    // 마지막 사용 날짜 업데이트
    if ($row_withdraw_flag) {
        $sql = "UPDATE {$g5['member_table']} SET point_limit_withdraw_used_last_date = '{$current_date}' WHERE mb_id = '{$member['mb_id']}'";
        sql_query($sql);
    }
}

// 최신 포인트 충전/구매 한도 정보 가져오기
$sql_point_limit_info = "SELECT * FROM {$g5['member_table']} WHERE mb_id = '{$member['mb_id']}'";
$row_point_limit_info = sql_fetch($sql_point_limit_info);
?>