From 460cf1e82cd49335957ef61c8ff073ae7cedc437 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Thu, 25 Feb 2016 12:56:34 +0100 Subject: [PATCH] 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. --- .../finance/invoices_controller.rb | 12 +++++------ app/models/delivery.rb | 2 +- app/models/invoice.rb | 4 ++-- app/models/order.rb | 2 +- app/views/finance/invoices/_form.html.haml | 12 +++++------ .../finance/invoices/_invoices.html.haml | 14 +++++++++---- app/views/finance/invoices/show.html.haml | 18 ++++++++++------ config/locales/de.yml | 4 ++-- config/locales/en.yml | 4 ++-- config/locales/fr.yml | 4 ++-- config/locales/nl.yml | 4 ++-- .../20160217134742_change_invoice_relation.rb | 21 +++++++++++++++++++ db/schema.rb | 7 +++---- 13 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 db/migrate/20160217134742_change_invoice_relation.rb diff --git a/app/controllers/finance/invoices_controller.rb b/app/controllers/finance/invoices_controller.rb index a38328db..a21d2f96 100644 --- a/app/controllers/finance/invoices_controller.rb +++ b/app/controllers/finance/invoices_controller.rb @@ -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 diff --git a/app/models/delivery.rb b/app/models/delivery.rb index 0ef7b62d..367cdc1f 100644 --- a/app/models/delivery.rb +++ b/app/models/delivery.rb @@ -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) } diff --git a/app/models/invoice.rb b/app/models/invoice.rb index fa8c1a1a..0491e153 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index ff2dbf38..4ad020dd 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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' diff --git a/app/views/finance/invoices/_form.html.haml b/app/views/finance/invoices/_form.html.haml index 7777d92a..2b305756 100644 --- a/app/views/finance/invoices/_form.html.haml +++ b/app/views/finance/invoices/_form.html.haml @@ -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 diff --git a/app/views/finance/invoices/_invoices.html.haml b/app/views/finance/invoices/_invoices.html.haml index c701bdab..830eb0e2 100644 --- a/app/views/finance/invoices/_invoices.html.haml +++ b/app/views/finance/invoices/_invoices.html.haml @@ -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, diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index 97034a04..748de7f4 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -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) + ':' diff --git a/config/locales/de.yml b/config/locales/de.yml index 0b8661a5..8950c7ff 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -57,12 +57,12 @@ de: created_at: Erstellt am created_by: Erstellt von date: Rechnungsdatum - delivery: Lieferung + deliveries: Lieferung deposit: Pfand berechnet deposit_credit: Pfand gutgeschrieben note: Notiz number: Nummer - order: Bestellung + orders: Bestellung paid_on: Bezahlt am supplier: Lieferant message: diff --git a/config/locales/en.yml b/config/locales/en.yml index 46855dc2..3ddc80b9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -57,12 +57,12 @@ en: created_at: Created at created_by: Created by date: Billing date - delivery: Delivery + deliveries: Delivery deposit: Deposit charged deposit_credit: Deposit returned note: Note number: Number - order: Order + orders: Order paid_on: Paid on supplier: Supplier message: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index a214ad9a..fc31d566 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -57,12 +57,12 @@ fr: created_at: Créé le created_by: "Établi par" date: Date de facturation - delivery: Réapprovisionnement + deliveries: Réapprovisionnement deposit: Consigne facturée deposit_credit: Consigne remboursée note: Note number: Numéro - order: Commande + orders: Commande paid_on: Payée le supplier: Fournisseur-e message: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 304fdbb9..f10f1546 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -57,12 +57,12 @@ nl: created_at: Gemaakt op created_by: Gemaakt door date: Factuurdatum - delivery: Levering + deliveries: Levering deposit: Statiegeld in rekening gebracht deposit_credit: Statiegeld teruggekregen note: Notitie number: Nummer - order: Bestelling + orders: Bestelling paid_on: Betaald op supplier: Leverancier message: diff --git a/db/migrate/20160217134742_change_invoice_relation.rb b/db/migrate/20160217134742_change_invoice_relation.rb new file mode 100644 index 00000000..55c3ad98 --- /dev/null +++ b/db/migrate/20160217134742_change_invoice_relation.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ac1d804c..edffedda 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| t.string "name", limit: 255, default: "", null: false @@ -71,6 +71,7 @@ ActiveRecord::Schema.define(version: 20160217124256) do t.date "delivered_on" t.datetime "created_at" t.text "note", limit: 65535 + t.integer "invoice_id" end 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| t.integer "supplier_id", limit: 4 - t.integer "delivery_id", limit: 4 - t.integer "order_id", limit: 4 t.string "number", limit: 255 t.date "date" t.date "paid_on" @@ -169,7 +168,6 @@ ActiveRecord::Schema.define(version: 20160217124256) do t.integer "created_by_user_id" 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 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.integer "created_by_user_id", limit: 4 t.datetime "boxfill" + t.integer "invoice_id" end add_index "orders", ["state"], name: "index_orders_on_state", using: :btree