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

View file

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

View file

@ -1,9 +1,9 @@
class Invoice < ActiveRecord::Base class Invoice < ActiveRecord::Base
belongs_to :supplier belongs_to :supplier
belongs_to :delivery
belongs_to :order
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' 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_presence_of :supplier_id
validates_numericality_of :amount, :deposit, :deposit_credit 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 :group_orders, :dependent => :destroy
has_many :ordergroups, :through => :group_orders has_many :ordergroups, :through => :group_orders
has_many :users_ordered, :through => :ordergroups, :source => :users has_many :users_ordered, :through => :ordergroups, :source => :users
has_one :invoice
has_many :comments, -> { order('created_at') }, :class_name => "OrderComment" has_many :comments, -> { order('created_at') }, :class_name => "OrderComment"
has_many :stock_changes has_many :stock_changes
belongs_to :invoice
belongs_to :supplier belongs_to :supplier
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id' 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' 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| = simple_form_for([:finance, @invoice]) do |f|
= f.hidden_field :delivery_id = f.association :deliveries, multiple: true, as: :hidden
= f.hidden_field :order_id = f.association :orders, multiple: true, as: :hidden
- if @invoice.delivery - if @invoice.deliveries.first
%p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.delivery])).html_safe %p= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.deliveries.first])).html_safe
- if @invoice.order - 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.order.id))).html_safe %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 - if @invoice.created_at
= f.input :created_at do = f.input :created_at do

View file

@ -10,8 +10,8 @@
%th= heading_helper Invoice, :date %th= heading_helper Invoice, :date
%th= heading_helper Invoice, :paid_on %th= heading_helper Invoice, :paid_on
%th= heading_helper Invoice, :amount %th= heading_helper Invoice, :amount
%th= heading_helper Invoice, :delivery %th= heading_helper Invoice, :deliveries
%th= heading_helper Invoice, :order %th= heading_helper Invoice, :orders
%th= heading_helper Invoice, :note %th= heading_helper Invoice, :note
%th %th
%th %th
@ -23,8 +23,14 @@
%td= link_to h(format_date invoice.date), finance_invoice_path(invoice) %td= link_to h(format_date invoice.date), finance_invoice_path(invoice)
%td= format_date invoice.paid_on %td= format_date invoice.paid_on
%td= number_to_currency invoice.amount %td= number_to_currency invoice.amount
%td= link_to Delivery.model_name.human, [invoice.supplier,invoice.delivery] if invoice.delivery %td><
%td= link_to format_date(invoice.order.ends), new_finance_order_path(order_id: invoice.order_id) if invoice.order - 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= truncate(invoice.note)
%td= link_to t('ui.edit'), edit_finance_invoice_path(invoice), class: 'btn btn-mini' %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, %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) + ':' %b= heading_helper(Invoice, :supplier) + ':'
= @invoice.supplier.name = @invoice.supplier.name
- if @invoice.delivery - if @invoice.deliveries.count > 0
%p %p
%b= heading_helper(Invoice, :delivery) + ':' %b= heading_helper(Invoice, :deliveries) + ':'
= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_delivery'), [@invoice.supplier,@invoice.delivery])).html_safe %span><
- if @invoice.order - @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 %p
%b= heading_helper(Invoice, :order) + ':' %b= heading_helper(Invoice, :orders) + ':'
= t('finance.invoices.linked', what_link: link_to(t('finance.invoices.linked_order'), @invoice.order)).html_safe %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 %p
%b= heading_helper(Invoice, :number) + ':' %b= heading_helper(Invoice, :number) + ':'

View file

@ -57,12 +57,12 @@ de:
created_at: Erstellt am created_at: Erstellt am
created_by: Erstellt von created_by: Erstellt von
date: Rechnungsdatum date: Rechnungsdatum
delivery: Lieferung deliveries: Lieferung
deposit: Pfand berechnet deposit: Pfand berechnet
deposit_credit: Pfand gutgeschrieben deposit_credit: Pfand gutgeschrieben
note: Notiz note: Notiz
number: Nummer number: Nummer
order: Bestellung orders: Bestellung
paid_on: Bezahlt am paid_on: Bezahlt am
supplier: Lieferant supplier: Lieferant
message: message:

View file

@ -57,12 +57,12 @@ en:
created_at: Created at created_at: Created at
created_by: Created by created_by: Created by
date: Billing date date: Billing date
delivery: Delivery deliveries: Delivery
deposit: Deposit charged deposit: Deposit charged
deposit_credit: Deposit returned deposit_credit: Deposit returned
note: Note note: Note
number: Number number: Number
order: Order orders: Order
paid_on: Paid on paid_on: Paid on
supplier: Supplier supplier: Supplier
message: message:

