From bd16b8c8514b90622abb4f5021b5e2f6b776cdd0 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 20 Apr 2017 22:01:14 +0200 Subject: [PATCH] Added flow to directly add articles and barcodes form purchase and inventory view --- grocy.js | 15 ++++++ version.txt | 2 +- views/consumption.js | 1 + views/inventory.js | 119 +++++++++++++++++++++++++++++++++++++---- views/inventory.php | 1 + views/locations.js | 2 +- views/productform.js | 18 ++++++- views/products.js | 2 +- views/purchase.js | 111 ++++++++++++++++++++++++++++++++++---- views/purchase.php | 1 + views/quantityunits.js | 2 +- 11 files changed, 247 insertions(+), 27 deletions(-) diff --git a/grocy.js b/grocy.js index 04294c18..d651fa7d 100644 --- a/grocy.js +++ b/grocy.js @@ -80,3 +80,18 @@ String.prototype.contains = function(search) { return this.toLowerCase().indexOf(search.toLowerCase()) !== -1; }; + +Grocy.GetUriParam = function(key) +{ + var currentUri = decodeURIComponent(window.location.search.substring(1)); + var vars = currentUri.split('&'); + + for (i = 0; i < vars.length; i++) { + var currentParam = vars[i].split('='); + + if (currentParam[0] === key) + { + return currentParam[1] === undefined ? true : currentParam[1]; + } + } +}; diff --git a/version.txt b/version.txt index afaf360d..7f207341 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.0.1 \ No newline at end of file diff --git a/views/consumption.js b/views/consumption.js index 077d78d7..7c6b0269 100644 --- a/views/consumption.js +++ b/views/consumption.js @@ -56,6 +56,7 @@ $('#product_id').on('change', function(e) $('#selected-product-last-used').text((productDetails.last_used || 'never').substring(0, 10)); $('#selected-product-last-used-timeago').text($.timeago(productDetails.last_used || '')); $('#amount').attr('max', productDetails.stock_amount); + $('#consumption-form').validator('update'); Grocy.EmptyElementWhenMatches('#selected-product-last-purchased-timeago', 'NaN years ago'); Grocy.EmptyElementWhenMatches('#selected-product-last-used-timeago', 'NaN years ago'); diff --git a/views/inventory.js b/views/inventory.js index f492e81d..203277bf 100644 --- a/views/inventory.js +++ b/views/inventory.js @@ -8,18 +8,50 @@ function (productDetails) { Grocy.FetchJson('/api/stock/inventory-product/' + jsonForm.product_id + '/' + jsonForm.new_amount + '?bestbeforedate=' + $('#best_before_date').val(), - function (result) { + function(result) + { + var addBarcode = Grocy.GetUriParam('addbarcodetoselection'); + if (addBarcode !== undefined) + { + var existingBarcodes = productDetails.product.barcode || ''; + if (existingBarcodes.length === 0) + { + productDetails.product.barcode = addBarcode; + } + else + { + productDetails.product.barcode += ',' + addBarcode; + } + + Grocy.PostJson('/api/edit-object/products/' + productDetails.product.id, productDetails.product, + function (result) { }, + function(xhr) + { + console.error(xhr); + } + ); + } + toastr.success('Stock amount of ' + productDetails.product.name + ' is now ' + jsonForm.new_amount.toString() + ' ' + productDetails.quantity_unit_stock.name); - $('#new_amount').val(''); - $('#best_before_date').val(''); - $('#product_id').val(''); - $('#product_id_text_input').focus(); - $('#product_id_text_input').val(''); - $('#product_id_text_input').trigger('change'); - $('#inventory-form').validator('validate'); + if (addBarcode !== undefined) + { + window.location.href = '/inventory'; + } + else + { + $('#inventory-change-info').hide(); + $('#new_amount').val(''); + $('#best_before_date').val(''); + $('#product_id').val(''); + $('#product_id_text_input').focus(); + $('#product_id_text_input').val(''); + $('#product_id_text_input').trigger('change'); + $('#inventory-form').validator('validate'); + } }, - function (xhr) { + function(xhr) + { console.error(xhr); } ); @@ -86,12 +118,47 @@ $(function() var input = $('#product_id_text_input').val().toString(); var possibleOptionElement = $("#product_id option[data-additional-searchdata*='" + input + "']").first(); - if (possibleOptionElement.length > 0) + if (Grocy.GetUriParam('addbarcodetoselection') === undefined && possibleOptionElement.length > 0) { $('#product_id').val(possibleOptionElement.val()); $('#product_id').data('combobox').refresh(); $('#product_id').trigger('change'); } + else + { + var optionElement = $("#product_id option:contains('" + input + "')").first(); + if (input.length > 0 && optionElement.length === 0 && Grocy.GetUriParam('addbarcodetoselection') === undefined ) + { + bootbox.dialog({ + message: '' + input + ' could not be resolved to a product, how do you want to proceed?', + title: 'Create or assign product', + onEscape: function() { }, + buttons: { + cancel: { + label: 'Cancel', + className: 'btn-default', + callback: function() { } + }, + addnewproduct: { + label: 'Add as new product', + className: 'btn-success', + callback: function() + { + window.location.href = '/product/new?prefillname=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname); + } + }, + addbarcode: { + label: 'Add as barcode to existing product', + className: 'btn-info', + callback: function() + { + window.location.href = '/inventory?addbarcodetoselection=' + encodeURIComponent(input); + } + } + } + }); + } + } }); $('#new_amount').val(''); @@ -140,6 +207,31 @@ $(function() } } }); + + var prefillProduct = Grocy.GetUriParam('createdproduct'); + if (prefillProduct !== undefined) + { + var possibleOptionElement = $("#product_id option[data-additional-searchdata*='" + prefillProduct + "']").first(); + if (possibleOptionElement.length === 0) + { + possibleOptionElement = $("#product_id option:contains('" + prefillProduct + "')").first(); + } + + if (possibleOptionElement.length > 0) + { + $('#product_id').val(possibleOptionElement.val()); + $('#product_id').data('combobox').refresh(); + $('#product_id').trigger('change'); + $('#new_amount').focus(); + } + } + + var addBarcode = Grocy.GetUriParam('addbarcodetoselection'); + if (addBarcode !== undefined) + { + $('#addbarcodetoselection').text(addBarcode); + $('#flow-info-addbarcodetoselection').removeClass('hide'); + } }); $('#best_before_date-datepicker-button').on('click', function(e) @@ -215,18 +307,23 @@ $('#new_amount').on('change', function(e) { var amountToAdd = newAmount - productDetails.stock_amount; $('#inventory-change-info').text('This means ' + amountToAdd.toString() + ' ' + productDetails.quantity_unit_stock.name + ' will be added to stock'); - $('#inventory-change-infoo').show(); + $('#inventory-change-info').show(); + $('#best_before_date').attr('required', 'required'); } else if (newAmount < productStockAmount) { var amountToRemove = productStockAmount - newAmount; $('#inventory-change-info').text('This means ' + amountToRemove.toString() + ' ' + productDetails.quantity_unit_stock.name + ' will be removed from stock'); $('#inventory-change-info').show(); + $('#best_before_date').removeAttr('required'); } else { $('#inventory-change-info').hide(); } + + $('#inventory-form').validator('update'); + $('#inventory-form').validator('validate'); }, function(xhr) { diff --git a/views/inventory.php b/views/inventory.php index b8a23d31..a81c88e6 100644 --- a/views/inventory.php +++ b/views/inventory.php @@ -12,6 +12,7 @@
+
will be added to the list of barcodes for the selected product on submit.
diff --git a/views/locations.js b/views/locations.js index b43511cf..7f6bcb17 100644 --- a/views/locations.js +++ b/views/locations.js @@ -14,7 +14,7 @@ }, callback: function(result) { - if (result == true) + if (result === true) { Grocy.FetchJson('/api/delete-object/locations/' + $(e.target).attr('data-location-id'), function(result) diff --git a/views/productform.js b/views/productform.js index 98734d04..8e7c1b96 100644 --- a/views/productform.js +++ b/views/productform.js @@ -2,12 +2,19 @@ { e.preventDefault(); + var redirectDestination = '/products'; + var returnTo = Grocy.GetUriParam('returnto'); + if (returnTo !== undefined) + { + redirectDestination = returnTo + '?createdproduct=' + encodeURIComponent($('#name').val()); + } + if (Grocy.EditMode === 'create') { Grocy.PostJson('/api/add-object/products', $('#product-form').serializeJSON(), function(result) { - window.location.href = '/products'; + window.location.href = redirectDestination; }, function(xhr) { @@ -20,7 +27,7 @@ Grocy.PostJson('/api/edit-object/products/' + Grocy.EditObjectId, $('#product-form').serializeJSON(), function(result) { - window.location.href = '/products'; + window.location.href = redirectDestination; }, function(xhr) { @@ -61,6 +68,13 @@ $(function() $('#name').focus(); $('#product-form').validator(); $('#product-form').validator('validate'); + + var prefillName = Grocy.GetUriParam('prefillname'); + if (prefillName !== undefined) + { + $('#name').val(prefillName); + $('#name').focus(); + } }); $('.input-group-qu').on('change', function(e) diff --git a/views/products.js b/views/products.js index 91b97aa4..364cae4e 100644 --- a/views/products.js +++ b/views/products.js @@ -14,7 +14,7 @@ }, callback: function(result) { - if (result == true) + if (result === true) { Grocy.FetchJson('/api/delete-object/products/' + $(e.target).attr('data-product-id'), function(result) diff --git a/views/purchase.js b/views/purchase.js index 3d5def6d..b403c45e 100644 --- a/views/purchase.js +++ b/views/purchase.js @@ -10,18 +10,49 @@ var amount = jsonForm.amount * productDetails.product.qu_factor_purchase_to_stock; Grocy.FetchJson('/api/stock/add-product/' + jsonForm.product_id + '/' + amount + '?bestbeforedate=' + $('#best_before_date').val(), - function (result) { + function(result) + { + var addBarcode = Grocy.GetUriParam('addbarcodetoselection'); + if (addBarcode !== undefined) + { + var existingBarcodes = productDetails.product.barcode || ''; + if (existingBarcodes.length === 0) + { + productDetails.product.barcode = addBarcode; + } + else + { + productDetails.product.barcode += ',' + addBarcode; + } + + Grocy.PostJson('/api/edit-object/products/' + productDetails.product.id, productDetails.product, + function (result) { }, + function(xhr) + { + console.error(xhr); + } + ); + } + toastr.success('Added ' + amount + ' ' + productDetails.quantity_unit_stock.name + ' of ' + productDetails.product.name + ' to stock'); - $('#amount').val(1); - $('#best_before_date').val(''); - $('#product_id').val(''); - $('#product_id_text_input').focus(); - $('#product_id_text_input').val(''); - $('#product_id_text_input').trigger('change'); - $('#purchase-form').validator('validate'); + if (addBarcode !== undefined) + { + window.location.href = '/purchase'; + } + else + { + $('#amount').val(1); + $('#best_before_date').val(''); + $('#product_id').val(''); + $('#product_id_text_input').focus(); + $('#product_id_text_input').val(''); + $('#product_id_text_input').trigger('change'); + $('#purchase-form').validator('validate'); + } }, - function (xhr) { + function(xhr) + { console.error(xhr); } ); @@ -92,12 +123,47 @@ $(function() var input = $('#product_id_text_input').val().toString(); var possibleOptionElement = $("#product_id option[data-additional-searchdata*='" + input + "']").first(); - if (possibleOptionElement.length > 0) + if (Grocy.GetUriParam('addbarcodetoselection') === undefined && possibleOptionElement.length > 0) { $('#product_id').val(possibleOptionElement.val()); $('#product_id').data('combobox').refresh(); $('#product_id').trigger('change'); } + else + { + var optionElement = $("#product_id option:contains('" + input + "')").first(); + if (input.length > 0 && optionElement.length === 0 && Grocy.GetUriParam('addbarcodetoselection') === undefined ) + { + bootbox.dialog({ + message: '' + input + ' could not be resolved to a product, how do you want to proceed?', + title: 'Create or assign product', + onEscape: function() { }, + buttons: { + cancel: { + label: 'Cancel', + className: 'btn-default', + callback: function() { } + }, + addnewproduct: { + label: 'Add as new product', + className: 'btn-success', + callback: function() + { + window.location.href = '/product/new?prefillname=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname); + } + }, + addbarcode: { + label: 'Add as barcode to existing product', + className: 'btn-info', + callback: function() + { + window.location.href = '/purchase?addbarcodetoselection=' + encodeURIComponent(input); + } + } + } + }); + } + } }); $('#amount').val(1); @@ -139,6 +205,31 @@ $(function() } } }); + + var prefillProduct = Grocy.GetUriParam('createdproduct'); + if (prefillProduct !== undefined) + { + var possibleOptionElement = $("#product_id option[data-additional-searchdata*='" + prefillProduct + "']").first(); + if (possibleOptionElement.length === 0) + { + possibleOptionElement = $("#product_id option:contains('" + prefillProduct + "')").first(); + } + + if (possibleOptionElement.length > 0) + { + $('#product_id').val(possibleOptionElement.val()); + $('#product_id').data('combobox').refresh(); + $('#product_id').trigger('change'); + $('#best_before_date').focus(); + } + } + + var addBarcode = Grocy.GetUriParam('addbarcodetoselection'); + if (addBarcode !== undefined) + { + $('#addbarcodetoselection').text(addBarcode); + $('#flow-info-addbarcodetoselection').removeClass('hide'); + } }); $('#best_before_date-datepicker-button').on('click', function(e) diff --git a/views/purchase.php b/views/purchase.php index 7ed8fc65..f274a469 100644 --- a/views/purchase.php +++ b/views/purchase.php @@ -12,6 +12,7 @@
+
will be added to the list of barcodes for the selected product on submit.
diff --git a/views/quantityunits.js b/views/quantityunits.js index 2a4ec1ed..587ea855 100644 --- a/views/quantityunits.js +++ b/views/quantityunits.js @@ -14,7 +14,7 @@ }, callback: function(result) { - if (result == true) + if (result === true) { Grocy.FetchJson('/api/delete-object/quantity_units/' + $(e.target).attr('data-quantityunit-id'), function(result)