Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit a9f9668

Browse files
authored
🐛 Fix parsing when coupon codes are printed on the receipt (#4)
1 parent 1960b11 commit a9f9668

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/Receipt.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ public function getPositions(): array {
193193

194194
for($lineNr = $this->getProductStartLine(); $lineNr <= $this->getProductEndLine(); $lineNr++) {
195195
if($this->isProductLine($lineNr)) {
196-
197196
if($lastPosition !== null) {
198197
$positions[] = $lastPosition;
199198
$lastPosition = null;
@@ -204,6 +203,10 @@ public function getPositions(): array {
204203
$lastPosition->setName(trim($match[1]));
205204
$lastPosition->setPriceTotal((float)str_replace(',', '.', $match[2]));
206205
$lastPosition->setTaxCode($match[3]);
206+
} elseif(preg_match('/ (\d{5,})/', $this->expl_receipt[$lineNr], $match)) {
207+
//This is a line with a coupon code or something similar.
208+
//Should be skipped.
209+
continue;
207210
} else {
208211
throw new ReceiptParseException("Error while parsing Product line");
209212
}
@@ -231,7 +234,6 @@ public function getPositions(): array {
231234
} else {
232235
throw new ReceiptParseException("Error while parsing unknown receipt line");
233236
}
234-
235237
}
236238

237239
if($lastPosition !== null) {

tests/ReceiptParsingTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public function testNegativeTotalAmount(): void {
2424
$this->assertEquals(-0.25, $receipt->getTotal());
2525
}
2626

27+
public function testReceiptWithCouponLine(): void {
28+
$receipt = Parser::parseFromText(file_get_contents(__DIR__ . '/receipts/coupon.txt'));
29+
$this->assertCount(1, $receipt->getPositions());
30+
$this->assertEquals('GUTSCHEINW50 V300', $receipt->getPositions()[0]->getName());
31+
//TODO: Name is still a bug. See issue #1
32+
}
33+
2734
/**
2835
* @return void
2936
* @throws ReceiptParseException

tests/receipts/coupon.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
REWE
2+
Musterstr. 1
3+
12345 Musterstadt
4+
UID Nr.: DE000000000
5+
EUR
6+
GUTSCHEINW50 V300 50,00 C *
7+
0000000123456789
8+
--------------------------------------
9+
SUMME EUR 50,00
10+
======================================
11+
Geg. BAR EUR 50,00
12+
Steuer % Netto Steuer Brutto
13+
Gesamtbetrag 50,00 0,00 50,00
14+
15+
Konzessionär: V300
16+
C= 0,0% 50,00 0,00 50,00
17+
Gesamtbetrag 50,00 0,00 50,00
18+
19+
TSE-Signaturzähler: 789456
20+
TSE-Transaktion: 123456
21+
TSE-Start: 2022-01-01T12:12:00.000
22+
23+
TSE-Stop: 2022-01-01T12:12:59.000
24+
Seriennnummer Kasse: REWE:00:00:00:00:00:00:00
25+
01.01.2022 12:34 Bon-Nr.:2345
26+
Markt:1234 Kasse:1 Bed.:121212
27+
****************************************
28+
29+
Ihre REWE PAYBACK Vorteile heute
30+
PAYBACK Karten-Nr.: #########1234
31+
Punktestand vor Einkauf: 5000
32+
Punktestand entspricht: 50,00 EUR
33+
34+
Sie erhalten 500000000 PAYBACK Punkte auf
35+
einen PAYBACK Umsatz von 50,00 EUR!
36+
37+
Für die mit * gekennzeichneten Produkte
38+
erhalten Sie leider keine Rabatte
39+
oder PAYBACK Punkte.
40+
41+
42+
REWE Markt GmbH
43+
44+
Sie haben Fragen?
45+
Antworten gibt es unter www.rewe.de

0 commit comments

Comments
 (0)