diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b4c4ea8c..456968d7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base include Concerns::FoodcoopScope include Concerns::Auth include Concerns::Locale + include PathHelper helper_method :current_user helper_method :available_locales diff --git a/app/controllers/finance/base_controller.rb b/app/controllers/finance/base_controller.rb index ef83872f..dd696c0c 100644 --- a/app/controllers/finance/base_controller.rb +++ b/app/controllers/finance/base_controller.rb @@ -2,7 +2,7 @@ class Finance::BaseController < ApplicationController before_action :authenticate_finance def index - @financial_transactions = FinancialTransaction.includes(:ordergroup).order('created_on DESC').limit(8) + @financial_transactions = FinancialTransaction.with_ordergroup.includes(:ordergroup).order(created_on: :desc).limit(8) @orders = Order.finished_not_closed.includes(:supplier).limit(8) @unpaid_invoices = Invoice.unpaid.includes(:supplier).limit(8) diff --git a/app/controllers/finance/financial_links_controller.rb b/app/controllers/finance/financial_links_controller.rb index a23406c1..a4dfacc6 100644 --- a/app/controllers/finance/financial_links_controller.rb +++ b/app/controllers/finance/financial_links_controller.rb @@ -16,7 +16,7 @@ class Finance::FinancialLinksController < Finance::BaseController { date: ft.created_on, type: t('activerecord.models.financial_transaction'), - description: "#{ft.ordergroup.name}: #{ft.note}", + description: "#{ft.ordergroup_name}: #{ft.note}", amount: ft.amount, link_to: finance_ordergroup_transactions_path(ft.ordergroup), remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft) diff --git a/app/models/financial_transaction.rb b/app/models/financial_transaction.rb index 78b2dc91..63ad09a2 100644 --- a/app/models/financial_transaction.rb +++ b/app/models/financial_transaction.rb @@ -9,12 +9,13 @@ class FinancialTransaction < ApplicationRecord belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' - validates_presence_of :amount, :note, :user_id, :ordergroup_id + validates_presence_of :amount, :note, :user_id validates_numericality_of :amount, greater_then: -100_000, less_than: 100_000 scope :visible, -> { joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil) } scope :without_financial_link, -> { where(financial_link: nil) } + scope :with_ordergroup, -> { where.not(ordergroup: nil) } localize_input_of :amount @@ -54,6 +55,10 @@ class FinancialTransaction < ApplicationRecord reverts.present? || reverted_by.present? end + def ordergroup_name + ordergroup ? ordergroup.name : I18n.t('model.financial_transaction.foodcoop_name') + end + # @todo rename in model, see #575 def created_at created_on diff --git a/app/views/finance/financial_links/_index_financial_transaction.html.haml b/app/views/finance/financial_links/_index_financial_transaction.html.haml index b2437412..9b7a293c 100644 --- a/app/views/finance/financial_links/_index_financial_transaction.html.haml +++ b/app/views/finance/financial_links/_index_financial_transaction.html.haml @@ -14,7 +14,7 @@ - for t in @financial_transactions %tr %td= link_to format_time(t.created_on), add_financial_transaction_finance_link_path(@financial_link, financial_transaction: t.id), method: :put - %td= t.ordergroup.name + %td= t.ordergroup_name - if FinancialTransactionType.has_multiple_types %td= h t.financial_transaction_type.name %td= number_to_currency t.amount diff --git a/app/views/finance/financial_transactions/_transactions.html.haml b/app/views/finance/financial_transactions/_transactions.html.haml index f12bb48c..cbca8059 100644 --- a/app/views/finance/financial_transactions/_transactions.html.haml +++ b/app/views/finance/financial_transactions/_transactions.html.haml @@ -34,7 +34,7 @@ - else = format_time(t.created_on) - if with_ordergroup - %td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup) + %td= h link_to t.ordergroup_name, finance_ordergroup_transactions_path(t.ordergroup) %td= h show_user(t.user) - if FinancialTransactionType.has_multiple_types %td= h t.financial_transaction_type.name diff --git a/app/views/finance/index.html.haml b/app/views/finance/index.html.haml index e476ced4..50ee822c 100644 --- a/app/views/finance/index.html.haml +++ b/app/views/finance/index.html.haml @@ -34,7 +34,7 @@ - @financial_transactions.each do |ft| %tr %td= format_date(ft.created_on) - %td= ft.ordergroup.name + %td= ft.ordergroup_name %td= ft.note %td.numeric= format_currency ft.amount .span6 diff --git a/config/locales/de.yml b/config/locales/de.yml index 5623aa92..9a0e864d 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1272,6 +1272,8 @@ de: model: delivery: each_stock_article_must_be_unique: Lieferung darf jeden Lagerartikel höchstens einmal auflisten. + financial_transaction: + foodcoop_name: Foodcoop financial_transaction_type: no_delete_last: Es muss mindestens ein Kontotransaktionstyp existieren. group_order: diff --git a/config/locales/en.yml b/config/locales/en.yml index e69d2bea..3eb1bb30 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1311,6 +1311,8 @@ en: model: delivery: each_stock_article_must_be_unique: Each stock article must not be listed more than once. + financial_transaction: + foodcoop_name: Foodcoop financial_transaction_type: no_delete_last: At least one financial transaction type must exist. group_order: diff --git a/db/migrate/20181201000300_allow_emtpy_ordergroup_in_financial_transaction.rb b/db/migrate/20181201000300_allow_emtpy_ordergroup_in_financial_transaction.rb new file mode 100644 index 00000000..fae980cc --- /dev/null +++ b/db/migrate/20181201000300_allow_emtpy_ordergroup_in_financial_transaction.rb @@ -0,0 +1,5 @@ +class AllowEmtpyOrdergroupInFinancialTransaction < ActiveRecord::Migration + def change + change_column_null :financial_transactions, :ordergroup_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index c1638fca..4ff4dc85 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -130,7 +130,7 @@ ActiveRecord::Schema.define(version: 20181205010000) do add_index "financial_transaction_types", ["name_short"], name: "index_financial_transaction_types_on_name_short", using: :btree create_table "financial_transactions", force: :cascade do |t| - t.integer "ordergroup_id", limit: 4, default: 0, null: false + t.integer "ordergroup_id", limit: 4 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