diff --git a/app/controllers/finance/invoices_controller.rb b/app/controllers/finance/invoices_controller.rb index 5b0dda36..c2f7f6e9 100644 --- a/app/controllers/finance/invoices_controller.rb +++ b/app/controllers/finance/invoices_controller.rb @@ -14,9 +14,21 @@ class Finance::InvoicesController < ApplicationController @invoice = Invoice.new :supplier_id => params[:supplier_id] @invoice.deliveries << Delivery.find_by_id(params[:delivery_id]) if params[:delivery_id] @invoice.orders << Order.find_by_id(params[:order_id]) if params[:order_id] + fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id end def edit + fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id + end + + def form_on_supplier_id_change + fill_deliveries_and_orders_collection params[:invoice_id], params[:supplier_id] + render :layout => false + end + + def fill_deliveries_and_orders_collection(invoice_id, supplier_id) + @deliveries_collection = Delivery.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ?)', invoice_id, supplier_id).order(:delivered_on) + @orders_collection = Order.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ? AND state = ?)', invoice_id, supplier_id, 'finished').order(:ends) end def create diff --git a/app/helpers/finance/invoices_helper.rb b/app/helpers/finance/invoices_helper.rb new file mode 100644 index 00000000..82538b72 --- /dev/null +++ b/app/helpers/finance/invoices_helper.rb @@ -0,0 +1,8 @@ +module Finance::InvoicesHelper + def format_delivery_item delivery + format_date(delivery.delivered_on) + end + def format_order_item order + "#{format_date(order.ends)} (#{number_to_currency(order.sum)})" + end +end diff --git a/app/views/finance/invoices/_form.html.haml b/app/views/finance/invoices/_form.html.haml index 82e8af01..eb879bce 100644 --- a/app/views/finance/invoices/_form.html.haml +++ b/app/views/finance/invoices/_form.html.haml @@ -1,24 +1,20 @@ = simple_form_for([:finance, @invoice]) do |f| - = f.association :deliveries, multiple: true, as: :hidden - = f.association :orders, multiple: true, as: :hidden - - - if @invoice.deliveries.first - %p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.deliveries.first])).html_safe - - if @invoice.orders.first - %p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_order'), new_finance_order_path(order_id: @invoice.orders.first.id))).html_safe - - .fold-line - .control-group - %label.control-label{for: 'created_at'} - = Invoice.human_attribute_name(:created_at) - .controls.control-text#article_fc_price - = format_date @invoice.created_at - .control-group - %label.control-label{for: 'created_by'} - = Invoice.human_attribute_name(:created_by) - .controls.control-text#article_fc_price - = show_user @invoice.created_by + - if @invoice.created_at + .fold-line + .control-group + %label.control-label{for: 'created_at'} + = Invoice.human_attribute_name(:created_at) + .controls.control-text#article_fc_price + = format_date @invoice.created_at + .control-group + %label.control-label{for: 'created_by'} + = Invoice.human_attribute_name(:created_by) + .controls.control-text#article_fc_price + = show_user @invoice.created_by = f.association :supplier, collection: Supplier.order(:name), hint: false + - if Delivery.any? + = f.association :deliveries, collection: @deliveries_collection, input_html: {size: 10}, multiple: true, label_method: method(:format_delivery_item) + = f.association :orders, collection: @orders_collection, multiple: true, label_method: method(:format_order_item) = f.input :number = f.input :date, as: :date_picker - if current_user.role_finance? @@ -30,3 +26,5 @@ .form-actions = f.submit class: 'btn' = link_to t('ui.or_cancel'), :back + += render :partial => 'form_js', :locals => {:invoice_id => invoice_id} diff --git a/app/views/finance/invoices/_form_js.html.haml b/app/views/finance/invoices/_form_js.html.haml new file mode 100644 index 00000000..62d5f3d7 --- /dev/null +++ b/app/views/finance/invoices/_form_js.html.haml @@ -0,0 +1,14 @@ +- content_for :javascript do + :javascript + $(function() { + if ($('#invoice_delivery_ids').length) + $('#invoice_delivery_ids').select2(); + $('#invoice_order_ids').select2(); + $('#invoice_supplier_id').change(function(e) { + $.ajax({ + url: '#{form_on_supplier_id_change_finance_invoices_path}', + type: 'get', + data: {invoice_id: #{invoice_id}, supplier_id: $('#invoice_supplier_id').val()} + }); + }); + }); diff --git a/app/views/finance/invoices/edit.html.haml b/app/views/finance/invoices/edit.html.haml index efdfbf27..f2a1595a 100644 --- a/app/views/finance/invoices/edit.html.haml +++ b/app/views/finance/invoices/edit.html.haml @@ -1,2 +1,2 @@ - title t('.title') -= render :partial => 'form' += render :partial => 'form', :locals => {:invoice_id => @invoice.id} diff --git a/app/views/finance/invoices/form_on_supplier_id_change.js.erb b/app/views/finance/invoices/form_on_supplier_id_change.js.erb new file mode 100644 index 00000000..894859ec --- /dev/null +++ b/app/views/finance/invoices/form_on_supplier_id_change.js.erb @@ -0,0 +1,16 @@ +<% +deliveries_data = @deliveries_collection.map {|d| { id: d.id, text: format_delivery_item(d) } } +orders_data = @orders_collection.map {|o| { id: o.id, text: format_order_item(o) } } +%> +(function() { + function update_select2(id, data) { + var ele = $(id); + if (!ele.length) + return; + ele.select2("destroy"); + ele.html(""); + ele.select2({data: data}); + } + update_select2("#invoice_delivery_ids", <%= raw deliveries_data.to_json %>); + update_select2("#invoice_order_ids", <%= raw orders_data.to_json %>); +})(); diff --git a/app/views/finance/invoices/new.html.haml b/app/views/finance/invoices/new.html.haml index f7e56474..9b5bfcf2 100644 --- a/app/views/finance/invoices/new.html.haml +++ b/app/views/finance/invoices/new.html.haml @@ -1,3 +1,3 @@ - title t('.title') -= render :partial => 'form' += render :partial => 'form', :locals => {:invoice_id => 0} = link_to t('ui.or_cancel'), finance_invoices_path diff --git a/config/locales/de.yml b/config/locales/de.yml index 3eb1d55a..27d8d3a0 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -780,9 +780,6 @@ de: index: action_new: Neue Rechnung anlegen title: Rechnungen - linked: Diese Rechnung ist mit %{what_link} verknüpft. - linked_delivery: einer Lieferung - linked_order: einer Bestellung new: title: Neue Rechnung anlegen show: diff --git a/config/locales/en.yml b/config/locales/en.yml index c8c3a685..2bf7267e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -793,9 +793,6 @@ en: index: action_new: Create new invoice title: Invoices - linked: This invoice is linked to %{what_link}. - linked_delivery: a delivery - linked_order: an order new: title: Create new invoice show: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 77e41290..680a0570 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -775,9 +775,6 @@ fr: index: action_new: Ajouter une facture title: Factures - linked: Cette facture est associée à %{what_link}. - linked_delivery: un réapprovisionnement - linked_order: une commande new: title: Ajouter une facture show: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 0055d16b..c2ba7765 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -773,9 +773,6 @@ nl: index: action_new: Nieuwe factuur toevoegen title: Facturen - linked: Deze factuur is aan %{what_link} gekoppeld. - linked_delivery: een levering - linked_order: een bestelling new: title: Nieuwe factuur toevoegen show: diff --git a/config/routes.rb b/config/routes.rb index cafc29aa..147bab13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -156,7 +156,9 @@ Foodsoft::Application.routes.draw do end end - resources :invoices + resources :invoices do + get :form_on_supplier_id_change, on: :collection + end resources :ordergroups, only: [:index] do resources :financial_transactions, as: :transactions