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

Sj fulfillment validation #569

Merged
merged 5 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 21 additions & 19 deletions app/models/fulfillment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class Fulfillment < ActiveRecord::Base
attr_accessible :unit_quantity
attr_accessible :quantity_type
attr_accessible :unit_type
attr_accessible :formatted_date
# attr_accessible :formatted_date

validates :date, presence: true

validates :time, format: { with: /\A\d+(?:\.\d{0,2})?\z/,
message: 'cannot be a decimal with more than two places after the decimal point. Correct format: "1.23"' },
Expand All @@ -47,16 +49,16 @@ class Fulfillment < ActiveRecord::Base
UNIT_TYPES = ['N/A', 'Each', 'Sample', 'Aliquot', '3kg unit']

def date=(date_arg)
write_attribute(:date, Time.strptime(date_arg, "%m-%d-%Y")) if date_arg.present?
write_attribute(:date, Time.strptime(date_arg, "%m/%d/%Y")) if date_arg.present?
end

def formatted_date
format_date self.date
end
# def formatted_date
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove me

# format_date self.date
# end

def formatted_date=(date)
self.date = parse_date(date)
end
# def formatted_date=(date)
# self.date = parse_date(date)
# end

def within_date_range? start_date, end_date
date = self.date.try(:to_date)
Expand All @@ -72,15 +74,15 @@ def within_date_range? start_date, end_date

private

def format_date(date)
date.try(:strftime, '%-m/%d/%Y')
end

def parse_date(str)
begin
Date.strptime(str.to_s.strip, '%m/%d/%Y').strftime("%m-%d-%Y")
rescue ArgumentError => e
nil
end
end
# def format_date(date)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove me

# date.try(:strftime, '%-m/%d/%Y')
# end

# def parse_date(str)
# begin
# Date.strptime(str.to_s.strip, '%m/%d/%Y').strftime("%m-%d-%Y")
# rescue ArgumentError => e
# nil
# end
# end
end
2 changes: 1 addition & 1 deletion app/views/dashboard/fulfillments/edit.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

$("#modal_place").html("<%= escape_javascript(render(:partial =>'dashboard/study_level_activities/fulfillment_form', locals: { fulfillment: @fulfillment, header_text: @header_text })) %>");
$("#modal_place").modal 'show'
$("#date_fulfilled_field").datetimepicker(format: 'MM-DD-YYYY')
$("#date_fulfilled_field").datetimepicker(format: 'MM/DD/YYYY', allowInputToggle: true)
$(".selectpicker").selectpicker()
2 changes: 1 addition & 1 deletion app/views/dashboard/fulfillments/new.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

