🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
164 lines
4.9 KiB
PHP
164 lines
4.9 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
if ($method !== 'POST' && $method !== 'DELETE') {
|
|
http_response_code(405);
|
|
echo json_encode([
|
|
"success" => false,
|
|
"error" => "Method not allowed"
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 요청 본문에서 invoiceId 가져오기
|
|
$rawInput = file_get_contents('php://input');
|
|
$input = json_decode($rawInput, true);
|
|
$invoiceId = $input['invoiceId'] ?? $_GET['invoiceId'] ?? null;
|
|
|
|
// 디버깅: 요청 데이터 로그
|
|
error_log("Delete request - Raw input: " . $rawInput);
|
|
error_log("Delete request - Parsed input: " . print_r($input, true));
|
|
error_log("Delete request - invoiceId: " . var_export($invoiceId, true));
|
|
|
|
if (!$invoiceId) {
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
"success" => false,
|
|
"error" => "invoiceId is required",
|
|
"debug" => [
|
|
"rawInput" => $rawInput,
|
|
"parsedInput" => $input,
|
|
"getParams" => $_GET
|
|
]
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$dataFile = __DIR__ . '/invoices_data.json';
|
|
|
|
// 파일에서 데이터 읽기
|
|
$existingData = [];
|
|
if (file_exists($dataFile)) {
|
|
$fileContent = file_get_contents($dataFile);
|
|
if ($fileContent !== false) {
|
|
$existingData = json_decode($fileContent, true);
|
|
if (!is_array($existingData)) {
|
|
$existingData = [];
|
|
}
|
|
} else {
|
|
error_log("Failed to read invoices_data.json file");
|
|
}
|
|
} else {
|
|
error_log("invoices_data.json file does not exist");
|
|
}
|
|
|
|
if (!isset($existingData['invoices'])) {
|
|
$existingData['invoices'] = [];
|
|
}
|
|
|
|
// 디버깅: 현재 파일에 있는 ID 목록
|
|
$currentIds = array_map(function($inv) {
|
|
return $inv['id'] ?? 'no-id';
|
|
}, $existingData['invoices']);
|
|
error_log("Current invoice IDs in file: " . implode(', ', $currentIds));
|
|
error_log("Looking for invoiceId: " . $invoiceId);
|
|
|
|
// 삭제된 ID 목록 파일
|
|
$deletedIdsFile = __DIR__ . '/deleted_ids.json';
|
|
$deletedIds = [];
|
|
|
|
// 삭제된 ID 목록 읽기
|
|
if (file_exists($deletedIdsFile)) {
|
|
$deletedContent = file_get_contents($deletedIdsFile);
|
|
if ($deletedContent !== false) {
|
|
$deletedIds = json_decode($deletedContent, true);
|
|
if (!is_array($deletedIds)) {
|
|
$deletedIds = [];
|
|
}
|
|
}
|
|
}
|
|
|
|
// 기본 mock 데이터인 경우, 삭제된 ID 목록에 추가
|
|
$defaultMockIds = ['inv_001', 'inv_002', 'inv_003'];
|
|
if (in_array($invoiceId, $defaultMockIds)) {
|
|
// 이미 삭제된 경우
|
|
if (in_array($invoiceId, $deletedIds)) {
|
|
http_response_code(404);
|
|
echo json_encode([
|
|
"success" => false,
|
|
"error" => "Invoice not found",
|
|
"message" => "이미 삭제된 항목입니다."
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 삭제된 ID 목록에 추가
|
|
$deletedIds[] = $invoiceId;
|
|
$deletedIds = array_unique($deletedIds); // 중복 제거
|
|
file_put_contents($deletedIdsFile, json_encode($deletedIds, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "Invoice deleted successfully",
|
|
"deletedId" => $invoiceId
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 해당 ID의 인보이스 찾아서 삭제
|
|
$beforeCount = count($existingData['invoices']);
|
|
$foundInvoice = null;
|
|
foreach ($existingData['invoices'] as $index => $invoice) {
|
|
$currentId = $invoice['id'] ?? '';
|
|
// 문자열 비교 (타입 변환)
|
|
if ((string)$currentId === (string)$invoiceId) {
|
|
$foundInvoice = $invoice;
|
|
unset($existingData['invoices'][$index]);
|
|
break;
|
|
}
|
|
}
|
|
$existingData['invoices'] = array_values($existingData['invoices']); // 인덱스 재정렬
|
|
$afterCount = count($existingData['invoices']);
|
|
|
|
if ($beforeCount === $afterCount) {
|
|
// 삭제할 항목이 없었음
|
|
error_log("Invoice not found - beforeCount: $beforeCount, afterCount: $afterCount");
|
|
http_response_code(404);
|
|
echo json_encode([
|
|
"success" => false,
|
|
"error" => "Invoice not found",
|
|
"message" => "해당 세금계산서를 찾을 수 없습니다. 이미 삭제되었거나 존재하지 않는 항목일 수 있습니다.",
|
|
"debug" => [
|
|
"invoiceId" => $invoiceId,
|
|
"invoiceIdType" => gettype($invoiceId),
|
|
"totalInvoices" => $beforeCount,
|
|
"availableIds" => $currentIds
|
|
]
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 파일에 저장
|
|
$jsonData = json_encode($existingData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
$saveResult = @file_put_contents($dataFile, $jsonData);
|
|
|
|
if ($saveResult === false) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
"success" => false,
|
|
"error" => "Failed to save data"
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "Invoice deleted successfully",
|
|
"deletedId" => $invoiceId
|
|
], JSON_UNESCAPED_UNICODE);
|
|
?>
|
|
|