diff --git a/app/controllers/finance/financial_links_controller.rb b/app/controllers/finance/financial_links_controller.rb new file mode 100644 index 00000000..7143674e --- /dev/null +++ b/app/controllers/finance/financial_links_controller.rb @@ -0,0 +1,27 @@ +class Finance::FinancialLinksController < Finance::BaseController + + def show + @financial_link = FinancialLink.find(params[:id]) + + @items = @financial_link.financial_transactions.map do |ft| + { + date: ft.created_on, + type: t('activerecord.models.financial_transaction'), + description: "#{ft.ordergroup.name}: #{ft.note}", + amount: ft.amount, + link_to: finance_ordergroup_transactions_path(ft.ordergroup) + } + end + @items += @financial_link.invoices.map do |invoice| + { + date: invoice.date || invoice.created_at, + type: t('activerecord.models.invoice'), + description: "#{invoice.supplier.name}: #{invoice.number}", + amount: invoice.amount, + link_to: finance_invoice_path(invoice) + } + end + @items.sort_by! { |item| item[:date] } + end + +end diff --git a/app/models/financial_link.rb b/app/models/financial_link.rb new file mode 100644 index 00000000..4b49f6f5 --- /dev/null +++ b/app/models/financial_link.rb @@ -0,0 +1,4 @@ +class FinancialLink < ActiveRecord::Base + has_many :financial_transactions + has_many :invoices +end diff --git a/app/models/financial_transaction.rb b/app/models/financial_transaction.rb index 631390b6..8ce83b0b 100644 --- a/app/models/financial_transaction.rb +++ b/app/models/financial_transaction.rb @@ -3,6 +3,7 @@ class FinancialTransaction < ActiveRecord::Base belongs_to :ordergroup belongs_to :user + belongs_to :financial_link validates_presence_of :amount, :note, :user_id, :ordergroup_id validates_numericality_of :amount, greater_then: -100_000, diff --git a/app/models/invoice.rb b/app/models/invoice.rb index e4b3af26..05390c35 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -3,6 +3,7 @@ class Invoice < ActiveRecord::Base belongs_to :supplier belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' + belongs_to :financial_link has_many :deliveries has_many :orders diff --git a/app/views/finance/financial_links/show.html.haml b/app/views/finance/financial_links/show.html.haml new file mode 100644 index 00000000..c4227ce0 --- /dev/null +++ b/app/views/finance/financial_links/show.html.haml @@ -0,0 +1,23 @@ +- title t('.title', number: @financial_link.id) + +%table.table.table-striped + %thead + %tr + %th= t '.date' + %th= t '.type' + %th= t '.description' + %th= t '.amount' + %tbody + - @items.each do |item| + %tr + %td + - if item[:link_to] + = link_to format_date(item[:date]), item[:link_to] + - else + = format_date item[:date] + %td= item[:type] + %td= item[:description] + %td.currency{:style => "color:#{item[:amount] < 0 ? 'red' : 'black'}; width:5em"}= number_to_currency(item[:amount]) + +%p + = @financial_link.note diff --git a/app/views/finance/financial_transactions/_transactions.html.haml b/app/views/finance/financial_transactions/_transactions.html.haml index b9af9370..279f0dcb 100644 --- a/app/views/finance/financial_transactions/_transactions.html.haml +++ b/app/views/finance/financial_transactions/_transactions.html.haml @@ -21,7 +21,11 @@ %tbody - @financial_transactions.each do |t| %tr - %td= format_time(t.created_on) + %td + - if t.financial_link + = link_to format_time(t.created_on), finance_link_path(t.financial_link) + - else + = format_time(t.created_on) - if with_ordergroup %td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup) %td= h show_user(t.user) diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index 067b0396..3f1eec2f 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -65,6 +65,10 @@ %p %b= heading_helper(Invoice, :note) + ':' =h @invoice.note +- if @invoice.financial_link + %p + %b= heading_helper(Invoice, :financial_link) + ':' + = link_to t('ui.show'), finance_link_path(@invoice.financial_link) - if @invoice.user_can_edit?(current_user) = link_to t('ui.edit'), edit_finance_invoice_path(@invoice), class: 'btn' diff --git a/config/locales/de.yml b/config/locales/de.yml index 77e942e7..cb8fc96a 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -894,6 +894,9 @@ de: without_extra_charge: 'ohne Aufschlag:' create: notice: Rechnung wurde erstellt. + financial_links: + show: + title: Finanzlink %{number} financial_transactions: controller: create: diff --git a/config/locales/en.yml b/config/locales/en.yml index 6af7185f..f38a9fb5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -896,6 +896,9 @@ en: without_extra_charge: 'without extra charge:' create: notice: Invoice was created. + financial_links: + show: + title: Financial link %{number} financial_transactions: controller: create: diff --git a/config/routes.rb b/config/routes.rb index 28a1e678..5eb546bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -163,6 +163,8 @@ Foodsoft::Application.routes.draw do get :unpaid, on: :collection end + resources :links, controller: 'financial_links', only: [:show] + resources :ordergroups, only: [:index] do resources :financial_transactions, as: :transactions end diff --git a/db/migrate/20171002000000_create_financial_links.rb b/db/migrate/20171002000000_create_financial_links.rb new file mode 100644 index 00000000..76375c40 --- /dev/null +++ b/db/migrate/20171002000000_create_financial_links.rb @@ -0,0 +1,10 @@ +class CreateFinancialLinks < ActiveRecord::Migration + def change + create_table :financial_links do |t| + t.text :note + end + + add_column :financial_transactions, :financial_link_id, :integer, index: true + add_column :invoices, :financial_link_id, :integer, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 31086758..ada899d0 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: 20171001020000) do +ActiveRecord::Schema.define(version: 20171002000000) do create_table "article_categories", force: :cascade do |t| t.string "name", limit: 255, default: "", null: false @@ -85,12 +85,17 @@ ActiveRecord::Schema.define(version: 20171001020000) do t.datetime "updated_at" end + create_table "financial_link", force: :cascade do |t| + t.text "note" + end + create_table "financial_transactions", force: :cascade do |t| - t.integer "ordergroup_id", limit: 4, default: 0, null: false - t.decimal "amount", precision: 8, scale: 2, default: 0, null: false - t.text "note", limit: 65535, null: false - t.integer "user_id", limit: 4, default: 0, null: false - t.datetime "created_on", null: false + t.integer "ordergroup_id", limit: 4, default: 0, null: false + t.decimal "amount", precision: 8, scale: 2, default: 0, null: false + t.text "note", limit: 65535, null: false + t.integer "user_id", limit: 4, default: 0, null: false + t.datetime "created_on", null: false + t.integer "financial_link_id" end add_index "financial_transactions", ["ordergroup_id"], name: "index_financial_transactions_on_ordergroup_id", using: :btree @@ -180,6 +185,7 @@ ActiveRecord::Schema.define(version: 20171001020000) do t.integer "created_by_user_id" t.string "attachment_mime" t.binary "attachment_data" + t.integer "financial_link_id" end add_index "invoices", ["supplier_id"], name: "index_invoices_on_supplier_id", using: :btree