enable actions for multi (group) orders and ordergroup invoices
This commit is contained in:
parent
f676497e43
commit
e902aa0d5a
44 changed files with 550 additions and 324 deletions
|
|
@ -1,21 +1,42 @@
|
|||
class MultiOrdersController < ApplicationController
|
||||
include SepaHelper
|
||||
|
||||
before_action :set_multi_order, only: [:generate_ordergroup_invoices]
|
||||
|
||||
def create
|
||||
orders = Order.where(id: multi_order_params[:order_ids_for_multi_order])
|
||||
|
||||
unclosed_orders = orders.select { |order| order.closed? == false }
|
||||
multi_orders = orders.select { |order| order.multi_order_id.present? }
|
||||
invoiced_orders = orders.select {|order| order.group_orders.map(&:group_order_invoice).compact.present? }
|
||||
|
||||
if multi_order_params[:multi_order_ids_for_multi_multi_order].present?
|
||||
#TODO: do the i18n and add a test
|
||||
flash[:alert] = "Du kannst keine Multi-Bestellungen in eine Multi-Multi-Bestellung umwandeln."
|
||||
redirect_to finance_order_index_path
|
||||
msg = "Du kannst keine Multi-Bestellungen in eine Multi-Multi-Bestellung umwandeln."
|
||||
flash[:alert] = msg
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.html { redirect_to finance_order_index_path }
|
||||
end
|
||||
return
|
||||
end
|
||||
if multi_orders.any? || unclosed_orders.any?
|
||||
#TODO: do the i18n and add a test
|
||||
flash[:alert] = "Die Bestellung ist noch nicht abgeschlossen oder ist bereits Teil einer Multi-Bestellung."
|
||||
redirect_to finance_order_index_path
|
||||
msg = "Die Bestellung ist bereits Teil einer Multi-Bestellung oder ist noch nicht abgeschlossen."
|
||||
flash[:alert] = msg
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.html { redirect_to finance_order_index_path }
|
||||
end
|
||||
return
|
||||
end
|
||||
if invoiced_orders.any?
|
||||
msg = "Zusammenführen nicht möglich. Es gibt bereits Rechnungen für einige der Bestellgruppen."
|
||||
flash[:alert] = msg
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.html { redirect_to finance_order_index_path }
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -24,15 +45,6 @@ class MultiOrdersController < ApplicationController
|
|||
@multi_order.orders = orders
|
||||
@multi_order.ends = orders.map(&:ends).max
|
||||
@multi_order.save!
|
||||
puts "
|
||||
" + "______________" + "
|
||||
" + "______________" + "
|
||||
" + "______________" + "
|
||||
" + "#{@multi_order.errors.inspect}" + "
|
||||
" + "______________"+ "
|
||||
" + "______________"+ "
|
||||
" + "______________"
|
||||
|
||||
#create multi group orders
|
||||
all_group_orders = orders.flat_map(&:group_orders)
|
||||
|
||||
|
|
@ -42,7 +54,6 @@ class MultiOrdersController < ApplicationController
|
|||
multi_group_order = MultiGroupOrder.create!(
|
||||
multi_order: @multi_order, group_orders: group_orders
|
||||
)
|
||||
|
||||
# Now, associate each group_order with the new multi_group_order
|
||||
group_orders.each do |group_order|
|
||||
group_order.update!(multi_group_order: multi_group_order)
|
||||
|
|
@ -51,9 +62,19 @@ class MultiOrdersController < ApplicationController
|
|||
redirect_to finance_order_index_path
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
flash[:alert] = t('errors.general_msg', msg: e.message)
|
||||
redirect_to finance_order_index_path
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.html { redirect_to finance_order_index_path }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@multi_order = MultiOrder.find(params[:id])
|
||||
@multi_order.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to finance_order_index_path }
|
||||
end
|
||||
end
|
||||
|
||||
def generate_ordergroup_invoices
|
||||
|
|
@ -65,6 +86,77 @@ class MultiOrdersController < ApplicationController
|
|||
redirect_to finance_order_index_path, alert: t('errors.general_msg', msg: e.message)
|
||||
end
|
||||
|
||||
def collective_direct_debit
|
||||
if foodsoft_sepa_ready?
|
||||
case params[:mode]
|
||||
when 'all'
|
||||
multi_group_orders = MultiGroupOrder.where(multi_order_id: params[:id])
|
||||
when 'selected'
|
||||
#TODO: !!! params and javascript
|
||||
multi_group_orders = MultiGroupOrder.where(id: params[:multi_group_order_ids])
|
||||
else
|
||||
redirect_to finance_order_index_path, alert: I18n.t('orders.collective_direct_debit.alert', ordergroup_names: '')
|
||||
end
|
||||
|
||||
@multi_order = MultiOrder.find(params[:id])
|
||||
ordergroups = multi_group_orders.flat_map(&:group_orders).map(&:ordergroup)
|
||||
|
||||
export_allowed = !ordergroups.map(&:sepa_possible?).include?(false) && !multi_group_orders.map { |go| go.ordergroup_invoice.present? }.include?(false)
|
||||
group_order_ids = multi_group_orders.map { |mgo| mgo.id if mgo.ordergroup_invoice.present? }
|
||||
|
||||
sepa_possible_ordergroup_names = ordergroups.map { |ordergroup| ordergroup.name if ordergroup.sepa_possible? }.compact_blank
|
||||
sepa_not_possible_ordergroup_names = ordergroups.map(&:name) - sepa_possible_ordergroup_names
|
||||
|
||||
if export_allowed && multi_group_orders.present?
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
collective_debit = OrderCollectiveDirectDebitXml.new(multi_group_orders)
|
||||
send_data collective_debit.xml_string, filename: @order.name + '_Sammellastschrift' + '.xml', type: 'text/xml'
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:mark_sepa_downloaded)
|
||||
rescue SEPA::Error => e
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:unmark_sepa_downloaded)
|
||||
redirect_to finance_order_index_path, alert: e.message
|
||||
rescue StandardError => e
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:unmark_sepa_downloaded)
|
||||
redirect_to finance_order_index_path, alert: I18n.t('orders.collective_direct_debit.alert', ordergroup_names: sepa_not_possible_ordergroup_names.join(', '), error: e.message)
|
||||
end
|
||||
format.xml do
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:mark_sepa_downloaded)
|
||||
collective_debit = OrderCollectiveDirectDebitXml.new(multi_group_orders)
|
||||
send_data collective_debit.xml_string, filename: @multi_order.orders.first.name + '_Sammellastschrift' + '.xml', type: 'text/xml'
|
||||
rescue SEPA::Error => e
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:unmark_sepa_downloaded)
|
||||
render json: { error: e.message }
|
||||
rescue StandardError => e
|
||||
multi_group_orders.map(&:ordergroup_invoice).each(&:unmark_sepa_downloaded)
|
||||
render json: { error: I18n.t('orders.collective_direct_debit.alert', ordergroup_names: sepa_not_possible_ordergroup_names.join(', '), error: e.message) }
|
||||
end
|
||||
format.js
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
redirect_to finance_order_index_path, alert: I18n.t('orders.collective_direct_debit.alert', ordergroup_names: sepa_not_possible_ordergroup_names.join(', '), error: '')
|
||||
end
|
||||
format.xml do
|
||||
render json: { error: I18n.t('orders.collective_direct_debit.alert', ordergroup_names: sepa_not_possible_ordergroup_names.join(', '), error: '') }
|
||||
end
|
||||
format.js
|
||||
end
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
redirect_to finance_order_index_path, alert: "Wichtige SEPA Konfiguration in Administration >> Einstellungen >> Finanzen nicht gesetzt!"
|
||||
end
|
||||
format.xml do
|
||||
redirect_to finance_order_index_path, alert: "Wichtige SEPA Konfiguration in Administration >> Einstellungen >> Finanzen nicht gesetzt!"
|
||||
end
|
||||
format.js
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_multi_order
|
||||
|
|
@ -72,6 +164,6 @@ class MultiOrdersController < ApplicationController
|
|||
end
|
||||
|
||||
def multi_order_params
|
||||
params.permit(:id, order_ids_for_multi_order: [], multi_order_ids_for_multi_multi_order: [])
|
||||
params.permit(:id, :foodcoop, order_ids_for_multi_order: [], multi_order_ids_for_multi_multi_order: [])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue