summaryrefslogtreecommitdiff
path: root/addons/barcodes/static/src/js/form_view_barcode_handler.js
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2016-03-24 15:12:00 +0100
committerChristophe Simonis <chs@odoo.com>2016-03-24 15:12:00 +0100
commit59d3f63c58b47dfc1991494f167216cc1d9a9469 (patch)
treef5fe9deadc80784cb7aa269fafe15cdc5dded42e /addons/barcodes/static/src/js/form_view_barcode_handler.js
parent0d86b91dd9e65814c78be1f79706534fbb759557 (diff)
parent9e4157b6483044265a1f62d1f1f747dedbcbd6bd (diff)
[MERGE] forward port of branch saas-7 up to 9e4157b
Diffstat (limited to 'addons/barcodes/static/src/js/form_view_barcode_handler.js')
-rw-r--r--addons/barcodes/static/src/js/form_view_barcode_handler.js90
1 files changed, 88 insertions, 2 deletions
diff --git a/addons/barcodes/static/src/js/form_view_barcode_handler.js b/addons/barcodes/static/src/js/form_view_barcode_handler.js
index 788a512dc15..a6fa4b55adf 100644
--- a/addons/barcodes/static/src/js/form_view_barcode_handler.js
+++ b/addons/barcodes/static/src/js/form_view_barcode_handler.js
@@ -6,10 +6,30 @@ var utils = require('web.utils');
var common = require('web.form_common');
var BarcodeEvents = require('barcodes.BarcodeEvents');
var BarcodeHandlerMixin = require('barcodes.BarcodeHandlerMixin');
+var KanbanRecord = require('web_kanban.Record');
var _t = core._t;
+// web_kanban.Record and web.list_common.Record do not implement the
+// same interface and are thus inherently incompatible with each
+// other. Luckily barcodes keeps things pretty simple when it comes to
+// the records it wants to use. So if we give the KanbanRecord a get()
+// function that behaves like the one of web.list.Record, everything
+// is fine.
+KanbanRecord.include({
+ get: function (key) {
+ return this.values[key] && this.values[key].value;
+ },
+});
+
var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
+ init: function(parent, context) {
+ this.__quantity_listener = _.bind(this._set_quantity_listener, this);
+ BarcodeHandlerMixin.init.apply(this, arguments);
+
+ return this._super.apply(this, arguments);
+ },
+
start: function() {
this._super();
this.form_view = this.field_manager;
@@ -31,7 +51,67 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
this.map_barcode_method['O-CMD.PAGER-NEXT'] = _.bind(this.form_view.pager.next, this.form_view.pager);
}
},
-
+
+ _display_no_edit_mode_warning: function() {
+ this.do_warn(_t('Error : Document not editable'), _t('To modify this document, please first start edition.'));
+ },
+
+ _display_no_last_scanned_warning: function() {
+ this.do_warn(_t('Error : No last scanned barcode'), _t('To set the quantity please scan a barcode first.'));
+ },
+
+ _set_quantity_listener: function(event) {
+ var self = this;
+ var character = String.fromCharCode(event.which);
+
+ // only catch the event if we're not focused in
+ // another field and it's a number
+ if ($(event.target).is('body') && /[0-9]/.test(character)) {
+ if (this.form_view.get('actual_mode') === 'view') {
+ this._display_no_edit_mode_warning();
+ } else {
+ var field = this.form_view.fields[this.m2x_field];
+ var view = field.viewmanager.active_view;
+
+ if (this.last_scanned_barcode) {
+ var new_qty = window.prompt(_t('Set quantity'), character) || "0";
+ new_qty = new_qty.replace(',', '.');
+ var record = this._get_records(field).find(function(record) {
+ return record.get('product_barcode') === self.last_scanned_barcode;
+ });
+ if (record) {
+ var values = {};
+ values[this.quantity_field] = parseFloat(new_qty);
+ field.data_update(record.get('id'), values).then(function () {
+ view.controller.reload_record(record);
+ });
+ } else {
+ this._display_no_last_scanned_warning();
+ }
+ } else {
+ this._display_no_last_scanned_warning();
+ }
+ }
+ }
+ },
+
+ start_listening: function() {
+ if (this.quantity_field && ! this.is_listening) {
+ core.bus.on('keypress', this, this.__quantity_listener);
+ }
+
+ BarcodeHandlerMixin.start_listening.call(this);
+ },
+
+ stop_listening: function() {
+ if (this.quantity_field && this.is_listening) {
+ core.bus.off('keypress', this, this.__quantity_listener);
+ delete this.last_scanned_barcode;
+ }
+
+ BarcodeHandlerMixin.stop_listening.call(this);
+ },
+
// Let subclasses add custom behaviour before onchange. Must return a deferred.
// Resolve the deferred with true proceed with the onchange, false to prevent it.
pre_onchange_hook: function(barcode) {
@@ -40,6 +120,7 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
on_barcode_scanned: function(barcode) {
var self = this;
+ self.last_scanned_barcode = barcode;
// Execute a harcoded action
var action = this.map_barcode_method[barcode];
if (typeof action === "function")
@@ -48,7 +129,7 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
return;
// Warn the user if form view is not editable
else if (this.form_view.get('actual_mode') === 'view')
- this.do_warn(_t('Error : Document not editable'), _t('To modify this document, please first start edition.'));
+ this._display_no_edit_mode_warning();
else {
// Call hook method possibly implemented by subclass
this.pre_onchange_hook(barcode).then(function(proceed) {
@@ -71,6 +152,11 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
});
}
},
+
+ _get_records: function(field) {
+ return field.viewmanager.active_view.controller.records || // tree view
+ field.viewmanager.active_view.controller.widgets; // kanban view
+ },
});
core.form_widget_registry.add('barcode_handler', FormViewBarcodeHandler);