Jump to content
thirty bees forum
  • 0

[solved] bulk / batch download invoices in backoffice orders


Question

Posted (edited)

I had in TB 1.4 an enhancement for me in the admin.js code that allowed me for selected orders in bo orders list to do a bulk action like downloading pdf.

My problem is that I cannot get it working with tb 1.6x. Even with the help of AI it won't work 🙂 

Line 875 admin.js

  if (window['kpis']) {
    $.each(window['kpis'], function(id, kpi) {
      if (kpi.initRefresh) {
        refresh_kpi(id, kpi);
      }
    });
  }
});

old V1.4 additional code

  if ($('.kpi-container').length) {
    refresh_kpis();
  }
  $('body.adminorders .bulk-actions ul').append('<li><a href="#" id="downloadBulkSingle"><i class="icon-download"></i>&nbsp;Bulk Rechnungsdownload</a></li>');
  $('a#downloadBulkSingle').click(function(e){
	  e.preventDefault();
	  $('[name="orderBox[]"]:checked').each((i,e)=>
		$(e).parents('tr').find('a[href*="generateInvoicePDF"]').each((a,c) => c.click()));
  });
});

ChatGPT thinks this would be proper....

In this version:

  • The code checks if #downloadBulkSingle already exists to avoid duplication.
  • It uses on for the click event, making it more flexible if the link is reloaded dynamically.
if (window['kpis']) {
    $.each(window['kpis'], function(id, kpi) {
        if (kpi.initRefresh) {
            refresh_kpi(id, kpi);
        }
    });

    if (!$('a#downloadBulkSingle').length) {  // Check if the link already exists
        $('body.adminorders .bulk-actions ul').append(
            '<li><a href="#" id="downloadBulkSingle"><i class="icon-download"></i>&nbsp;Bulk Rechnungsdownload</a></li>'
        );

        $('body').on('click', 'a#downloadBulkSingle', function(e) {
            e.preventDefault();
            $('[name="orderBox[]"]:checked').each((i, e) => {
                $(e).closest('tr').find('a[href*="generateInvoicePDF"]').each((a, c) => c.click());
            });
        });
    }
}

 

Would be nice if someone could make this work with 1.6 again.

thx

Edited by Pedalman

2 answers to this question

Recommended Posts

  • 0
Posted

The code works for me.

Browser doesn't like it much, though. When you select multiple orders with invoices, and then click on multi-download, browser opens multiple tabs for every invoice. This is not a normal behaviour, and browser quite correctly treat this as unwanted popups. I had to click on 'always allow popup on this site' to make this functionaltiy work.

Note: please remove this code from admin.js -- that is a core file, and should not be modified. 

The best way to implement these changes is using custom module.

I've quickly prepared a very simple module that adds custom js file to all admin pages:

backofficejs-v1.0.0.zip

Just install it, and then modify file /modules/backofficejs/views/js/backoffice.js in order to add your code. 

 

  • Thanks 1
  • 0
Posted (edited)

For Edge the code wasn't very convenient. Here is a working one that allows you to batch/bulk download invoices in backoffice/orders withouth Edge complaining. Just put it into Datakick's module at the appropriate place.

 

$(function() {
    if (window['kpis']) {
        $.each(window['kpis'], function(id, kpi) {
            if (kpi.initRefresh) {
                refresh_kpi(id, kpi);
            }
        });
    
        if (!$('a#downloadBulkSingle').length) {  // Check if the link already exists
            $('body.adminorders .bulk-actions ul').append(
                '<li><a href="#" id="downloadBulkSingle"><i class="icon-download"></i>&nbsp;Bulk Rechnungsdownload</a></li>'
            );
    
            $('body').on('click', 'a#downloadBulkSingle', function(e) {
                e.preventDefault();
    
                var delay = 0; // Initial delay in milliseconds
                $('[name="orderBox[]"]:checked').each((i, e) => {
                    var invoiceUrl = $(e).closest('tr').find('a[href*="generateInvoicePDF"]').attr('href');
    
                    // Set a delay before each download
                    setTimeout(() => {
                        fetch(invoiceUrl)
                            .then(response => {
                                // Extract filename from Content-Disposition header
                                var filename = "Rechnung_" + (i + 1) + ".pdf";
                                var contentDisposition = response.headers.get("Content-Disposition");
                                if (contentDisposition && contentDisposition.includes("filename=")) {
                                    filename = contentDisposition
                                        .split("filename=")[1]
                                        .split(";")[0] // Take only the part before the semicolon
                                        .replace(/['"]/g, "");  // Remove any quotes
                                }
                                return response.blob().then(blob => ({ blob, filename }));
                            })
                            .then(({ blob, filename }) => {
                                var link = document.createElement('a');
                                link.href = URL.createObjectURL(blob);
                                link.download = filename;  // Use the extracted filename
                                link.click();  // Trigger the download
                            })
                            .catch(error => console.error('Fehler beim Download der Rechnung:', error));
                    }, delay);
    
                    delay += 500;  // Increase the delay by 500ms for the next download
                });
            });
        }
    }
});

 

Edited by Pedalman
file naming

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...