From 01a9d43ebbb56b16f34262d8e2599c854fe9551a Mon Sep 17 00:00:00 2001 From: Katharina Bogad Date: Fri, 11 Jun 2021 18:52:16 +0200 Subject: [PATCH] Grocycode: Default settings for label printing --- controllers/StockApiController.php | 4 +-- localization/de/strings.po | 20 ++++++++++++- localization/strings.pot | 18 ++++++++++++ migrations/0128.sql | 11 +++++++ public/js/grocy.js | 16 +++++++--- public/viewjs/productform.js | 16 ++++++++++ public/viewjs/purchase.js | 28 +++++++++++++++++- services/StockService.php | 21 +++++++++---- views/productform.blade.php | 47 ++++++++++++++++++++++++++++++ views/purchase.blade.php | 24 +++++++-------- 10 files changed, 180 insertions(+), 25 deletions(-) create mode 100644 migrations/0128.sql diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index ef2b425f..e25c1144 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -141,9 +141,9 @@ class StockApiController extends BaseApiController $transactionType = $requestBody['transactiontype']; } $runPrinterWebhook = false; - if (array_key_exists('print_stock_label', $requestBody) && boolval($requestBody['print_stock_label'])) + if (array_key_exists('print_stock_label', $requestBody) && intval($requestBody['print_stock_label'])) { - $runPrinterWebhook = true; + $runPrinterWebhook = intval($requestBody['print_stock_label']); } $transactionId = $this->getStockService()->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId, $shoppingLocationId, $unusedTransactionId, $runPrinterWebhook); diff --git a/localization/de/strings.po b/localization/de/strings.po index ba6f8d3f..b8816137 100644 --- a/localization/de/strings.po +++ b/localization/de/strings.po @@ -2403,4 +2403,22 @@ msgid "Print Product Label" msgstr "Produkt-Etikett drucken" msgid "Print stock entry label" -msgstr "Etikett drucken" \ No newline at end of file +msgstr "Etikett drucken" + +msgid "Stock label" +msgstr "Etikett" + +msgid "No Label" +msgstr "Kein Etikett" + +msgid "Single Label" +msgstr "Ein Etikett pro Einkauf" + +msgid "Label per Unit" +msgstr "Ein Etikett pro Mengeneinheit" + +msgid "Allow label printing per unit" +msgstr "Erlaube Etikettendruck pro Mengeneinheit" + +msgid "Allow printing of one label per unit in a purchase (after conversion). E.g. 1 purchased pack adding 10 pieces of stock would print 10 labels." +msgstr "Erlaube Etikettendruck pro Mengeneinheit nach Umrechnung. Beispiel: 1 gekauftes Paket, das 10 Einheiten hinzufügt, druckt 10 Etiketten." \ No newline at end of file diff --git a/localization/strings.pot b/localization/strings.pot index 0c94cd1e..6d285725 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -2115,3 +2115,21 @@ msgstr "" msgid "Print stock entry label" msgstr "" + +msgid "Stock label" +msgstr "" + +msgid "No Label" +msgstr "" + +msgid "Single Label" +msgstr "" + +msgid "Label per Unit" +msgstr "" + +msgid "Allow label printing per unit" +msgstr "" + +msgid "Allow printing of one label per unit in a purchase (after conversion). E.g. 1 purchased pack adding 10 pieces of stock would print 10 labels." +msgstr "" diff --git a/migrations/0128.sql b/migrations/0128.sql new file mode 100644 index 00000000..51db2d3f --- /dev/null +++ b/migrations/0128.sql @@ -0,0 +1,11 @@ +ALTER TABLE products +ADD default_print_stock_label INTEGER NOT NULL DEFAULT 0; + +UPDATE products +SET default_print_stock_label = 0; + +ALTER TABLE products +ADD allow_label_per_unit INTEGER NOT NULL DEFAULT 0; + +UPDATE products +SET allow_label_per_unit = 0; \ No newline at end of file diff --git a/public/js/grocy.js b/public/js/grocy.js index 4e956250..1fad2d8f 100644 --- a/public/js/grocy.js +++ b/public/js/grocy.js @@ -476,14 +476,22 @@ Grocy.FrontendHelpers.DeleteUserSetting = function(settingsKey, reloadPageOnSucc ); } -Grocy.FrontendHelpers.RunWebhook = function(webhook, data) +Grocy.FrontendHelpers.RunWebhook = function(webhook, data, repetitions = 1) { Object.assign(data, webhook.extra_data); + var hasAlreadyFailed = false; - $.post(webhook.hook, data).fail(function(req, status, errorThrown) + for (i = 0; i < repetitions; i++) { - Grocy.FrontendHelpers.ShowGenericError(__t("Unable to connect to webhook.", { "status": status, "errorThrown": errorThrown })); - }); + $.post(webhook.hook, data).fail(function(req, status, errorThrown) + { + if (!hasAlreadyFailed) + { + hasAlreadyFailed = true; + Grocy.FrontendHelpers.ShowGenericError(__t("Unable to connect to webhook.", { "status": status, "errorThrown": errorThrown })); + } + }); + } } $(document).on("keyup paste change", "input, textarea", function() diff --git a/public/viewjs/productform.js b/public/viewjs/productform.js index 3812bc44..e22e487e 100644 --- a/public/viewjs/productform.js +++ b/public/viewjs/productform.js @@ -403,6 +403,22 @@ $('#qu_id_stock').change(function(e) } }); +$('#allow_label_per_unit').on('change', function() +{ + if (this.checked) + { + $('#label-option-per-unit').prop("disabled", false); + } + else + { + if ($('#default_print_stock_label').val() == "2") + { + $("#default_print_stock_label").val("0"); + } + $('#label-option-per-unit').prop("disabled", true); + } +}); + $(window).on("message", function(e) { var data = e.originalEvent.data; diff --git a/public/viewjs/purchase.js b/public/viewjs/purchase.js index 760a1cd8..f23ecb30 100644 --- a/public/viewjs/purchase.js +++ b/public/viewjs/purchase.js @@ -128,7 +128,16 @@ $('#save-purchase-button').on('click', function(e) { post_data.duedate = __t('DD') + ': ' + result[0].best_before_date } - Grocy.FrontendHelpers.RunWebhook(Grocy.Webhooks.labelprinter, post_data); + + if (jsonForm.print_stock_label > 0) + { + var reps = 1; + if (jsonForm.print_stock_label == 2) + { + reps = Math.floor(jsonData.amount); + } + Grocy.FrontendHelpers.RunWebhook(Grocy.Webhooks.labelprinter, post_data, reps); + } } } @@ -295,6 +304,23 @@ if (Grocy.Components.ProductPicker !== undefined) } } + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABELPRINTER) + { + $("#print_stock_label").val(productDetails.product.default_print_stock_label); + if (productDetails.product.allow_label_per_unit) + { + if ($('#default_print_stock_label').val() == "2") + { + $("#default_print_stock_label").val("0"); + } + $('#label-option-per-unit').prop("disabled", true); + } + else + { + $('#label-option-per-unit').prop("disabled", false); + } + } + $("#display_amount").focus(); Grocy.FrontendHelpers.ValidateForm('purchase-form'); diff --git a/services/StockService.php b/services/StockService.php index f50b4620..c60d424c 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -105,7 +105,7 @@ class StockService extends BaseService } } - public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null, $shoppingLocationId = null, &$transactionId = null, $runWebhook = false, &$webhookData = null) + public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null, $shoppingLocationId = null, &$transactionId = null, $runWebhook = 0) { if (!$this->ProductExists($productId)) { @@ -182,17 +182,28 @@ class StockService extends BaseService if (GROCY_FEATURE_FLAG_LABELPRINTER && GROCY_LABEL_PRINTER_RUN_SERVER && $runWebhook) { + $reps = 1; + if ($runWebhook == 2) + { // 2 == run $amount times + $reps = intval(floor($amount)); + } + $webhookData = array_merge([ - 'product' => $stockEntry->product()->name, - 'grocycode' => (string)(new Grocycode(Grocycode::PRODUCT, $stockEntry->product_id, [$stockEntry->stock_id])), + 'product' => $productDetails->product->name, + 'grocycode' => (string)(new Grocycode(Grocycode::PRODUCT, $productId, [$stockId])), ], GROCY_LABEL_PRINTER_PARAMS); - if (GROCY_FEATURE_FLAG_BEST_BEFORE_DATE_TRACKING) + if (GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING) { $webhookData['duedate'] = $this->getLocalizationService()->__t('DD') . ': ' . $bestBeforeDate; } - (new WebhookRunner())->run(GROCY_LABEL_PRINTER_WEBHOOK, $webhookData, GROCY_LABEL_PRINTER_HOOK_JSON); + $runner = new WebhookRunner(); + + for ($i = 0; $i < $reps; $i++) + { + $runner->run(GROCY_LABEL_PRINTER_WEBHOOK, $webhookData, GROCY_LABEL_PRINTER_HOOK_JSON); + } } return $transactionId; diff --git a/views/productform.blade.php b/views/productform.blade.php index d09a778a..76699f7c 100644 --- a/views/productform.blade.php +++ b/views/productform.blade.php @@ -405,6 +405,53 @@ 'entity' => 'products' )) + @if(GROCY_FEATURE_FLAG_LABELPRINTER) +
+
+ allow_label_per_unit == 1) checked @endif class="form-check-input custom-control-input" type="checkbox" id="allow_label_per_unit" name="allow_label_per_unit" value="1"> + +
+
+ + @php + $no_label = ""; + $single_label = ""; + $per_unit_label = ""; + $disable_per_unit = ""; + + if($mode == 'edit') { + switch($product->default_print_stock_label) { + case 0: $no_label = "selected"; break; + case 1: $single_label = "selected"; break; + case 2: $per_unit_label = "selected"; break; + default: break; // yolo + } + if($product->allow_label_per_unit == 0) { + $disable_per_unit="disabled"; + $per_unit_label = ""; + } + } + @endphp + +
+ + +
+ @endif +
-Grocy.QuantityUnits = {!! json_encode($quantityUnits) !!}; + Grocy.QuantityUnits = {!! json_encode($quantityUnits) !!}; Grocy.QuantityUnitConversionsResolved = {!! json_encode($quantityUnitConversionsResolved) !!}; Grocy.DefaultMinAmount = '{{ $DEFAULT_MIN_AMOUNT }}'; @@ -149,17 +149,17 @@ Grocy.QuantityUnits = {!! json_encode($quantityUnits) !!}; @endif @if(GROCY_FEATURE_FLAG_LABELPRINTER) -
-
- - -
+
+ + +
{{ $__t('A quantity unit is required') }}
@endif