From ea7155b7a54cd31eafb47ad2e866c29c23b9e0b5 Mon Sep 17 00:00:00 2001 From: ormelflores Date: Wed, 24 Apr 2024 15:27:31 +0800 Subject: [PATCH 1/2] [#11] Integrate direct printing --- .env.example | 2 + README.md | 1 + .../Controllers/TransactionController.php | 3 + app/Jobs/PrintReceiptJob.php | 40 +++ composer.json | 1 + composer.lock | 257 +++++++++++++++++- config/printing.php | 84 ++++++ 7 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 app/Jobs/PrintReceiptJob.php create mode 100644 config/printing.php diff --git a/.env.example b/.env.example index f896499..be5ae6e 100644 --- a/.env.example +++ b/.env.example @@ -56,3 +56,5 @@ VITE_PUSHER_HOST="${PUSHER_HOST}" VITE_PUSHER_PORT="${PUSHER_PORT}" VITE_PUSHER_SCHEME="${PUSHER_SCHEME}" VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" + +PRINT_NODE_API_KEY= \ No newline at end of file diff --git a/README.md b/README.md index f915360..6514320 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This project will be used for taking customer's order. It was built with Laravel - Automated generation of receipt - Generating reports - Activity logs per user + - Direct printing of receipt - Configurations: - Category - Products diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index a8502fa..afa0ed6 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -7,6 +7,7 @@ use App\Actions\Transactions\GenerateReceipt; use App\Http\Requests\Store\TransactionRequest; use App\Http\Requests\Update\TransactionRequest as UpdateTransactionRequest; +use App\Jobs\PrintReceiptJob; use App\Models\Transaction; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; @@ -39,6 +40,8 @@ public function store(TransactionRequest $request): RedirectResponse GenerateReceipt::run($transaction); + PrintReceiptJob::dispatch($transaction); + return back(); } diff --git a/app/Jobs/PrintReceiptJob.php b/app/Jobs/PrintReceiptJob.php new file mode 100644 index 0000000..66c6359 --- /dev/null +++ b/app/Jobs/PrintReceiptJob.php @@ -0,0 +1,40 @@ +printer($printers[0]->id()) + ->url($this->transaction->receipt->file) + ->send(); + } + } +} diff --git a/composer.json b/composer.json index 247a6ab..7126590 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "laravel/framework": "^11.1", "laravel/sanctum": "^4.0", "laravel/tinker": "^2.7", + "rawilk/laravel-printing": "^3.0", "spatie/laravel-permission": "^6.4", "tightenco/ziggy": "^1.0" }, diff --git a/composer.lock b/composer.lock index 2541a52..ecb9e6e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "382152039141e528ef2c4e481e539415", + "content-hash": "ffac2e1950dc76c7b1e385bcc404aa5d", "packages": [ { "name": "barryvdh/laravel-dompdf", @@ -2500,6 +2500,112 @@ }, "time": "2024-02-16T16:26:57+00:00" }, + { + "name": "mike42/escpos-php", + "version": "v4.0", + "source": { + "type": "git", + "url": "https://github.com/mike42/escpos-php.git", + "reference": "74fd89a3384135c90a8c6dc4b724e03df7c0e4f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mike42/escpos-php/zipball/74fd89a3384135c90a8c6dc4b724e03df7c0e4f9", + "reference": "74fd89a3384135c90a8c6dc4b724e03df7c0e4f9", + "shasum": "" + }, + "require": { + "ext-intl": "*", + "ext-json": "*", + "ext-zlib": "*", + "mike42/gfx-php": "^0.6", + "php": ">=7.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^9", + "squizlabs/php_codesniffer": "^3.3" + }, + "suggest": { + "ext-gd": "Used for image printing if present.", + "ext-imagick": "Will be used for image printing if present. Required for PDF printing or use of custom fonts." + }, + "type": "library", + "autoload": { + "psr-4": { + "Mike42\\": "src/Mike42" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Billington", + "email": "michael.billington@gmail.com" + } + ], + "description": "PHP receipt printer library for use with ESC/POS-compatible thermal and impact printers", + "homepage": "https://github.com/mike42/escpos-php", + "keywords": [ + "Epson", + "barcode", + "escpos", + "printer", + "receipt-printer" + ], + "support": { + "issues": "https://github.com/mike42/escpos-php/issues", + "source": "https://github.com/mike42/escpos-php/tree/v4.0" + }, + "time": "2022-05-23T11:05:09+00:00" + }, + { + "name": "mike42/gfx-php", + "version": "v0.6", + "source": { + "type": "git", + "url": "https://github.com/mike42/gfx-php.git", + "reference": "ed9ded2a9298e4084a9c557ab74a89b71e43dbdb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mike42/gfx-php/zipball/ed9ded2a9298e4084a9c557ab74a89b71e43dbdb", + "reference": "ed9ded2a9298e4084a9c557ab74a89b71e43dbdb", + "shasum": "" + }, + "require": { + "php": ">=7.0.0" + }, + "require-dev": { + "phpbench/phpbench": "@dev", + "phpunit/phpunit": "^6.5", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Mike42\\": "src/Mike42" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1-or-later" + ], + "authors": [ + { + "name": "Michael Billington", + "email": "michael.billington@gmail.com" + } + ], + "description": "The pure PHP graphics library", + "homepage": "https://github.com/mike42/gfx-php", + "support": { + "issues": "https://github.com/mike42/gfx-php/issues", + "source": "https://github.com/mike42/gfx-php/tree/v0.6" + }, + "time": "2019-10-05T02:44:33+00:00" + }, { "name": "mobiledetect/mobiledetectlib", "version": "2.8.45", @@ -3998,6 +4104,95 @@ ], "time": "2023-11-08T05:53:05+00:00" }, + { + "name": "rawilk/laravel-printing", + "version": "v3.0.4", + "source": { + "type": "git", + "url": "https://github.com/rawilk/laravel-printing.git", + "reference": "600c3e57d2e59989642b12ceaaf2f3687679f515" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rawilk/laravel-printing/zipball/600c3e57d2e59989642b12ceaaf2f3687679f515", + "reference": "600c3e57d2e59989642b12ceaaf2f3687679f515", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^7.5", + "illuminate/support": "^8.0|^9.0|^10.0|^11.0", + "mike42/escpos-php": "^4.0", + "php": "^8.0|^8.1|^8.2|^8.3", + "spatie/laravel-package-tools": "^1.2|^1.13" + }, + "require-dev": { + "laravel/pint": "^1.5", + "mockery/mockery": ">=1.4", + "orchestra/testbench": "^6.0|^7.0|^8.0|^9.0", + "pestphp/pest": "^1.20|^2.34", + "pestphp/pest-plugin-laravel": "^1.0|^2.2", + "php-http/message-factory": "^1.1", + "php-http/socket-client": "^2.1", + "psr/http-client": "^1.0", + "psr/http-message": "1.*", + "smalot/cups-ipp": "^0.5.0", + "spatie/laravel-ray": "^1.0|^1.29" + }, + "suggest": { + "smalot/cups-ipp": "Required when using the CUPS driver" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Rawilk\\Printing\\PrintingServiceProvider" + ], + "aliases": { + "Printing": "Rawilk\\Printing\\Facades\\Printing" + } + } + }, + "autoload": { + "psr-4": { + "Rawilk\\Printing\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Randall Wilk", + "email": "randall@randallwilk.dev", + "homepage": "https://randallwilk.dev", + "role": "Developer" + } + ], + "description": "Direct printing for Laravel apps", + "homepage": "https://github.com/rawilk/laravel-printing", + "keywords": [ + "CUPS", + "Direct printing", + "PrintNode", + "Raw printing", + "Receipt printing", + "ipp", + "laravel-printing", + "rawilk" + ], + "support": { + "issues": "https://github.com/rawilk/laravel-printing/issues", + "source": "https://github.com/rawilk/laravel-printing/tree/v3.0.4" + }, + "funding": [ + { + "url": "https://github.com/rawilk", + "type": "github" + } + ], + "time": "2024-03-10T22:10:03+00:00" + }, { "name": "sabberworm/php-css-parser", "version": "v8.5.1", @@ -4063,6 +4258,66 @@ }, "time": "2024-02-15T16:41:13+00:00" }, + { + "name": "spatie/laravel-package-tools", + "version": "1.16.4", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-package-tools.git", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^9.28|^10.0|^11.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.5", + "orchestra/testbench": "^7.7|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.5.24", + "spatie/pest-plugin-test-time": "^1.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\LaravelPackageTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "role": "Developer" + } + ], + "description": "Tools for creating Laravel packages", + "homepage": "https://github.com/spatie/laravel-package-tools", + "keywords": [ + "laravel-package-tools", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-package-tools/issues", + "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.4" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-03-20T07:29:11+00:00" + }, { "name": "spatie/laravel-permission", "version": "6.4.0", diff --git a/config/printing.php b/config/printing.php new file mode 100644 index 0000000..49a6fcb --- /dev/null +++ b/config/printing.php @@ -0,0 +1,84 @@ + env('PRINTING_DRIVER', 'printnode'), + + /* + |-------------------------------------------------------------------------- + | Drivers + |-------------------------------------------------------------------------- + | + | Configuration for each driver. + | + */ + 'drivers' => [ + 'printnode' => [ + 'key' => env('PRINT_NODE_API_KEY'), + ], + 'cups' => [ + 'ip' => env('CUPS_SERVER_IP'), + 'username' => env('CUPS_SERVER_USERNAME'), + 'password' => env('CUPS_SERVER_PASSWORD'), + 'port' => env('CUPS_SERVER_PORT', 631), + ], + + /* + * Add your custom drivers here: + * + * 'custom' => [ + * 'driver' => 'custom_driver', + * // other config for your custom driver + * ], + */ + ], + + /* + |-------------------------------------------------------------------------- + | Default Printer Id + |-------------------------------------------------------------------------- + | + | If you know the id of a default printer you want to use, enter it here. + | + */ + 'default_printer_id' => null, + + /* + |-------------------------------------------------------------------------- + | Receipt Printer Options + |-------------------------------------------------------------------------- + | + */ + 'receipts' => [ + /* + * How many characters fit across a single line on the receipt paper. + * Adjust according to your needs. + */ + 'line_character_length' => 45, + + /* + * The width of the print area in dots. + * Adjust according to your needs. + */ + 'print_width' => 550, + + /* + * The height (in dots) barcodes should be printed normally. + */ + 'barcode_height' => 64, + + /* + * The width (magnification) each barcode should be printed in normally. + */ + 'barcode_width' => 2, + ], +]; From 1d31993f91da38e5c0c2e2a12b3a65aef4bf2406 Mon Sep 17 00:00:00 2001 From: ormelflores Date: Wed, 24 Apr 2024 15:37:48 +0800 Subject: [PATCH 2/2] [#11] Fix test --- tests/Feature/TransactionTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Feature/TransactionTest.php b/tests/Feature/TransactionTest.php index 63ff4e3..bb207d8 100644 --- a/tests/Feature/TransactionTest.php +++ b/tests/Feature/TransactionTest.php @@ -5,6 +5,7 @@ use App\Models\Role; use App\Models\Transaction; use App\Models\User; +use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Storage; use function Pest\Laravel\actingAs; @@ -31,7 +32,7 @@ ]); it('can create transaction', function () { - Storage::fake(); + Queue::fake(); $user = User::factory()->create(); $product = Product::factory()->create(['quantity' => 10, 'status' => 'available']); @@ -56,6 +57,7 @@ 'total_order' => 3, ]; + Storage::fake(); actingAs($user) ->post(route('transactions.store'), $data) ->assertRedirect('/'); @@ -66,7 +68,7 @@ }); it('can update transaction status to void, and it will remove generated receipt', function () { - Storage::fake(); + Queue::fake(); Role::factory()->admin()->create(); $user = User::factory()->admin(); @@ -92,6 +94,7 @@ 'total_order' => 3, ]; + Storage::fake(); actingAs($user) ->post(route('transactions.store'), $data) ->assertRedirect('/');