Skip to content

Commit

Permalink
feat(code128): adicionado código de barras CODE 128
Browse files Browse the repository at this point in the history
  • Loading branch information
mazinsw committed Aug 18, 2023
1 parent b185b7e commit 9ad19c3
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/Thermal/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ class Printer
const DRAWER_1 = 0;
const DRAWER_2 = 1;

const BARCODE_UPC_A = 0;
const BARCODE_UPC_E = 1;
const BARCODE_EAN13 = 2;
const BARCODE_EAN8 = 3;
const BARCODE_UPC_A = 0;
const BARCODE_UPC_E = 1;
const BARCODE_EAN13 = 2;
const BARCODE_EAN8 = 3;
const BARCODE_CODE128 = 73;

/**
* Model
Expand Down
1 change: 1 addition & 0 deletions src/Thermal/Profile/Daruma.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function barcode($data, $format)
Printer::BARCODE_UPC_E => 8,
Printer::BARCODE_EAN13 => 1,
Printer::BARCODE_EAN8 => 2,
Printer::BARCODE_CODE128 => 5,
];
$new_format = $tipo[$format];
$this->getConnection()->write("\eb" . chr($new_format) . chr(2) . chr(50) . chr(0) . $data . chr(0));
Expand Down
8 changes: 7 additions & 1 deletion src/Thermal/Profile/Diebold.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ public function barcode($data, $format)
Printer::BARCODE_UPC_E => '8',
Printer::BARCODE_EAN13 => '0',
Printer::BARCODE_EAN8 => '4',
Printer::BARCODE_CODE128 => '3',
];
$new_format = $tipo[$format];
$this->getConnection()->write("\e|" . $new_format . chr(50) . chr(0b00010010) . chr(0) . $data);
if ($format === Printer::BARCODE_CODE128) {
$len = strlen($data);
$this->getConnection()->write("\e|" . $new_format . chr(50) . chr(0b00010010) . chr(0) . chr($len) . $data);
} else {
$this->getConnection()->write("\e|" . $new_format . chr(50) . chr(0b00010010) . chr(0) . $data);
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Thermal/Profile/Epson.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ public function drawer($number, $on_time, $off_time)

public function barcode($data, $format)
{
$this->getConnection()->write("\x1dk" . chr($format) . $data . chr(0));
if ($format === Printer::BARCODE_CODE128) {
$len = strlen($data);
$this->getConnection()->write("\x1dk" . chr($format) . chr($len) . $data);
} else {
$this->getConnection()->write("\x1dk" . chr($format) . $data . chr(0));
}
}

public function qrcode($data, $size)
Expand Down
13 changes: 13 additions & 0 deletions tests/Thermal/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public function testBarcode()
);
}

public function testBarcodeCode128()
{
$this->connection->clear();
$this->printer->setAlignment(Printer::ALIGN_CENTER);
$this->printer->barcode('0123456789101', Printer::BARCODE_CODE128);
$this->printer->setAlignment(Printer::ALIGN_LEFT);

$this->assertEquals(
self::getExpectedBuffer('barcode_code128_MP-4200_TH', $this->connection->getBuffer()),
$this->connection->getBuffer()
);
}

public function testQrcode()
{
$this->connection->clear();
Expand Down
11 changes: 11 additions & 0 deletions tests/Thermal/Profile/DarumaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public function testBarcode()
);
}

public function testBarcodeCode128()
{
$this->connection->clear();
$profile = $this->model->getProfile();
$profile->barcode('0123456789101', Printer::BARCODE_CODE128);
$this->assertEquals(
PrinterTest::getExpectedBuffer('barcode_code128_daruma', $this->connection->getBuffer()),
$this->connection->getBuffer()
);
}

public function testQrcode()
{
$this->connection->clear();
Expand Down
11 changes: 11 additions & 0 deletions tests/Thermal/Profile/DieboldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public function testBarcode()
);
}

public function testBarcodeCode128()
{
$this->connection->clear();
$profile = $this->model->getProfile();
$profile->barcode('0123456789101', Printer::BARCODE_CODE128);
$this->assertEquals(
PrinterTest::getExpectedBuffer('barcode_code128_IM453_Diebold', $this->connection->getBuffer()),
$this->connection->getBuffer()
);
}

public function testDrawer()
{
$this->connection->clear();
Expand Down
Binary file added tests/resources/barcode_code128_IM453_Diebold.bin
Binary file not shown.
1 change: 1 addition & 0 deletions tests/resources/barcode_code128_MP-4200_TH.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a1kI0123456789101a0
Expand Down
Binary file added tests/resources/barcode_code128_daruma.bin
Binary file not shown.

0 comments on commit 9ad19c3

Please sign in to comment.