mirror of
https://github.com/frappe/erpnext.git
synced 2026-10-29 22:57:42 +00:00
fix(stock): ask about existing drafts before the pick list creates the delivery note
the pick list mapper saves the delivery note on the server, so the draft warning ran after the draft already existed, listed the one just created, and answering no left it behind. ask before calling the mapper, and let the late guard pass for a target the mapper already saved.
(cherry picked from commit 862d5f4371)
# Conflicts:
# erpnext/public/js/utils/draft_link_guard.js
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
frappe.provide("erpnext.utils");
|
||||
|
||||
// Warns before creating a follow-up document (e.g. Delivery Note from Sales Order)
|
||||
// when a draft of the target DocType already exists for the same source document.
|
||||
|
||||
erpnext.utils.confirm_if_drafts_exist = async function (source_doc, target_doctype) {
|
||||
// resolves true to proceed; fails open so a broken check never blocks creation
|
||||
if (!source_doc || !source_doc.name || source_doc.__islocal) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let drafts;
|
||||
try {
|
||||
drafts = await frappe.xcall("erpnext.controllers.draft_links.get_existing_drafts", {
|
||||
source_doctype: source_doc.doctype,
|
||||
source_name: source_doc.name,
|
||||
target_doctype: target_doctype,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!drafts.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
frappe.confirm(
|
||||
get_draft_warning(source_doc, target_doctype, drafts),
|
||||
() => resolve(true),
|
||||
() => resolve(false)
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
if (frappe.model.add_mapped_doc_guard) {
|
||||
frappe.model.add_mapped_doc_guard((mapped_doc, opts) => {
|
||||
// a mapper that saved the target already created the draft, so ask before calling it instead
|
||||
if (mapped_doc.name && !mapped_doc.__islocal) {
|
||||
return true;
|
||||
}
|
||||
return erpnext.utils.confirm_if_drafts_exist(opts.frm && opts.frm.doc, mapped_doc.doctype);
|
||||
});
|
||||
}
|
||||
|
||||
function get_draft_warning(source_doc, target_doctype, drafts) {
|
||||
const links = drafts.map((name) => frappe.utils.get_form_link(target_doctype, name, true)).join(", ");
|
||||
|
||||
if (drafts.length === 1) {
|
||||
return __("A draft {0} already exists for this {1}: {2}. Do you still want to create a new one?", [
|
||||
__(target_doctype),
|
||||
__(source_doc.doctype),
|
||||
links,
|
||||
]);
|
||||
}
|
||||
return __(
|
||||
"{0} draft {1} documents already exist for this {2}: {3}. Do you still want to create a new one?",
|
||||
[drafts.length, __(target_doctype), __(source_doc.doctype), links]
|
||||
);
|
||||
}
|
||||
@@ -237,7 +237,11 @@ frappe.ui.form.on("Pick List", {
|
||||
frm.clear_table("locations");
|
||||
frm.trigger("add_get_items_button");
|
||||
},
|
||||
create_delivery(frm, doctype) {
|
||||
async create_delivery(frm, doctype) {
|
||||
if (!(await erpnext.utils.confirm_if_drafts_exist(frm.doc, doctype))) {
|
||||
return;
|
||||
}
|
||||
|
||||
frappe.model.open_mapped_doc({
|
||||
method: "erpnext.stock.doctype.pick_list.pick_list.create_delivery",
|
||||
args: {
|
||||
|
||||
Reference in New Issue
Block a user