Skip to content

Commit

Permalink
feat(barcode): adicionado impressão de código de barra
Browse files Browse the repository at this point in the history
  • Loading branch information
mazinsw committed Aug 18, 2023
1 parent 6ac2df4 commit b185b7e
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ test:
-w /app \
grandchef/php:8.2.2-fpm-dev php ./vendor/bin/phpunit --configuration . --no-coverage --colors=always

cover:
@docker run --rm -it \
-u $(CURRENT_UID) \
-v $(shell pwd):/app \
-w /app \
grandchef/php:8.2.2-fpm-dev bash -c "XDEBUG_MODE=coverage php ./vendor/bin/phpunit --configuration . --coverage-html storage/coverage --colors=always"

analisys:
@docker run --rm -it \
-u $(CURRENT_UID) \
Expand Down
11 changes: 11 additions & 0 deletions src/Thermal/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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;

/**
* Model
*
Expand Down Expand Up @@ -83,6 +88,12 @@ public function qrcode($data, $size = null)
return $this;
}

public function barcode($data, $format = self::BARCODE_EAN13)
{
$this->model->getProfile()->barcode($data, $format);
return $this;
}

public function setAlignment($align)
{
$this->model->getProfile()->setAlignment($align);
Expand Down
14 changes: 13 additions & 1 deletion src/Thermal/Profile/Daruma.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,24 @@ public function feed($lines)
return $this;
}

public function barcode($data, $format)
{
$tipo = [
Printer::BARCODE_UPC_A => 8,
Printer::BARCODE_UPC_E => 8,
Printer::BARCODE_EAN13 => 1,
Printer::BARCODE_EAN8 => 2,
];
$new_format = $tipo[$format];
$this->getConnection()->write("\eb" . chr($new_format) . chr(2) . chr(50) . chr(0) . $data . chr(0));
}

public function qrcode($data, $size)
{
$len = strlen($data) + 2;
$pL = $len & 0xFF;
$pH = ($len >> 8) & 0xFF;

$error = 'M';
$size = min(7, max(3, $size ?: 4));
$this->getConnection()->write("\e\x81");
Expand Down
12 changes: 12 additions & 0 deletions src/Thermal/Profile/Diebold.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ public function buzzer()
return $this;
}

public function barcode($data, $format)
{
$tipo = [
Printer::BARCODE_UPC_A => '7',
Printer::BARCODE_UPC_E => '8',
Printer::BARCODE_EAN13 => '0',
Printer::BARCODE_EAN8 => '4',
];
$new_format = $tipo[$format];
$this->getConnection()->write("\e|" . $new_format . chr(50) . chr(0b00010010) . chr(0) . $data);
}

/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
Expand Down
5 changes: 5 additions & 0 deletions src/Thermal/Profile/Epson.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function drawer($number, $on_time, $off_time)
return $this;
}

public function barcode($data, $format)
{
$this->getConnection()->write("\x1dk" . chr($format) . $data . chr(0));
}

public function qrcode($data, $size)
{
$tipo = '2';
Expand Down
9 changes: 9 additions & 0 deletions src/Thermal/Profile/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ public function draw($image)
return $this;
}

/**
* Draw Bar Code 2D
*
* @param string $data data to encode
* @param int $format size of QR Code
* @return void
*/
abstract public function barcode($data, $format);

/**
* Draw QR Code
*
Expand Down
25 changes: 22 additions & 3 deletions tests/Thermal/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Thermal;

use Thermal\Connection\Buffer;

use Thermal\Graphics\Image;
use Thermal\Connection\Buffer;

class PrinterTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -25,7 +24,7 @@ class PrinterTest extends \PHPUnit\Framework\TestCase
/**
* Connection
*
* @var Connection
* @var Buffer
*/
private $connection;

Expand Down Expand Up @@ -119,6 +118,19 @@ public function testDraw()
);
}

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

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

public function testQrcode()
{
$this->connection->clear();
Expand Down Expand Up @@ -264,4 +276,11 @@ public function testColumns()
$this->assertEquals(42, $this->printer->getColumns());
$this->printer->setColumns($columns);
}

public function testClose()
{
$this->printer->close();
$this->connection->open();
$this->assertNull($this->connection->getBuffer());
}
}
11 changes: 11 additions & 0 deletions tests/Thermal/Profile/DarumaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function testDraw()
);
}

public function testBarcode()
{
$this->connection->clear();
$profile = $this->model->getProfile();
$profile->barcode('0123456789101', Printer::BARCODE_EAN13);
$this->assertEquals(
PrinterTest::getExpectedBuffer('barcode_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 @@ -37,6 +37,17 @@ public function testBuzzer()
);
}

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

public function testDrawer()
{
$this->connection->clear();
Expand Down
Binary file added tests/resources/barcode_IM453_Diebold.bin
Binary file not shown.
Binary file added tests/resources/barcode_MP-4200_TH.bin
Binary file not shown.
Binary file added tests/resources/barcode_daruma.bin
Binary file not shown.

0 comments on commit b185b7e

Please sign in to comment.