Files
sam-kd/estimate/fetch_price.php
hskwon aca1767eb9 초기 커밋: 5130 레거시 시스템
- URL 하드코딩 → .env APP_URL 기반 동적 URL로 변경
- DB 연결 하드코딩 → .env 기반으로 변경
- MySQL strict mode DATE 오류 수정
2025-12-10 20:14:31 +09:00

91 lines
2.7 KiB
PHP

<?php
// fetch_price.php
require_once($_SERVER['DOCUMENT_ROOT'] . '/session.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/lib/mydb.php');
// 에러 표시/로그 설정
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'].'/php_errors.log');
// JSON 응답 헤더
header('Content-Type: application/json; charset=utf-8');
/**
* 에러 응답을 보내고 종료합니다.
*/
function respondError(string $msg, int $code = 400): void {
http_response_code($code);
echo json_encode([
'success' => false,
'error' => $msg
], JSON_UNESCAPED_UNICODE);
exit;
}
// 파라미터 확인
$tablename = $_REQUEST['tablename'] ?? '';
$codes = $_REQUEST['codes'] ?? '';
if (!$tablename || !$codes) {
respondError('테이블명 또는 자재 코드가 누락되었습니다.', 400);
}
// JSON → 배열
$wantedNames = json_decode($codes, true);
if (!is_array($wantedNames) || empty($wantedNames)) {
respondError('자재 코드를 파싱할 수 없습니다.', 400);
}
try {
$pdo = db_connect();
// itemList 라는 JSON 컬럼을 담고 있는 테이블을 가정
$sql = "SELECT itemList
FROM chandj.{$tablename}
WHERE is_deleted IS NULL OR is_deleted = 0";
$st = $pdo->prepare($sql);
$st->execute();
$found = [];
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$items = json_decode($row['itemList'], true);
if (!is_array($items)) {
continue;
}
foreach ($items as $item) {
// item_name 필드가 우리가 찾는 목록에 있으면 그대로 수집
if (isset($item['item_name'])
&& in_array($item['item_name'], $wantedNames, true)
) {
// 사양(spec)과 길이(length)도 함께 전달
$found[] = [
'item_name' => $item['item_name'],
'spec' => $item['spec'] ?? '',
'length' => $item['length'] ?? '',
'price' => $item['price'] ?? 0,
];
}
}
}
if (empty($found)) {
respondError('해당 자재를 찾을 수 없습니다.', 404);
}
// 성공 응답
echo json_encode([
'success' => true,
'data' => $found
], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
} catch (PDOException $e) {
error_log('Database Error: ' . $e->getMessage());
respondError('데이터베이스 오류가 발생했습니다.', 500);
} catch (Exception $e) {
error_log('General Error: ' . $e->getMessage());
respondError('서버 오류가 발생했습니다.', 500);
}