Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2508 from ekylibre/debug/journal-entry-should-be-…
…locked-during-opened-exchanges-only

JournalEntries should be locked during opened exchanges only
  • Loading branch information
Aquaj committed Apr 12, 2019
2 parents 53a1f02 + a2af9c0 commit e119e91
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
20 changes: 10 additions & 10 deletions app/models/journal_entry.rb
Expand Up @@ -250,7 +250,7 @@ def self.states
errors.add(:printed_on, :out_of_existing_financial_year)
end
end
if in_financial_year_exchange? && !importing_from_exchange
if in_opened_financial_year_exchange? && !importing_from_exchange
errors.add(:printed_on, :frozen_by_financial_year_exchange)
end
errors.add(:items, :empty) unless items.any?
Expand Down Expand Up @@ -400,15 +400,15 @@ def mark_for_exchange_import!

private

attr_accessor :importing_from_exchange
attr_accessor :importing_from_exchange

#
def add!(name, account, amount, options = {})
items.create!(JournalEntryItem.attributes_for(name, account, amount, options))
end
#
def add!(name, account, amount, options = {})
items.create!(JournalEntryItem.attributes_for(name, account, amount, options))
end

def in_financial_year_exchange?
return unless financial_year
financial_year.exchanges.any? { |e| (e.started_on..e.stopped_on).cover?(printed_on) }
end
def in_opened_financial_year_exchange?
return unless financial_year
financial_year.exchanges.opened.any? { |e| (e.started_on..e.stopped_on).cover?(printed_on) }
end
end
26 changes: 23 additions & 3 deletions test/models/journal_entry_test.rb
Expand Up @@ -217,25 +217,45 @@ class JournalEntryTest < ActiveSupport::TestCase
assert_equal ['is it me you\'re looking for?', 'EUR', 1.0], item_attributes
end

test 'cannot be created when in financial year exchange date range' do
test 'cannot be created when in opened financial year exchange date range' do
financial_year = financial_years(:financial_years_025)
exchange = create(:financial_year_exchange, financial_year: financial_year)
exchange = create(:financial_year_exchange, :opened, financial_year: financial_year)
journal = create(:journal)
entry = JournalEntry.new(journal: journal, printed_on: exchange.stopped_on + 1.day, items: fake_items)
assert entry.valid?
entry.printed_on = exchange.started_on + 1.day
refute entry.valid?
end

test 'cannot be updated to a date in financial year exchange date range' do
test 'can be created when in closed financial year exchange date range' do
financial_year = financial_years(:financial_years_025)
exchange = create(:financial_year_exchange, financial_year: financial_year)
journal = create(:journal)
entry = JournalEntry.new(journal: journal, printed_on: exchange.stopped_on + 1.day, items: fake_items)
assert entry.valid?
entry.printed_on = exchange.started_on + 1.day
assert entry.valid?
end

test 'cannot be updated to a date in opened financial year exchange date range' do
financial_year = financial_years(:financial_years_025)
exchange = create(:financial_year_exchange, :opened, financial_year: financial_year)
entry = create(:journal_entry, printed_on: exchange.stopped_on + 1.day, items: fake_items)
assert entry.valid?
entry.printed_on = exchange.started_on + 1.day
refute entry.valid?
end

test 'can be updated to a date in closed financial year exchange date range' do
financial_year = financial_years(:financial_years_025)
exchange = create(:financial_year_exchange, financial_year: financial_year)
exchange.close!
entry = create(:journal_entry, printed_on: exchange.stopped_on + 1.day, items: fake_items)
assert entry.valid?
entry.printed_on = exchange.started_on + 1.day
assert entry.valid?
end

def fake_items(options = {})
amount = options[:amount] || (500 * rand + 1).round(2)
name = options[:name] || 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
Expand Down

0 comments on commit e119e91

Please sign in to comment.