Allow editing of linked deliveries and orders at invoice
This commit is contained in:
parent
14e2fd179e
commit
efb929e64f
12 changed files with 72 additions and 34 deletions
|
@ -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
|
||||
|
|
8
app/helpers/finance/invoices_helper.rb
Normal file
8
app/helpers/finance/invoices_helper.rb
Normal 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
|
|
@ -1,12 +1,5 @@
|
|||
= 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
|
||||
|
||||
- if @invoice.created_at
|
||||
.fold-line
|
||||
.control-group
|
||||
%label.control-label{for: 'created_at'}
|
||||
|
@ -19,6 +12,9 @@
|
|||
.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}
|
||||
|
|
14
app/views/finance/invoices/_form_js.html.haml
Normal file
14
app/views/finance/invoices/_form_js.html.haml
Normal 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()}
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,2 +1,2 @@
|
|||
- title t('.title')
|
||||
= render :partial => 'form'
|
||||
= render :partial => 'form', :locals => {:invoice_id => @invoice.id}
|
||||
|
|
16
app/views/finance/invoices/form_on_supplier_id_change.js.erb
Normal file
16
app/views/finance/invoices/form_on_supplier_id_change.js.erb
Normal 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 %>);
|
||||
})();
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue