mirror of
https://github.com/grocy/grocy.git
synced 2025-09-29 19:12:24 +00:00
Add support for printing shoppinglist with thermal printer (#1273)
* Added escpos-php library * Added button to shoppinglist print menu * Added to translation * Added basic printing logic and API call * Working implementation for printing with the API * Added openapi json * Correctly parsing boolean parameter * Working button in UI * Change to grocy formatting * Add Date * Only show thermal print button when Feature Flag is set * Fixed API call and added error message parsing * Undo translation * Add flag to print quantities as well * Added printing notes * Added quantity conversion * Increse feed * Fixed that checkbox was undefined, as dialog was already closed * Added padding * Formatting * Added note about user permission * Fixed error when using notes instead of products * Review - Default FEATURE_FLAG_THERMAL_PRINTER to disabled - Added missing localization strings (and slightly adjusted one) * Fixed merge conflicts Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
@@ -970,6 +970,88 @@ class StockService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shoppinglist as an array with lines for a printer
|
||||
* @param int $listId ID of shopping list
|
||||
* @return string[] Returns an array in the format "[amount] [name of product]"
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function GetShoppinglistInPrintableStrings($listId = 1): array
|
||||
{
|
||||
if (!$this->ShoppingListExists($listId))
|
||||
{
|
||||
throw new \Exception('Shopping list does not exist');
|
||||
}
|
||||
|
||||
$result_product = array();
|
||||
$result_quantity = array();
|
||||
$rowsShoppingListProducts = $this->getDatabase()->uihelper_shopping_list()->where('shopping_list_id = :1', $listId)->fetchAll();
|
||||
foreach ($rowsShoppingListProducts as $row)
|
||||
{
|
||||
$isValidProduct = ($row->product_id != null && $row->product_id != "");
|
||||
if ($isValidProduct)
|
||||
{
|
||||
$product = $this->getDatabase()->products()->where('id = :1', $row->product_id)->fetch();
|
||||
$conversion = $this->getDatabase()->quantity_unit_conversions_resolved()->where('product_id = :1 AND from_qu_id = :2 AND to_qu_id = :3', $product->id, $product->qu_id_stock, $row->qu_id)->fetch();
|
||||
$factor = 1.0;
|
||||
if ($conversion != null)
|
||||
{
|
||||
$factor = floatval($conversion->factor);
|
||||
}
|
||||
$amount = round($row->amount * $factor);
|
||||
$note = "";
|
||||
if (GROCY_TPRINTER_PRINT_NOTES)
|
||||
{
|
||||
if ($row->note != "") {
|
||||
$note = ' (' . $row->note . ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (GROCY_TPRINTER_PRINT_QUANTITY_NAME && $isValidProduct)
|
||||
{
|
||||
$quantityname = $row->qu_name;
|
||||
if ($amount > 1)
|
||||
{
|
||||
$quantityname = $row->qu_name_plural;
|
||||
}
|
||||
array_push($result_quantity, $amount . ' ' . $quantityname);
|
||||
array_push($result_product, $row->product_name . $note);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($isValidProduct)
|
||||
{
|
||||
array_push($result_quantity, $amount);
|
||||
array_push($result_product, $row->product_name . $note);
|
||||
}
|
||||
else
|
||||
{
|
||||
array_push($result_quantity, round($row->amount));
|
||||
array_push($result_product, $row->note);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//Add padding to look nicer
|
||||
$maxlength = 1;
|
||||
foreach ($result_quantity as $quantity)
|
||||
{
|
||||
if (strlen($quantity) > $maxlength)
|
||||
{
|
||||
$maxlength = strlen($quantity);
|
||||
}
|
||||
}
|
||||
$result = array();
|
||||
$length = count($result_quantity);
|
||||
for ($i = 0; $i < $length; $i++)
|
||||
{
|
||||
$quantity = str_pad($result_quantity[$i], $maxlength);
|
||||
array_push($result, $quantity . ' ' . $result_product[$i]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function TransferProduct(int $productId, float $amount, int $locationIdFrom, int $locationIdTo, $specificStockEntryId = 'default', &$transactionId = null)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
|
Reference in New Issue
Block a user