Change relationship of invoices #390

This change will allow us to have invoices for more than one order/delivery
in the future. There are no UI changes for now.
This commit is contained in:
Patrick Gansterer 2016-02-25 12:56:34 +01:00 committed by wvengen
parent 9f919c3e54
commit 460cf1e82c
13 changed files with 70 additions and 38 deletions

View file

@ -1,7 +1,7 @@
class Finance::InvoicesController < ApplicationController
def index
@invoices = Invoice.includes(:supplier, :delivery, :order).order('date DESC').page(params[:page]).per(@per_page)
@invoices = Invoice.includes(:supplier, :deliveries, :orders).order('date DESC').page(params[:page]).per(@per_page)
end
def show
@ -9,9 +9,9 @@ class Finance::InvoicesController < ApplicationController
end
def new
@invoice = Invoice.new :supplier_id => params[:supplier_id],
:delivery_id => params[:delivery_id],
:order_id => params[:order_id]
@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]
end
def edit
@ -24,9 +24,9 @@ class Finance::InvoicesController < ApplicationController
if @invoice.save
flash[:notice] = I18n.t('finance.create.notice')
if @invoice.order
if @invoice.orders.count == 1
# Redirect to balancing page
redirect_to new_finance_order_url(order_id: @invoice.order.id)
redirect_to new_finance_order_url(order_id: @invoice.orders.first.id)
else
redirect_to [:finance, @invoice]
end

View file

@ -1,7 +1,7 @@
class Delivery < ActiveRecord::Base
belongs_to :supplier
has_one :invoice
belongs_to :invoice
has_many :stock_changes, -> { includes(:stock_article).order('articles.name ASC') }, :dependent => :destroy
scope :recent, -> { order('created_at DESC').limit(10) }

View file

@ -1,9 +1,9 @@
class Invoice < ActiveRecord::Base
belongs_to :supplier
belongs_to :delivery
belongs_to :order
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
has_many :deliveries
has_many :orders
validates_presence_of :supplier_id
validates_numericality_of :amount, :deposit, :deposit_credit

View file

@ -9,9 +9,9 @@ class Order < ActiveRecord::Base
has_many :group_orders, :dependent => :destroy
has_many :ordergroups, :through => :group_orders
has_many :users_ordered, :through => :ordergroups, :source => :users
has_one :invoice
has_many :comments, -> { order('created_at') }, :class_name => "OrderComment"
has_many :stock_changes
belongs_to :invoice
belongs_to :supplier
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id'
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'

View file

@ -1,11 +1,11 @@
= simple_form_for([:finance, @invoice]) do |f|
= f.hidden_field :delivery_id
= f.hidden_field :order_id
= f.association :deliveries, multiple: true, as: :hidden
= f.association :orders, multiple: true, as: :hidden
- if @invoice.delivery
%p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.delivery])).html_safe
- if @invoice.order
%p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_order'), new_finance_order_path(order_id: @invoice.order.id))).html_safe
- 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
= f.input :created_at do

View file

@ -10,8 +10,8 @@
%th= heading_helper Invoice, :date
%th= heading_helper Invoice, :paid_on
%th= heading_helper Invoice, :amount
%th= heading_helper Invoice, :delivery
%th= heading_helper Invoice, :order
%th= heading_helper Invoice, :deliveries
%th= heading_helper Invoice, :orders
%th= heading_helper Invoice, :note
%th
%th
@ -23,8 +23,14 @@
%td= link_to h(format_date invoice.date), finance_invoice_path(invoice)
%td= format_date invoice.paid_on
%td= number_to_currency invoice.amount
%td= link_to Delivery.model_name.human, [invoice.supplier,invoice.delivery] if invoice.delivery
%td= link_to format_date(invoice.order.ends), new_finance_order_path(order_id: invoice.order_id) if invoice.order
%td><
- invoice.deliveries.each_with_index do |delivery, index|
= ', ' if index > 0
= link_to format_date(delivery.delivered_on), [delivery.supplier,delivery]
%td><
- invoice.orders.each_with_index do |order, index|
= ', ' if index > 0
= link_to format_date(order.ends), new_finance_order_path(order_id: order)
%td= truncate(invoice.note)
%td= link_to t('ui.edit'), edit_finance_invoice_path(invoice), class: 'btn btn-mini'
%td= link_to t('ui.delete'), finance_invoice_path(invoice), :data => {:confirm => t('.confirm_delete')}, :method => :delete,

View file

@ -10,14 +10,20 @@
%b= heading_helper(Invoice, :supplier) + ':'
= @invoice.supplier.name
- if @invoice.delivery
- if @invoice.deliveries.count > 0
%p
%b= heading_helper(Invoice, :delivery) + ':'
= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.delivery])).html_safe
- if @invoice.order
%b= heading_helper(Invoice, :deliveries) + ':'
%span><
- @invoice.deliveries.order(:delivered_on).each_with_index do |delivery, index|
= ', ' if index > 0
= link_to format_date(delivery.delivered_on), [delivery.supplier,delivery]
- if @invoice.orders.count > 0
%p
%b= heading_helper(Invoice, :order) + ':'
= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_order'), @invoice.order)).html_safe
%b= heading_helper(Invoice, :orders) + ':'
%span><
- @invoice.orders.order(:ends).each_with_index do |order, index|
= ', ' if index > 0
= link_to format_date(order.ends), new_finance_order_path(order_id: order)
%p
%b= heading_helper(Invoice, :number) + ':'