Skip to content

Commit

Permalink
fix: Scan Barcode UX
Browse files Browse the repository at this point in the history
(cherry picked from commit e1f9ba7)

# Conflicts:
#	erpnext/public/js/utils/barcode_scanner.js
  • Loading branch information
rohitwaghchaure authored and mergify[bot] committed Nov 4, 2022
1 parent 7e15c47 commit 1944f4d
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 21 deletions.
14 changes: 12 additions & 2 deletions erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.json
Expand Up @@ -8,6 +8,7 @@
"engine": "InnoDB",
"field_order": [
"barcode",
"has_item_scanned",
"item_code",
"col_break1",
"item_name",
Expand Down Expand Up @@ -808,17 +809,26 @@
"fieldtype": "Check",
"label": "Grant Commission",
"read_only": 1
},
{
"default": "0",
"depends_on": "barcode",
"fieldname": "has_item_scanned",
"fieldtype": "Check",
"label": "Has Item Scanned",
"read_only": 1
}
],
"istable": 1,
"links": [],
"modified": "2021-10-05 12:23:47.506290",
"modified": "2022-11-02 12:52:39.125295",
"modified_by": "Administrator",
"module": "Accounts",
"name": "POS Invoice Item",
"naming_rule": "Random",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC"
"sort_order": "DESC",
"states": []
}
Expand Up @@ -8,6 +8,7 @@
"engine": "InnoDB",
"field_order": [
"barcode",
"has_item_scanned",
"item_code",
"col_break1",
"item_name",
Expand Down Expand Up @@ -872,12 +873,20 @@
"label": "Purchase Order Item",
"print_hide": 1,
"read_only": 1
},
{
"default": "0",
"depends_on": "barcode",
"fieldname": "has_item_scanned",
"fieldtype": "Check",
"label": "Has Item Scanned",
"read_only": 1
}
],
"idx": 1,
"istable": 1,
"links": [],
"modified": "2022-10-26 11:38:36.119339",
"modified": "2022-11-02 12:53:12.693217",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Sales Invoice Item",
Expand Down
223 changes: 217 additions & 6 deletions erpnext/public/js/utils/barcode_scanner.js
Expand Up @@ -42,6 +42,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
return;
}

<<<<<<< HEAD
frappe
.call({
method: this.scan_api,
Expand All @@ -61,24 +62,58 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
me.update_table(data).then(row => {
row ? resolve(row) : reject();
});
=======
this.scan_api_call(input, (r) => {
const data = r && r.message;
if (!data || Object.keys(data).length === 0) {
this.show_alert(__("Cannot find Item with this Barcode"), "red");
this.clean_up();
this.play_fail_sound();
reject();
return;
}

me.update_table(data).then(row => {
this.play_success_sound();
resolve(row);
}).catch(() => {
this.play_fail_sound();
reject();
>>>>>>> e1f9ba78e5 (fix: Scan Barcode UX)
});
});
});
}

scan_api_call(input, callback) {
frappe
.call({
method: this.scan_api,
args: {
search_value: input,
},
})
.then((r) => {
callback(r);
});
}