$("#modal_place").html("<%= escape_javascript(render(:partial =>'dashboard/study_level_activities/fulfillment_form', locals: { fulfillment: @fulfillment, header_text: @header_text })) %>");
$("#modal_place").modal 'show'
$("#date_fulfilled_field").datetimepicker(format: 'MM-DD-YYYY')
$(".selectpicker").selectpicker()
$(".datetimepicker").datetimepicker(format: 'MM/DD/YYYY', allowInputToggle: true)
2 changes: 1 addition & 1 deletion app/views/dashboard/line_items/edit.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ $("#modal_place").html("<%= escape_javascript(render(:partial =>'dashboard/study
<% end %>
$("#modal_place").modal 'show'
$(".selectpicker").selectpicker()
$(".datetimepicker").datetimepicker(format: 'MM-DD-YYYY', allowInputToggle: true)
$(".datetimepicker").datetimepicker(format: 'MM/DD/YYYY', allowInputToggle: true)
2 changes: 1 addition & 1 deletion app/views/dashboard/line_items/new.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
$("#modal_place").html("<%= escape_javascript(render(:partial =>'dashboard/study_level_activities/study_level_activity_form', locals: { line_item: @line_item, header_text: @header_text })) %>");
$("#modal_place").modal 'show'
$(".selectpicker").selectpicker()
$(".datetimepicker").datetimepicker(format: 'MM-DD-YYYY', allowInputToggle: true)
$(".datetimepicker").datetimepicker(format: 'MM/DD/YYYY', allowInputToggle: true)
6 changes: 3 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ en:
edit: " Edit Fulfillment"
delete: " Delete Fulfillment"
form:
date: "Fulfillment Date:"
time: "Quantity:"
time_unit: "Qty Type:"
date: "Fulfillment Date: *"
Copy link
Contributor

Choose a reason for hiding this comment

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

Everywhere else in the application we put the '*' directly after the ':'

i.e. Fulfillment Date:*

time: "Quantity: *"
time_unit: "Qty Type: *"

################
# LINE ITEMS #
Expand Down
43 changes: 12 additions & 31 deletions spec/models/fulfillment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,30 @@
RSpec.describe Fulfillment, type: :model do
describe '#formatted_date' do
context 'date present' do
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m-%d-%Y")) }
it 'should return date in format: -m/%d/%Y' do
expect(fulfillment.formatted_date).to eq '1/02/2001'
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m/%d/%Y")) }
it 'should return date in format: m/%d/%Y' do
expect(fulfillment.date.strftime("%m/%d/%Y")).to eq '01/02/2001'
end
end

context 'date not present' do
let!(:fulfillment) { Fulfillment.create(date: nil) }

it 'should return nil' do
expect(fulfillment.formatted_date).to eq nil
end
end
end

describe '#formatted_date=' do
let!(:fulfillment) { Fulfillment.create(date: nil) }

context 'right hand side is a valid date string in the form: %m/%d/%Y' do
it 'should update date' do
fulfillment.formatted_date = '02/01/2000'
expect(fulfillment.date.to_date).to eq Date.new(2000, 2, 1)
end
end

context 'right hand side is not a valid date string in the form: %m/%d/%Y' do
it 'should set date to nil' do
fulfillment.formatted_date = '13/01/2000'
expect(fulfillment.date).to eq nil
fulfillment = build(:fulfillment, date: nil)
expect(fulfillment).not_to be_valid
end
end
end

describe '#within_date_range?' do
context 'start_date nil' do
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m-%d-%Y")) }
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m/%d/%Y")) }
it 'should return false' do
expect(fulfillment.within_date_range?(nil, Date.new(2002, 1, 2))).to eq false
end
end

context 'end_date nil' do
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m-%d-%Y")) }
let!(:fulfillment) { Fulfillment.create(date: Date.new(2001, 1, 2).strftime("%m/%d/%Y")) }
it 'should return false' do
expect(fulfillment.within_date_range?(Date.new(2000, 1, 2), nil)).to eq false
end
Expand All @@ -64,7 +45,7 @@

it 'should return true if Fulfillment date occurs on or after start date and occurs on or before end_date' do
@dates.combination(3) do |start_date, date, end_date|
fulfillment.update_attributes(date: date.strftime("%m-%d-%Y"))
fulfillment.update_attributes(date: date.strftime("%m/%d/%Y"))
expect(fulfillment.within_date_range?(start_date, end_date)).to eq(
(date >= start_date) && (date <= end_date))
end
Expand All @@ -74,25 +55,25 @@

describe 'time validation' do
it 'should validate the format of time' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 1.23)
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m/%d/%Y"), time: 1.23)

expect(fulfillment).to be_valid
end

it 'should not validate the format of time up to two numbers after decimal point' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 1.23434)
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m/%d/%Y"), time: 1.23434)

expect(fulfillment).not_to be_valid
end

it 'should not validate the format of time - should be greater than zero' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 0)
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m/%d/%Y"), time: 0)

expect(fulfillment).not_to be_valid
end

it 'should not validate the format of time - no strings allowed' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 'ooga booga')
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m/%d/%Y"), time: 'ooga booga')

expect(fulfillment).not_to be_valid
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@
context "direct costs for one time fees with fulfillments" do

let!(:otf_line_item) { create(:line_item, service_request_id: service_request.id, service_id: service.id, sub_service_request_id: sub_service_request.id, quantity: 5, units_per_quantity: 1) }
let!(:fulfillment1) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.yesterday.strftime("%m-%d-%Y"), time: 1.23) }
let!(:fulfillment2) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.today.strftime("%m-%d-%Y"), time: 1.23) }
let!(:fulfillment3) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.today.strftime("%m-%d-%Y"), time: 1.23) }
let!(:fulfillment1) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.yesterday.strftime("%m/%d/%Y"), time: 1.23) }
let!(:fulfillment2) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.today.strftime("%m/%d/%Y"), time: 1.23) }
let!(:fulfillment3) { create(:fulfillment, quantity: 5, line_item_id: otf_line_item.id, date: Date.today.strftime("%m/%d/%Y"), time: 1.23) }
let!(:pricing_map2) { create(:pricing_map, service_id: service.id, unit_type: 'ea', effective_date: Date.today, display_date: Date.today, full_rate: 600, exclude_from_indirect_cost: 0, unit_minimum: 1)}

it "should correctly calculate a line item's cost that has multiple fulfillments" do
Expand All @@ -253,7 +253,7 @@

it "should correctly calculate a line item's cost that has a unit factor greater than one" do
pricing_map2.update_attributes(unit_factor: 5)
fulfillment3 = create(:fulfillment, quantity: 6, line_item_id: otf_line_item.id, date: Date.today.strftime("%m-%d-%Y"), time: 1.23)
fulfillment3 = create(:fulfillment, quantity: 6, line_item_id: otf_line_item.id, date: Date.today.strftime("%m/%d/%Y"), time: 1.23)
# ceiling(quantity:16/unit_factor:5) * rate:(percentage:0.5 * cost:600)
expect(otf_line_item.reload.direct_cost_for_one_time_fee_with_fulfillments(Date.today, Date.today)).to eq(1200.0)
end
Expand Down