|
| 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 | +} |
0 commit comments