function doTheDownload(selectedGroupOrderIds, orderId, url, supplier, mode = "all") { if (mode == "all") { var data = { order_id: orderId } } else { var data = { group_order_ids: selectedGroupOrderIds } } if (mode == "all" || selectedGroupOrderIds.length > 0) { //suppress generic error warning $.ajaxSetup({ global: false, }); $.ajax({ url: url, method: 'GET', // You may adjust the HTTP method as needed data: data, dataType: 'xml', success: function (response) { // Handle success response // Convert XML response to a Blob var blob = new Blob([new XMLSerializer().serializeToString(response)], { type: 'text/xml' }); var order_id = orderId // Create a temporary link element var link = document.createElement('a'); link.href = URL.createObjectURL(blob); if (selectedGroupOrderIds.length > 1) { link.download = supplier + "-" + orderId + "-Sammellastschrift.xml"; } else { link.download = supplier + "-" + orderId + "-Lastschrift.xml"; } // Append the link to the document and trigger the click event document.body.appendChild(link); link.click(); // Clean up document.body.removeChild(link); $("group-order-invoices-for-order-" + orderId + " .expand-trigger a").click(); var modalSelector = "#order_" + orderId + "_modal"; // Update the value attribute of checkboxes with IDs starting with "sepa_downloaded" to '1' if (selectedGroupOrderIds.length >= 1) { selectedGroupOrderIds.forEach(function (groupOrderId) { var modalSelector = "#group_order_" + groupOrderId; checkbox_element = $(modalSelector + ' input[id^="sepa_downloaded"]'); checkbox_element.val('1'); checkbox_element.prop('checked', true); }); } else { $(modalSelector + ' input[id^="sepa_downloaded"]').each(function () { $(this).val('1'); $(this).prop('checked', true); }); } }, error: function (error) { // Handle error if (error.responseJSON) { alert('AJAX request error:' + "\n" + error.responseJSON.message); } else { var errorText = JSON.parse(error.responseText).error; var alertDiv = '
' + errorText + '
'; $('.page-header').before(alertDiv); $('modal_') } } }); } else { var errorText = "Nothing selected"; var alertDiv = '
' + errorText + '
'; $('.page-header').before(alertDiv); } } $(document).off('change', '[class^="ajax-update-all-link-"] select').on('change', '[class^="ajax-update-all-link-"] select', function () { var selectedValue = $(this).val(); var url = $(this).closest('a').attr('href'); $.ajax({ url: url, method: 'PATCH', data: { sepa_sequence_type: selectedValue }, success: function (response) { // Handle success response }, error: function (error) { console.log(error); } }); }); $(document).off('change', '[class^="ajax-update-link-"] select').on('change', '[class^="ajax-update-link-"] select', function () { var selectedValue = $(this).val(); var url = $(this).closest('a').attr('href'); $.ajax({ url: url, method: 'PATCH', data: { sepa_sequence_type: selectedValue }, success: function (response) { // Handle success response }, error: function (error) { console.log(error); } }); }); $(document).on('ready turbolinks:load', function () { $('.expand-trigger').click(function () { var orderId = $(this).closest('tr').data('order_id'); var expandedRow = $('#expanded-row-' + orderId); // Toggle visibility of the expanded row expandedRow.slideToggle(); return false; // Prevent the default behavior of the link }); }); $(document).off('click', '[id^="collective-direct-debit-link-selected-"]').on('click', '[id^="collective-direct-debit-link-selected-"]', function (e) { e.preventDefault(); var orderId = $(this).data("order-id"); var supplier = $(this).data("supplier"); // Extract selected group_order_ids var selectedGroupOrderIds = $('input[name^="group_order_ids_for_order_' + orderId + '"]:checked').map(function () { return $(this).val(); }).get(); console.log(selectedGroupOrderIds); var url = $(this).closest('a').attr('href'); doTheDownload(selectedGroupOrderIds, orderId, url, supplier, "selected"); }); $(document).off('click', '[id^="collective-direct-debit-link-all-"]').on('click', '[id^="collective-direct-debit-link-all-"]', function (e) { e.preventDefault(); var orderId = $(this).data("order-id"); var supplier = $(this).data("supplier"); var url = $(this).closest('a').attr('href'); doTheDownload([], orderId, url, supplier, "all"); });