Applicazione PHP per gestione ordini e fatture con generazione PDF tramite TCPDF e invio email simulato, ideale per estensioni con database e frontend.
<?php
// config.php - configurazioni generali
return [
'pdf_path' => __DIR__ . '/pdf/',
'email_log' => __DIR__ . '/logs/email.log',
'app_log' => __DIR__ . '/logs/app.log',
'email_from' => 'noreply@azienda.it',
'email_subject' => 'Fattura Ordine',
];
// Autoload Composer (assicurarsi di aver eseguito 'composer require tecnickcom/tcpdf')
require_once __DIR__ . '/vendor/autoload.php';
use TCPDF;
/**
* Classe per gestione ordini
*/
class OrderManager {
private array $orders = [];
/**
* Aggiunge un ordine dopo validazione
* @param array $order Dati ordine
* @return bool
*/
public function addOrder(array $order): bool {
if (!$this->validateOrder($order)) {
$this->log('Ordine non valido: ' . json_encode($order));
return false;
}
$this->orders[] = $order;
$this->log('Ordine aggiunto: ' . json_encode($order));
return true;
}
/**
* Valida i dati dell'ordine
* @param array $order
* @return bool
*/
private function validateOrder(array $order): bool {
// Controllo campi obbligatori
$required = ['id', 'cliente', 'email', 'items', 'totale'];
foreach ($required as $field) {
if (empty($order[$field])) {
return false;
}
}
// Validazione email
if (!filter_var($order['email'], FILTER_VALIDATE_EMAIL)) {
return false;
}
// Validazione items
if (!is_array($order['items']) || count($order['items']) === 0) {
return false;
}
foreach ($order['items'] as $item) {
if (empty($item['descrizione']) || !is_numeric($item['quantita']) || !is_numeric($item['prezzo'])) {
return false;
}
}
// Totale deve essere numerico e positivo
if (!is_numeric($order['totale']) || $order['totale'] <= 0) {
return false;
}
return true;
}
/**
* Restituisce tutti gli ordini
* @return array
*/
public function getOrders(): array {
return $this->orders;
}
/**
* Log semplice su file
* @param string $message
* @return void
*/
private function log(string $message): void {
$config = include __DIR__ . '/config.php';
$logfile = $config['app_log'];
$date = date('Y-m-d H:i:s');
file_put_contents($logfile, "[$date] $message\n", FILE_APPEND);
}
}
/**
* Classe per generazione PDF fattura
*/
class InvoicePDF {
private TCPDF $pdf;
public function __construct() {
// Inizializza TCPDF
$this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->SetCreator('Sistema Ordini');
$this->pdf->SetAuthor('Azienda');
$this->pdf->SetTitle('Fattura Ordine');
$this->pdf->SetMargins(15, 27, 15);
$this->pdf->SetHeaderMargin(5);
$this->pdf->SetFooterMargin(10);
$this->pdf->SetAutoPageBreak(TRUE, 25);
$this->pdf->setFontSubsetting(true);
$this->pdf->SetFont('helvetica', '', 12);
$this->pdf->AddPage();
}
/**
* Genera PDF da dati ordine
* @param array $order
* @return string Path file PDF generato
*/
public function generate(array $order): string {
$html = $this->buildHTML($order);
$this->pdf->writeHTML($html, true, false, true, false, '');
$config = include __DIR__ . '/config.php';
$filename = $config['pdf_path'] . 'fattura_' . $order['id'] . '.pdf';
$this->pdf->Output($filename, 'F'); // Salva su file
return $filename;
}
/**
* Costruisce HTML per PDF
* @param array $order
* @return string
*/
private function buildHTML(array $order): string {
$html = '<h1>Fattura Ordine #' . htmlspecialchars($order['id']) . '</h1>';
$html .= '<p><strong>Cliente:</strong> ' . htmlspecialchars($order['cliente']) . '</p>';
$html .= '<p><strong>Email:</strong> ' . htmlspecialchars($order['email']) . '</p>';
$html .= '<table border="1" cellpadding="5">';
$html .= '<thead><tr><th>Descrizione</th><th>Quantità</th><th>Prezzo Unitario</th><th>Totale</th></tr></thead><tbody>';
foreach ($order['items'] as $item) {
$totItem = $item['quantita'] * $item['prezzo'];
$html .= '<tr>' .
'<td>' . htmlspecialchars($item['descrizione']) . '</td>' .
'<td align="right">' . intval($item['quantita']) . '</td>' .
'<td align="right">€ ' . number_format($item['prezzo'], 2, ',', '.') . '</td>' .
'<td align="right">€ ' . number_format($totItem, 2, ',', '.') . '</td>' .
'</tr>';
}
$html .= '</tbody></table>';
$html .= '<h3>Totale: € ' . number_format($order['totale'], 2, ',', '.') . '</h3>';
return $html;
}
}
/**
* Funzione per simulare invio email
* @param string $to
* @param string $subject
* @param string $body
* @param string $attachmentPath
* @return bool
*/
function sendEmail(string $to, string $subject, string $body, string $attachmentPath): bool {
$config = include __DIR__ . '/config.php';
$logfile = $config['email_log'];
$date = date('Y-m-d H:i:s');
$logEntry = "[$date] Email simulata a: $to | Oggetto: $subject | Allegato: $attachmentPath\n";
file_put_contents($logfile, $logEntry, FILE_APPEND);
return true;
}
// Esempio di utilizzo
// Creazione ordine di esempio
$ordine = [
'id' => 'ORD12345',
'cliente' => 'Mario Rossi',
'email' => 'mario.rossi@example.com',
'items' => [
['descrizione' => 'Prodotto A', 'quantita' => 2, 'prezzo' => 15.50],
['descrizione' => 'Prodotto B', 'quantita' => 1, 'prezzo' => 45.00],
],
'totale' => 76.00,
];
$orderManager = new OrderManager();
if ($orderManager->addOrder($ordine)) {
$invoice = new InvoicePDF();
$pdfPath = $invoice->generate($ordine);
$config = include __DIR__ . '/config.php';
$emailSent = sendEmail($ordine['email'], $config['email_subject'], 'In allegato la fattura del tuo ordine.', $pdfPath);
if ($emailSent) {
echo "Ordine elaborato e email (simulata) inviata con successo.\n";
} else {
echo "Errore nell'invio email.\n";
}
} else {
echo "Ordine non valido, impossibile elaborare.\n";
}
// Wiring Arduino (simulazione):
// Pin 2: LED stato invio email (acceso se email inviata con successo)
// Pin 3: Pulsante reset ordini (premuto resetta array ordini)
// Alimentazione 5V
// Collegamento seriale USB per debug e log
?>Backend PHP 8+
Libreria TCPDF per generazione PDF (https://tcpdf.org/)
Configurazione Composer per autoload e dipendenze
Simulazione invio email tramite funzione PHP mail() con log su file
Array dati per ordini e clienti (simulazione DB)
Validazione dati input con filtri PHP
Configurazione wiring Arduino (simulazione):