frappe.listview_settings['Sales Invoice'] = {
onload: function(listview) {
const secondary_action = listview.page.set_secondary_action('Bulk Print', function() {
const selected_docs = listview.get_checked_items();
if (selected_docs.length > 0) {
const docnames = selected_docs.map(doc => doc.name);
// Fetch print formats for Sales Invoice doctype
frappe.call({
method: 'frappe.client.get_list',
args: {
doctype: 'Print Format',
filters: {
doc_type: listview.doctype
},
fields: ['name']
},
callback: function(response) {
if (response.message) {
const print_format_options = response.message.map(format => format.name);
const dialog = new frappe.ui.Dialog({
title: 'Bulk Print Options',
fields: [
{
label: 'Letter Head',
fieldname: 'letter_head',
fieldtype: 'Link',
options: 'Letter Head',
default: 'No Letterhead',
},
{
label: 'Print Format',
fieldname: 'print_format',
fieldtype: 'Select',
options: print_format_options,
default: print_format_options[0],
},
{
label: 'Page Size',
fieldname: 'page_size',
fieldtype: 'Select',
options: ['A4', 'Letter', 'Legal'],
default: 'A4',
},
],
// primary_action_label: 'Preview',
// primary_action: function() {
// const values = dialog.get_values();
// dialog.hide();
// docnames.forEach(docname => {
// let url = `/api/method/frappe.utils.print_format.download_pdf?doctype=${encodeURIComponent(listview.doctype)}&name=${encodeURIComponent(docname)}&format=${encodeURIComponent(values.print_format)}&letterhead=${encodeURIComponent(values.letter_head)}&no_letterhead=${values.letter_head === 'No Letterhead' ? 1 : 0}&page_size=${encodeURIComponent(values.page_size)}`;
// window.open(url, '_blank'); // Open the document in a new tab for preview
// });
// },
primary_action_label: 'Download PDF',
primary_action: function() {
const values = dialog.get_values();
dialog.hide();
// Function to download a single file
const downloadFile = (docname, index) => {
let url = `/api/method/frappe.utils.print_format.download_pdf?doctype=${encodeURIComponent(listview.doctype)}&name=${encodeURIComponent(docname)}&format=${encodeURIComponent(values.print_format)}&letterhead=${encodeURIComponent(values.letter_head)}&no_letterhead=${values.letter_head === 'No Letterhead' ? 1 : 0}&page_size=${encodeURIComponent(values.page_size)}`;
let link = document.createElement('a');
link.href = url;
let filename = `${listview.doctype}-${docname}.pdf`;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Download the next file after a short delay
if (index < docnames.length - 1) {
setTimeout(() => downloadFile(docnames[index + 1], index + 1), 200); // Adjust delay as needed
}
};
// Start downloading the first file
downloadFile(docnames[0], 0);
},
secondary_action_label: 'Download Zip',
secondary_action: function() {
const values = dialog.get_values();
dialog.hide();
const zip = new JSZip();
let count = 0;
docnames.forEach(docname => {
let url = `/api/method/frappe.utils.print_format.download_pdf?doctype=${encodeURIComponent(listview.doctype)}&name=${encodeURIComponent(docname)}&format=${encodeURIComponent(values.print_format)}&letterhead=${encodeURIComponent(values.letter_head)}&no_letterhead=${values.letter_head === 'No Letterhead' ? 1 : 0}&page_size=${encodeURIComponent(values.page_size)}`;
fetch(url)
.then(response => response.blob())
.then(blob => {
zip.file(`${listview.doctype}-${docname}.pdf`, blob);
count++;
if (count === docnames.length) {
zip.generateAsync({ type: 'blob' }).then(content => {
saveAs(content, 'SalesInvoices.zip');
});
}
});
});
}
});
dialog.show();
}
}
});
} else {
frappe.msgprint(__('Please select at least one Sales Invoice to print.'));
}
}, 'printer');
// Initially disable the button
secondary_action.addClass('disabled');
// Enable or disable the button based on row selection
listview.on_change = function() {
if (listview.get_checked_items().length > 0) {
secondary_action.removeClass('disabled');
} else {
secondary_action.addClass('disabled');
}
};
// Manually trigger the on_change function to set the initial state
listview.on_change();
}
};
// Load JSZip and FileSaver libraries
frappe.require([
'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js'
]);
For reference i put images here