getConnection(); // --- Carica clienti --- $clients = $pdo->query("SELECT idclient, client_name FROM clients ORDER BY client_name")->fetchAll(PDO::FETCH_ASSOC); // --- Messaggio di stato --- $message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['price_pdf'])) { $idclient = intval($_POST['idclient'] ?? 0); if ($idclient > 0 && $_FILES['price_pdf']['error'] === UPLOAD_ERR_OK) { $uploadDir = __DIR__ . '/uploads/'; if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true); $filename = basename($_FILES['price_pdf']['name']); $targetPath = $uploadDir . $filename; if (move_uploaded_file($_FILES['price_pdf']['tmp_name'], $targetPath)) { try { require_once(dirname(__DIR__, 2) . '/vendor/autoload.php'); $parser = new Parser(); $pdf = $parser->parseFile($targetPath); $text = $pdf->getText(); // Salva testo completo per analisi file_put_contents(__DIR__ . '/debug_pdf_text.txt', $text); // Pattern iniziale: "499 1C 23 €123,45" o "499 1C 23 123,45" $pattern = '/(\d{3})\s([A-Z0-9]{1,2})\s(\d{2})\s*[€]?\s?(\d{1,4},\d{2})/'; preg_match_all($pattern, $text, $matches, PREG_SET_ORDER); // Salva tutti i match trovati $debugMatches = "Matches found: " . count($matches) . "\n\n"; foreach ($matches as $m) { $debugMatches .= implode(' | ', $m) . "\n"; } file_put_contents(__DIR__ . '/debug_matches.txt', $debugMatches); $inserted = 0; $stmt = $pdo->prepare(" INSERT INTO price_mapping (idclient, price_ref, price, source_file) VALUES (:idclient, :price_ref, :price, :source_file) "); foreach ($matches as $m) { $ref = trim($m[1] . $m[2] . $m[3]); $price = str_replace(',', '.', $m[4]); $stmt->execute([ ':idclient' => $idclient, ':price_ref' => $ref, ':price' => $price, ':source_file' => $filename ]); $inserted++; } $message = "
✅ Imported $inserted prices successfully.
"; } catch (Exception $e) { $message = "
Error parsing PDF: " . htmlspecialchars($e->getMessage()) . "
"; } } else { $message = "
Upload failed.
"; } } else { $message = "
Select a client and upload a valid PDF file.
"; } } ?> Import PDF Prices

📄 Import Price List from PDF


📊 Imported Prices
ID Client Code Ref Price (€) Source File Created At