77 lines
2.6 KiB
Ruby
77 lines
2.6 KiB
Ruby
class MultiOrdersController < ApplicationController
|
|
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? }
|
|
|
|
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
|
|
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
|
|
return
|
|
end
|
|
|
|
begin
|
|
@multi_order = MultiOrder.new
|
|
@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)
|
|
|
|
grouped_by_ordergroup = all_group_orders.group_by(&:ordergroup_id)
|
|
|
|
grouped_by_ordergroup.each do |ordergroup_id, group_orders|
|
|
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)
|
|
end
|
|
end
|
|
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
|
|
end
|
|
|
|
end
|
|
|
|
def generate_ordergroup_invoices
|
|
@multi_order.group_orders.group_by(&:ordergroup_id).each do |_, group_orders|
|
|
OrdergroupInvoice.create!(group_orders: group_orders)
|
|
end
|
|
redirect_to finance_order_index_path, notice: t('finance.balancing.close.notice')
|
|
rescue StandardError => e
|
|
redirect_to finance_order_index_path, alert: t('errors.general_msg', msg: e.message)
|
|
end
|
|
|
|
private
|
|
|
|
def set_multi_order
|
|
@multi_order = MultiOrder.find(params[:id])
|
|
end
|
|
|
|
def multi_order_params
|
|
params.permit(:id, order_ids_for_multi_order: [], multi_order_ids_for_multi_multi_order: [])
|
|
end
|
|
end
|