Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invoice - check overlap dates #261

Merged
merged 3 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# id :integer not null, primary key
# organization_id :integer not null
# customer_id :integer not null
# starts_at :datetime
# ends_at :datetime not null
# starts_at :date
# ends_at :date not null
# currency :string default("USD"), not null
# amount_cents :integer default(0), not null
# sent_at :datetime
Expand All @@ -32,18 +32,32 @@ class Invoice < ActiveRecord::Base
validates :ends_at, presence: true
validates :amount, presence: true
validates :currency, presence: true
validates :customer_name, presence: true

validates :amount, numericality: { greater_than: 0,
less_than_or_equal_to: Dictionaries.money_max }
validates :currency, inclusion: { in: Dictionaries.currencies,
message: "%{value} is not a valid currency" }
validate :check_overlap_dates_for_customer, if: 'customer_name.present?'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yunixon please check this gem https://github.com/robinbortlik/validates_overlap I think it will be better to use this, instead of custom solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexanderUlitin this gem show overlaping error only on starts_at field, even if field not present


scope :ordered, -> { order('created_at DESC') }
scope :ordered, -> { order('created_at DESC') }
scope :all_except, -> (invoice) { where.not(id: invoice) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yunixon you don't use this scope, let's remove it.


after_save :set_currency

private

def check_overlap_dates_for_customer
self.customer.invoices.all_except(self.id).where(organization: self.organization).each do |i|
self_start = self.starts_at ? self.starts_at : self.ends_at
i_start = i.starts_at ? i.starts_at : i.ends_at
if (self_start..self.ends_at).overlaps? (i_start..i.ends_at)
errors.add(:starts_at, 'Overlap date for this customer') if self.starts_at
errors.add(:ends_at, 'Overlap date for this customer')
end
end
end

def set_currency
invoice_items.each{ |i| i.update(currency: currency) }
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20150910054909_change_invoice_date_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeInvoiceDateColumns < ActiveRecord::Migration
def change
change_column :invoices, :starts_at, :date
change_column :invoices, :ends_at, :date
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150909054901) do
ActiveRecord::Schema.define(version: 20150910054909) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -94,8 +94,8 @@
create_table "invoices", force: :cascade do |t|
t.integer "organization_id", null: false
t.integer "customer_id", null: false
t.datetime "starts_at"
t.datetime "ends_at", null: false
t.date "starts_at"
t.date "ends_at", null: false
t.string "currency", default: "USD", null: false
t.integer "amount_cents", default: 0, null: false
t.datetime "sent_at"
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
factory :invoice do
organization
customer
customer_name
ends_at { Time.now }
currency 'RUB'
amount 500
Expand Down
43 changes: 41 additions & 2 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# id :integer not null, primary key
# organization_id :integer not null
# customer_id :integer not null
# starts_at :datetime
# ends_at :datetime not null
# starts_at :date
# ends_at :date not null
# currency :string default("USD"), not null
# amount_cents :integer default(0), not null
# sent_at :datetime
Expand All @@ -28,8 +28,47 @@
it { is_expected.to validate_presence_of(:organization) }
it { is_expected.to validate_presence_of(:ends_at) }
it { is_expected.to validate_presence_of(:currency) }
it { is_expected.to validate_presence_of(:customer_name) }
it { is_expected.to validate_numericality_of(:amount).
is_greater_than(0).is_less_than_or_equal_to(Dictionaries.money_max) }
it { is_expected.to validate_inclusion_of(:currency).in_array(Dictionaries.currencies) }

context 'check customer date range crossing' do
let!(:org) { create :organization }
let!(:customer) { create :customer }
let!(:invoice) { create :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 10.days, ends_at: Date.today }
let(:invoice1) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 9.days, ends_at: Date.today + 1.days }
let(:invoice2) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 11.days, ends_at: Date.today }
let(:invoice3) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 9.days, ends_at: Date.today }
let(:invoice4) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 11.days, ends_at: Date.today + 1.days }
let(:invoice5) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today + 2.days, ends_at: Date.today + 10.days }

it 'Show error on starts_at' do
invoice1.valid?
expect(invoice1.errors[:ends_at]).to include('Overlap date for this customer')
end
it 'Show error on ends_at' do
invoice2.valid?
expect(invoice2.errors[:ends_at]).to include('Overlap date for this customer')
end
it 'Show errors on starts_at and ends_at' do
invoice3.valid?
expect(invoice3.errors[:ends_at]).to include('Overlap date for this customer')
end
it 'Show errors on starts_at and ends_at' do
invoice4.valid?
expect(invoice4.errors[:ends_at]).to include('Overlap date for this customer')
end
it 'Dont show errors on starts_at and ends_at' do
invoice5.valid?
expect(invoice5.errors[:ends_at]).to_not include('Overlap date for this customer')
end
end
end
end