113 lines
3.6 KiB
PHP
113 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* 중도금 입금 알림 자동 발송 크론 작업
|
|
*
|
|
* 매일 실행하여 중도금 입금 기한이 도래한 견적에 대해 대리점에게 알림을 발송합니다.
|
|
*
|
|
* 사용법:
|
|
* 1. 웹 브라우저: http://yourdomain.com/adm/order_manage/cron_interim_reminders.php?key=your_secret_key
|
|
* 2. 커맨드라인: php /path/to/adm/order_manage/cron_interim_reminders.php
|
|
* 3. 크론탭: 0 9 * * * php /path/to/adm/order_manage/cron_interim_reminders.php
|
|
*/
|
|
|
|
// 보안 키 확인 (웹 접근 시)
|
|
$security_key = 'order_cron_2024';
|
|
if (isset($_GET['key']) && $_GET['key'] !== $security_key) {
|
|
die('Invalid security key');
|
|
}
|
|
|
|
// 그누보드 환경 로드
|
|
if (!defined('_GNUBOARD_')) {
|
|
$g5_path = dirname(dirname(dirname(__FILE__)));
|
|
include_once($g5_path . '/common.php');
|
|
}
|
|
|
|
// 로그 함수
|
|
function log_message($message)
|
|
{
|
|
$log_file = G5_DATA_PATH . '/log/interim_reminders.log';
|
|
$log_dir = dirname($log_file);
|
|
|
|
if (!is_dir($log_dir)) {
|
|
@mkdir($log_dir, 0755, true);
|
|
}
|
|
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$log_entry = "[{$timestamp}] {$message}" . PHP_EOL;
|
|
|
|
file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX);
|
|
|
|
// 콘솔 출력 (CLI 모드)
|
|
if (php_sapi_name() === 'cli') {
|
|
echo $log_entry;
|
|
}
|
|
}
|
|
|
|
try {
|
|
log_message("중도금 알림 크론 작업 시작");
|
|
|
|
// DealerNotification 클래스 로드
|
|
require_once G5_PATH . '/adm/order_manage/dealer_notification.php';
|
|
$dealer_notification = new DealerNotification();
|
|
|
|
// 중도금 입금 알림 일괄 발송
|
|
$result = $dealer_notification->sendInterimPaymentReminders();
|
|
|
|
if ($result['success']) {
|
|
log_message("알림 발송 완료: " . $result['message']);
|
|
log_message("발송 건수: " . $result['sent_count']);
|
|
|
|
// 상세 결과 로그
|
|
foreach ($result['results'] as $item) {
|
|
$status = $item['result']['success'] ? 'SUCCESS' : 'FAILED';
|
|
$message = $item['result']['message'];
|
|
log_message("견적 {$item['wr_id']} (대리점: {$item['dealer_id']}) - {$status}: {$message}");
|
|
}
|
|
} else {
|
|
log_message("알림 발송 실패: " . $result['message']);
|
|
}
|
|
|
|
// 추가 작업: 오래된 로그 파일 정리 (30일 이상)
|
|
$log_files = glob(G5_DATA_PATH . '/log/interim_reminders_*.log');
|
|
$cutoff_date = strtotime('-30 days');
|
|
|
|
foreach ($log_files as $log_file) {
|
|
if (filemtime($log_file) < $cutoff_date) {
|
|
@unlink($log_file);
|
|
log_message("오래된 로그 파일 삭제: " . basename($log_file));
|
|
}
|
|
}
|
|
|
|
log_message("중도금 알림 크론 작업 완료");
|
|
|
|
// 웹 접근 시 JSON 응답
|
|
if (isset($_GET['key'])) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '중도금 알림 발송이 완료되었습니다.',
|
|
'sent_count' => $result['sent_count'] ?? 0,
|
|
'timestamp' => date('Y-m-d H:i:s')
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$error_message = "크론 작업 오류: " . $e->getMessage();
|
|
log_message($error_message);
|
|
|
|
// 웹 접근 시 에러 응답
|
|
if (isset($_GET['key'])) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => $error_message,
|
|
'timestamp' => date('Y-m-d H:i:s')
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
// CLI 모드에서는 exit code 1로 종료
|
|
if (php_sapi_name() === 'cli') {
|
|
exit(1);
|
|
}
|
|
}
|
|
?>
|