Change to grocy formatting

This commit is contained in:
Marc Ole Bulling 2021-01-15 01:59:18 +01:00
parent a8fdedab64
commit 2088e4be5b
No known key found for this signature in database
GPG Key ID: C126AFC2A47B06FF
4 changed files with 37 additions and 24 deletions

24
composer.lock generated
View File

@ -2205,16 +2205,16 @@
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.22.0",
"version": "v1.20.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13"
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531",
"shasum": ""
},
"require": {
@ -2226,7 +2226,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.22-dev"
"dev-main": "1.20-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -2264,20 +2264,20 @@
"portable",
"shim"
],
"time": "2021-01-07T16:49:33+00:00"
"time": "2020-10-23T14:02:19+00:00"
},
{
"name": "symfony/polyfill-php80",
"version": "v1.22.0",
"version": "v1.20.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91"
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/dc3063ba22c2a1fd2f45ed856374d79114998f91",
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"shasum": ""
},
"require": {
@ -2286,7 +2286,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.22-dev"
"dev-main": "1.20-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -2330,7 +2330,7 @@
"portable",
"shim"
],
"time": "2021-01-07T16:49:33+00:00"
"time": "2020-10-23T14:02:19+00:00"
},
{
"name": "symfony/translation",

View File

@ -5,11 +5,13 @@ namespace Grocy\Controllers;
use Grocy\Controllers\Users\User;
use Grocy\Services\StockService;
class PrintApiController extends BaseApiController {
class PrintApiController extends BaseApiController
{
public function PrintShoppingListThermal(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) {
try {
try
{
User::checkPermission($request, User::PERMISSION_SHOPPINGLIST);
$params = $request->getQueryParams();
@ -25,7 +27,9 @@ class PrintApiController extends BaseApiController {
}
$items = $this->getStockService()->GetShoppinglistInPrintableStrings($listId);
return $this->ApiResponse($response, $this->getPrintService()->printShoppingList($printHeader, $items));
} catch (\Exception $ex) {
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}

View File

@ -7,7 +7,8 @@ use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
class PrintService extends BaseService {
class PrintService extends BaseService
{
/**
@ -15,7 +16,8 @@ class PrintService extends BaseService {
* @return Printer handle
* @throws Exception if unable to connect to printer
*/
private static function getPrinterHandle() {
private static function getPrinterHandle()
{
if (GROCY_TPRINTER_IS_NETWORK_PRINTER) {
$connector = new NetworkPrintConnector(GROCY_TPRINTER_IP, GROCY_TPRINTER_PORT);
} else {
@ -29,7 +31,8 @@ class PrintService extends BaseService {
* Prints a grocy logo
* @param $printer
*/
private static function printHeader(Printer $printer) {
private static function printHeader(Printer $printer)
{
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer->setTextSize(4, 4);
@ -48,16 +51,19 @@ class PrintService extends BaseService {
* @return string[]
* @throws Exception
*/
public function printShoppingList(bool $printHeader, array $items): array {
public function printShoppingList(bool $printHeader, array $items): array
{
$printer = self::getPrinterHandle();
if ($printer === false)
throw new Exception("Unable to connect to printer");
if ($printHeader) {
if ($printHeader)
{
self::printHeader($printer);
}
foreach ($items as $item) {
foreach ($items as $item)
{
$printer->text($item);
$printer->feed();
}

View File

@ -941,14 +941,17 @@ class StockService extends BaseService
* @return string[] Returns an array in the format "[amount] [name of product]"
* @throws \Exception
*/
public function GetShoppinglistInPrintableStrings($listId = 1): array {
if (!$this->ShoppingListExists($listId)) {
public function GetShoppinglistInPrintableStrings($listId = 1): array
{
if (!$this->ShoppingListExists($listId))
{
throw new \Exception('Shopping list does not exist');
}
$result = array();
$rowsShoppingListProducts = $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId)->fetchAll();
foreach ($rowsShoppingListProducts as $row) {
foreach ($rowsShoppingListProducts as $row)
{
$product = $this->getDatabase()->products()->where('id = :1', $row['product_id'])->fetch();
array_push($result, $row["amount"] . " " . $product["name"]);
}