2012-11-10 16:44:05 +01:00
|
|
|
class Finance::BalancingController < Finance::BaseController
|
2009-01-10 22:22:16 +01:00
|
|
|
def index
|
2012-11-10 16:44:05 +01:00
|
|
|
@orders = Order.finished.page(params[:page]).per(@per_page).order('ends DESC')
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2012-07-27 18:03:46 +02:00
|
|
|
@order = Order.find(params[:order_id])
|
2023-07-24 10:50:35 +02:00
|
|
|
flash.now.alert = t('finance.balancing.new.alert') if @order.closed? && flash[:alert].blank?
|
2009-01-10 22:22:16 +01:00
|
|
|
@comments = @order.comments
|
2009-04-01 00:46:01 +02:00
|
|
|
|
2013-10-02 16:32:29 +02:00
|
|
|
@articles = @order.order_articles.ordered_or_member.includes(:article, :article_price,
|
2021-03-01 15:27:26 +01:00
|
|
|
group_order_articles: { group_order: :ordergroup })
|
2009-04-01 00:46:01 +02:00
|
|
|
|
2013-03-12 18:54:51 +01:00
|
|
|
sort_param = params['sort'] || 'name'
|
|
|
|
@articles = case sort_param
|
2023-05-12 13:01:12 +02:00
|
|
|
when 'name'
|
2021-03-01 15:27:26 +01:00
|
|
|
@articles.order('articles.name ASC')
|
2023-05-12 13:01:12 +02:00
|
|
|
when 'name_reverse'
|
2021-03-01 15:27:26 +01:00
|
|
|
@articles.order('articles.name DESC')
|
2023-05-12 13:01:12 +02:00
|
|
|
when 'order_number'
|
2021-03-01 15:27:26 +01:00
|
|
|
@articles.order('articles.order_number ASC')
|
2023-05-12 13:01:12 +02:00
|
|
|
when 'order_number_reverse'
|
2021-03-01 15:27:26 +01:00
|
|
|
@articles.order('articles.order_number DESC')
|
|
|
|
else
|
|
|
|
@articles
|
2013-03-12 18:54:51 +01:00
|
|
|
end
|
2009-04-03 17:50:17 +02:00
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
render layout: false if request.xhr?
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
2020-02-28 12:17:27 +01:00
|
|
|
|
2013-12-31 13:25:29 +01:00
|
|
|
def new_on_order_article_create # See publish/subscribe design pattern in /doc.
|
|
|
|
@order_article = OrderArticle.find(params[:order_article_id])
|
2020-02-28 12:17:27 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-31 13:25:29 +01:00
|
|
|
end
|
2020-02-28 12:17:27 +01:00
|
|
|
|
2013-12-30 13:09:49 +01:00
|
|
|
def new_on_order_article_update # See publish/subscribe design pattern in /doc.
|
|
|
|
@order_article = OrderArticle.find(params[:order_article_id])
|
2020-02-28 12:17:27 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-30 13:09:49 +01:00
|
|
|
end
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2013-03-17 15:47:50 +01:00
|
|
|
def update_summary
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
def edit_note
|
|
|
|
@order = Order.find(params[:id])
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
def update_note
|
|
|
|
@order = Order.find(params[:id])
|
2022-10-13 18:25:52 +02:00
|
|
|
if @order.update(params[:order])
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2009-01-29 21:28:22 +01:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: :edit_note, layout: false
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-19 00:22:20 +01:00
|
|
|
def edit_transport
|
|
|
|
@order = Order.find(params[:id])
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2020-03-19 00:22:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_transport
|
|
|
|
@order = Order.find(params[:id])
|
2022-10-13 18:25:52 +02:00
|
|
|
@order.update!(params[:order])
|
2020-03-19 00:22:20 +01:00
|
|
|
redirect_to new_finance_order_path(order_id: @order.id)
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
redirect_to new_finance_order_path(order_id: @order.id), alert: t('errors.general_msg', msg: e.message)
|
2020-03-19 00:22:20 +01:00
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# before the order will booked, a view lists all Ordergroups and its order_prices
|
2009-01-10 22:22:16 +01:00
|
|
|
def confirm
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# Balances the Order, Update of the Ordergroup.account_balances
|
2009-01-10 22:22:16 +01:00
|
|
|
def close
|
|
|
|
@order = Order.find(params[:id])
|
2018-04-25 01:06:48 +02:00
|
|
|
@type = FinancialTransactionType.find_by_id(params.permit(:type)[:type])
|
2017-03-04 14:15:18 +01:00
|
|
|
@order.close!(@current_user, @type)
|
2023-07-24 10:50:35 +02:00
|
|
|
note = t('finance.balancing.close.notice')
|
|
|
|
if @order.closed?
|
|
|
|
alert = t('finance.balancing.close.alert')
|
|
|
|
if FoodsoftConfig[:group_order_invoices]&.[](:use_automatic_invoices)
|
|
|
|
@order.group_orders.each do |go|
|
|
|
|
alert = t('finance.balancing.close.settings_not_set')
|
|
|
|
goi = GroupOrderInvoice.find_or_create_by!(group_order_id: go.id)
|
|
|
|
if goi.save!
|
|
|
|
NotifyGroupOrderInvoiceJob.perform_later(goi)
|
|
|
|
note = t('finance.balancing.close.notice_mail')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alert ||= t('finance.balancing.close.alert')
|
|
|
|
redirect_to finance_order_index_url, notice: note
|
|
|
|
rescue => error
|
|
|
|
redirect_to new_finance_order_url(order_id: @order.id), notice: note, alert: alert, msg: error.message
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
# Close the order directly, without automaticly updating ordergroups account balances
|
|
|
|
def close_direct
|
2009-01-10 22:22:16 +01:00
|
|
|
@order = Order.find(params[:id])
|
2012-07-27 18:03:46 +02:00
|
|
|
@order.close_direct!(@current_user)
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to finance_order_index_url, notice: t('.notice')
|
|
|
|
rescue StandardError => e
|
|
|
|
redirect_to finance_order_index_url, alert: t('.alert', message: e.message)
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2020-02-28 12:17:27 +01:00
|
|
|
def close_all_direct_with_invoice
|
|
|
|
count = 0
|
|
|
|
Order.transaction do
|
|
|
|
Order.finished_not_closed.with_invoice.each do |order|
|
|
|
|
order.close_direct! current_user
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to finance_order_index_url, notice: t('.notice', count: count)
|
|
|
|
rescue StandardError => e
|
|
|
|
redirect_to finance_order_index_url, alert: t('errors.general_msg', msg: e.message)
|
2020-02-28 12:17:27 +01:00
|
|
|
end
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|