Grocycode: Datamatrix generation

This commit is contained in:
Katharina Bogad 2021-06-06 23:41:45 +02:00
parent 8a36b09485
commit aa4675589c
6 changed files with 98 additions and 42 deletions

16
.vscode/settings.json vendored
View File

@ -14,20 +14,4 @@
"php-cs-fixer.formatHtml": true, "php-cs-fixer.formatHtml": true,
"php-cs-fixer.autoFixBySemicolon": true, "php-cs-fixer.autoFixBySemicolon": true,
"php-cs-fixer.onsave": true, "php-cs-fixer.onsave": true,
"phpfmt.passes": [
"PSR2KeywordsLowerCase",
"PSR2LnAfterNamespace",
"PSR2CurlyOpenNextLine",
"PSR2ModifierVisibilityStaticOrder",
"PSR2SingleEmptyLineAndStripClosingTag",
"ReindentSwitchBlocks",
"AllmanStyleBraces",
"StripExtraCommaInArray"
],
"phpfmt.exclude": [
"ReindentComments",
"StripNewlineWithinClassBody"
],
"phpfmt.psr2": false,
"phpfmt.indent_with_space": false,
} }

View File

@ -11,7 +11,8 @@
"eluceo/ical": "^0.16.0", "eluceo/ical": "^0.16.0",
"erusev/parsedown": "^1.7", "erusev/parsedown": "^1.7",
"gumlet/php-image-resize": "^1.9", "gumlet/php-image-resize": "^1.9",
"ezyang/htmlpurifier": "^4.13" "ezyang/htmlpurifier": "^4.13",
"jucksearm/php-barcode": "^1.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

56
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6155c0eb959bd8118ce2aabce7fd8a6a", "content-hash": "2f15993a36a55cfc2018969a144c73b0",
"packages": [ "packages": [
{ {
"name": "doctrine/inflector", "name": "doctrine/inflector",
@ -828,6 +828,58 @@
}, },
"time": "2019-06-20T13:13:59+00:00" "time": "2019-06-20T13:13:59+00:00"
}, },
{
"name": "jucksearm/php-barcode",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/jucksearm/php-barcode.git",
"reference": "066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jucksearm/php-barcode/zipball/066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b",
"reference": "066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^6.1"
},
"type": "library",
"autoload": {
"psr-4": {
"jucksearm\\barcode\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPLv3"
],
"authors": [
{
"name": "jucksearm",
"email": "jucksearm.bkk@gmail.com"
}
],
"description": "Barcode Generation Package inspired by Nicola Asuni.",
"keywords": [
"CODE 128",
"barcode",
"datamatrix",
"pdf417",
"php barcode",
"qr code",
"qrcode"
],
"support": {
"issues": "https://github.com/jucksearm/php-barcode/issues",
"source": "https://github.com/jucksearm/php-barcode/tree/1.0.0"
},
"time": "2017-06-05T04:41:51+00:00"
},
{ {
"name": "morris/lessql", "name": "morris/lessql",
"version": "v0.4.1", "version": "v0.4.1",
@ -2528,5 +2580,5 @@
"php": ">=7.4" "php": ">=7.4"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.0.0" "plugin-api-version": "2.1.0"
} }

View File

