feat: Implement invoice generation and emailing functionality
- Added invoice generation logic using DomPDF. - Integrated invoice data retrieval from the API. - Implemented language determination for invoices based on customer data. - Added options to email invoices to customers and admin. - Included HTML output option for direct viewing in the browser. - Ensured proper redirection and error handling throughout the process.
This commit is contained in:
151
factuur.php
Normal file
151
factuur.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
defined(page_security_key) or exit;
|
||||
|
||||
if (debug && debug_id == $_SESSION['id']) {
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
}
|
||||
|
||||
include_once './assets/functions.php';
|
||||
include_once './settings/settings_redirector.php';
|
||||
|
||||
// Check if allowed
|
||||
if (isAllowed('order', $_SESSION['profile'], $_SESSION['permission'], 'R') === 0) {
|
||||
header('location: index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get transaction ID from POST or GET (for HTML output)
|
||||
$order_number = $_POST['txn_id'] ?? $_GET['txn_id'] ?? '';
|
||||
|
||||
if (empty($order_number)) {
|
||||
header('Location: index.php?page=orders');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Connect to database
|
||||
$pdo = dbConnect($dbname);
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Get detailed invoice data
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
$api_url = '/v2/invoice/list=invoice&txn_id=' . $order_number;
|
||||
$invoice_data = ioServer($api_url, '');
|
||||
$invoice_data = json_decode($invoice_data, true);
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Determine invoice language
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$available_languages = [
|
||||
'NL' => 'NL',
|
||||
'BE' => 'NL',
|
||||
'US' => 'US',
|
||||
'GB' => 'US',
|
||||
'DE' => 'DE',
|
||||
'FR' => 'FR',
|
||||
'ES' => 'ES'
|
||||
];
|
||||
|
||||
$invoice_language = 'US'; // Default
|
||||
if (!empty($invoice_data['customer']['language'])) {
|
||||
$invoice_language = strtoupper($invoice_data['customer']['language']);
|
||||
} elseif (!empty($invoice_data['customer']['country']) && isset($available_languages[strtoupper($invoice_data['customer']['country'])])) {
|
||||
$invoice_language = $available_languages[strtoupper($invoice_data['customer']['country'])];
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Generate invoice HTML
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
var_dump($invoice_data);
|
||||
list($data, $customer_email, $order_id) = generateSoftwareInvoice($invoice_data, $order_number, $invoice_language);
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Check for HTML output request
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if (isset($_GET['output']) && $_GET['output'] === 'html') {
|
||||
// Output HTML directly to browser
|
||||
echo $data;
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Initialize DomPDF
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
use Dompdf\Dompdf;
|
||||
use Dompdf\Options;
|
||||
|
||||
$options = new Options();
|
||||
$options->set('isRemoteEnabled', true);
|
||||
$dompdf = new Dompdf($options);
|
||||
|
||||
// Load HTML content
|
||||
$dompdf->loadHtml($data);
|
||||
|
||||
// Setup paper size and orientation
|
||||
$dompdf->setPaper('A4', 'portrait');
|
||||
|
||||
// Render the HTML as PDF
|
||||
$dompdf->render();
|
||||
|
||||
$file_name = 'Factuur - ' . $order_id;
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Handle different actions
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
// Email invoice to customer
|
||||
if (isset($_POST['email_invoice'])) {
|
||||
$to = $customer_email;
|
||||
$subject = 'Factuur - ' . $order_id;
|
||||
$message = $data;
|
||||
$attachment = $dompdf->output();
|
||||
$attachment_name = $file_name . '.pdf';
|
||||
|
||||
$header_redirect = 'Location: index.php?page=order&id=' . $order_id . '&success=invoice_sent';
|
||||
|
||||
// Send to PHPMailer
|
||||
send_mail($to, $subject, $message, $attachment, $attachment_name);
|
||||
|
||||
header($header_redirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Email invoice to admin/bookkeeping
|
||||
if (isset($_POST['email_invoice_to_admin'])) {
|
||||
$to = $customer_email;
|
||||
$subject = 'Factuur - ' . $order_id;
|
||||
$message = $data;
|
||||
$attachment = $dompdf->output();
|
||||
$attachment_name = $file_name . '.pdf';
|
||||
|
||||
$header_redirect = 'Location: index.php?page=order&id=' . $order_id . '&success=invoice_sent_admin';
|
||||
|
||||
// Send to bookkeeping if configured
|
||||
if (defined('invoice_bookkeeping') && invoice_bookkeeping && defined('email_bookkeeping') && email_bookkeeping) {
|
||||
send_mail(email_bookkeeping, $subject, $message, $attachment, $attachment_name);
|
||||
}
|
||||
|
||||
header($header_redirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Show invoice in browser
|
||||
if (isset($_POST['show_invoice'])) {
|
||||
// Clean output buffer to prevent corrupted PDF
|
||||
if (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
// Stream PDF to browser
|
||||
$dompdf->stream($file_name . ".pdf", array("Attachment" => false));
|
||||
exit;
|
||||
}
|
||||
|
||||
// If no action specified, redirect back
|
||||
header('Location: index.php?page=order&id=' . $order_number);
|
||||
exit;
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user