Skip to content

Commit 951aaba

Browse files
jakejackson1claude
andcommitted
Print an invoice as HTML formatted for a country
HtmlInvoiceWriter prints an invoice for the page: its details, parties and the address it went to, its lines with their allowances and charges, the invoice's own allowances and charges, the VAT of each category and rate, the totals, and how to pay. A Formatter writes its numbers, amounts, dates and addresses as a country does, and labels translate it. WriteInvoice() takes it beside the XML writer, printing the invoice and embedding its XML in one call. The five snapshots print an invoice, a reverse charge, a French credit note, a dollar export to New York and forty lines over three pages. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 02d1d58 commit 951aaba

17 files changed

Lines changed: 1048 additions & 21 deletions

‎src/Invoice/EN16931/Writer/HtmlInvoiceWriter.php‎

Lines changed: 517 additions & 0 deletions
Large diffs are not rendered by default.

‎src/Mpdf.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,10 @@ function SetFacturX($xml, $conformanceLevel = null)
20122012
*
20132013
* Every writer runs before the document is touched, so one that cannot express the invoice leaves it as it was.
20142014
*
2015-
* $mpdf->WriteInvoice($invoice, [new CiiInvoiceWriter(FacturX::EN16931)]);
2015+
* $mpdf->WriteInvoice($invoice, [
2016+
* new HtmlInvoiceWriter(new Formatter(new FrancePreset())),
2017+
* new CiiInvoiceWriter(FacturX::EN16931),
2018+
* ]);
20162019
*
20172020
* @param \Mpdf\Invoice\TradeDocument $document
20182021
* @param \Mpdf\Invoice\WriterInterface[] $writers

‎tests/Mpdf/Invoice/EN16931/InvoiceFixtures.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace Mpdf\Invoice\EN16931;
44

55
use Mpdf\Invoice\AllowanceCharge;
6+
use Mpdf\Invoice\EN16931\Writer\HtmlInvoiceWriter;
7+
use Mpdf\Invoice\Formatter;
68
use Mpdf\Invoice\LineItem;
79
use Mpdf\Invoice\Party;
810
use Mpdf\Invoice\PaymentMeans;
11+
use Mpdf\Invoice\Preset\UnitedKingdomPreset;
912

1013
/**
11-
* The invoices behind the fixtures in tests/data/invoice, whose XML Mustang validated for each profile
14+
* The invoices behind the fixtures in tests/data/invoice, whose XML Mustang validated for each profile, and the HTML
15+
* writer they are printed with
1216
*/
1317
trait InvoiceFixtures
1418
{
@@ -207,4 +211,16 @@ private function seller()
207211
->setElectronicAddress('billing@seller.example');
208212
}
209213

