diff --git a/index.php b/index.php index a3c6393..48bd657 100644 --- a/index.php +++ b/index.php @@ -419,7 +419,7 @@ if (isset($_GET['action']) || isset($_POST['action'])) { if ($type === 'purchase') { $stmt = db()->prepare("SELECT pr.*, c.name as party_name FROM purchase_returns pr LEFT JOIN customers c ON pr.supplier_id = c.id WHERE pr.id = ?"); - $stmt->execute([$return_id]); + $stmt->execute([return_id]); $return = $stmt->fetch(PDO::FETCH_ASSOC); if ($return) { $stmtItems = db()->prepare("SELECT pri.*, i.name_en, i.name_ar, i.sku FROM purchase_return_items pri JOIN stock_items i ON pri.item_id = i.id WHERE pri.return_id = ?"); @@ -439,6 +439,35 @@ if (isset($_GET['action']) || isset($_POST['action'])) { echo json_encode($return); exit; } + + if ($action === 'translate') { + header('Content-Type: application/json'); + require_once __DIR__ . '/ai/LocalAIApi.php'; + $text = $_POST['text'] ?? ''; + $target = $_POST['target'] ?? 'ar'; + + if (empty($text)) { + echo json_encode(['success' => false, 'error' => 'No text provided']); + exit; + } + + $prompt = "Translate the following product name to " . ($target === 'ar' ? 'Arabic' : 'English') . ". Return ONLY the translation, no extra text or explanations.\n\nText: " . $text; + + $resp = LocalAIApi::createResponse([ + 'input' => [ + ['role' => 'system', 'content' => 'You are a translation assistant. You translate product names between English and Arabic.'], + ['role' => 'user', 'content' => $prompt], + ], + ]); + + if (!empty($resp['success'])) { + $translated = trim(LocalAIApi::extractText($resp)); + echo json_encode(['success' => true, 'translated' => $translated]); + } else { + echo json_encode(['success' => false, 'error' => $resp['error'] ?? 'Translation failed']); + } + exit; + } } // Redirect to login if not authenticated @@ -3436,11 +3465,21 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';