Allow editing of linked deliveries and orders at invoice

This commit is contained in:
Patrick Gansterer 2016-03-11 13:23:13 +01:00
parent 14e2fd179e
commit efb929e64f
12 changed files with 72 additions and 34 deletions

View file

@ -14,9 +14,21 @@ class Finance::InvoicesController < ApplicationController
@invoice = Invoice.new :supplier_id => params[:supplier_id] @invoice = Invoice.new :supplier_id => params[:supplier_id]
@invoice.deliveries << Delivery.find_by_id(params[:delivery_id]) if params[:delivery_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] @invoice.orders << Order.find_by_id(params[:order_id]) if params[:order_id]
fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id
end end
def edit 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 end
def create def create

View file

@ -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

View file

@ -1,12 +1,5 @@
= simple_form_for([:finance, @invoice]) do |f| = simple_form_for([:finance, @invoice]) do |f|
= f.association :deliveries, multiple: true, as: :hidden - if @invoice.created_at
= 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 .fold-line
.control-group .control-group
%label.control-label{for: 'created_at'} %label.control-label{for: 'created_at'}
@ -19,6 +12,9 @@
.controls.control-text#article_fc_price .controls.control-text#article_fc_price
= show_user @invoice.created_by = show_user @invoice.created_by
= f.association :supplier, collection: Supplier.order(:name), hint: false = 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 :number
= f.input :date, as: :date_picker = f.input :date, as: :date_picker
- if current_user.role_finance? - if current_user.role_finance?
@ -30,3 +26,5 @@
.form-actions .form-actions
= f.submit class: 'btn' = f.submit class: 'btn'
= link_to t('ui.or_cancel'), :back = link_to t('ui.or_cancel'), :back
= render :partial => 'form_js', :locals => {:invoice_id => invoice_id}

View file

@ -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()}
});
});
});

View file

@ -1,2 +1,2 @@
- title t('.title') - title t('.title')
= render :partial => 'form' = render :partial => 'form', :locals => {:invoice_id => @invoice.id}

View file

@ -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 %>);
})();

View file

@ -1,3 +1,3 @@
- title t('.title') - title t('.title')
= render :partial => 'form' = render :partial => 'form', :locals => {:invoice_id => 0}
= link_to t('ui.or_cancel'), finance_invoices_path = link_to t('ui.or_cancel'), finance_invoices_path

View file

@ -780,9 +780,6 @@ de:
index: index:
action_new: Neue Rechnung anlegen action_new: Neue Rechnung anlegen
title: Rechnungen title: Rechnungen
linked: Diese Rechnung ist mit %{what_link} verknüpft.
linked_delivery: einer Lieferung
linked_order: einer Bestellung
new: new:
title: Neue Rechnung anlegen title: Neue Rechnung anlegen
show: show:

View file

@ -793,9 +793,6 @@ en:
index: index:
action_new: Create new invoice action_new: Create new invoice
title: Invoices title: Invoices
linked: This invoice is linked to %{what_link}.
linked_delivery: a delivery
linked_order: an order
new: new:
title: Create new invoice title: Create new invoice
show: show:

View file

@ -775,9 +775,6 @@ fr:
index: index:
action_new: Ajouter une facture action_new: Ajouter une facture
title: Factures title: Factures
linked: Cette facture est associée à %{what_link}.
linked_delivery: un réapprovisionnement
linked_order: une commande
new: new:
title: Ajouter une facture title: Ajouter une facture
show: show:

View file

@ -773,9 +773,6 @@ nl:
index: index:
action_new: Nieuwe factuur toevoegen action_new: Nieuwe factuur toevoegen
title: Facturen title: Facturen
linked: Deze factuur is aan %{what_link} gekoppeld.
linked_delivery: een levering
linked_order: een bestelling
new: new:
title: Nieuwe factuur toevoegen title: Nieuwe factuur toevoegen
show: show:

View file

@ -156,7 +156,9 @@ Foodsoft::Application.routes.draw do
end end
end end
resources :invoices resources :invoices do
get :form_on_supplier_id_change, on: :collection
end
resources :ordergroups, only: [:index] do resources :ordergroups, only: [:index] do
resources :financial_transactions, as: :transactions resources :financial_transactions, as: :transactions