update_table(data) {
return new Promise(resolve => {
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;

const {item_code, barcode, batch_no, serial_no, uom} = data;

let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom);
let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom, barcode);

this.is_new_row = false;
if (!row) {
if (this.dont_allow_new_row) {
this.show_alert(__("Maximum quantity scanned for item {0}.", [item_code]), "red");
this.clean_up();
return;
}
this.is_new_row = true;

// add new row if new item/batch is scanned
row = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name);
Expand All @@ -93,7 +128,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {

frappe.run_serially([
() => this.set_selector_trigger_flag(data),
() => this.set_item(row, item_code).then(qty => {
() => this.set_item(row, item_code, barcode, batch_no, serial_no).then(qty => {
this.show_scan_message(row.idx, row.item_code, qty);
}),
() => this.set_barcode_uom(row, uom),
Expand Down Expand Up @@ -124,7 +159,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
frappe.flags.hide_serial_batch_dialog = false;
}

set_item(row, item_code) {
set_item(row, item_code, barcode, batch_no, serial_no) {
return new Promise(resolve => {
const increment = async (value = 1) => {
const item_data = {item_code: item_code};
Expand All @@ -138,11 +173,184 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
increment(value).then((value) => resolve(value));
});
} else {
increment().then((value) => resolve(value));
this.prepare_item_for_scan(row, item_code, barcode, batch_no, serial_no);
}
});
}

prepare_item_for_scan(row, item_code, barcode, batch_no, serial_no) {
var me = this;
this.dialog = new frappe.ui.Dialog({
title: __("Scan barcode for item {0}", [item_code]),
fields: me.get_fields_for_dialog(row, item_code, barcode, batch_no, serial_no),
})

this.dialog.set_primary_action(__("Update"), () => {
const item_data = {item_code: item_code};
item_data[this.qty_field] = this.dialog.get_value("scanned_qty");
item_data["has_item_scanned"] = 1;

this.remaining_qty = flt(this.dialog.get_value("qty")) - flt(this.dialog.get_value("scanned_qty"));
frappe.model.set_value(row.doctype, row.name, item_data);

frappe.run_serially([
() => this.set_batch_no(row, this.dialog.get_value("batch_no")),
() => this.set_barcode(row, this.dialog.get_value("barcode")),
() => this.set_serial_no(row, this.dialog.get_value("serial_no")),
() => this.add_child_for_remaining_qty(row),
() => this.clean_up()
]);

this.dialog.hide();
});

this.dialog.show();

this.$scan_btn = this.dialog.$wrapper.find(".link-btn");
this.$scan_btn.css("display", "inline");
}

get_fields_for_dialog(row, item_code, barcode, batch_no, serial_no) {
let fields = [
{
fieldtype: "Data",
fieldname: "barcode_scanner",
options: "Barcode",
label: __("Scan Barcode"),
onchange: (e) => {
if (!e) {
return;
}

debugger
if (e.target.value) {
this.scan_api_call(e.target.value, (r) => {
if (r.message) {
this.update_dialog_values(item_code, r);
}
})
}
}
},
{
fieldtype: "Section Break",
},
{
fieldtype: "Float",
fieldname: "qty",
label: __("Quantity to Scan"),
default: row[this.qty_field] || 1,
},
{
fieldtype: "Column Break",
fieldname: "column_break_1",
},
{
fieldtype: "Float",
read_only: 1,
fieldname: "scanned_qty",
label: __("Scanned Quantity"),
default: 1,
},
{
fieldtype: "Section Break",
}
]

if (batch_no) {
fields.push({
fieldtype: "Link",
fieldname: "batch_no",
options: "Batch No",
label: __("Batch No"),
default: batch_no,
read_only: 1,
hidden: 1
});
}

if (serial_no) {
fields.push({
fieldtype: "Small Text",
fieldname: "serial_no",
label: __("Serial Nos"),
default: serial_no,
read_only: 1,
});
}

if (barcode) {
fields.push({
fieldtype: "Data",
fieldname: "barcode",
options: "Barcode",
label: __("Barcode"),
default: barcode,
read_only: 1,
hidden: 1
});
}

return fields;
}

update_dialog_values(scanned_item, r) {
const {item_code, barcode, batch_no, serial_no, uom} = r.message;

this.dialog.set_value("barcode_scanner", "");
if (item_code === scanned_item &&
(this.dialog.get_value("barcode") === barcode || batch_no || serial_no)) {

if (batch_no) {
this.dialog.set_value("batch_no", batch_no);
}

if (serial_no) {

this.validate_duplicate_serial_no(serial_no);
let serial_nos = this.dialog.get_value("serial_no") + "\n" + serial_no;
this.dialog.set_value("serial_no", serial_nos);
}

let qty = flt(this.dialog.get_value("scanned_qty")) + 1.0;
this.dialog.set_value("scanned_qty", qty);
}
}

validate_duplicate_serial_no(serial_no) {
let serial_nos = this.dialog.get_value("serial_no") ?
this.dialog.get_value("serial_no").split("\n") : [];

if (in_list(serial_nos, serial_no)) {
frappe.throw(__("Serial No {0} already scanned", [serial_no]));
}
}

add_child_for_remaining_qty(prev_row) {
if (this.remaining_qty && this.remaining_qty >0) {
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
let row = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name);

let ignore_fields = ["name", "idx", "batch_no", "barcode",
"received_qty", "serial_no", "has_item_scanned"];

for (let key in prev_row) {
if (in_list(ignore_fields, key)) {
continue;
}

row[key] = prev_row[key];
}

row[this.qty_field] = this.remaining_qty;
if (this.qty_field == "qty" && frappe.meta.has_field(row.doctype, "stock_qty")) {
row["stock_qty"] = this.remaining_qty * row.conversion_factor;
}

this.frm.script_manager.trigger("item_code", row.doctype, row.name);
}
}

async set_serial_no(row, serial_no) {
if (serial_no && frappe.meta.has_field(row.doctype, this.serial_no_field)) {
const existing_serial_nos = row[this.serial_no_field];
Expand All @@ -164,6 +372,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
}

async set_batch_no(row, batch_no) {
debugger
if (batch_no && frappe.meta.has_field(row.doctype, this.batch_no_field)) {
await frappe.model.set_value(row.doctype, row.name, this.batch_no_field, batch_no);
}
Expand Down Expand Up @@ -193,7 +402,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
return is_duplicate;
}

get_row_to_modify_on_scan(item_code, batch_no, uom) {
get_row_to_modify_on_scan(item_code, batch_no, uom, barcode) {
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;

// Check if batch is scanned and table has batch no field
Expand All @@ -202,12 +411,14 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {

const matching_row = (row) => {
const item_match = row.item_code == item_code;
const batch_match = row[this.batch_no_field] == batch_no;
const batch_match = (!row[this.batch_no_field] || row[this.batch_no_field] == batch_no);
const uom_match = !uom || row[this.uom_field] == uom;
const qty_in_limit = flt(row[this.qty_field]) < flt(row[this.max_qty_field]);
const item_scanned = row.has_item_scanned;

return item_match
&& uom_match
&& !item_scanned
&& (!is_batch_no_scan || batch_match)
&& (!check_max_qty || qty_in_limit)
}
Expand Down

0 comments on commit 1944f4d

Please sign in to comment.