dolibarr.exportzugferd/preview.php
data 6e33cc7096 Version 2.0 - ZUGFeRD PDF-Einbettung
- ZUGFeRD/Factur-X XML-Generierung (EN16931)
- XRechnung 3.0 Unterstützung
- PDF-Einbettung (echtes ZUGFeRD-PDF)
- Option XML nach Einbettung zu löschen
- ODT-Template Unterstützung
- E-Mail Anhang Funktion
- XML-Vorschau

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-12 15:36:15 +01:00

404 lines
14 KiB
PHP

<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file preview.php
* \ingroup exportzugferd
* \brief Preview ZUGFeRD XML values for an invoice
*/
// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
$i--;
$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../main.inc.php")) {
$res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
$res = @include "../../main.inc.php";
}
if (!$res) {
die("Include of main fails");
}
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
require_once __DIR__ . '/class/zugferdgenerator.class.php';
// Security check
$id = GETPOSTINT('id');
if (!$user->hasRight('exportzugferd', 'export') && !$user->hasRight('facture', 'lire')) {
accessforbidden();
}
$langs->loadLangs(array('exportzugferd@exportzugferd', 'bills'));
// Load invoice
$invoice = new Facture($db);
$result = $invoice->fetch($id);
if ($result <= 0) {
setEventMessages($langs->trans('ErrorRecordNotFound'), null, 'errors');
header('Location: ' . DOL_URL_ROOT . '/compta/facture/list.php');
exit;
}
// Security check on third party
$socid = $invoice->socid;
if ($user->socid > 0 && $socid != $user->socid) {
accessforbidden();
}
// Fetch related data
$invoice->fetch_thirdparty();
if (empty($invoice->lines)) {
$invoice->fetch_lines();
}
/*
* View
*/
$title = $langs->trans('PreviewZugferdXML') . ' - ' . $invoice->ref;
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-exportzugferd page-preview');
$head = array();
$head[0][0] = DOL_URL_ROOT . '/compta/facture/card.php?id=' . $invoice->id;
$head[0][1] = $langs->trans('Invoice');
$head[0][2] = 'card';
$head[1][0] = dol_buildpath('/exportzugferd/preview.php', 1) . '?id=' . $invoice->id;
$head[1][1] = $langs->trans('ZugferdPreview');
$head[1][2] = 'zugferd';
print dol_get_fiche_head($head, 'zugferd', $langs->trans('Invoice'), -1, 'bill');
// Invoice info
print '<div class="fichecenter">';
print '<div class="underbanner clearboth"></div>';
print '<table class="border centpercent tableforfield">';
print '<tr><td class="titlefield">' . $langs->trans('Ref') . '</td>';
print '<td>' . $invoice->getNomUrl(1) . '</td></tr>';
print '<tr><td>' . $langs->trans('Customer') . '</td>';
print '<td>' . $invoice->thirdparty->getNomUrl(1) . '</td></tr>';
print '<tr><td>' . $langs->trans('DateInvoice') . '</td>';
print '<td>' . dol_print_date($invoice->date, 'day') . '</td></tr>';
print '<tr><td>' . $langs->trans('AmountTTC') . '</td>';
print '<td>' . price($invoice->total_ttc) . ' ' . $conf->currency . '</td></tr>';
print '</table>';
print '</div>';
print dol_get_fiche_end();
// ZUGFeRD XML Preview
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td colspan="2">' . $langs->trans('ZugferdXMLValues') . '</td>';
print '</tr>';
// Document Info
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('DocumentInfo') . '</strong></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="titlefield">' . $langs->trans('InvoiceNumber') . '</td>';
print '<td><code>' . htmlspecialchars($invoice->ref) . '</code></td>';
print '</tr>';
$typeCode = '380'; // Standard invoice
if ($invoice->type == Facture::TYPE_CREDIT_NOTE) {
$typeCode = '381';
} elseif ($invoice->type == Facture::TYPE_DEPOSIT) {
$typeCode = '386';
}
print '<tr class="oddeven">';
print '<td>' . $langs->trans('TypeCode') . '</td>';
print '<td><code>' . $typeCode . '</code> (' . ($typeCode == '380' ? 'Commercial Invoice' : ($typeCode == '381' ? 'Credit Note' : 'Deposit')) . ')</td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('IssueDate') . '</td>';
print '<td><code>' . dol_print_date($invoice->date, '%Y%m%d') . '</code></td>';
print '</tr>';
if (!empty($invoice->date_lim_reglement)) {
print '<tr class="oddeven">';
print '<td>' . $langs->trans('DueDate') . '</td>';
print '<td><code>' . dol_print_date($invoice->date_lim_reglement, '%Y%m%d') . '</code></td>';
print '</tr>';
}
// Seller Info
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('Seller') . ' (SellerTradeParty)</strong></td>';
print '</tr>';
global $mysoc;
print '<tr class="oddeven">';
print '<td class="titlefield">' . $langs->trans('Name') . '</td>';
print '<td><code>' . htmlspecialchars($mysoc->name) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Address') . '</td>';
print '<td><code>' . htmlspecialchars($mysoc->address) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Zip') . ' / ' . $langs->trans('Town') . '</td>';
print '<td><code>' . htmlspecialchars($mysoc->zip) . '</code> / <code>' . htmlspecialchars($mysoc->town) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Country') . '</td>';
print '<td><code>' . htmlspecialchars($mysoc->country_code) . '</code></td>';
print '</tr>';
if (!empty($mysoc->tva_intra)) {
print '<tr class="oddeven">';
print '<td>' . $langs->trans('VATIntra') . '</td>';
print '<td><code>' . htmlspecialchars($mysoc->tva_intra) . '</code></td>';
print '</tr>';
}
// Buyer Info
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('Customer') . ' (BuyerTradeParty)</strong></td>';
print '</tr>';
$buyer = $invoice->thirdparty;
print '<tr class="oddeven">';
print '<td class="titlefield">' . $langs->trans('Name') . '</td>';
print '<td><code>' . htmlspecialchars($buyer->name) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Address') . '</td>';
print '<td><code>' . htmlspecialchars($buyer->address) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Zip') . ' / ' . $langs->trans('Town') . '</td>';
print '<td><code>' . htmlspecialchars($buyer->zip) . '</code> / <code>' . htmlspecialchars($buyer->town) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>' . $langs->trans('Country') . '</td>';
print '<td><code>' . htmlspecialchars($buyer->country_code) . '</code></td>';
print '</tr>';
if (!empty($buyer->tva_intra)) {
print '<tr class="oddeven">';
print '<td>' . $langs->trans('VATIntra') . '</td>';
print '<td><code>' . htmlspecialchars($buyer->tva_intra) . '</code></td>';
print '</tr>';
}
// Leitweg-ID (for XRechnung)
if (!empty($buyer->array_options['options_leitweg_id'])) {
print '<tr class="oddeven">';
print '<td>' . $langs->trans('LeitwegID') . ' (BuyerReference)</td>';
print '<td><code>' . htmlspecialchars($buyer->array_options['options_leitweg_id']) . '</code></td>';
print '</tr>';
}
// Payment Info
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('Payment') . '</strong></td>';
print '</tr>';
// Bank Account Info
if (!empty($invoice->fk_account)) {
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
$bankAccount = new Account($db);
if ($bankAccount->fetch($invoice->fk_account) > 0) {
if (!empty($bankAccount->iban)) {
print '<tr class="oddeven">';
print '<td class="titlefield">IBAN</td>';
print '<td><code>' . htmlspecialchars($bankAccount->iban) . '</code></td>';
print '</tr>';
}
if (!empty($bankAccount->bic)) {
print '<tr class="oddeven">';
print '<td>BIC</td>';
print '<td><code>' . htmlspecialchars($bankAccount->bic) . '</code></td>';
print '</tr>';
}
}
}
// Line Items
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('Lines') . ' (IncludedSupplyChainTradeLineItem)</strong></td>';
print '</tr>';
$lineNo = 0;
foreach ($invoice->lines as $line) {
// Skip title/subtotal lines
if (!empty($line->special_code) && in_array($line->special_code, array(104777, 104778, 104779))) {
continue;
}
if ($line->qty == 0 && $line->total_ht == 0 && empty($line->fk_product)) {
continue;
}
$lineNo++;
print '<tr class="oddeven">';
print '<td colspan="2" style="background-color: #f5f5f5;"><strong>' . $langs->trans('Line') . ' ' . $lineNo . '</strong></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="titlefield paddingleft">&nbsp;&nbsp;LineID</td>';
print '<td><code>' . $lineNo . '</code></td>';
print '</tr>';
if (!empty($line->fk_product)) {
$product = new Product($db);
$product->fetch($line->fk_product);
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;SellerAssignedID</td>';
print '<td><code>' . htmlspecialchars($product->ref) . '</code></td>';
print '</tr>';
}
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;Name</td>';
print '<td><code>' . htmlspecialchars($line->product_label ?: $line->desc) . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;BilledQuantity</td>';
print '<td><code>' . number_format($line->qty, 4, '.', '') . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;NetPriceAmount (ChargeAmount)</td>';
print '<td><code>' . number_format($line->subprice, 2, '.', '') . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;LineTotalAmount</td>';
print '<td><code>' . number_format($line->total_ht, 2, '.', '') . '</code></td>';
print '</tr>';
$vatRate = $line->tva_tx;
$categoryCode = ($vatRate > 0) ? 'S' : 'Z';
print '<tr class="oddeven">';
print '<td class="paddingleft">&nbsp;&nbsp;VAT (CategoryCode / Rate)</td>';
print '<td><code>' . $categoryCode . '</code> / <code>' . number_format($vatRate, 2, '.', '') . '%</code></td>';
print '</tr>';
}
// Totals
print '<tr class="liste_titre">';
print '<td colspan="2"><strong>' . $langs->trans('Totals') . ' (SpecifiedTradeSettlementHeaderMonetarySummation)</strong></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td class="titlefield">LineTotalAmount</td>';
print '<td><code>' . number_format($invoice->total_ht, 2, '.', '') . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>TaxBasisTotalAmount</td>';
print '<td><code>' . number_format($invoice->total_ht, 2, '.', '') . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>TaxTotalAmount</td>';
print '<td><code>' . number_format($invoice->total_tva, 2, '.', '') . '</code> (currencyID="' . $conf->currency . '")</td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>GrandTotalAmount</td>';
print '<td><code>' . number_format($invoice->total_ttc, 2, '.', '') . '</code></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>DuePayableAmount</td>';
print '<td><code>' . number_format($invoice->total_ttc - $invoice->getSommePaiement(), 2, '.', '') . '</code></td>';
print '</tr>';
print '</table>';
print '</div>';
// Check if XML exists and show it
$dir = $conf->facture->dir_output . '/' . dol_sanitizeFileName($invoice->ref);
$zugferdFile = $dir . '/factur-x.xml';
$xrechnungFile = $dir . '/xrechnung-' . $invoice->ref . '.xml';
$xmlFile = null;
if (file_exists($zugferdFile)) {
$xmlFile = $zugferdFile;
} elseif (file_exists($xrechnungFile)) {
$xmlFile = $xrechnungFile;
}
if ($xmlFile) {
print '<br>';
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>' . $langs->trans('GeneratedXML') . ' (' . basename($xmlFile) . ')</td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td>';
print '<pre style="white-space: pre-wrap; word-wrap: break-word; font-size: 11px; max-height: 500px; overflow-y: auto;">';
print htmlspecialchars(file_get_contents($xmlFile));
print '</pre>';
print '</td>';
print '</tr>';
print '</table>';
print '</div>';
}
// Actions
print '<div class="tabsAction">';
print '<a class="butAction" href="' . dol_buildpath('/exportzugferd/download.php', 1) . '?id=' . $invoice->id . '&action=generate_xml">';
print '<span class="fa fa-sync paddingright"></span>';
print $langs->trans('RegenerateZugferdXML');
print '</a>';
print '<a class="butAction" href="' . dol_buildpath('/exportzugferd/download.php', 1) . '?id=' . $invoice->id . '&action=download_xml">';
print '<span class="fa fa-download paddingright"></span>';
print $langs->trans('DownloadZugferdXML');
print '</a>';
print '<a class="butAction" href="' . DOL_URL_ROOT . '/compta/facture/card.php?id=' . $invoice->id . '">';
print '<span class="fa fa-arrow-left paddingright"></span>';
print $langs->trans('BackToInvoice');
print '</a>';
print '</div>';
llxFooter();
$db->close();