theloftstore/public/userarea/import_prices_pdf.php

200 lines
7.3 KiB
PHP

<?php
include('include/headscript.php');
require_once(__DIR__ . '/class/db-functions.php'); // PDO class
use Smalot\PdfParser\Parser;
$db = DBHandlerSelect::getInstance();
$pdo = $db->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 = "<div class='alert alert-success mt-3'>✅ Imported $inserted prices successfully.</div>";
} catch (Exception $e) {
$message = "<div class='alert alert-danger mt-3'>Error parsing PDF: " . htmlspecialchars($e->getMessage()) . "</div>";
}
} else {
$message = "<div class='alert alert-danger mt-3'>Upload failed.</div>";
}
} else {
$message = "<div class='alert alert-warning mt-3'>Select a client and upload a valid PDF file.</div>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import PDF Prices</title>
<?php include('cssinclude.php'); ?>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<style>
.form-section {
background: #f9f9f9;
border-radius: 8px;
padding: 20px;
}
</style>
</head>
<body>
<?php include('include/navbar.php'); ?>
<div class="dashboard-main-wrapper wrapper">
<?php include('include/topbar.php'); ?>
<div class="dashboard-body">
<div class="card shadow-sm">
<div class="card-body">
<h4 class="mb-3">📄 Import Price List from PDF</h4>
<form method="POST" enctype="multipart/form-data" class="form-section row g-3 align-items-end">
<div class="col-md-4">
<label for="idclient" class="form-label">Select Client</label>
<select name="idclient" id="idclient" class="form-select" required>
<option value="">-- Choose Client --</option>
<?php foreach ($clients as $cl): ?>
<option value="<?= htmlspecialchars($cl['idclient']) ?>">
<?= htmlspecialchars($cl['client_name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="price_pdf" class="form-label">Upload PDF File</label>
<input type="file" name="price_pdf" id="price_pdf" class="form-control" accept="application/pdf" required>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100">
<i class="fas fa-upload"></i> Import
</button>
</div>
</form>
<?= $message; ?>
<hr>
<h5 class="mt-4 mb-2">📊 Imported Prices</h5>
<div class="table-responsive">
<table id="priceTable" class="table table-striped table-bordered w-100">
<thead>
<tr>
<th>ID</th>
<th>Client</th>
<th>Code Ref</th>
<th>Price (€)</th>
<th>Source File</th>
<th>Created At</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function() {
$('#priceTable').DataTable({
ajax: {
url: 'price_mapping_list.php',
dataSrc: ''
},
columns: [{
data: 'id'
},
{
data: 'client_name'
},
{
data: 'price_ref'
},
{
data: 'price'
},
{
data: 'source_file'
},
{
data: 'created_at'
}
],
pageLength: 25,
order: [
[0, 'desc']
],
responsive: true,
autoWidth: false
});
});
</script>
</body>
</html>