PDO::ERRMODE_EXCEPTION, // PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // ] // ); // } catch (Exception $e) { // echo json_encode(["status" => "fail", "message" => "DB 연결 실패"]); // exit; // } // $action = $_POST['action'] ?? $_GET['action'] ?? ""; // /* ========================= // 1) READ // ========================= */ // if ($action === "list") { // $stmt = $pdo->query("SELECT * FROM sales_clients ORDER BY created_at DESC"); // $rows = $stmt->fetchAll(); // echo json_encode([ // "status" => "ok", // "records" => $rows // ]); // exit; // } // /* ========================= // 2) INSERT — client_code 자동 생성 // ========================= */ // if ($action === "insert") { // // 가장 큰 client_code 조회 // $stmt = $pdo->query(" // SELECT client_code // FROM sales_clients // ORDER BY client_code DESC // LIMIT 1 // "); // $row = $stmt->fetch(); // $last_code = $row['client_code'] ?? 'C000'; // // 숫자 증가 // $num = (int)substr($last_code, 1); // $new_code = "C" . str_pad($num + 1, 3, "0", STR_PAD_LEFT); // // 신규 데이터 생성 // $stmt = $pdo->prepare(" // INSERT INTO sales_clients // (client_code, client_name, business_type, org_type, manager_emp_no, biz_number, address, phone, remarks, created_at) // VALUES (?, '', '', '', '', '', '', '', '', NOW()) // "); // $stmt->execute([$new_code]); // echo json_encode([ // "status" => "ok", // "client_code" => $new_code // ]); // exit; // } // /* ========================= // 3) UPDATE — 추가된 4개 필드도 자동 처리 // ========================= */ // if ($action === "update") { // $code = $_POST['client_code']; // unset($_POST['action'], $_POST['client_code']); // $fields = []; // $params = []; // foreach ($_POST as $key => $val) { // $fields[] = "$key = ?"; // $params[] = $val; // } // if (!empty($fields)) { // $sql = "UPDATE sales_clients SET " . implode(", ", $fields) . ", updated_at = NOW() WHERE client_code = ?"; // $params[] = $code; // $stmt = $pdo->prepare($sql); // $stmt->execute($params); // } // echo json_encode(["status" => "ok"]); // exit; // } // /* ========================= // 4) DELETE // ========================= */ // if ($action === "delete") { // $stmt = $pdo->prepare("DELETE FROM sales_clients WHERE client_code=?"); // $stmt->execute([$_POST['client_code']]); // echo json_encode(["status" => "ok"]); // exit; // } // echo json_encode(["status" => "fail", "message" => "알 수 없는 요청"]); ?> PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); } catch (Exception $e) { echo json_encode(["status" => "fail", "message" => "DB 연결 실패"]); exit; } $action = $_POST['action'] ?? $_GET['action'] ?? ""; // /* ==================================== // 1) OPTION LIST (기관/업종) // ==================================== */ // if ($action === "get_options") { // /* 기관구분 옵션 */ // $stmt = $pdo->query("SELECT org_type FROM sales_org_types ORDER BY id ASC"); // $orgTypes = []; // while ($row = $stmt->fetch()) { // $orgTypes[] = [ // "id" => $row['org_type'], // "text" => $row['org_type'] // ]; // } // /* 업종 옵션 */ // $stmt = $pdo->query("SELECT business_type FROM sales_business_types ORDER BY id ASC"); // $businessTypes = []; // while ($row = $stmt->fetch()) { // $businessTypes[] = [ // "id" => $row['business_type'], // "text" => $row['business_type'] // ]; // } // echo json_encode([ // "status" => "ok", // "orgTypes" => $orgTypes, // "businessTypes" => $businessTypes // ]); // exit; // } /* ==================================== 2) READ (전체 목록) ==================================== */ if ($action === "list") { $stmt = $pdo->query("SELECT * FROM sales_clients ORDER BY created_at DESC"); $rows = $stmt->fetchAll(); echo json_encode([ "status" => "ok", "records" => $rows ]); exit; } /* ==================================== 3) INSERT (client_code 자동생성) ==================================== */ if ($action === "insert") { // 가장 큰 client_code 탐색 $stmt = $pdo->query(" SELECT client_code FROM sales_clients ORDER BY client_code DESC LIMIT 1 "); $row = $stmt->fetch(); $last_code = $row['client_code'] ?? 'C000'; // 숫자 증가 $num = (int)substr($last_code, 1); $new_code = "C" . str_pad($num + 1, 3, "0", STR_PAD_LEFT); // 신규 insert $stmt = $pdo->prepare(" INSERT INTO sales_clients (client_code, client_name, business_type, org_type, manager_emp_no, biz_number, address, phone, remarks, created_at) VALUES (?, '', '', '', '', '', '', '', '', NOW()) "); $stmt->execute([$new_code]); echo json_encode([ "status" => "ok", "client_code" => $new_code ]); exit; } /* ==================================== 4) UPDATE (모든 필드 자동 처리) ==================================== */ if ($action === "update") { $client_code = $_POST['client_code']; unset($_POST['client_code'], $_POST['action']); $fields = []; $params = []; foreach ($_POST as $key => $val) { $fields[] = "$key = ?"; $params[] = $val; } // updated_at 자동 업데이트 if ($fields) { $sql = "UPDATE sales_clients SET " . implode(", ", $fields) . ", updated_at = NOW() WHERE client_code = ?"; $params[] = $client_code; $stmt = $pdo->prepare($sql); $stmt->execute($params); } echo json_encode(["status" => "ok"]); exit; } /* ==================================== 5) DELETE ==================================== */ if ($action === "delete") { $stmt = $pdo->prepare("DELETE FROM sales_clients WHERE client_code=?"); $stmt->execute([$_POST['client_code']]); echo json_encode(["status" => "ok"]); exit; } /* ==================================== DEFAULT ==================================== */ echo json_encode(["status" => "fail", "message" => "알 수 없는 요청"]); exit; ?>