214+
/**
215+
* The HTML writer in the British convention, with any labels given
216+
*
217+
* @param string[] $labels
218+
*
219+
* @return \Mpdf\Invoice\EN16931\Writer\HtmlInvoiceWriter
220+
*/
221+
private function htmlWriter(array $labels = [])
222+
{
223+
return new HtmlInvoiceWriter(new Formatter(new UnitedKingdomPreset()), $labels);
224+
}
225+
210226
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace Mpdf\Invoice\EN16931\Writer;
4+
5+
use Mpdf\Invoice\EN16931\Invoice;
6+
use Mpdf\Invoice\EN16931\InvoiceFixtures;
7+
use Mpdf\Invoice\Formatter;
8+
use Mpdf\Invoice\LineItem;
9+
use Mpdf\Invoice\Party;
10+
use Mpdf\Invoice\Preset\GermanyPreset;
11+
use Mpdf\Invoice\TradeDocument;
12+
use Mpdf\Invoice\WriterInterface;
13+
use Mpdf\MpdfException;
14+
15+
class HtmlInvoiceWriterTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
16+
{
17+
18+
use InvoiceFixtures;
19+
20+
/**
21+
* The invoice is written with its details, parties, lines, totals and payment, its text escaped
22+
*/
23+
public function testWritesTheInvoice()
24+
{
25+
$writer = $this->htmlWriter();
26+
27+
$this->assertSame(WriterInterface::HTML, $writer->getFormat());
28+
$this->assertStringEqualsFile(__DIR__ . '/../../../../data/invoice/invoice.html', $writer->write($this->invoice()));
29+
}
30+
31+
/**
32+
* Without a prepayment the grand total is what is due, and an exemption reason follows its VAT group
33+
*/
34+
public function testWritesTheReverseCharge()
35+
{
36+
$html = $this->htmlWriter()->write($this->reverseChargeInvoice());
37+
38+
$this->assertStringContainsString('VAT 0% on 900.00 EUR (Reverse charge)', $html);
39+
$this->assertStringContainsString('<strong>900.00 EUR</strong>', $html);
40+
$this->assertStringNotContainsString('Amount due', $html);
41+
}
42+
43+
/**
44+
* Labels given replace the defaults, including the title of each type of invoice
45+
*/
46+
public function testTakesLabels()
47+
{
48+
$invoice = $this->invoice()->setTypeCode(Invoice::TYPE_CREDIT_NOTE);
49+
$html = $this->htmlWriter([Invoice::TYPE_CREDIT_NOTE => 'Avoir', 'issueDate' => 'Date', 'vatGroup' => 'TVA %1$s sur %2$s'])->write($invoice);
50+
51+
$this->assertStringContainsString('<h1>Avoir INV-2026-0001</h1>', $html);
52+
$this->assertStringContainsString('<td>Date</td>', $html);
53+
$this->assertStringContainsString('TVA 20% sur 900.00 EUR', $html);
54+
$this->assertStringContainsString('<td>Due date</td>', $html);
55+
}
56+
57+
/**
58+
* A label written around a value is a pattern, so a translation places the value and its punctuation
59+
*/
60+
public function testTakesLabelPatterns()
61+
{
62+
$html = $this->htmlWriter(['paymentReference' => 'Référence de paiement : %s', 'vatId' => 'N° TVA : %s'])->write($this->invoice());
63+
64+
$this->assertStringContainsString('Référence de paiement : INV-2026-0001', $html);
65+
$this->assertStringContainsString('N° TVA : FR32123456789', $html);
66+
}
67+
68+
/**
69+
* A label the writer has no use for is refused, lest a mistyped key leave its English label in place, but any type
70+
* of invoice may be given a title
71+
*/
72+
public function testRefusesAnUnknownLabel()
73+
{
74+
$html = $this->htmlWriter(['326' => 'Partial invoice'])->write($this->invoice()->setTypeCode('326'));
75+
$this->assertStringContainsString('<h1>Partial invoice INV-2026-0001</h1>', $html);
76+
77+
$this->expectException(MpdfException::class);
78+
$this->expectExceptionMessage('"dueDat" is not an invoice label');
79+
80+
$this->htmlWriter(['dueDat' => 'Échéance']);
81+
}
82+
83+
/**
84+
* A line not subject to VAT says so rather than giving a rate
85+
*/
86+
public function testSaysWhenALineIsNotSubjectToVat()
87+
{
88+
$invoice = $this->blankInvoice()->addLine(new LineItem('Donation', 1, 10, 0, 'O'))->setExemptionReason('O', 'Outside the scope of VAT');
89+
90+
$this->assertStringContainsString('<td class="invoice-number">Not subject to VAT</td>', $this->htmlWriter()->write($invoice));
91+
}
92+
93+
/**
94+
* The formatter given writes every quantity, amount, rate and date
95+
*/
96+
public function testWritesWithTheFormatter()
97+
{
98+
$formatter = (new Formatter(new GermanyPreset()))->withCurrencyFormat('EUR', '€%s')->withPercentFormat('%s pc');
99+
$html = (new HtmlInvoiceWriter($formatter))->write($this->invoice());
100+
101+
$this->assertStringContainsString('>7,5<', $html);
102+
$this->assertStringContainsString('>€12,99<', $html);
103+
$this->assertStringContainsString('>5,5 pc<', $html);
104+
$this->assertStringContainsString('<strong>€1.021,11</strong>', $html);
105+
$this->assertStringContainsString('<td>Due date</td><td>23.10.2026</td>', $html);
106+
}
107+
108+
/**
109+
* A line's discount shows under it, and the invoice's discount and shipping between the lines and the total before
110+
* VAT, with the card it was paid by and the seller's contact
111+
*/
112+
public function testWritesAllowancesChargesAndCard()
113+
{
114+
$html = $this->htmlWriter()->write($this->shopInvoice());
115+
116+
$this->assertStringContainsString('<br><small>Display model -50.00 EUR</small>', $html);
117+
$this->assertStringContainsString('>Total of the lines</td><td class="invoice-number">592.00 EUR<', $html);
118+
$this->assertStringContainsString('>Loyalty discount</td><td class="invoice-number">-59.20 EUR<', $html);
119+
$this->assertStringContainsString('>Shipping</td><td class="invoice-number">24.90 EUR<', $html);
120+
$this->assertStringContainsString('>Total excluding VAT</td><td class="invoice-number">557.70 EUR<', $html);
121+
$this->assertStringContainsString('Card ending 4242', $html);
122+
$this->assertStringContainsString('Contact: Accounts, +33 1 23 45 67 89, accounts@seller.example', $html);
123+
}
124+
125+
/**
126+
* A credit note names the invoice it corrects
127+
*/
128+
public function testNamesTheInvoiceCorrected()
129+
{
130+
$html = $this->htmlWriter()->write($this->creditNote());
131+
132+
$this->assertStringContainsString('<h1>Credit note CN-2026-0007</h1>', $html);
133+
$this->assertStringContainsString('<td>Corrects invoice</td><td>INV-2026-0001 (23/09/2026)</td>', $html);
134+
}
135+
136+
/**
137+
* Where the goods went takes a column of its own, a direct debit says which account it is taken from, and an
138+
* electronic address that is not an email address is left to the XML
139+
*/
140+
public function testWritesTheDeliveryAndDirectDebit()
141+
{
142+
$html = $this->htmlWriter()->write($this->intraCommunityInvoice());
143+
144+
$this->assertStringContainsString('<td width="33%"><strong>Deliver to</strong><br>Buyer GmbH Lager<br>Industriestraße 5<br>20457 Hamburg<br>DE</td>', $html);
145+
$this->assertStringContainsString('Direct debit from DE02120300000000202051 under mandate MANDATE-42, creditor ID FR98ZZZ999999', $html);
146+
$this->assertStringNotContainsString('04011000-12345-34', $html);
147+
}
148+
149+
/**
150+
* The option to pay VAT on debits is printed, as France requires it to be mentioned
151+
*/
152+
public function testMentionsVatOnDebits()
153+
{
154+
$this->assertStringContainsString('<br>VAT is paid on debits</p>', $this->htmlWriter()->write($this->frenchInvoice()));
155+
$this->assertStringNotContainsString('debits', $this->htmlWriter()->write($this->invoice()));
156+
}
157+
158+
/**
159+
* A party's address is laid out as its country lays it out: a British postcode on a line of its own
160+
*/
161+
public function testLaysOutTheAddressesByCountry()
162+
{
163+
$this->assertStringContainsString('<br>10 Downing Street<br>London<br>SW1A 2AA<br>GB</td>', $this->htmlWriter()->write($this->ukInvoice()));
164+
}
165+
166+
/**
167+
* A trade document that is not an invoice is refused
168+
*/
169+
public function testRefusesADocumentThatIsNotAnInvoice()
170+
{
171+
$document = $this->getMockBuilder(TradeDocument::class)->disableOriginalConstructor()->getMockForAbstractClass();
172+
173+
$this->expectException(MpdfException::class);
174+
$this->expectExceptionMessage('writes invoices, not');
175+
176+
$this->htmlWriter()->write($document);
177+
}
178+
179+
/**
180+
* The fixture invoice to a British buyer
181+
*
182+
* @return \Mpdf\Invoice\EN16931\Invoice
183+
*/
184+
private function ukInvoice()
185+
{
186+
$invoice = new Invoice('INV-2026-0003', new \DateTime('2026-09-23'), 'GBP', $this->seller(), (new Party('Buyer Ltd', 'GB'))->setAddress('10 Downing Street', 'SW1A 2AA', 'London'));
187+
188+
return $invoice->addLine(new LineItem('Consulting', 1, 100, 20));
189+
}
190+
191+
}

