Skip to content

Commit

Permalink
Merge pull request frappe#27715 from frappe/mergify/bp/version-13-hot…
Browse files Browse the repository at this point in the history
…fix/pr-27668

fix: Batch scans get overwritten on the same row (backport frappe#27668)
  • Loading branch information
mergify[bot] committed Sep 30, 2021
1 parent 5d1de91 commit 15c9c08
Showing 1 changed file with 81 additions and 46 deletions.
127 changes: 81 additions & 46 deletions erpnext/public/js/controllers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,26 +345,14 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
},

scan_barcode: function() {
let scan_barcode_field = this.frm.fields_dict["scan_barcode"];

let show_description = function(idx, exist = null) {
if (exist) {
frappe.show_alert({
message: __('Row #{0}: Qty increased by 1', [idx]),
indicator: 'green'
});
} else {
frappe.show_alert({
message: __('Row #{0}: Item added', [idx]),
indicator: 'green'
});
}
}
let me = this;

if(this.frm.doc.scan_barcode) {
frappe.call({
method: "erpnext.selling.page.point_of_sale.point_of_sale.search_for_serial_or_batch_or_barcode_number",
args: { search_value: this.frm.doc.scan_barcode }
args: {
search_value: this.frm.doc.scan_barcode
}
}).then(r => {
const data = r && r.message;
if (!data || Object.keys(data).length === 0) {
Expand All @@ -375,50 +363,97 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
return;
}

let cur_grid = this.frm.fields_dict.items.grid;
me.modify_table_after_scan(data);
});
}
return false;
},

modify_table_after_scan(data) {
let scan_barcode_field = this.frm.fields_dict["scan_barcode"];
let cur_grid = this.frm.fields_dict.items.grid;
let row_to_modify = null;

let row_to_modify = null;
const existing_item_row = this.frm.doc.items.find(d => d.item_code === data.item_code);
const blank_item_row = this.frm.doc.items.find(d => !d.item_code);
// 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");

if (existing_item_row) {
row_to_modify = existing_item_row;
} else if (blank_item_row) {
row_to_modify = blank_item_row;
}
if (batch_no_scan) {
row_to_modify = this.get_batch_row_to_modify(data.batch_no);
} else {
// serial or barcode scan
row_to_modify = this.get_row_to_modify_on_scan(row_to_modify, data);
}

if (!row_to_modify) {
// add new row if new item/batch is scanned
row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, 'items');
}

this.show_scan_message(row_to_modify.idx, row_to_modify.item_code);
this.set_scanned_values(row_to_modify, data, scan_barcode_field);
},

set_scanned_values(row_to_modify, data, scan_barcode_field) {
// increase qty and set scanned value and item in row
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 (!row_to_modify) {
// add new row
row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, 'items');
if (is_serial_no) {
value = row_to_modify[field] + '\n' + data[field];
}

show_description(row_to_modify.idx, row_to_modify.item_code);
frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, field, value);
}
});

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
});
scan_barcode_field.set_value('');
refresh_field("items");
},

['serial_no', 'batch_no', 'barcode'].forEach(field => {
if (data[field] && frappe.meta.has_field(row_to_modify.doctype, field)) {
get_row_to_modify_on_scan(row_to_modify, data) {
// get an existing item row to increment or blank row to modify
const existing_item_row = this.frm.doc.items.find(d => d.item_code === data.item_code);
const blank_item_row = this.frm.doc.items.find(d => !d.item_code);

let value = (row_to_modify[field] && field === "serial_no")
? row_to_modify[field] + '\n' + data[field] : data[field];
if (existing_item_row) {
row_to_modify = existing_item_row;
} else if (blank_item_row) {
row_to_modify = blank_item_row;
}

frappe.model.set_value(row_to_modify.doctype,
row_to_modify.name, field, value);
}
});
return row_to_modify;
},

scan_barcode_field.set_value('');
refresh_field("items");
get_batch_row_to_modify(batch_no) {
// get row if batch already exists in table
const existing_batch_row = this.frm.doc.items.find(d => d.batch_no === batch_no);
return existing_batch_row || null;
},

show_scan_message (idx, exist = null) {
// show new row or qty increase toast
if (exist) {
frappe.show_alert({
message: __('Row #{0}: Qty increased by 1', [idx]),
indicator: 'green'
});
} else {
frappe.show_alert({
message: __('Row #{0}: Item added', [idx]),
indicator: 'green'
});
}
return false;
},

apply_default_taxes: function() {
apply_default_taxes() {
var me = this;
var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
me.frm.doc.name);
Expand Down

0 comments on commit 15c9c08

Please sign in to comment.