'error', 'message' => '필수 정보가 누락되었습니다.'])); } $estimateManager = new EstimateManager(); $result = false; $message = '처리 실패'; // 2. 대상 게시물 정보 조회 (g5_write_ 테이블) global $g5; // $g5 변수를 전역으로 선언 $write_table = $g5['write_prefix'] . $bo_table; $target_post = sql_fetch("SELECT * FROM `{$write_table}` WHERE wr_id = '{$target_wr_id}'"); if (!$target_post) { die(json_encode(['status' => 'error', 'message' => '존재하지 않는 게시물입니다.'])); } // 3. 사용자 역할 정의 $current_user_level = $member['mb_level'] ?? 0; $is_admin = ($current_user_level >= 8); // 8:직원, 9:부관리자, 10:관리자 $is_agent = false; // 💡 [추가] 대리점 기능 비활성화 try { // Determine if it's an original post or a reply $is_reply_post = (!empty($target_post['wr_reply'])); // Check wr_reply for threaded replies if ($is_reply_post) { // Action on a reply post (e.g., '견적채택') $origin_wr_id = $target_post['wr_parent']; // wr_parent is the wr_id of the original post $origin_wr_num = $target_post['wr_num']; // wr_num is the wr_num $origin_estimate = sql_fetch("SELECT * FROM `estimate` WHERE wr_id = '{$origin_wr_id}' AND is_deleted = 0"); if (!$origin_estimate) { die(json_encode(['status' => 'error', 'message' => '원본 견적서를 찾을 수 없습니다.'])); } $is_origin_owner = ($member['mb_id'] && $member['mb_id'] === $origin_estimate['created_by']); if ($new_status === '견적채택') { if (!$is_admin && !$is_origin_owner) { die(json_encode(['status' => 'error', 'message' => '견적 채택 권한이 없습니다.'])); } // 💡 [수정] 대리점 관련 로직은 실행되지 않도록 막거나, 필요 없으면 삭제 if ($is_agent) { // 대리점 기능 비활성화로 인해 이 블록은 실행되지 않음 die(json_encode(['status' => 'error', 'message' => '대리점은 견적 채택을 할 수 없습니다.'])); } // Start transaction sql_query("START TRANSACTION"); // 1. Update selected reply's wr_1 to '견적채택' $result = sql_query("UPDATE `{$write_table}` SET wr_1 = '견적채택' WHERE wr_id = '{$target_wr_id}'"); if (!$result) throw new Exception("선택된 답변 상태 업데이트 실패."); // 2. Update other replies for the same origin post to '견적취소' $result = sql_query("UPDATE `{$write_table}` SET wr_1 = '견적취소' WHERE wr_num = '{$origin_wr_num}' AND wr_id != '{$target_wr_id}'"); if (!$result) throw new Exception("다른 답변 상태 업데이트 실패."); // 3. Update original estimate status to '입금예정' $result = $estimateManager->updateEstimateStatus($origin_estimate['id'], '입금예정'); if (!$result) throw new Exception("원본 견적 상태 업데이트 실패."); // 4. Update estimate_bidding table // Find the estimate_bidding entry for the selected reply $selected_bidding = sql_fetch("SELECT id FROM `estimate_bidding` WHERE wr_id = '{$target_wr_id}' AND estimate_id = '{$origin_estimate['id']}'"); if ($selected_bidding) { $result = sql_query("UPDATE `estimate_bidding` SET status = 'selected' WHERE id = '{$selected_bidding['id']}'"); if (!$result) throw new Exception("선택된 입찰 상태 업데이트 실패."); } // Update other bids for the same estimate to 'unselected' $result = sql_query("UPDATE `estimate_bidding` SET status = 'unselected' WHERE estimate_id = '{$origin_estimate['id']}' AND wr_id != '{$target_wr_id}'"); if (!$result) throw new Exception("다른 입찰 상태 업데이트 실패."); sql_query("COMMIT"); $message = '견적이 성공적으로 채택되었습니다.'; } else { // Admin can change reply status (e.g., revert from '견적채택' for testing/correction) if ($is_admin) { $result = sql_query("UPDATE `{$write_table}` SET wr_1 = '{$new_status}' WHERE wr_id = '{$target_wr_id}'"); if (!$result) throw new Exception("답변 상태 변경 실패."); $message = '답변 상태가 성공적으로 변경되었습니다.'; } else { $message = '답변 상태 변경 권한이 없습니다.'; } } } else { // Action on an original post (e.g., admin changing status) $origin_estimate = sql_fetch("SELECT * FROM `estimate` WHERE wr_id = '{$target_wr_id}' AND is_deleted = 0"); if (!$origin_estimate) { die(json_encode(['status' => 'error', 'message' => '원본 견적서를 찾을 수 없습니다.'])); } $is_origin_owner = ($member['mb_id'] && $member['mb_id'] === $origin_estimate['created_by']); // Admin can change status, including reverting from '입금예정' if ($is_admin) { $result = $estimateManager->updateEstimateStatus($origin_estimate['id'], $new_status); $message = $result ? '원본 견적 상태가 성공적으로 변경되었습니다.' : '원본 견적 상태 변경에 실패했습니다.'; } // Customer can change status from '견적신청중' to '작성완료' (if applicable, though list.php handles this via link) elseif ($is_origin_owner && $new_status === '작성완료' && $origin_estimate['status'] === '견적신청중') { $result = $estimateManager->updateEstimateStatus($origin_estimate['id'], $new_status); $message = $result ? '원본 견적 상태가 성공적으로 변경되었습니다.' : '원본 견적 상태 변경에 실패했습니다.'; } else { $message = '원본 견적 상태 변경 권한이 없습니다.'; } } } catch (Exception $e) { sql_query("ROLLBACK"); // Rollback on error $message = '오류 발생: ' . $e->getMessage(); error_log("estimate_select.php 오류: " . $e->getMessage()); } // 4. 결과 반환 if ($result) { echo json_encode(['status' => 'success', 'message' => $message]); } else { echo json_encode(['status' => 'error', 'message' => $message]); } exit;