<?php
$sub_menu = "200200";
include_once('./_common.php');

auth_check($auth[$sub_menu], 'w');

check_token();

// 변수 설정입니다. 필요에 맞게 설정하세요.
$year = $_POST['year'];
$cut = $_POST['number'];
$min = 0; // 그 해에 포인트가 10건 이하이면 압축하지 않습니다.
$expire = preg_replace('/[^0-9]/', '', $_POST['po_expire_term']);

// po_datetime을 기준으로 해당 연도의 포인트 개수와 포인트 총합을 가져옴. (항목 많은 순서로 정렬)
$sql = "select `mb_id`, count(`po_point`) as `cnt`, sum(`po_point`) as `sum`
	from `$g5[point_table]` where `po_datetime` like '$year%'
	group by `mb_id` having cnt > '$min' order by `cnt` desc limit $cut";
$query = sql_query($sql);
$total_cnt = 0;
for($i=0; $row = sql_fetch_array($query); $i++) {
	$whereclause = "`mb_id` = '$row[mb_id]' and `po_datetime` like '$year%'";

	// 복사했으니 삭제해버리기
	$sql = "delete from `$g5[point_table]` where $whereclause";
	sql_query($sql);
	
	// 옮겼으니 압축된 항목 삽입하기
        insert_point($row[mb_id], $row[sum], $year.'년 포인트 내역 - 압축', '@point_zip', $row[mb_id], $member['mb_id'].'-'.uniqid(''), $expire);

	// 압축한 건수를 더함
	$total_cnt += $row['cnt'];
}

echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
echo $i . '명의 ' . $year . '년도 포인트 ' . $total_cnt . '건을 정리하였습니다.<br />';
if($i == $cut) echo 'F5를 누르면 추가로 적용됩니다. <a href="./point_list.php?'.$qstr.'">[여기]</a> 를 누르시면 포인트관리 페이지로 이동됩니다.';
?>