tüit Logo Direkt zum Hauptinhalt

Get Items From other document

Einbau eines 'Get Items From'-Buttons

Das Skript ist einfach einzufügen und an einigen wenigen Stellen anzupassen.

Minimal running version
  1. Anzupassen sind die fetch-from Felder in den Zeilen 3 bis 9.
  2. Zeile 11 der Doctype auf dem wir den Button einbauen wollen. 
  3. Zeile 24 und Zeile 40 jeweils der Doctype von dem wir die Itempositionen abholen wollen
// The fetch-from fields
var fields = [
  "item_code",
  "item_name",
  "positionsart",
  "description",
  "qty",
  "uom",
  "rate"];

frappe.ui.form.on('Quotation', { // Doctype mit dem 'Get Items From'-Button
	refresh(frm) {
	    frm.add_custom_button('Angebotsvorlage', 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: "Angebotsvorlage", // Doctype we want to pick up
		target: frm,
		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 name = selections[n];
		        frappe.db.get_doc("Angebotsvorlage", name) // Again, the Doctype we want to pick up
                .then(doc => {
                    // Remove the first empty element of the table
                    if(!('item_code' in frm.get_field("items").grid.grid_rows[0].doc)){
                        frm.get_field("items").grid.grid_rows[0].remove();
                    }
                    
                    // Run through all items of the template quotation
                    for(var n = 0; n < doc.angebotsvorlage_item.length; n++){
                        // Declare variables and add table row
                        var item=doc.angebotsvorlage_item[n];
                        var row=frm.add_child("items"); // Zeile anlegen
                        frm.refresh_fields("items"); // Refresh Tabelle
                        
                        // Copy-Paste Operation
                        for(var m = 0; m < fields.length; m++){
                            frm.get_field("items").grid.grid_rows[n+1].doc[fields[m]] = item[fields[m]];
                            frm.get_field("items").grid.grid_rows[n+1].refresh_field(fields[m]);
                        }
                        frm.refresh_fields("items"); // Refresh Tabelle
                    }
                });
		    }
		}
	});
}