mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-25 04:53:01 +00:00
Co-authored-by: Sagar Vora <16315650+sagarvora@users.noreply.github.com> Co-authored-by: Soni Karm <93865733+karm1000@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
|
||||
setup() {
|
||||
super.setup();
|
||||
let me = this;
|
||||
this.barcode_scanner = new erpnext.utils.BarcodeScanner({ frm: this.frm });
|
||||
|
||||
this.set_fields_onload_for_line_item();
|
||||
this.frm.ignore_doctypes_on_cancel_all = ["Serial and Batch Bundle"];
|
||||
@@ -473,8 +474,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
|
||||
|
||||
scan_barcode() {
|
||||
frappe.flags.dialog_set = false;
|
||||
const barcode_scanner = new erpnext.utils.BarcodeScanner({frm:this.frm});
|
||||
barcode_scanner.process_scan();
|
||||
this.barcode_scanner.process_scan();
|
||||
}
|
||||
|
||||
barcode(doc, cdt, cdn) {
|
||||
|
||||
@@ -12,6 +12,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
this.batch_no_field = opts.batch_no_field || "batch_no";
|
||||
this.uom_field = opts.uom_field || "uom";
|
||||
this.qty_field = opts.qty_field || "qty";
|
||||
this.warehouse_field = opts.warehouse_field || "warehouse";
|
||||
// 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.
|
||||
@@ -20,7 +21,6 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
this.prompt_qty = opts.prompt_qty;
|
||||
|
||||
this.items_table_name = opts.items_table_name || "items";
|
||||
this.items_table = this.frm.doc[this.items_table_name];
|
||||
|
||||
// optional sound name to play when scan either fails or passes.
|
||||
// see https://frappeframework.com/docs/v14/user/en/python-api/hooks#sounds
|
||||
@@ -34,8 +34,10 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
// batch_no: "LOT12", // present if batch was scanned
|
||||
// serial_no: "987XYZ", // present if serial no was scanned
|
||||
// uom: "Kg", // present if barcode UOM is different from default
|
||||
// warehouse: "Store-001", // present if warehouse was found (location-first scanning)
|
||||
// }
|
||||
this.scan_api = opts.scan_api || "erpnext.stock.utils.scan_barcode";
|
||||
this.has_last_scanned_warehouse = frappe.meta.has_field(this.frm.doctype, "last_scanned_warehouse");
|
||||
}
|
||||
|
||||
process_scan() {
|
||||
@@ -50,14 +52,31 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
|
||||
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");
|
||||
if (
|
||||
!data ||
|
||||
Object.keys(data).length === 0 ||
|
||||
(data.warehouse && !this.has_last_scanned_warehouse)
|
||||
) {
|
||||
this.show_alert(
|
||||
this.has_last_scanned_warehouse
|
||||
? __("Cannot find Item or Warehouse with this Barcode")
|
||||
: __("Cannot find Item with this Barcode"),
|
||||
"red"
|
||||
);
|
||||
this.clean_up();
|
||||
this.play_fail_sound();
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle warehouse scanning
|
||||
if (data.warehouse) {
|
||||
this.handle_warehouse_scan(data);
|
||||
this.play_success_sound();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
me.update_table(data)
|
||||
.then((row) => {
|
||||
this.play_success_sound();
|
||||
@@ -77,6 +96,10 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
method: this.scan_api,
|
||||
args: {
|
||||
search_value: input,
|
||||
ctx: {
|
||||
set_warehouse: this.frm.doc.set_warehouse,
|
||||
company: this.frm.doc.company,
|
||||
},
|
||||
},
|
||||
})
|
||||
.then((r) => {
|
||||
@@ -89,11 +112,14 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
|
||||
frappe.flags.trigger_from_barcode_scanner = true;
|
||||
|
||||
const { item_code, barcode, batch_no, serial_no, uom } = data;
|
||||
const { item_code, barcode, batch_no, serial_no, uom, default_warehouse } = data;
|
||||
|
||||
let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom, barcode);
|
||||
const warehouse = this.has_last_scanned_warehouse
|
||||
? this.frm.doc.last_scanned_warehouse || default_warehouse
|
||||
: null;
|
||||
|
||||
this.is_new_row = false;
|
||||
let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom, barcode, warehouse);
|
||||
const is_new_row = !row?.item_code;
|
||||
if (!row) {
|
||||
if (this.dont_allow_new_row) {
|
||||
this.show_alert(__("Maximum quantity scanned for item {0}.", [item_code]), "red");
|
||||
@@ -101,7 +127,6 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
reject();
|
||||
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);
|
||||
@@ -120,12 +145,13 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
() => this.set_selector_trigger_flag(data),
|
||||
() =>
|
||||
this.set_item(row, item_code, barcode, batch_no, serial_no).then((qty) => {
|
||||
this.show_scan_message(row.idx, row.item_code, qty);
|
||||
this.show_scan_message(row.idx, !is_new_row, qty);
|
||||
}),
|
||||
() => this.set_barcode_uom(row, uom),
|
||||
() => this.set_serial_no(row, serial_no),
|
||||
() => this.set_batch_no(row, batch_no),
|
||||
() => this.set_barcode(row, barcode),
|
||||
() => this.set_warehouse(row, warehouse),
|
||||
() => this.clean_up(),
|
||||
() => this.revert_selector_flag(),
|
||||
() => resolve(row),
|
||||
@@ -386,9 +412,17 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
}
|
||||
}
|
||||
|
||||
show_scan_message(idx, exist = null, qty = 1) {
|
||||
async set_warehouse(row, warehouse) {
|
||||
const warehouse_field = this.get_warehouse_field();
|
||||
|
||||
if (warehouse && frappe.meta.has_field(row.doctype, warehouse_field)) {
|
||||
await frappe.model.set_value(row.doctype, row.name, warehouse_field, warehouse);
|
||||
}
|
||||
}
|
||||
|
||||
show_scan_message(idx, is_existing_row = false, qty = 1) {
|
||||
// show new row or qty increase toast
|
||||
if (exist) {
|
||||
if (is_existing_row) {
|
||||
this.show_alert(__("Row #{0}: Qty increased by {1}", [idx, qty]), "green");
|
||||
} else {
|
||||
this.show_alert(__("Row #{0}: Item added", [idx]), "green");
|
||||
@@ -404,13 +438,16 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
return is_duplicate;
|
||||
}
|
||||
|
||||
get_row_to_modify_on_scan(item_code, batch_no, uom, barcode) {
|
||||
get_row_to_modify_on_scan(item_code, batch_no, uom, barcode, warehouse) {
|
||||
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);
|
||||
|
||||
const warehouse_field = this.get_warehouse_field();
|
||||
let has_warehouse_field = frappe.meta.has_field(cur_grid.doctype, warehouse_field);
|
||||
|
||||
const matching_row = (row) => {
|
||||
const item_match = row.item_code == item_code;
|
||||
const batch_match = !row[this.batch_no_field] || row[this.batch_no_field] == batch_no;
|
||||
@@ -418,20 +455,94 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
||||
const qty_in_limit = flt(row[this.qty_field]) < flt(row[this.max_qty_field]);
|
||||
const item_scanned = row.has_item_scanned;
|
||||
|
||||
let warehouse_match = true;
|
||||
if (has_warehouse_field) {
|
||||
if (warehouse) {
|
||||
warehouse_match = row[warehouse_field] === warehouse;
|
||||
} else {
|
||||
warehouse_match = !row[warehouse_field];
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
item_match &&
|
||||
uom_match &&
|
||||
warehouse_match &&
|
||||
!item_scanned &&
|
||||
(!is_batch_no_scan || batch_match) &&
|
||||
(!check_max_qty || qty_in_limit)
|
||||
);
|
||||
};
|
||||
|
||||
return this.items_table.find(matching_row) || this.get_existing_blank_row();
|
||||
const items_table = this.frm.doc[this.items_table_name] || [];
|
||||
|
||||
return items_table.find(matching_row) || items_table.find((d) => !d.item_code);
|
||||
}
|
||||
|
||||
get_existing_blank_row() {
|
||||
return this.items_table.find((d) => !d.item_code);
|
||||
setup_last_scanned_warehouse() {
|
||||
this.frm.set_df_property("last_scanned_warehouse", "options", "Warehouse");
|
||||
this.frm.set_df_property("last_scanned_warehouse", "fieldtype", "Link");
|
||||
this.frm.set_df_property("last_scanned_warehouse", "formatter", function (value, df, options, doc) {
|
||||
const link_formatter = frappe.form.get_formatter(df.fieldtype);
|
||||
const link_value = link_formatter(value, df, options, doc);
|
||||
|
||||
if (!value) {
|
||||
return link_value;
|
||||
}
|
||||
|
||||
const clear_btn = `
|
||||
<a class="btn-clear-last-scanned-warehouse" title="${__("Clear Last Scanned Warehouse")}">
|
||||
${frappe.utils.icon("close", "xs", "es-icon")}
|
||||
</a>
|
||||
`;
|
||||
|
||||
return link_value + clear_btn;
|
||||
});
|
||||
|
||||
this.frm.$wrapper.on("click", ".btn-clear-last-scanned-warehouse", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.clear_warehouse_context();
|
||||
});
|
||||
}
|
||||
|
||||
handle_warehouse_scan(data) {
|
||||
const warehouse = data.warehouse;
|
||||
const warehouse_field = this.get_warehouse_field();
|
||||
const warehouse_field_label = frappe.meta.get_label(this.items_table_name, warehouse_field);
|
||||
|
||||
if (!this.last_scanned_warehouse_initialized) {
|
||||
this.setup_last_scanned_warehouse();
|
||||
this.last_scanned_warehouse_initialized = true;
|
||||
}
|
||||
|
||||
this.frm.set_value("last_scanned_warehouse", warehouse);
|
||||
this.show_alert(
|
||||
__("{0} will be set as the {1} in subsequently scanned items", [
|
||||
__(warehouse).bold(),
|
||||
__(warehouse_field_label).bold(),
|
||||
]),
|
||||
"green",
|
||||
6
|
||||
);
|
||||
}
|
||||
|
||||
clear_warehouse_context() {
|
||||
this.frm.set_value("last_scanned_warehouse", null);
|
||||
this.show_alert(
|
||||
__(
|
||||
"The last scanned warehouse has been cleared and won't be set in the subsequently scanned items"
|
||||
),
|
||||
"blue",
|
||||
6
|
||||
);
|
||||
}
|
||||
|
||||
get_warehouse_field() {
|
||||
if (typeof this.warehouse_field === "function") {
|
||||
return this.warehouse_field(this.frm.doc);
|
||||
}
|
||||
return this.warehouse_field;
|
||||
}
|
||||
|
||||
play_success_sound() {
|
||||
|
||||
@@ -593,3 +593,11 @@ body[data-route="pos"] {
|
||||
.frappe-control[data-fieldname="other_charges_calculation"] .ql-editor {
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
.btn-clear-last-scanned-warehouse {
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user