View file

@ -57,12 +57,12 @@ fr:
created_at: Créé le created_at: Créé le
created_by: "Établi par" created_by: "Établi par"
date: Date de facturation date: Date de facturation
delivery: Réapprovisionnement deliveries: Réapprovisionnement
deposit: Consigne facturée deposit: Consigne facturée
deposit_credit: Consigne remboursée deposit_credit: Consigne remboursée
note: Note note: Note
number: Numéro number: Numéro
order: Commande orders: Commande
paid_on: Payée le paid_on: Payée le
supplier: Fournisseur-e supplier: Fournisseur-e
message: message:

View file

@ -57,12 +57,12 @@ nl:
created_at: Gemaakt op created_at: Gemaakt op
created_by: Gemaakt door created_by: Gemaakt door
date: Factuurdatum date: Factuurdatum
delivery: Levering deliveries: Levering
deposit: Statiegeld in rekening gebracht deposit: Statiegeld in rekening gebracht
deposit_credit: Statiegeld teruggekregen deposit_credit: Statiegeld teruggekregen
note: Notitie note: Notitie
number: Nummer number: Nummer
order: Bestelling orders: Bestelling
paid_on: Betaald op paid_on: Betaald op
supplier: Leverancier supplier: Leverancier
message: message:

View file

@ -0,0 +1,21 @@
class ChangeInvoiceRelation < ActiveRecord::Migration
def up
add_column :deliveries, :invoice_id, :integer
execute 'UPDATE deliveries SET invoice_id = (SELECT id FROM invoices WHERE delivery_id = deliveries.id)'
remove_column :invoices, :delivery_id
add_column :orders, :invoice_id, :integer
execute 'UPDATE orders SET invoice_id = (SELECT id FROM invoices WHERE order_id = orders.id)'
remove_column :invoices, :order_id
end
def down
add_column :invoices, :delivery_id
execute 'UPDATE invoices SET delivery_id = (SELECT id FROM deliveries WHERE invoice_id = invoices.id)'
remove_column :deliveries, :invoice_id, :integer
add_column :invoices, :order_id
execute 'UPDATE invoices SET order_id = (SELECT id FROM orders WHERE invoice_id = invoices.id)'
remove_column :orders, :invoice_id, :integer
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160217124256) do ActiveRecord::Schema.define(version: 20160217134742) do
create_table "article_categories", force: :cascade do |t| create_table "article_categories", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false t.string "name", limit: 255, default: "", null: false
@ -71,6 +71,7 @@ ActiveRecord::Schema.define(version: 20160217124256) do
t.date "delivered_on" t.date "delivered_on"
t.datetime "created_at" t.datetime "created_at"
t.text "note", limit: 65535 t.text "note", limit: 65535
t.integer "invoice_id"
end end
add_index "deliveries", ["supplier_id"], name: "index_deliveries_on_supplier_id", using: :btree add_index "deliveries", ["supplier_id"], name: "index_deliveries_on_supplier_id", using: :btree
@ -155,8 +156,6 @@ ActiveRecord::Schema.define(version: 20160217124256) do
create_table "invoices", force: :cascade do |t| create_table "invoices", force: :cascade do |t|
t.integer "supplier_id", limit: 4 t.integer "supplier_id", limit: 4
t.integer "delivery_id", limit: 4
t.integer "order_id", limit: 4
t.string "number", limit: 255 t.string "number", limit: 255
t.date "date" t.date "date"
t.date "paid_on" t.date "paid_on"
@ -169,7 +168,6 @@ ActiveRecord::Schema.define(version: 20160217124256) do
t.integer "created_by_user_id" t.integer "created_by_user_id"
end end
add_index "invoices", ["delivery_id"], name: "index_invoices_on_delivery_id", using: :btree
add_index "invoices", ["supplier_id"], name: "index_invoices_on_supplier_id", using: :btree add_index "invoices", ["supplier_id"], name: "index_invoices_on_supplier_id", using: :btree
create_table "memberships", force: :cascade do |t| create_table "memberships", force: :cascade do |t|
@ -226,6 +224,7 @@ ActiveRecord::Schema.define(version: 20160217124256) do
t.decimal "foodcoop_result", precision: 8, scale: 2 t.decimal "foodcoop_result", precision: 8, scale: 2
t.integer "created_by_user_id", limit: 4 t.integer "created_by_user_id", limit: 4
t.datetime "boxfill" t.datetime "boxfill"
t.integer "invoice_id"
end end
add_index "orders", ["state"], name: "index_orders_on_state", using: :btree add_index "orders", ["state"], name: "index_orders_on_state", using: :btree