diff --git a/erpnext/public/js/utils/barcode_scanner.js b/erpnext/public/js/utils/barcode_scanner.js index 455b9716fa4..4d73ab9bcf2 100644 --- a/erpnext/public/js/utils/barcode_scanner.js +++ b/erpnext/public/js/utils/barcode_scanner.js @@ -9,6 +9,13 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { this.items_table_name = opts.items_table_name || "items"; this.items_table = this.frm.doc[this.items_table_name]; + // any API that takes `search_value` as input and returns dictionary as follows + // { + // item_code: "HORSESHOE", // present if any item was found + // bar_code: "123456", // present if barcode was scanned + // batch_no: "LOT12", // present if batch was scanned + // serial_no: "987XYZ", // present if serial no was scanned + // } this.scan_api = opts.scan_api || "erpnext.selling.page.point_of_sale.point_of_sale.search_for_serial_or_batch_or_barcode_number"; } @@ -35,64 +42,74 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { return; } - me.modify_table_after_scan(data); + me.update_table(data.item_code, data.barcode, data.batch_no, data.serial_no); }); - this.clean_up(); } - - modify_table_after_scan(data) { + update_table(item_code, barcode, batch_no, serial_no) { let cur_grid = this.frm.fields_dict[this.items_table_name].grid; - let row_to_modify = null; + let row = null; // Check if batch is scanned and table has batch no field - let batch_no_scan = Boolean(data.batch_no) && frappe.meta.has_field(cur_grid.doctype, "batch_no"); + let batch_no_scan = Boolean(batch_no) && frappe.meta.has_field(cur_grid.doctype, "batch_no"); if (batch_no_scan) { - row_to_modify = this.get_batch_row_to_modify(data.batch_no); + row = this.get_batch_row_to_modify(batch_no); } else { // serial or barcode scan - row_to_modify = this.get_row_to_modify_on_scan(row_to_modify, data); + row = this.get_row_to_modify_on_scan(row, item_code); } - if (!row_to_modify) { + if (!row) { // add new row if new item/batch is scanned - row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name); + row = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name); // trigger any row add triggers defined on child table. - this.frm.script_manager.trigger(`${this.items_table_name}_add`, row_to_modify.doctype, row_to_modify.name); + this.frm.script_manager.trigger(`${this.items_table_name}_add`, row.doctype, row.name); } - if (this.is_duplicate_serial_no(row_to_modify, data.serial_no)) { + if (this.is_duplicate_serial_no(row, serial_no)) { this.clean_up(); return; }; - this.show_scan_message(row_to_modify.idx, row_to_modify.item_code); - this.set_scanned_values(row_to_modify, data); + this.show_scan_message(row.idx, row.item_code); + this.set_item(row, item_code); + this.set_serial_no(row, serial_no); + this.set_batch_no(row, batch_no); + this.set_barcode(row, barcode); + this.clean_up(); } - set_scanned_values(row_to_modify, data) { + set_item(row, item_code) { + frappe.model.set_value(row.doctype, row.name, { + item_code: item_code, + qty: (row.qty || 0) + 1, // TODO: harcoded fieldname + }); + } + + set_serial_no(row, serial_no) { + if (serial_no && frappe.meta.has_field(row.doctype, "serial_no")) { + // TODO: fieldname hardcoded + const value = row["serial_no"] + '\n' + serial_no; + frappe.model.set_value(row.doctype, row.name, "serial_no", value); + } + } + + set_batch_no(row, batch_no) { + if (batch_no && frappe.meta.has_field(row.doctype, "batch_no")) { + // TODO: fieldname hardcoded + frappe.model.set_value(row.doctype, row.name, "batch_no", batch_no); + } + } + + set_barcode(row, barcode) { // increase qty and set scanned value and item in row + // XXX: tightly coupled global flag on frm object used in transaction.js -_- this.frm.from_barcode = this.frm.from_barcode ? this.frm.from_barcode + 1 : 1; - frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, { - item_code: data.item_code, - qty: (row_to_modify.qty || 0) + 1 - }); - - ['serial_no', 'batch_no', 'barcode'].forEach(field => { - if (data[field] && frappe.meta.has_field(row_to_modify.doctype, field)) { - let is_serial_no = row_to_modify[field] && field === "serial_no"; - let value = data[field]; - - if (is_serial_no) { - value = row_to_modify[field] + '\n' + data[field]; - } - - frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, field, value); - } - }); - - this.clean_up(); + if (barcode && frappe.meta.has_field(row.doctype, "barcode")) { + // TODO: fieldname hardcoded + frappe.model.set_value(row.doctype, row.name, "barcode", barcode); + } } show_scan_message (idx, exist = null) { @@ -129,9 +146,9 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { return existing_batch_row || null; } - get_row_to_modify_on_scan(row_to_modify, data) { + get_row_to_modify_on_scan(row_to_modify, item_code) { // get an existing item row to increment or blank row to modify - const existing_item_row = this.items_table.find(d => d.item_code === data.item_code); + const existing_item_row = this.items_table.find(d => d.item_code === item_code); const blank_item_row = this.items_table.find(d => !d.item_code); if (existing_item_row) {