Multiselect dialog for getting items (#8583)
* Concatenate docnames and send to mapper * Multiselect with checkboxes * set setters as separate filters * Map filter fields to target_doc * Get items from quotation (in SO) working * [major] Set dialog setters for all forms * Add date field * Specify non-default date_fields * [minor] add test_mapper.py * [minor] remove cur_frm * [minor][fix] test * [minor] fix test with make_test_records
This commit is contained in:
committed by
Rushabh Mehta
parent
62e3e75555
commit
edeb4dc7e0
@@ -114,63 +114,68 @@ erpnext.utils.map_current_doc = function(opts) {
|
||||
}
|
||||
}
|
||||
var _map = function() {
|
||||
// remove first item row if empty
|
||||
if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
|
||||
// remove first item row if empty
|
||||
if(!cur_frm.doc.items[0].item_code) {
|
||||
cur_frm.doc.items = cur_frm.doc.items.splice(1);
|
||||
}
|
||||
|
||||
// find the doctype of the items table
|
||||
var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
|
||||
|
||||
|
||||
// find the link fieldname from items table for the given
|
||||
// source_doctype
|
||||
var link_fieldname = null;
|
||||
frappe.get_meta(items_doctype).fields.forEach(function(d) {
|
||||
frappe.get_meta(items_doctype).fields.forEach(function(d) {
|
||||
if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
|
||||
|
||||
// search in existing items if the source_name is already set and full qty fetched
|
||||
var already_set = false;
|
||||
var item_qty_map = {};
|
||||
|
||||
$.each(cur_frm.doc.items, function(i, d) {
|
||||
if(d[link_fieldname]==opts.source_name) {
|
||||
already_set = true;
|
||||
if (item_qty_map[d.item_code])
|
||||
item_qty_map[d.item_code] += flt(d.qty);
|
||||
else
|
||||
item_qty_map[d.item_code] = flt(d.qty);
|
||||
}
|
||||
});
|
||||
|
||||
if(already_set) {
|
||||
frappe.model.with_doc(opts.source_doctype, opts.source_name, function(r) {
|
||||
var source_doc = frappe.model.get_doc(opts.source_doctype, opts.source_name);
|
||||
$.each(source_doc.items || [], function(i, row) {
|
||||
if(row.qty > flt(item_qty_map[row.item_code])) {
|
||||
already_set = false;
|
||||
return false;
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if(already_set) {
|
||||
frappe.msgprint(__("You have already selected items from {0} {1}",
|
||||
[opts.source_doctype, opts.source_name]));
|
||||
return;
|
||||
}
|
||||
$.each(cur_frm.doc.items, function(i, d) {
|
||||
opts.source_name.forEach(function(src) {
|
||||
if(d[link_fieldname]==src) {
|
||||
already_set = true;
|
||||
if (item_qty_map[d.item_code])
|
||||
item_qty_map[d.item_code] += flt(d.qty);
|
||||
else
|
||||
item_qty_map[d.item_code] = flt(d.qty);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if(already_set) {
|
||||
opts.source_name.forEach(function(src) {
|
||||
frappe.model.with_doc(opts.source_doctype, src, function(r) {
|
||||
var source_doc = frappe.model.get_doc(opts.source_doctype, src);
|
||||
$.each(source_doc.items || [], function(i, row) {
|
||||
if(row.qty > flt(item_qty_map[row.item_code])) {
|
||||
already_set = false;
|
||||
return false;
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if(already_set) {
|
||||
frappe.msgprint(__("You have already selected items from {0} {1}",
|
||||
[opts.source_doctype, src]));
|
||||
return;
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return frappe.call({
|
||||
// Sometimes we hit the limit for URL length of a GET request
|
||||
// as we send the full target_doc. Hence this is a POST request.
|
||||
type: "POST",
|
||||
method: opts.method,
|
||||
method: 'frappe.model.mapper.map_docs',
|
||||
args: {
|
||||
"source_name": opts.source_name,
|
||||
"target_doc": cur_frm.doc
|
||||
"method": opts.method,
|
||||
"source_names": opts.source_name,
|
||||
"target_doc": cur_frm.doc,
|
||||
},
|
||||
callback: function(r) {
|
||||
if(!r.exc) {
|
||||
@@ -181,29 +186,26 @@ erpnext.utils.map_current_doc = function(opts) {
|
||||
});
|
||||
}
|
||||
if(opts.source_doctype) {
|
||||
var d = new frappe.ui.Dialog({
|
||||
title: __("Get From ") + __(opts.source_doctype),
|
||||
fields: [
|
||||
{
|
||||
fieldtype: "Link",
|
||||
label: __(opts.source_doctype),
|
||||
fieldname: opts.source_doctype,
|
||||
options: opts.source_doctype,
|
||||
get_query: opts.get_query,
|
||||
reqd:1
|
||||
},
|
||||
]
|
||||
var d = new frappe.ui.form.MultiSelectDialog({
|
||||
doctype: opts.source_doctype,
|
||||
target: opts.target,
|
||||
date_field: opts.date_field || undefined,
|
||||
setters: opts.setters,
|
||||
get_query: opts.get_query,
|
||||
action: function(selections, args) {
|
||||
let values = selections;
|
||||
if(values.length === 0){
|
||||
frappe.msgprint(__("Please select Quotations"))
|
||||
return;
|
||||
}
|
||||
opts.source_name = values;
|
||||
opts.setters = args;
|
||||
d.dialog.hide();
|
||||
_map();
|
||||
},
|
||||
});
|
||||
d.set_primary_action(__('Get Items'), function() {
|
||||
var values = d.get_values();
|
||||
if(!values)
|
||||
return;
|
||||
opts.source_name = values[opts.source_doctype];
|
||||
d.hide();
|
||||
_map();
|
||||
})
|
||||
d.show();
|
||||
} else if(opts.source_name) {
|
||||
opts.source_name = [opts.source_name];
|
||||
_map();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user