From 675660e130be89e873c052e48eddb264e8e5d84b Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 26 Nov 2019 11:19:55 -0600 Subject: [PATCH] Expose catalog cost, allow updating, for receiving batch rows --- tailbone/templates/receiving/view.mako | 162 ++++++++++++++++++++----- tailbone/views/purchasing/receiving.py | 42 ++++--- 2 files changed, 162 insertions(+), 42 deletions(-) diff --git a/tailbone/templates/receiving/view.mako b/tailbone/templates/receiving/view.mako index 72758aae..1bd2427e 100644 --- a/tailbone/templates/receiving/view.mako +++ b/tailbone/templates/receiving/view.mako @@ -9,10 +9,11 @@ % if not batch.executed: // keep track of which cost value is currently being edited - var editing = null; + var editing_catalog_cost = null; + var editing_invoice_cost = null; function start_editing(td) { - var value = 0; + var value = null; var text = td.text().replace(/^\s+|\s+$/g, ''); if (text) { td.data('previous-value', text); @@ -21,27 +22,58 @@ } var input = $(''); td.append(input); - input.val(value.toString()).select().focus(); - editing = td; + value = value ? value.toString() : ''; + input.val(value).select().focus(); } - function start_editing_next() { - var tr = editing.parents('tr:first'); + function start_editing_catalog_cost(td) { + start_editing(td); + editing_catalog_cost = td; + } + + function start_editing_invoice_cost(td) { + start_editing(td); + editing_invoice_cost = td; + } + + function start_editing_next_catalog_cost() { + var tr = editing_catalog_cost.parents('tr:first'); var next = tr.next('tr:first'); if (next.length) { - start_editing(next.find('td.invoice_unit_cost')); + start_editing_catalog_cost(next.find('td.catalog_unit_cost')); + } else { + editing_catalog_cost = null; } } - function cancel_edit() { - var input = editing.find('input'); + function start_editing_next_invoice_cost() { + var tr = editing_invoice_cost.parents('tr:first'); + var next = tr.next('tr:first'); + if (next.length) { + start_editing_invoice_cost(next.find('td.invoice_unit_cost')); + } else { + editing_invoice_cost = null; + } + } + + function cancel_edit(td) { + var input = td.find('input'); input.blur(); input.remove(); - var value = editing.data('previous-value'); + var value = td.data('previous-value'); if (value) { - editing.text(value); + td.text(value); } - editing = null; + } + + function cancel_edit_catalog_cost() { + cancel_edit(editing_catalog_cost); + editing_catalog_cost = null; + } + + function cancel_edit_invoice_cost() { + cancel_edit(editing_invoice_cost); + editing_invoice_cost = null; } % endif @@ -49,13 +81,80 @@ $(function() { % if not batch.executed: - $('.grid-wrapper').on('click', '.grid td.invoice_unit_cost', function() { - if (editing) { - editing.find('input').focus(); + $('.grid-wrapper').on('click', '.grid td.catalog_unit_cost', function() { + if (editing_catalog_cost) { + editing_catalog_cost.find('input').focus(); + return + } + if (editing_invoice_cost) { + editing_invoice_cost.find('input').focus(); return } var td = $(this); - start_editing(td); + start_editing_catalog_cost(td); + }); + + $('.grid-wrapper').on('click', '.grid td.invoice_unit_cost', function() { + if (editing_invoice_cost) { + editing_invoice_cost.find('input').focus(); + return + } + if (editing_catalog_cost) { + editing_catalog_cost.find('input').focus(); + return + } + var td = $(this); + start_editing_invoice_cost(td); + }); + + $('.grid-wrapper').on('keyup', '.grid td.catalog_unit_cost input', function(event) { + var input = $(this); + + // let numeric keys modify input value + if (! key_modifies(event)) { + + // when user presses Enter while editing cost value, submit + // value to server for immediate persistence + if (event.which == 13) { + $('.grid-wrapper').mask("Updating cost..."); + var url = '${url('receiving.update_row_cost', uuid=batch.uuid)}'; + var td = input.parents('td:first'); + var tr = td.parents('tr:first'); + var data = { + '_csrf': $('[name="_csrf"]').val(), + 'row_uuid': tr.data('uuid'), + 'catalog_unit_cost': input.val() + }; + $.post(url, data, function(data) { + if (data.error) { + alert(data.error); + } else { + var total = null; + + // update catalog cost for row + td.text(data.row.catalog_unit_cost); + + // mark cost as confirmed + if (data.row.catalog_cost_confirmed) { + tr.addClass('catalog_cost_confirmed'); + } + + input.blur(); + input.remove(); + start_editing_next_catalog_cost(); + } + $('.grid-wrapper').unmask(); + }); + + // When user presses Escape while editing totals, cancel the edit. + } else if (event.which == 27) { + cancel_edit_catalog_cost(); + + // Most other keys at this point should be unwanted... + } else if (! key_allowed(event)) { + return false; + } + } }); $('.grid-wrapper').on('keyup', '.grid td.invoice_unit_cost input', function(event) { @@ -100,14 +199,14 @@ input.blur(); input.remove(); - start_editing_next(); + start_editing_next_invoice_cost(); } $('.grid-wrapper').unmask(); }); // When user presses Escape while editing totals, cancel the edit. } else if (event.which == 27) { - cancel_edit(); + cancel_edit_invoice_cost(); // Most other keys at this point should be unwanted... } else if (! key_allowed(event)) { @@ -167,13 +266,16 @@ ${parent.extra_styles()} % if not batch.executed and master.has_perm('edit_row'):