Merge pull request #54 from VandorpeDavid/negative_purchases
Enforce stock max en 0 min for amount of products in orders
This commit is contained in:
commit
4dcf2c2b1b
2 changed files with 29 additions and 9 deletions
|
@ -9,16 +9,36 @@ ready = ->
|
||||||
increment($(this), -1)
|
increment($(this), -1)
|
||||||
|
|
||||||
$('.form_row').each((index, row) ->
|
$('.form_row').each((index, row) ->
|
||||||
disIfNec(row)
|
updateInput(row, false)
|
||||||
$(row).on('input', recalculate)
|
$(row).on('input', ->
|
||||||
|
updateInput(row)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
recalculate()
|
recalculate()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Validate input, and then update
|
||||||
|
updateInput = (row, useRecalculate = true) ->
|
||||||
|
cell = row.querySelector("input")
|
||||||
|
if ! cell.validity.valid
|
||||||
|
if(parseInt(cell.value) > parseInt(cell.max))
|
||||||
|
cell.value = parseInt(cell.max)
|
||||||
|
else
|
||||||
|
cell.value = 0
|
||||||
|
|
||||||
|
# Disable buttons if necessary
|
||||||
|
disIfNec(row)
|
||||||
|
|
||||||
|
if useRecalculate
|
||||||
|
recalculate();
|
||||||
|
|
||||||
|
|
||||||
disIfNec = (row) ->
|
disIfNec = (row) ->
|
||||||
counter = parseInt($(row).find('.row_counter').val())
|
counter = parseInt($(row).find('.row_counter').val())
|
||||||
$(row).find('.btn-dec').prop('disabled', counter == 0);
|
$(row).find('.btn-dec').prop('disabled', counter == 0);
|
||||||
$(row).find('.btn-inc').prop('disabled', counter == parseInt($(row).find('.stock').val()))
|
$(row).find('.btn-inc').prop('disabled', counter == parseInt($(row).find('.row_counter').attr('max')))
|
||||||
|
|
||||||
recalculate = () ->
|
recalculate = () ->
|
||||||
value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b
|
value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b
|
||||||
|
@ -29,12 +49,12 @@ increment = (button, n) ->
|
||||||
|
|
||||||
# Fix the counter
|
# Fix the counter
|
||||||
counter = $(row).find('.row_counter')
|
counter = $(row).find('.row_counter')
|
||||||
counter.val(parseInt(counter.val()) + n)
|
value = parseInt(counter.val())
|
||||||
|
if isNaN(value)
|
||||||
|
value = 0
|
||||||
|
counter.val(value + n)
|
||||||
|
|
||||||
# Disable buttons if necessary
|
updateInput(row[0])
|
||||||
disIfNec(row)
|
|
||||||
|
|
||||||
recalculate()
|
|
||||||
|
|
||||||
$(document).ready(ready)
|
$(document).ready(ready)
|
||||||
$(document).on('page:load', ready)
|
$(document).on('page:load', ready)
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
<%= content_tag :span, product.name %>
|
<%= content_tag :span, product.name %>
|
||||||
<%= content_tag :small, euro(product.price) %>
|
<%= content_tag :small, euro(product.price) %>
|
||||||
</h4>
|
</h4>
|
||||||
<%= f.counter :count, max: 1000, skip_label: true, wrapper_class: "input-group", class: "row_counter" %>
|
<%= f.counter :count, min: 0, max: product.stock, skip_label: true, wrapper_class: "input-group", class: "row_counter" %>
|
||||||
<%= f.fields_for :product do |product| %>
|
<%= f.fields_for :product do |product| %>
|
||||||
<%= product.hidden_field :price_cents, class: :price %>
|
<%= product.hidden_field :price_cents, class: :price %>
|
||||||
<%= product.hidden_field :stock, class: :stock %>
|
<%= product.hidden_field :stock, class: :stock %>
|
||||||
|
|
Loading…
Reference in a new issue