From 50017fefa833e0dd8b6e9a288d9e4e264578275c Mon Sep 17 00:00:00 2001 From: Viehlieb <3feuerba@informatik.uni-hamburg.de> Date: Thu, 23 Dec 2021 01:51:44 +0100 Subject: [PATCH] add functionality in views and controllers for generation of group order invoices --- .../finance/balancing_controller.rb | 18 +++++++++++++++--- app/views/finance/balancing/_orders.html.haml | 10 ++++++++++ .../group_order_invoices/_links.html.haml | 11 +++++++++++ app/views/group_order_invoices/create.js.erb | 1 + app/views/group_order_invoices/destroy.js.erb | 1 + app/views/group_order_invoices/show.html.haml | 0 app/views/group_order_invoices/show.pdf.prawn | 1 + config/routes.rb | 3 ++- 8 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 app/views/group_order_invoices/_links.html.haml create mode 100644 app/views/group_order_invoices/create.js.erb create mode 100644 app/views/group_order_invoices/destroy.js.erb create mode 100644 app/views/group_order_invoices/show.html.haml create mode 100644 app/views/group_order_invoices/show.pdf.prawn diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index 09c109f8..870f7159 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -81,9 +81,21 @@ class Finance::BalancingController < Finance::BaseController @order = Order.find(params[:id]) @type = FinancialTransactionType.find_by_id(params.permit(:type)[:type]) @order.close!(@current_user, @type) - redirect_to finance_order_index_url, notice: t('finance.balancing.close.notice') - rescue => error - redirect_to new_finance_order_url(order_id: @order.id), alert: t('finance.balancing.close.alert', message: error.message) + note = t('finance.balancing.close.notice') + if @order.closed? + if FoodsoftConfig[:group_order_invoices]&.[](:use) && FoodsoftConfig[:contact]&.[](:tax_number) + @order.group_orders.each do |go| + 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 + redirect_to finance_order_index_url, notice: note + rescue => error + redirect_to new_finance_order_url(order_id: @order.id), notice: note, alert: t('finance.balancing.close.alert', message: error.message) end # Close the order directly, without automaticly updating ordergroups account balances diff --git a/app/views/finance/balancing/_orders.html.haml b/app/views/finance/balancing/_orders.html.haml index 3f20d850..addbf930 100644 --- a/app/views/finance/balancing/_orders.html.haml +++ b/app/views/finance/balancing/_orders.html.haml @@ -9,6 +9,8 @@ %th= t('.end') %th= t('.state') %th= heading_helper Order, :updated_by + %th= heading_helper GroupOrderInvoice, :name + %th %th %tbody - @orders.each do |order| @@ -17,6 +19,14 @@ %td=h format_time(order.ends) unless order.ends.nil? %td= order.closed? ? t('.cleared', amount: number_to_currency(order.foodcoop_result)) : t('.ended') %td= show_user(order.updated_by) + %td{id: "generate-invoice#{order.id}"} + - if order.closed? + -if FoodsoftConfig[:contact][:tax_number] + = render :partial => 'group_order_invoices/links', locals:{order: order} + -else + = I18n.t('activerecord.attributes.group_order_invoice.tax_number_not_set') + - else + = t('orders.index.not_closed') %td - unless order.closed? - if current_user.role_orders? diff --git a/app/views/group_order_invoices/_links.html.haml b/app/views/group_order_invoices/_links.html.haml new file mode 100644 index 00000000..0d270756 --- /dev/null +++ b/app/views/group_order_invoices/_links.html.haml @@ -0,0 +1,11 @@ +.row + - order.group_orders.includes([:group_order_invoice, :ordergroup]).each do |go| + .row + = label_tag go.ordergroup.name + - if go.group_order_invoice + = link_to I18n.t('activerecord.attributes.group_order_invoice.links.download'), group_order_invoice_path(go.group_order_invoice, :format => 'pdf'), class: 'btn btn-small' + = link_to I18n.t('activerecord.attributes.group_order_invoice.links.delete'), go.group_order_invoice, method: :delete, class: 'btn btn-danger btn-small', remote: true + - else + = button_to I18n.t('activerecord.attributes.group_order_invoice.links.generate'), group_order_invoices_path(:method => :post, group_order: go) ,class: 'btn btn-small', params: {id: order.id}, remote: true + %br + diff --git a/app/views/group_order_invoices/create.js.erb b/app/views/group_order_invoices/create.js.erb new file mode 100644 index 00000000..5a43e85d --- /dev/null +++ b/app/views/group_order_invoices/create.js.erb @@ -0,0 +1 @@ +$("#generate-invoice<%= params[:id] %>").html("<%= escape_javascript(render partial: 'links', locals: {order: @order}) %>"); diff --git a/app/views/group_order_invoices/destroy.js.erb b/app/views/group_order_invoices/destroy.js.erb new file mode 100644 index 00000000..30ce5985 --- /dev/null +++ b/app/views/group_order_invoices/destroy.js.erb @@ -0,0 +1 @@ +$("#generate-invoice<%= @order.id %>").html("<%= escape_javascript(render partial: 'links', locals: {order: @order}) %>"); \ No newline at end of file diff --git a/app/views/group_order_invoices/show.html.haml b/app/views/group_order_invoices/show.html.haml new file mode 100644 index 00000000..e69de29b diff --git a/app/views/group_order_invoices/show.pdf.prawn b/app/views/group_order_invoices/show.pdf.prawn new file mode 100644 index 00000000..a280e3b5 --- /dev/null +++ b/app/views/group_order_invoices/show.pdf.prawn @@ -0,0 +1 @@ +pdf.text "Hello World" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d7ec44a7..b0f5c300 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -139,7 +139,7 @@ Rails.application.routes.draw do end end end - + resources :group_order_invoices resources :article_categories ########### Finance @@ -172,6 +172,7 @@ Rails.application.routes.draw do get :unpaid, on: :collection end + resources :links, controller: 'financial_links', only: [:create, :show] do collection do get :incomplete