@ -2,7 +2,9 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use Grocy\Helpers\Grocycode;
use Grocy\Services\RecipesService; use Grocy\Services\RecipesService;
use jucksearm\barcode\lib\DatamatrixFactory;
class StockController extends BaseController class StockController extends BaseController
{ {
@ -39,7 +41,7 @@ class StockController extends BaseController
'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'),
'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'),
'users' => $usersService->GetUsersAsDto(), 'users' => $usersService->GetUsersAsDto(),
'transactionTypes' => GetClassConstants('\Grocy\Services\StockService', 'TRANSACTION_TYPE_'), 'transactionTypes' => GetClassConstants('\Grocy\Services\StockService', 'TRANSACTION_TYPE_')
]); ]);
} }
@ -428,6 +430,20 @@ class StockController extends BaseController
]); ]);
} }
public function StockEntryGrocycodeImage(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
$stockEntry = $this->getDatabase()->stock()->where('id', $args['entryId'])->fetch();
$gc = new Grocycode(Grocycode::PRODUCT, $stockEntry->product_id, [$stockEntry->stock_id]);
$png = (new DatamatrixFactory())->setCode((string) $gc)->getDatamatrixPngData();
$response = $response->withHeader('Content-Type', 'image/png')
->withHeader('Content-Length', strlen($png))
->withHeader('Cache-Control', 'no-cache')
->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$response->getBody()->write($png);
return $response;
}
public function StockSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) public function StockSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{ {
return $this->renderPage($response, 'stocksettings', [ return $this->renderPage($response, 'stocksettings', [

View File

@ -17,19 +17,20 @@ namespace Grocy\Helpers;
*/ */
class Grocycode class Grocycode
{ {
public const PRODUCT = 'p';
public const BATTERY = 'b';
public const CHORE = 'c';
public const PRODUCT = "p"; public const MAGIC = 'grcy';
public const BATTERY = "b";
public const CHORE = "c";
public const MAGIC = "grcy";
/** /**
* An array that registers all valid grocycode types. Register yours here by appending to this array. * An array that registers all valid grocycode types. Register yours here by appending to this array.
*/ */
public static $Items = [self::PRODUCT, self::BATTERY, self::CHORE]; public static $Items = [self::PRODUCT, self::BATTERY, self::CHORE];
private $type, $id, $extra_data = []; private $type;
private $id;
private $extra_data = [];
/** /**
* Validates a grocycode. * Validates a grocycode.
@ -65,7 +66,7 @@ class Grocycode
$this->setFromCode($args[0]); $this->setFromCode($args[0]);
return; return;
} }
else if ($argc == 2 || $argc == 3) elseif ($argc == 2 || $argc == 3)
{ {
if ($argc == 2) if ($argc == 2)
{ {
@ -75,7 +76,7 @@ class Grocycode
return; return;
} }
throw new \Exception("No suitable overload found."); throw new \Exception('No suitable overload found.');
} }
/** /**
@ -83,20 +84,19 @@ class Grocycode
*/ */
private function setFromCode($code) private function setFromCode($code)
{ {
$parts = array_reverse(explode(":", $barcode)); $parts = array_reverse(explode(':', $barcode));
if (array_pop($parts) != self::MAGIC) if (array_pop($parts) != self::MAGIC)
{ {
throw new \Exception("Not a grocycode"); throw new \Exception('Not a grocycode');
} }
if (!in_array($this->type = array_pop($parts), self::$Items)) if (!in_array($this->type = array_pop($parts), self::$Items))
{ {
throw new \Exception("Unknown grocycode type"); throw new \Exception('Unknown grocycode type');
} }
$this->id = array_pop($parts); $this->id = array_pop($parts);
$this->extra_data = array_reverse($parse); $this->extra_data = array_reverse($parse);
} }
/** /**
@ -106,11 +106,11 @@ class Grocycode
{ {
if (!is_array($extra_data)) if (!is_array($extra_data))
{ {
throw new \Exception("Extra data must be array of string"); throw new \Exception('Extra data must be array of string');
} }
if (!in_array($type, self::$Items)) if (!in_array($type, self::$Items))
{ {
throw new \Exception("Unknown grocycode type"); throw new \Exception('Unknown grocycode type');
} }
$this->type = $type; $this->type = $type;
@ -137,6 +137,6 @@ class Grocycode
{ {
$arr = array_merge([self::MAGIC, $this->type, $this->id], $this->extra_data); $arr = array_merge([self::MAGIC, $this->type, $this->id], $this->extra_data);
return implode(":", $arr); return implode(':', $arr);
} }
} }

View File

@ -1,12 +1,12 @@
<?php <?php
use Grocy\Middleware\AuthMiddleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Routing\RouteCollectorProxy;
use Grocy\Middleware\JsonMiddleware; use Grocy\Middleware\JsonMiddleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;
$app->group('', function (RouteCollectorProxy $group) { $app->group('', function (RouteCollectorProxy $group)
{
// System routes // System routes
$group->get('/', '\Grocy\Controllers\SystemController:Root')->setName('root'); $group->get('/', '\Grocy\Controllers\SystemController:Root')->setName('root');
$group->get('/about', '\Grocy\Controllers\SystemController:About'); $group->get('/about', '\Grocy\Controllers\SystemController:About');
@ -60,6 +60,7 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->get('/quantityunitpluraltesting', '\Grocy\Controllers\StockController:QuantityUnitPluralFormTesting'); $group->get('/quantityunitpluraltesting', '\Grocy\Controllers\StockController:QuantityUnitPluralFormTesting');
$group->get('/stockjournal/summary', '\Grocy\Controllers\StockController:JournalSummary'); $group->get('/stockjournal/summary', '\Grocy\Controllers\StockController:JournalSummary');
$group->get('/productbarcodes/{productBarcodeId}', '\Grocy\Controllers\StockController:ProductBarcodesEditForm'); $group->get('/productbarcodes/{productBarcodeId}', '\Grocy\Controllers\StockController:ProductBarcodesEditForm');
$group->get('/stockentry/{entryId}/grocycode', '\Grocy\Controllers\StockController:StockEntryGrocycodeImage');
} }
// Stock price tracking // Stock price tracking
@ -139,7 +140,8 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->get('/manageapikeys/new', '\Grocy\Controllers\OpenApiController:CreateNewApiKey'); $group->get('/manageapikeys/new', '\Grocy\Controllers\OpenApiController:CreateNewApiKey');
}); });
$app->group('/api', function (RouteCollectorProxy $group) { $app->group('/api', function (RouteCollectorProxy $group)
{
// OpenAPI // OpenAPI
$group->get('/openapi/specification', '\Grocy\Controllers\OpenApiController:DocumentationSpec'); $group->get('/openapi/specification', '\Grocy\Controllers\OpenApiController:DocumentationSpec');
@ -245,6 +247,7 @@ $app->group('/api', function (RouteCollectorProxy $group) {
})->add(JsonMiddleware::class); })->add(JsonMiddleware::class);
// Handle CORS preflight OPTIONS requests // Handle CORS preflight OPTIONS requests
$app->options('/api/{routes:.+}', function (Request $request, Response $response): Response { $app->options('/api/{routes:.+}', function (Request $request, Response $response): Response
{
return $response->withStatus(204); return $response->withStatus(204);
}); });