‎tests/Mpdf/Invoice/WriteInvoiceTest.php‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class WriteInvoiceTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
1515
use PageStreams;
1616

1717
/**
18-
* An HTML writer prints the invoice and the XML writer embeds it, in one call
18+
* The HTML writer prints the invoice and the XML writer embeds it, in one call
1919
*/
2020
public function testPrintsAndEmbedsTheInvoice()
2121
{
@@ -28,7 +28,7 @@ public function testPrintsAndEmbedsTheInvoice()
2828
}
2929

3030
/**
31-
* With only an HTML writer the invoice is printed and nothing is embedded, so the document need not be PDF/A
31+
* With only the HTML writer the invoice is printed and nothing is embedded, so the document need not be PDF/A
3232
*/
3333
public function testPrintsTheInvoiceAlone()
3434
{
@@ -38,7 +38,9 @@ public function testPrintsTheInvoiceAlone()
3838

3939
$this->assertNull($mpdf->facturX);
4040
$this->assertStringNotContainsString('/EmbeddedFiles', $output);
41-
$this->assertStringContainsString('Invoice INV-2026-0001', $this->drawnText($output));
41+
$text = $this->drawnText($output);
42+
$this->assertStringContainsString('Invoice INV-2026-0001', $text);
43+
$this->assertStringContainsString('1,021.11 EUR', $text);
4244
}
4345

