Merge pull request #421 from foodcoop1040/edit_links

Add page to edit deliveries and orders linked to an invoice
This commit is contained in:
wvengen 2016-03-11 13:39:27 +01:00
commit 4433a665e4
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.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

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,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}

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')
= 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')
= render :partial => 'form'
= render :partial => 'form', :locals => {:invoice_id => 0}
= link_to t('ui.or_cancel'), finance_invoices_path

View File

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

View File

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

View File

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

View File

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

View File

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