2022-03-27 14:07:23 +00:00
|
|
|
erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
2022-03-27 13:38:15 +00:00
|
|
|
constructor(opts) {
|
2022-03-27 14:43:00 +00:00
|
|
|
this.frm = opts.frm;
|
2022-03-27 13:57:07 +00:00
|
|
|
|
|
|
|
// field from which to capture input of scanned data
|
|
|
|
this.scan_field_name = opts.scan_field_name || "scan_barcode";
|
|
|
|
this.scan_barcode_field = this.frm.fields_dict[this.scan_field_name];
|
2022-03-27 14:07:23 +00:00
|
|
|
|
2022-03-27 14:43:00 +00:00
|
|
|
this.barcode_field = opts.barcode_field || "barcode";
|
|
|
|
this.serial_no_field = opts.serial_no_field || "serial_no";
|
|
|
|
this.batch_no_field = opts.batch_no_field || "batch_no";
|
|
|
|
this.qty_field = opts.qty_field || "qty";
|
2022-05-11 13:15:31 +00:00
|
|
|
// field name on row which defines max quantity to be scanned e.g. picklist
|
|
|
|
this.max_qty_field = opts.max_qty_field;
|
|
|
|
// scanner won't add a new row if this flag is set.
|
2022-05-04 15:06:16 +00:00
|
|
|
this.dont_allow_new_row = opts.dont_allow_new_row;
|
2022-05-11 13:15:31 +00:00
|
|
|
// scanner will ask user to type the quantity instead of incrementing by 1
|
2022-05-05 13:39:19 +00:00
|
|
|
this.prompt_qty = opts.prompt_qty;
|
2022-03-27 14:43:00 +00:00
|
|
|
|
2022-03-27 14:07:23 +00:00
|
|
|
this.items_table_name = opts.items_table_name || "items";
|
|
|
|
this.items_table = this.frm.doc[this.items_table_name];
|
|
|
|
|
2022-03-27 14:38:16 +00:00
|
|
|
// 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
|
|
|
|
// }
|
2022-04-01 09:50:40 +00:00
|
|
|
this.scan_api = opts.scan_api || "erpnext.stock.utils.scan_barcode";
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process_scan() {
|
2022-05-16 07:44:26 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let me = this;
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-16 07:44:26 +00:00
|
|
|
const input = this.scan_barcode_field.value;
|
2022-05-29 16:49:09 +00:00
|
|
|
this.scan_barcode_field.set_value("");
|
2022-05-16 07:44:26 +00:00
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-27 13:57:07 +00:00
|
|
|
|
2022-05-16 07:44:26 +00:00
|
|
|
frappe
|
|
|
|
.call({
|
|
|
|
method: this.scan_api,
|
|
|
|
args: {
|
|
|
|
search_value: input,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((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();
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
me.update_table(data).then(row => {
|
|
|
|
row ? resolve(row) : reject();
|
|
|
|
});
|
2022-05-16 07:44:26 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:51:22 +00:00
|
|
|
update_table(data) {
|
2022-05-29 16:49:09 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
|
|
|
|
|
|
|
|
const {item_code, barcode, batch_no, serial_no} = data;
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
let row = this.get_row_to_modify_on_scan(item_code, batch_no);
|
2022-04-01 10:51:22 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
if (!row) {
|
|
|
|
if (this.dont_allow_new_row) {
|
|
|
|
this.show_alert(__("Maximum quantity scanned for item {0}.", [item_code]), "red");
|
|
|
|
this.clean_up();
|
|
|
|
return;
|
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
// add new row if new item/batch is scanned
|
|
|
|
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.doctype, row.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.is_duplicate_serial_no(row, serial_no)) {
|
2022-04-28 02:07:25 +00:00
|
|
|
this.clean_up();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
frappe.run_serially([
|
|
|
|
() => this.set_selector_trigger_flag(row, data),
|
|
|
|
() => this.set_item(row, item_code).then(qty => {
|
|
|
|
this.show_scan_message(row.idx, row.item_code, qty);
|
|
|
|
}),
|
|
|
|
() => this.set_serial_no(row, serial_no),
|
|
|
|
() => this.set_batch_no(row, batch_no),
|
|
|
|
() => this.set_barcode(row, barcode),
|
|
|
|
() => this.clean_up(),
|
2022-05-31 08:45:13 +00:00
|
|
|
() => this.revert_selector_flag(),
|
2022-05-29 16:49:09 +00:00
|
|
|
() => resolve(row)
|
|
|
|
]);
|
2022-05-05 13:39:19 +00:00
|
|
|
});
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:51:22 +00:00
|
|
|
// batch and serial selector is reduandant when all info can be added by scan
|
|
|
|
// this flag on item row is used by transaction.js to avoid triggering selector
|
|
|
|
set_selector_trigger_flag(row, data) {
|
|
|
|
const {batch_no, serial_no, has_batch_no, has_serial_no} = data;
|
|
|
|
|
|
|
|
const require_selecting_batch = has_batch_no && !batch_no;
|
|
|
|
const require_selecting_serial = has_serial_no && !serial_no;
|
|
|
|
|
|
|
|
if (!(require_selecting_batch || require_selecting_serial)) {
|
2022-05-31 08:44:27 +00:00
|
|
|
frappe.flags.hide_serial_batch_dialog = true;
|
2022-04-01 10:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 08:44:27 +00:00
|
|
|
revert_selector_flag() {
|
|
|
|
frappe.flags.hide_serial_batch_dialog = false;
|
|
|
|
}
|
|
|
|
|
2022-03-27 14:38:16 +00:00
|
|
|
set_item(row, item_code) {
|
2022-05-05 13:39:19 +00:00
|
|
|
return new Promise(resolve => {
|
2022-05-29 16:49:09 +00:00
|
|
|
const increment = async (value = 1) => {
|
2022-05-05 13:39:19 +00:00
|
|
|
const item_data = {item_code: item_code};
|
2022-05-05 14:46:40 +00:00
|
|
|
item_data[this.qty_field] = Number((row[this.qty_field] || 0)) + Number(value);
|
2022-05-29 16:49:09 +00:00
|
|
|
await frappe.model.set_value(row.doctype, row.name, item_data);
|
|
|
|
return value;
|
2022-05-05 13:39:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (this.prompt_qty) {
|
|
|
|
frappe.prompt(__("Please enter quantity for item {0}", [item_code]), ({value}) => {
|
2022-05-29 16:49:09 +00:00
|
|
|
increment(value).then((value) => resolve(value));
|
2022-05-05 13:39:19 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-05-29 16:49:09 +00:00
|
|
|
increment().then((value) => resolve(value));
|
2022-05-05 13:39:19 +00:00
|
|
|
}
|
|
|
|
});
|
2022-03-27 14:38:16 +00:00
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
async set_serial_no(row, serial_no) {
|
2022-03-27 14:43:00 +00:00
|
|
|
if (serial_no && frappe.meta.has_field(row.doctype, this.serial_no_field)) {
|
2022-03-27 14:56:55 +00:00
|
|
|
const existing_serial_nos = row[this.serial_no_field];
|
2022-03-27 15:07:39 +00:00
|
|
|
let new_serial_nos = "";
|
2022-03-27 14:56:55 +00:00
|
|
|
|
|
|
|
if (!!existing_serial_nos) {
|
2022-03-27 15:07:39 +00:00
|
|
|
new_serial_nos = existing_serial_nos + "\n" + serial_no;
|
2022-03-27 14:56:55 +00:00
|
|
|
} else {
|
|
|
|
new_serial_nos = serial_no;
|
|
|
|
}
|
2022-05-29 16:49:09 +00:00
|
|
|
await frappe.model.set_value(row.doctype, row.name, this.serial_no_field, new_serial_nos);
|
2022-03-27 14:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
async set_batch_no(row, batch_no) {
|
2022-03-27 14:43:00 +00:00
|
|
|
if (batch_no && frappe.meta.has_field(row.doctype, this.batch_no_field)) {
|
2022-05-29 16:49:09 +00:00
|
|
|
await frappe.model.set_value(row.doctype, row.name, this.batch_no_field, batch_no);
|
2022-03-27 14:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-29 16:49:09 +00:00
|
|
|
async set_barcode(row, barcode) {
|
2022-03-27 14:43:00 +00:00
|
|
|
if (barcode && frappe.meta.has_field(row.doctype, this.barcode_field)) {
|
2022-05-29 16:49:09 +00:00
|
|
|
await frappe.model.set_value(row.doctype, row.name, this.barcode_field, barcode);
|
2022-03-27 14:38:16 +00:00
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 13:39:19 +00:00
|
|
|
show_scan_message(idx, exist = null, qty = 1) {
|
2022-03-27 13:38:15 +00:00
|
|
|
// show new row or qty increase toast
|
|
|
|
if (exist) {
|
2022-05-11 13:22:14 +00:00
|
|
|
this.show_alert(__("Row #{0}: Qty increased by {1}", [idx, qty]), "green");
|
2022-03-27 13:38:15 +00:00
|
|
|
} else {
|
2022-05-11 13:22:14 +00:00
|
|
|
this.show_alert(__("Row #{0}: Item added", [idx]), "green")
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is_duplicate_serial_no(row, serial_no) {
|
2022-05-11 13:22:14 +00:00
|
|
|
const is_duplicate = row[this.serial_no_field]?.includes(serial_no);
|
2022-03-27 13:38:15 +00:00
|
|
|
|
|
|
|
if (is_duplicate) {
|
2022-05-11 13:22:14 +00:00
|
|
|
this.show_alert(__("Serial No {0} is already added", [serial_no]), "orange");
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
|
|
|
return is_duplicate;
|
|
|
|
}
|
|
|
|
|
2022-05-11 13:57:16 +00:00
|
|
|
get_row_to_modify_on_scan(item_code, batch_no) {
|
|
|
|
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
|
|
|
|
|
|
|
|
// Check if batch is scanned and table has batch no field
|
|
|
|
let is_batch_no_scan = batch_no && frappe.meta.has_field(cur_grid.doctype, this.batch_no_field);
|
|
|
|
let check_max_qty = this.max_qty_field && frappe.meta.has_field(cur_grid.doctype, this.max_qty_field);
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-05-11 13:57:16 +00:00
|
|
|
const matching_row = (row) => {
|
|
|
|
const item_match = row.item_code == item_code;
|
|
|
|
const batch_match = row.batch_no == batch_no;
|
|
|
|
const qty_in_limit = flt(row[this.qty_field]) < flt(row[this.max_qty_field]);
|
|
|
|
|
|
|
|
return item_match
|
|
|
|
&& (!is_batch_no_scan || batch_match)
|
|
|
|
&& (!check_max_qty || qty_in_limit)
|
|
|
|
}
|
2022-04-28 02:03:39 +00:00
|
|
|
|
2022-05-11 13:57:16 +00:00
|
|
|
return this.items_table.find(matching_row) || this.get_existing_blank_row();
|
2022-04-07 16:08:18 +00:00
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
|
2022-04-07 16:08:18 +00:00
|
|
|
get_existing_blank_row() {
|
|
|
|
return this.items_table.find((d) => !d.item_code);
|
2022-03-27 13:38:15 +00:00
|
|
|
}
|
2022-03-27 13:43:16 +00:00
|
|
|
|
|
|
|
clean_up() {
|
|
|
|
this.scan_barcode_field.set_value("");
|
2022-03-27 14:07:23 +00:00
|
|
|
refresh_field(this.items_table_name);
|
2022-03-27 13:43:16 +00:00
|
|
|
}
|
2022-05-11 13:22:14 +00:00
|
|
|
show_alert(msg, indicator, duration=3) {
|
|
|
|
frappe.show_alert({message: msg, indicator: indicator}, duration);
|
|
|
|
}
|
2022-03-27 13:38:15 +00:00
|
|
|
};
|