4446
/**
@@ -102,20 +104,4 @@ public function testRefusesAnUnknownFormat()
102104
$this->mpdf()->WriteInvoice($this->invoice(), [$writer]);
103105
}
104106

105-
/**
106-
* An HTML writer that prints the invoice's number
107-
*
108-
* @return \Mpdf\Invoice\WriterInterface
109-
*/
110-
private function htmlWriter()
111-
{
112-
$writer = $this->getMockBuilder(WriterInterface::class)->getMock();
113-
$writer->method('getFormat')->willReturn(WriterInterface::HTML);
114-
$writer->method('write')->willReturnCallback(function (TradeDocument $document) {
115-
return '<h1>Invoice ' . $document->getId() . '</h1>';
116-
});
117-
118-
return $writer;
119-
}
120-
121107
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
use Mpdf\Invoice\EN16931\InvoiceFixtures;
6+
use Mpdf\Invoice\LineItem;
7+
8+
/**
9+
* An invoice of more lines than fit on a page: the column headings repeat on the next, and the totals follow the last
10+
* line
11+
*
12+
* @group snapshot
13+
*/
14+
class InvoiceManyLinesSnapshotTest extends InvoiceSnapshot
15+
{
16+
17+
use InvoiceFixtures;
18+
19+
/**
20+
* @return string A unique identifier / name for the snapshot
21+
*/
22+
public function getId()
23+
{
24+
return 'invoice-many-lines';
25+
}
26+
27+
/**
28+
* Forty lines at three VAT rates, every fifth with a description long enough to wrap
29+
*
30+
* @return \Mpdf\Invoice\EN16931\Invoice
31+
*/
32+
protected function getInvoice()
33+
{
34+
$invoice = $this->blankInvoice();
35+
$rates = [20, 10, 5.5];
36+
37+
for ($i = 1; $i <= 40; $i++) {
38+
$line = new LineItem('Item ' . $i, $i % 7 + 1, 4.75 * $i, $rates[$i % 3]);
39+
if ($i % 5 === 0) {
40+
$line->setDescription('Delivered in instalments over the quarter, as agreed in the framework contract signed at the start of the year');
41+
}
42+
$invoice->addLine($line);
43+
}
44+
45+
return $invoice;
46+
}
47+
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
use Mpdf\Invoice\EN16931\InvoiceFixtures;
6+
7+
/**
8+
* An invoice under the reverse charge: no VAT, the reason for none beside its group, and the total in bold as nothing
9+
* was prepaid
10+
*
11+
* @group snapshot
12+
*/
13+
class InvoiceReverseChargeSnapshotTest extends InvoiceSnapshot
14+
{
15+
16+
use InvoiceFixtures;
17+
18+
/**
19+
* @return string A unique identifier / name for the snapshot
20+
*/
21+
public function getId()
22+
{
23+
return 'invoice-reverse-charge';
24+
}
25+
26+
/**
27+
* @return \Mpdf\Invoice\EN16931\Invoice
28+
*/
29+
protected function getInvoice()
30+
{
31+
return $this->reverseChargeInvoice();
32+
}
33+
34+
}

0 commit comments

Comments
 (0)