fix xhr and javascript problems for group orderinvoice modal

This commit is contained in:
viehlieb 2023-12-11 18:19:12 +01:00
parent bdeee02873
commit 24c25b5278
10 changed files with 151 additions and 143 deletions

View file

@ -21,6 +21,7 @@
//= require touchclick
//= require delta_input
//= require recurring_select
//= require order
$.fn.select2.defaults.set('theme', 'bootstrap');

View file

@ -0,0 +1,131 @@
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'
$(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 = '<div class="alert fade in alert-error"><button class="close" data-dismiss="alert">×</button>' + errorText + '</div>';
$('.page-header').before(alertDiv);
$('modal_')
}
}
});
}
else {
var errorText = "Nothing selected";
var alertDiv = '<div class="alert fade in alert-error"><button class="close" data-dismiss="alert">×</button>' + errorText + '</div>';
$('.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");
});

View file

@ -23,7 +23,8 @@ class OrderCollectiveDirectDebitXml
creditor_identifier: FoodsoftConfig[:group_order_invoices][:creditor_identifier],
)
group_orders.each do |group_order|
# Second: Add transactions
next if group_order.price == 0
sdd.add_transaction(
# Name of the debtor, in German: "Zahlungspflichtiger"
# String, max. 70 char

View file

@ -52,7 +52,7 @@ class GroupOrderInvoice < ApplicationRecord
invoice_data = {}
order = group_order.order
invoice_data[:pickup] = order.pickup
invoice_data[:supplier] = order.supplier.name
invoice_data[:supplier] = order.supplier&.name
invoice_data[:ordergroup] = group_order.ordergroup
invoice_data[:group_order] = group_order
invoice_data[:invoice_number] = invoice_number

View file

@ -40,17 +40,3 @@
= render partial: 'group_order_invoices/modal', locals: { order: order }
- else
%i= t('.no_closed_orders')
- content_for :javascript do
:javascript
$(document).ready(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
});
});

View file

@ -1 +1,11 @@
$('#ordersTable').html('#{j(render('orders'))}');
$('#ordersTable').html('#{j(render('orders'))}');
:plain
$('.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
});

View file

@ -1,6 +1,6 @@
- if foodsoft_sepa_ready?
= link_to 'Sammellastschrift für alle (.xml)', collective_direct_debit_order_path(id: order.id, mode: 'all'), class: 'btn btn-block', data: { turbolinks: false }, id: "collective-direct-debit-link-all-#{order.id}"
= link_to 'Sammellastschrift für ausgewählt (.xml)', collective_direct_debit_order_path(id: order.id, mode: 'selected'), class: 'btn btn-block', data: { turbolinks: false }, id: "collective-direct-debit-link-selected-#{order.id}"
= link_to 'Sammellastschrift für alle (.xml)', collective_direct_debit_order_path(id: order.id, mode: 'all'), class: 'btn btn-block', data: { turbolinks: false, order_id: order.id, supplier: order.supplier&.name }, id: "collective-direct-debit-link-all-#{order.id}"
= link_to 'Sammellastschrift für ausgewählt (.xml)', collective_direct_debit_order_path(id: order.id, mode: 'selected'), class: 'btn btn-block', data: { turbolinks: false, order_id: order.id, supplier: order.supplier&.name}, id: "collective-direct-debit-link-selected-#{order.id}"
- else
%i
= t('activerecord.attributes.group_order_invoice.links.sepa_not_ready')

View file

@ -1,123 +1,2 @@
.div{id: "order_#{order.id}_modal"}
.div{id: "order_#{order.id}_modal", class: 'order-modal', data: { order_id: order.id, supplier: order.supplier&.name } }
= render :partial => 'group_order_invoices/links', locals: { order: order }
- content_for :javascript do
:javascript
$(document).ready(function() {
// Function For Downloading the xml
function doTheDownload(selectedGroupOrderIds, url, mode="all"){
if(mode == "all")
{
var data = { order_id: "#{order.id}" }
}
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 ="#{order.id}"
// Create a temporary link element
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
if(selectedGroupOrderIds.length > 1){
link.download = "#{order.supplier.name}-#{order.id}-Sammellastschrift.xml";
} else {
link.download = "#{order.supplier.name}-#{order.id}-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-#{order.id}" + " .expand-trigger a").click();
$("#order_" + order_id + "_modal").load(window.location + " #order_" + order_id + "_modal");
},
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 = '<div class="alert fade in alert-error"><button class="close" data-dismiss="alert">×</button>' + errorText + '</div>';
$('.page-header').before(alertDiv);
$('modal_')
}
}
});
}
else{
var errorText = "Nothing selected";
var alertDiv = '<div class="alert fade in alert-error"><button class="close" data-dismiss="alert">×</button>' + errorText + '</div>';
$('.page-header').before(alertDiv);
}
}
// register onClick listener for selected group_orders
$("#order_#{order.id}_modal").on('click', "#collective-direct-debit-link-selected-#{order.id}", function(e) {
e.preventDefault();
// Extract selected group_order_ids
var selectedGroupOrderIds = $('input[name^="group_order_ids_for_order_#{order.id}"]:checked').map(function() {
return $(this).val();
}).get();
var url = $(this).closest('a').attr('href');
doTheDownload(selectedGroupOrderIds, url, "selected");
});
// register onClick listener for all group_orders
$("#order_#{order.id}_modal").on('click', "#collective-direct-debit-link-all-#{order.id}", function(e) {
e.preventDefault();
var url = $(this).closest('a').attr('href');
doTheDownload([], url, "all");
});
});
- content_for :javascript do
:javascript
$("#order_#{order.id}_modal").off('change', ".ajax-update-all-link-#{order.id} select").on('change', ".ajax-update-all-link-#{order.id} 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);
}
});})
- order.group_orders.each do |group_order|
- content_for :javascript do
:javascript
$(document).off('change', ".ajax-update-link-#{group_order.id} select").on('change', ".ajax-update-link-#{group_order.id} 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) {
// Handle error
}
});})

View file

@ -115,7 +115,7 @@ de:
sepa_sequence_type: SEPA Typ
open_details_modal: Details ein/ausklappen
payment_method: Guthaben
tax_number_not_set: Steuernummer in den Einstellungen nicht gesetzt
tax_number_not_set: Steuernummer in den Einstellungen nicht gesetzt oder keine Bestellgruppe vorhanden
invoice:
amount: Betrag
attachment: Anhang

View file

@ -115,7 +115,7 @@ en:
sepa_sequence_type: SEPA type
open_details_modal: Toggle details
payment_method: Credit
tax_number_not_set: Tax number not set in configs
tax_number_not_set: Tax number not set in configs or no ordergroup present
invoice:
amount: Amount
attachment: Attachment