Copy childtable ( Get Items From )
Einbau eines 'Get Items From'-Buttons
Das Skript ist einfach einzufügen und an einigen wenigen Stellen anzupassen. Der Skript wird als "Client Script" im ERPNext hinzugefügt. (1) Ist der Doctype auf dem der Button erscheint. (2) Ist eine Skriptart die es auszuwählen gilt ( 'Form' ) und mit (3) kann der Script aktiviert werden.
Minimal running version
Der Script muss leicht angepasst werden bevor wir kopieren können.
AnzupassenZeilesind9dieenthältfetch-fromdenFelderDoctype in denZeilendie3Tabellenpositionenbiskopiert9.werdenInZeile 11bis 13 die Feldnamen und Doctypes anpassenUnd in Zeile 15enthält den Doctypeaufvon demderdieButtonPositionenerscheinenstammen- Zeile
eintragen20 bis 29 müssen entsprechend den Kommentaren auf einen anderen Doctype angepasst werden.
//*
TheAuthor: Marius Widmann
Datum: 17.12.2021
Version: 2.0
Usage:
To use this script create a client script for the doctype where the table should be pasted to
and change line 9,11,20,21 and the fetch-from fields starting in line 22.
*/
frappe.ui.form.on('Quotation', { // Doctype we are working on
refresh(frm) {
frm.add_custom_button('Angebotsvorlage', function () { frm.trigger('get_items') }, __("Get Items From")); // Button name = 'Angebotsvorlage'
},
get_items(frm){
copy_child_table(frm);
}
});
function copy_child_table(frm) {
var cur_frm = frm;
var from = ["Angebotsvorlage","angebotsvorlage_item"]; // From ["Doctype","Doctypechildtable"]
var to = [cur_frm,"items"]; // To [frm, "Doctypechildtable"]
var fields = [ // Fetch these fields
"item_code",
"item_name",
"positionsart",
"description",
"qty",
"uom",
"rate"];
var other_doctype = "Angebotsvorlage"; // The other doctype from where to pick up items
var other_table = "angebotsvorlage_item"; // The table on the other doctype
var original_table = "items"; // The table within the original doctype
frappe.ui.form.on("Quotation", { // Doctype mit dem 'Get Items From'-Button
refresh(frm) {
frm.add_custom_button(other_doctype, function () { frm.trigger('get_items') }, __("Get Items From"));
},
get_items(frm){
start_dialog(frm);
}
});
function start_dialog(frm) {
let dialog = new frappe.ui.form.MultiSelectDialog({
// Read carefully and adjust parameters
doctype: other_doctype, // Doctype we want to pick upfrom[0],
target: frm,to[0],
setters: {
// MultiDialog Filterfields
// customer: frm.doc.customer, },
date_field: "creation",
// "modified", "creation", ...
get_query() {
// MultiDialog Listfilter
return {
filters: { }
};
},
action(selections) {
for(var n = 0; n < selections.length; n++){
var nameid = selections[n]0];
frappe.db.get_doc(other_doctype,from[0], name) // Again, the Doctype we want to pick upid)
.then(doc => {
// Remove the first empty element of the table
if(!('item_code' in frm.get_field("items").grid.grid_rows[0].doc)){ // This if is specific for item table's only
frm.get_field("items").grid.grid_rows[0].remove();
}
// Run through all items of the template quotation
for(var n = 0; n < doc[other_table]from[1]].length; n++){
// DeclareCopy-Paste variables and add table rowOperation
var item=doc[other_table]from[1]][n];
var row=frm.add_child(original_table)child = {}; // Zeile anlegen
frm.refresh_fields(original_table); // Refresh Tabelle
// Copy-Paste Operation
for(var m = 0; m < fields.length; m++){
frm.get_field(original_table).grid.grid_rows[n+1].doc[child[fields[m]] = item[fields[m]];
frm.get_field(original_table).grid.grid_rows[n+1].refresh_field(fields[m]);
}
frm.refresh_fields(original_table)cur_frm.add_child(to[1],child);
// Refresh Tabellecur_frm.refresh_fields(to[1]);
}
});
}
}
});
}