PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); } catch (Exception $e) { echo json_encode(["status" => "fail", "message" => "DB μ—°κ²° μ‹€νŒ¨"]); exit; } /* ----------------------------------------------------- πŸ”΅ 곡톡 λ‚ μ§œ λ³€ν™˜ ν•¨μˆ˜ (MM/DD/YYYY β†’ YYYY-MM-DD) ----------------------------------------------------- */ function normalize_date($dateStr) { if (!$dateStr) return null; // 이미 YYYY-MM-DD라면 κ·ΈλŒ€λ‘œ λ°˜ν™˜ if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateStr)) { return $dateStr; } // MM/DD/YYYY β†’ YYYY-MM-DD (ν•œμžλ¦¬/λ‘μžλ¦¬ λͺ¨λ‘ ν—ˆμš©) if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/', $dateStr, $m)) { $month = str_pad($m[1], 2, '0', STR_PAD_LEFT); $day = str_pad($m[2], 2, '0', STR_PAD_LEFT); return "{$m[3]}-$month-$day"; } // ν˜•μ‹ μ΄μƒν•˜λ©΄ null 리턴 return null; } /* ----------------------------------------------------- πŸ”΅ μš”μ²­ μ•‘μ…˜ ----------------------------------------------------- */ $action = $_POST['action'] ?? $_GET['action'] ?? ""; /* ===================================================== 1) LIST ===================================================== */ if ($action === "list") { $stmt = $pdo->query(" SELECT r.*, m.emp_name FROM sales_results r LEFT JOIN sales_members m ON r.emp_no = m.emp_no ORDER BY r.seq_no DESC "); echo json_encode([ "status" => "ok", "records" => $stmt->fetchAll() ]); exit; } /* ===================================================== 2) INSERT (seq_no μžλ™ 증가) ===================================================== */ if ($action === "insert") { $sales_date = normalize_date($_POST['sales_date']); if (!$sales_date) { echo json_encode([ "status" => "error", "message" => "싀적일(sales_date) ν˜•μ‹ 였λ₯˜. YYYY-MM-DD λ˜λŠ” MM/DD/YYYY 둜 μž…λ ₯ν•˜μ„Έμš”." ]); exit; } $next_seq = $pdo->query("SELECT IFNULL(MAX(seq_no), 0) + 1 FROM sales_results")->fetchColumn(); // πŸ”₯ μ„œλ²„μ—μ„œ μ΄κΈˆμ•‘ 계산 $qty = (int)($_POST['quantity'] ?? 0); $unit = (int)($_POST['unit_price'] ?? 0); $discount = (int)($_POST['discount'] ?? 0); $total_amount = ($qty * $unit) - $discount; if ($total_amount < 0) $total_amount = 0; $stmt = $pdo->prepare(" INSERT INTO sales_results (seq_no, sales_date, emp_no, client_code, product_code, quantity, unit_price, discount, total_amount, remarks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $next_seq, $sales_date, $_POST['emp_no'], $_POST['client_code'], $_POST['product_code'], $qty, $unit, $discount, $total_amount, // πŸ”₯ ν΄λΌμ΄μ–ΈνŠΈ κ°’ λ¬΄μ‹œ, μ„œλ²„ 계산값 λ„£κΈ° $_POST['remarks'] ]); echo json_encode(["status" => "ok"]); exit; } /* ===================================================== 3) UPDATE ===================================================== */ if ($action === "update") { $seq_no = $_POST['seq_no'] ?? ''; if (!$seq_no) { echo json_encode(["status" => "error", "message" => "seq_no λˆ„λ½"]); exit; } unset($_POST['action'], $_POST['seq_no']); /* ----------------------------- πŸ”΅ λ‚ μ§œ λ³€ν™˜ (MM/DD/YYYY β†’ YYYY-MM-DD) ----------------------------- */ if (!empty($_POST['sales_date'])) { $date = normalize_date($_POST['sales_date']); if (!$date) { echo json_encode(["status" => "error", "message" => "싀적일(sales_date) ν˜•μ‹ 였λ₯˜"]); exit; } $_POST['sales_date'] = $date; } /* -------------------------------------------------- πŸ”₯ quantity / unit_price / discount λ³€κ²½ μ—¬λΆ€ 확인 ----------------------------------------------------- */ $qtyChanged = array_key_exists('quantity', $_POST); $unitChanged = array_key_exists('unit_price', $_POST); $discountChanged = array_key_exists('discount', $_POST); if ($qtyChanged || $unitChanged || $discountChanged) { // κΈ°μ‘΄ κ°’ κ°€μ Έμ˜€κΈ° $old = $pdo->prepare(" SELECT quantity, unit_price, discount FROM sales_results WHERE seq_no = ? "); $old->execute([$seq_no]); $oldData = $old->fetch(); // μƒˆ 값이 있으면 μƒˆ κ°’ μ‚¬μš©, μ—†μœΌλ©΄ κΈ°μ‘΄ κ°’ μ‚¬μš© $qty = isset($_POST['quantity']) ? (int)$_POST['quantity'] : (int)$oldData['quantity']; $unit = isset($_POST['unit_price']) ? (int)$_POST['unit_price'] : (int)$oldData['unit_price']; $discount = isset($_POST['discount']) ? (int)$_POST['discount'] : (int)$oldData['discount']; // μ„œλ²„μ—μ„œ μ΄κΈˆμ•‘ μž¬κ³„μ‚° $total_amount = ($qty * $unit) - $discount; if ($total_amount < 0) $total_amount = 0; $_POST['total_amount'] = $total_amount; // πŸ”₯ κ°•μ œ 반영 } /* ----------------------------- πŸ”΅ Partial Update (λΉˆκ°’μ€ λ¬΄μ‹œ) ----------------------------- */ $fields = []; $params = []; foreach ($_POST as $key => $val) { // NULL, 빈문자, undefinedλŠ” UPDATE μ•ˆν•¨ if ($val === '' || $val === null || $val === 'undefined') { continue; } $fields[] = "$key = ?"; $params[] = $val; } if (!empty($fields)) { $sql = "UPDATE sales_results SET " . implode(", ", $fields) . ", updated_at = NOW() WHERE seq_no = ?"; $params[] = $seq_no; $stmt = $pdo->prepare($sql); $stmt->execute($params); } echo json_encode(["status" => "ok"]); exit; } /* ===================================================== 4) DELETE ===================================================== */ if ($action === "delete") { $stmt = $pdo->prepare("DELETE FROM sales_results WHERE seq_no = ?"); $stmt->execute([$_POST['seq_no']]); echo json_encode(["status" => "ok"]); exit; } /* ===================================================== μš”μ²­ μ—†μŒ ===================================================== */ echo json_encode(["status" => "fail", "message" => "잘λͺ»λœ μš”μ²­"]); exit; ?>