refactor: dont hardcode child table name

This commit is contained in:
Ankush Menat 2022-03-27 20:13:00 +05:30 committed by Ankush Menat
parent 3a8656b3c8
commit b3c604d679

View File

@ -1,11 +1,16 @@
erpnext.utils.BarcodeScanner = class BarcodeScanner { erpnext.utils.BarcodeScanner = class BarcodeScanner {
constructor(opts) { constructor(opts) {
$.extend(this, opts); this.frm = opts.frm;
// field from which to capture input of scanned data // field from which to capture input of scanned data
this.scan_field_name = opts.scan_field_name || "scan_barcode"; this.scan_field_name = opts.scan_field_name || "scan_barcode";
this.scan_barcode_field = this.frm.fields_dict[this.scan_field_name]; this.scan_barcode_field = this.frm.fields_dict[this.scan_field_name];
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";
this.items_table_name = opts.items_table_name || "items"; this.items_table_name = opts.items_table_name || "items";
this.items_table = this.frm.doc[this.items_table_name]; this.items_table = this.frm.doc[this.items_table_name];
@ -51,7 +56,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
let row = null; let row = null;
// Check if batch is scanned and table has batch no field // Check if batch is scanned and table has batch no field
let batch_no_scan = Boolean(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, this.batch_no_field);
if (batch_no_scan) { if (batch_no_scan) {
row = this.get_batch_row_to_modify(batch_no); row = this.get_batch_row_to_modify(batch_no);
@ -81,24 +86,21 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
} }
set_item(row, item_code) { set_item(row, item_code) {
frappe.model.set_value(row.doctype, row.name, { const item_data = {item_code: item_code}
item_code: item_code, item_data[this.qty_field] = (row[this.qty_field] || 0) + 1;
qty: (row.qty || 0) + 1, // TODO: harcoded fieldname frappe.model.set_value(row.doctype, row.name, item_data);
});
} }
set_serial_no(row, serial_no) { set_serial_no(row, serial_no) {
if (serial_no && frappe.meta.has_field(row.doctype, "serial_no")) { if (serial_no && frappe.meta.has_field(row.doctype, this.serial_no_field)) {
// TODO: fieldname hardcoded const value = row[this.serial_no_field] + '\n' + serial_no;
const value = row["serial_no"] + '\n' + serial_no; frappe.model.set_value(row.doctype, row.name, this.serial_no_field, value);
frappe.model.set_value(row.doctype, row.name, "serial_no", value);
} }
} }
set_batch_no(row, batch_no) { set_batch_no(row, batch_no) {
if (batch_no && frappe.meta.has_field(row.doctype, "batch_no")) { if (batch_no && frappe.meta.has_field(row.doctype, this.batch_no_field)) {
// TODO: fieldname hardcoded frappe.model.set_value(row.doctype, row.name, this.batch_no_field, batch_no);
frappe.model.set_value(row.doctype, row.name, "batch_no", batch_no);
} }
} }
@ -106,9 +108,9 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
// increase qty and set scanned value and item in row // increase qty and set scanned value and item in row
// XXX: tightly coupled global flag on frm object used in transaction.js -_- // 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; this.frm.from_barcode = this.frm.from_barcode ? this.frm.from_barcode + 1 : 1;
if (barcode && frappe.meta.has_field(row.doctype, "barcode")) {
// TODO: fieldname hardcoded if (barcode && frappe.meta.has_field(row.doctype, this.barcode_field)) {
frappe.model.set_value(row.doctype, row.name, "barcode", barcode); frappe.model.set_value(row.doctype, row.name, this.barcode_field, barcode);
} }
} }