Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add item using serial number from search term in pos #45176

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 83 additions & 3 deletions erpnext/selling/page/point_of_sale/pos_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,20 +556,100 @@ erpnext.PointOfSale.Controller = class {
item_row = this.get_item_from_frm(item);
const item_row_exists = !$.isEmptyObject(item_row);

const from_selector = field === "qty" && value === "+1";
if (from_selector) value = flt(item_row.qty) + flt(value);
const from_selector = (field === "qty" && value === "+1") || field === "serial_no";
if (from_selector) {
if (field === "qty") {
value = flt(item_row.qty) + flt(value);
}
}

if (item_row_exists) {
if (item_row.serial_and_batch_bundle) {
const r = await frappe.db.get_value("Item", item_row.item_code, "has_serial_no");
if (r.message.has_serial_no) {
frappe.msgprint({
title: __("Serial and Batch Bundle already created."),
indicator: "orange",
message: __(
"Serial and Batch Bundle for the item already created. Remove the item from the cart and add again."
),
});
return;
}
}

if (field === "qty") value = flt(value);

if (field === "serial_no") {
if (value.constructor !== Array) {
await this.check_serial_no_availablilty(
item.item_code,
this.frm.doc.set_warehouse,
item.serial_no
);

if (item_row.serial_no.split("\n").includes(value)) {
frappe.msgprint({
title: __("Duplicate Serial No"),
message: __("Serial No is already added to the cart."),
indicator: "orange",
});
return;
}
value = `${item_row.serial_no}\n${value}`;
}

if (value.constructor === Array) {
let serial_nos = [];
for (let serial_no of value) {
await this.check_serial_no_availablilty(
item.item_code,
this.frm.doc.set_warehouse,
serial_no
);
serial_nos.push(serial_no);
}
value = serial_nos;
}
}

if (["qty", "conversion_factor"].includes(field) && value > 0 && !this.allow_negative_stock) {
const qty_needed =
field === "qty" ? value * item_row.conversion_factor : item_row.qty * value;
await this.check_stock_availability(item_row, qty_needed, this.frm.doc.set_warehouse);
}

if (this.is_current_item_being_edited(item_row) || from_selector) {
await frappe.model.set_value(item_row.doctype, item_row.name, field, value);
if (field !== "serial_no") {
await frappe.model.set_value(item_row.doctype, item_row.name, field, value);
}

if (field === "serial_no") {
if (value.constructor !== Array) {
await frappe.model.set_value(item_row.doctype, item_row.name, field, value);
await frappe.model.set_value(
item_row.doctype,
item_row.name,
"qty",
item_row.qty + 1
);
}

if (value.constructor === Array) {
await frappe.model.set_value(
item_row.doctype,
item_row.name,
field,
value.join("\n")
);
await frappe.model.set_value(
item_row.doctype,
item_row.name,
"qty",
value.length
);
}
}
this.update_cart_html(item_row);
}
} else {
Expand Down
35 changes: 19 additions & 16 deletions erpnext/selling/page/point_of_sale/pos_item_details.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,23 @@ erpnext.PointOfSale.ItemDetails = class {
}

make_auto_serial_selection_btn(item) {
if (item.has_serial_no || item.has_batch_no) {
if (item.has_serial_no && item.has_batch_no) {
this.$form_container.append(
`<div class="btn btn-sm btn-secondary auto-fetch-btn" style="margin-top: 6px">${__(
"Select Serial No / Batch No"
)}</div>`
if ((item.has_serial_no || item.has_batch_no) && !(item.has_serial_no && item.has_batch_no)) {
const classname = item.has_serial_no ? ".serial_no-control" : ".batch_no-control";
const label = item.has_serial_no ? __("Select Serial No") : __("Select Batch No");
this.$form_container
.find(classname)
.append(
`<div class="btn btn-sm btn-secondary auto-fetch-btn" style="margin-top: 6px">${label}</div>`
);
} else {
const classname = item.has_serial_no ? ".serial_no-control" : ".batch_no-control";
const label = item.has_serial_no ? __("Select Serial No") : __("Select Batch No");
this.$form_container
.find(classname)
.append(
`<div class="btn btn-sm btn-secondary auto-fetch-btn" style="margin-top: 6px">${label}</div>`
);
}
}

if (item.has_serial_no) {
this.$form_container.find(".serial_no-control").find("textarea").css("height", "6rem");
}

if (item.has_serial_no && item.has_batch_no) {
this.batch_no_control.df.read_only = 1;
}
}

bind_custom_control_change_event() {
Expand Down Expand Up @@ -293,7 +292,10 @@ erpnext.PointOfSale.ItemDetails = class {
this.serial_no_control.df.reqd = 1;
this.serial_no_control.df.onchange = async function () {
!me.current_item.batch_no && (await me.auto_update_batch_no());
me.events.form_updated(me.current_item, "serial_no", this.value);
let serial_nos = [];
this.value.split("\n").forEach((serial_no) => serial_nos.push(serial_no.trim()));
serial_nos = [...new Set(serial_nos)];
me.events.form_updated(me.current_item, "serial_no", serial_nos);
};
this.serial_no_control.refresh();
}
Expand Down Expand Up @@ -414,6 +416,7 @@ erpnext.PointOfSale.ItemDetails = class {
let item_row = this.item_row;
item_row.type_of_transaction = "Outward";

item_row.parent = frm.doc.name;
new erpnext.SerialBatchPackageSelector(frm, item_row, (r) => {
if (r) {
frappe.model.set_value(item_row.doctype, item_row.name, {
Expand Down
10 changes: 10 additions & 0 deletions erpnext/selling/page/point_of_sale/pos_item_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ erpnext.PointOfSale.ItemSelector = class {
rate = rate === "undefined" ? undefined : rate;
stock_uom = stock_uom === "undefined" ? undefined : stock_uom;

if (serial_no) {
me.events.item_selected({
field: "serial_no",
value: serial_no,
item: { item_code, batch_no, serial_no, uom, rate, stock_uom },
});
me.search_field.set_focus();
return;
}

me.events.item_selected({
field: "qty",
value: "+1",
Expand Down
Loading