Skip to content
This repository
Browse code

WIP - fixing centers and center_meeting_days once and for all

  • Loading branch information...
commit 03ea9d8ebf79b881978cc33f12d51e07bc0f766b 1 parent 05153ae
Siddharth Sharma authored
145 app/models/center.rb
@@ -2,14 +2,8 @@ class Center
2 2 include DataMapper::Resource
3 3 include DateParser
4 4
5   - attr_accessor :meeting_day_change_date
6   -
7   - before :save, :convert_blank_to_nil
8   - after :save, :handle_meeting_date_change
9   - before :save, :set_meeting_change_date
10   - before :create, :set_meeting_change_date
11 5 before :valid?, :convert_blank_to_nil
12   -
  6 + before :valid?, :handle_meeting_days
13 7
14 8 DAYS = [:none, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
15 9
@@ -19,7 +13,7 @@ class Center
19 13 property :address, Text, :lazy => true
20 14 property :contact_number, String, :length => 40, :lazy => true
21 15 property :landmark, String, :length => 100, :lazy => true
22   - property :meeting_day, Enum.send('[]', *DAYS), :nullable => false, :default => :none, :index => true
  16 + property :meeting_day, Enum.send('[]', *DAYS), :nullable => true, :default => :none, :index => true # DEPRECATED
23 17 property :meeting_time_hours, Integer, :length => 2, :index => true
24 18 property :meeting_time_minutes, Integer, :length => 2, :index => true
25 19 property :meeting_calendar, Text # this is a comma separated list of dates and takes precedence over everything else.
@@ -44,8 +38,6 @@ class Center
44 38 validates_with_method :meeting_time_hours, :method => :hours_valid?
45 39 validates_with_method :meeting_time_minutes, :method => :minutes_valid?
46 40
47   - # validates_with_method :creation_date_ok
48   -
49 41 def self.from_csv(row, headers)
50 42 hour, minute = row[headers[:center_meeting_time_in_24h_format]].split(":")
51 43 branch = Branch.first(:name => row[headers[:branch]].strip)
@@ -70,12 +62,23 @@ def self.meeting_days
70 62 DAYS
71 63 end
72 64
  65 + def get_meeting_dates(to = SEP_DATE,from = creation_date)
  66 + # DEPRECATED - Please use Center#meeting_dates. get_meeting_dates is non-idiomatic ruby
  67 + meeting_dates(to, from)
  68 + end
  69 +
  70 +
73 71 # get a list of meeting dates between from and to if to is a Date. Else gets "to" meeting dates if to is an integer
74   - #
75 72 # a center must take the responsibility that center_meeting_days never overlap.
76   - def get_meeting_dates(to = Date.new(2100,12,31),from = creation_date)
77   - # to can be a date or a number
78   - # first find the date_Vectors for all center_meeting_days as a hash {:valid_from => DateVector}
  73 + # to can be a date or a number
  74 + def meeting_dates(to = nil,from = nil)
  75 + debugger
  76 + # sometimes loans from another center might be moved to this center. they can be created before this centers creation date
  77 + # therefore, we refer to the loan history table first and if there are no rows there, we refer to the creation date for the 'from' date if none is specified
  78 + min_max_dates = LoanHistory.all(:center_id => self.id).aggregate(:date.min, :date.max)
  79 + from ||= (min_max_dates[0] || self.creation_date)
  80 + to ||= (min_max_dates[1] || SEP_DATE)
  81 + # first refer to the meeting_calendar
79 82 unless self.meeting_calendar.blank?
80 83 ds = self.meeting_calendar.split(/[\s,]/).reject(&:blank?).map{|d| Date.parse(d) rescue nil}.compact.select{|d| d >= from}.sort
81 84 if to
@@ -84,6 +87,8 @@ def get_meeting_dates(to = Date.new(2100,12,31),from = creation_date)
84 87 end
85 88 return ds
86 89 end
  90 +
  91 + # then check the date vectors
87 92 select = to.class == Date ? {:valid_from.lte => to} : {}
88 93 dvs = center_meeting_days.all(select).map{|cmd| [cmd.valid_from, cmd.date_vector]}.to_hash
89 94
@@ -122,7 +127,7 @@ def self.catalog(user=nil)
122 127 result = {}
123 128 branch_names = {}
124 129
125   - if user.staff_member
  130 + if (user or Nothing).staff_member
126 131 staff_member = user.staff_member
127 132 [staff_member.centers.branches, staff_member.branches].flatten.each{|b| branch_names[b.id] = b.name }
128 133 centers = [staff_member.centers, staff_member.branches.centers].flatten
@@ -140,7 +145,8 @@ def self.catalog(user=nil)
140 145 end
141 146
142 147
143   -
  148 +
  149 + # returns the meeting day for a given date
144 150 def meeting_day_for(date)
145 151 @meeting_days ||= self.center_meeting_days(:order => [:valid_from])
146 152 if @meeting_days.length==0
@@ -155,36 +161,26 @@ def meeting_day_for(date)
155 161 end
156 162
157 163 def next_meeting_date_from(date)
  164 + # first refer to the LoanHistory. Sometimes, some funky loans might be in here and we don't want to depend on center meeting dates in
  165 + # the first instance
158 166 r_date = (LoanHistory.first(:center_id => self.id, :date.gt => date, :order => [:date], :limit => 1) or Nothing).date
159   - unless r_date
160   - number = get_meeting_date(date, :next)
161   - if meeting_day != :none and (date + number - get_meeting_date(date + number, :previous)).cweek == (date + number).cweek
162   - r_date = (date + number + get_meeting_date(date + number, :next)).holiday_bump
163   - else
164   - r_date = (date + number).holiday_bump
165   - end
166   - end
167   - r_date
168   -
  167 + return r_date if r_date
  168 + #oops...no loans in this center. use center_meeting_dates
  169 + debugger
  170 + self.meeting_dates(1, date)[0]
169 171 end
170 172
171 173 def previous_meeting_date_from(date)
  174 + #likewise for this (see comment above)
172 175 r_date = (LoanHistory.first(:center_id => self.id, :date.lte => date, :order => [:date.desc], :limit => 1) or Nothing).date
173   - unless r_date
174   - number = get_meeting_date(date, :previous)
175   - if meeting_day != :none and (date - number - get_meeting_date(date - number, :previous)).cweek == (date - number).cweek
176   - r_date = (date - number - get_meeting_date(date - number, :previous)).holiday_bump
177   - else
178   - r_date = (date - number).holiday_bump
179   - end
180   - end
181   - r_date
182   -
  176 + return r_date if r_date
  177 + #oops...no loans in this center. use center_meeting_dates
  178 + self.meeting_dates(date)[-1]
183 179 end
184 180
185 181
186 182 def meeting_day?(date)
187   - LoanHistory.all(:date => date).aggregate(:center_id).include?(self.id)
  183 + Center.meeting_days.include?(date)
188 184 end
189 185
190 186 def meeting_time
@@ -192,6 +188,7 @@ def meeting_time
192 188 end
193 189
194 190 def self.paying_today(user, date = Date.today, branch_id = nil)
  191 + # returns a list of centers paying today
195 192 selection = {:date => date}.merge(branch_id ? {:branch_id => branch_id} : {})
196 193 center_ids = LoanHistory.all(selection).aggregate(:center_id)
197 194 centers = center_ids.blank? ? [] : Center.all(:id => center_ids)
@@ -219,6 +216,7 @@ def location
219 216 end
220 217
221 218 def self.meeting_today(date=Date.today, user=nil)
  219 + # this makes no sense
222 220 user = User.first
223 221 center_ids = LoanHistory.all(:date => date).aggregate(:center_id)
224 222 # restrict branch manager and center managers to their own branches
@@ -229,6 +227,9 @@ def self.meeting_today(date=Date.today, user=nil)
229 227 Center.all(:id => center_ids)
230 228 end
231 229
  230 +
  231 + private
  232 +
232 233 def hours_valid?
233 234 return true if (0..23).include? meeting_time_hours.to_i
234 235 [false, "Hours of the meeting time should be within 0-23"]
@@ -239,14 +240,9 @@ def minutes_valid?
239 240 end
240 241 def manager_is_an_active_staff_member?
241 242 return true if manager and manager.active
242   - [false, "Receiving staff member is currently not active"]
  243 + [false, "Cannot set #{self.manager.name} as center manager because this staff member is not currently not active"]
243 244 end
244 245
245   - def creation_date_ok
246   - return true if clients.map{|c| c.loans}.count == 0
247   - return true if creation_date <= loans.aggregate(:applied_on.min)
248   - return [false, "Creation date cannot be after the first loan application date"]
249   - end
250 246
251 247 def handle_meeting_date_change
252 248 # no need to do all this if meeting date was not changed
@@ -284,45 +280,40 @@ def handle_meeting_date_change
284 280 return true
285 281 end
286 282
287   - def set_meeting_change_date
288   - if self.new?
289   - self.meeting_day_change_date = self.creation_date
290   - else
291   - # Check if meeting date was changed.
292   - if self.dirty_attributes.map{|x| x.first.name}.include?(:meeting_day)
293   - if self.meeting_day_change_date.class==String and not self.meeting_day_change_date.blank?
294   - self.meeting_day_change_date = parse_date(self.meeting_day_change_date)
295   - else
296   - # if meeting_day was indeed changed and no meeting_change_date is foudn then set it as today's date
297   - self.meeting_day_change_date ||= Date.today
298   - end
299   - else
300   - # If meeting_day was not changed then set meeting_day_change_date as nil.
301   - self.meeting_day_change_date = nil
  283 + def handle_meeting_days
  284 + # this function creates the first center meeting day for the center when only a meeting day is specified.
  285 + # we will soon deprecate the meeting_day field and work only with center_meeting_days
  286 + if center_meeting_days.blank?
  287 + unless meeting_day == :none
  288 + cmd = CenterMeetingDay.new(:valid_from => nil, :valid_upto => nil, :center_id => self.id, :meeting_day => (meeting_day || :none))
  289 + self.center_meeting_days << cmd
302 290 end
303 291 end
304   - end
305 292
306   - def get_meeting_date(date, direction)
307   - number = 1
308   - if direction == :next
309   - nwday = (date + number).wday
310   - while (meet_day = Center.meeting_days.index(meeting_day_for(date + number)) and meet_day > 0 and nwday != meet_day)
311   - number += 1
312   - nwday = (date + number).wday
313   - nwday = 7 if nwday == 0
314   - end
315   - else
316   - nwday = (date - number).wday
317   - while (meet_day = Center.meeting_days.index(meeting_day_for(date - number)) and meet_day > 0 and nwday != meet_day)
318   - number += 1
319   - nwday = (date - number).wday
320   - nwday = 7 if nwday == 0
321   - end
322   - end
323   - return number
324 293 end
325 294
  295 + # def get_meeting_date(date, direction)
  296 + # DEPRECATED. Commenting out right now so that we can restore it if it is being referenced from somewhere
  297 + # TODO remove this from the codebase if nothing borks by 2012-02-28
  298 + # number = 1
  299 + # if direction == :next
  300 + # nwday = (date + number).wday
  301 + # while (meet_day = Center.meeting_days.index(meeting_day_for(date + number)) and meet_day > 0 and nwday != meet_day)
  302 + # number += 1
  303 + # nwday = (date + number).wday
  304 + # nwday = 7 if nwday == 0
  305 + # end
  306 + # else
  307 + # nwday = (date - number).wday
  308 + # while (meet_day = Center.meeting_days.index(meeting_day_for(date - number)) and meet_day > 0 and nwday != meet_day)
  309 + # number += 1
  310 + # nwday = (date - number).wday
  311 + # nwday = 7 if nwday == 0
  312 + # end
  313 + # end
  314 + # return number
  315 + # end
  316 +
326 317 def convert_blank_to_nil
327 318 self.attributes.each{|k, v|
328 319 if v.is_a?(String) and v.empty? and self.class.properties.find{|x| x.name == k}.type==Integer
3  app/models/center_meeting_day.rb
@@ -98,8 +98,9 @@ def to_s
98 98
99 99
100 100 def valid_from_is_lesser_than_valid_upto
  101 + return true if self.valid_from.blank? and self.valid_upto.blank? # neither is set
101 102 self.valid_from = Date.parse(self.valid_from) unless self.valid_from.is_a? Date
102   - self.valid_upto = (self.valid_upto.blank? ? Date.new(2100,12,31) : Date.parse(self.valid_upto)) if self.valid_upto.class == String
  103 + self.valid_upto = (self.valid_upto.blank? ? SEP_DATE : Date.parse(self.valid_upto)) if self.valid_upto.class == String
103 104
104 105 if self.valid_from and self.valid_upto
105 106 return [false, "Valid from date cannot be before than valid upto date"] if self.valid_from > self.valid_upto
3  app/models/loan.rb
@@ -1248,10 +1248,7 @@ def calculate_history
1248 1248 actual_outstanding_principal = outstanding ? actual[:balance].round(2) : 0
1249 1249 actual_outstanding_total = outstanding ? actual[:total_balance].round(2) : 0
1250 1250 actual_outstanding_interest = outstanding ? (actual_outstanding_total - actual_outstanding_principal) : 0
1251   -<<<<<<< HEAD
1252 1251
1253   -=======
1254   ->>>>>>> e8b0fb3120ed49f25dac950f47aa21be9b15129f
1255 1252 _apo = [0,total_principal_paid.round(2) - total_principal_due.round(2)].max # advance principal outstanding at the start
1256 1253 _api = [0,total_interest_paid.round(2) - total_interest_due.round(2)].max
1257 1254 advance_principal_outstanding = outstanding ? _apo : 0
3  config/constants.rb
@@ -122,3 +122,6 @@
122 122 # in order to avoid overrunning the SQL max packet size, we split the cacher update into chunks
123 123 # 2500 should be good for the standard SQL max_packet_size of 16MB
124 124 CHUNK_SIZE = 2500
  125 +
  126 +
  127 +SEP_DATE = Date.new(2100,12,31) # SEP = Someone Else's Problem http://en.wikipedia.org/wiki/Somebody_Else's_Problem i.e. a date so far in the future as to represent infinity
2  spec/factories.rb
@@ -582,7 +582,7 @@
582 582 name 'home'
583 583 title 'Home page'
584 584 route '/'
585   - type Types.first
  585 + type BookmarkTypes.first
586 586 share_with User::ROLES.first
587 587
588 588 association :user
274 spec/models/center_spec.rb
@@ -11,10 +11,12 @@
11 11
12 12 @branch = Factory(:branch, :manager => @manager)
13 13 @branch.should be_valid
  14 +
14 15 end
15 16
16 17 before(:each) do
17 18 Center.all.destroy!
  19 + CenterMeetingDay.all.destroy!
18 20 @center = Center.new(:name => "Munnar hill center")
19 21 @center.manager = @manager
20 22 @center.branch = @branch
@@ -35,141 +37,143 @@
35 37 @center.should_not be_valid
36 38 end
37 39
38   - # The following specs are currently failing because the method that sets up center_meeting_days
39   - # is borked. This is documented in the Center model itself.
40   -# it "should have meeting_days" do
41   -# @center.center_meeting_days.length.should eql(1)
42   -# @center.should be_valid
43   -# end
44   -#
45   -# it "should have meeting date change should create a new center_meeting_day entry" do
46   -# @center.meeting_day = :tuesday
47   -# @center.save
48   -# @center.center_meeting_days.length.should eql(2)
49   -# @center.should be_valid
50   -# end
51   -#
52   -# it "meeting date change should happen on the date specified" do
53   -# @center.meeting_day_change_date = Date.new(2010, 10, 12)
54   -# @center.meeting_day = :tuesday
55   -# @center.save
56   -# @center = Center.get(@center.id)
57   -# @center.meeting_day_for(Date.new(2010, 10, 12)).should eql(:tuesday)
58   -# @center.meeting_day_for(Date.new(2010, 10, 11)).should eql(:monday)
59   -# @center.meeting_day_for(Date.new(2010, 10, 13)).should eql(:tuesday)
60   -# end
61   -#
62   -# it "meeting date change should happen on the date specified" do
63   -# @center.meeting_day_change_date = Date.new(2010, 10, 12)
64   -# @center.meeting_day = :tuesday
65   -# @center.save
66   -# @center = Center.get(@center.id)
67   -#
68   -# @center.next_meeting_date_from(Date.new(2010, 10, 12)).weekday.should eql(:tuesday)
69   -# @center.previous_meeting_date_from(Date.new(2010, 10, 11)).weekday.should eql(:monday)
70   -#
71   -# @center.next_meeting_date_from(Date.new(2010, 10, 13)).weekday.should eql(:tuesday)
72   -# @center.previous_meeting_date_from(Date.new(2010, 10, 13)).weekday.should eql(:monday)
73   -#
74   -# @center.next_meeting_date_from(Date.new(2010, 10, 11)).weekday.should eql(:tuesday)
75   -# @center.previous_meeting_date_from(Date.new(2010, 10, 11)).weekday.should eql(:monday)
76   -# end
77   -#
78   -# it "next and previous meeting dates should be correct" do
79   -# center = Center.create(:branch => @branch, :name => "center 75", :code => "c75", :creation_date => Date.new(2010, 03, 17),
80   -# :meeting_day => :wednesday, :manager => @manager)
81   -# center.should be_valid
82   -#
83   -# center.next_meeting_date_from(Date.new(2010, 6, 30)).should == Date.new(2010, 7, 7)
84   -# center.next_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 7, 7)
85   -# center.next_meeting_date_from(Date.new(2010, 7, 3)).should == Date.new(2010, 7, 7)
86   -# center.next_meeting_date_from(Date.new(2010, 7, 5)).should == Date.new(2010, 7, 7)
87   -# center.next_meeting_date_from(Date.new(2010, 7, 6)).should == Date.new(2010, 7, 7)
88   -#
89   -# center.meeting_day_change_date = Date.new(2010, 7, 8)
90   -# center.meeting_day = :tuesday
91   -# center.save
92   -# center = Center.get(center.id)
93   -#
94   -# center.previous_meeting_date_from(Date.new(2010, 7, 7)).should == Date.new(2010, 6, 30)
95   -# center.previous_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 07)
96   -# center.previous_meeting_date_from(Date.new(2010, 7, 6)).should == Date.new(2010, 6, 30)
97   -# center.previous_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 6, 30)
98   -#
99   -# center.next_meeting_date_from(Date.new(2010, 7, 7)).should == Date.new(2010, 7, 13)
100   -# center.next_meeting_date_from(Date.new(2010, 7, 10)).should == Date.new(2010, 7, 13)
101   -# center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 13)
102   -#
103   -# center.next_meeting_date_from(Date.new(2010, 7, 10)).should == Date.new(2010, 7, 13)
104   -# center.next_meeting_date_from(Date.new(2010, 7, 11)).should == Date.new(2010, 7, 13)
105   -# center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 13)
106   -#
107   -# center.next_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 20)
108   -# center.next_meeting_date_from(Date.new(2010, 7, 15)).should == Date.new(2010, 7, 20)
109   -# center.next_meeting_date_from(Date.new(2010, 7, 19)).should == Date.new(2010, 7, 20)
110   -#
111   -# center.previous_meeting_date_from(Date.new(2010, 7, 20)).should == Date.new(2010, 7, 13)
112   -# center.previous_meeting_date_from(Date.new(2010, 7, 19)).should == Date.new(2010, 7, 13)
113   -# center.previous_meeting_date_from(Date.new(2010, 7, 14)).should == Date.new(2010, 7, 13)
114   -#
115   -# center.previous_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 7)
116   -# center.previous_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 7)
117   -# center.previous_meeting_date_from(Date.new(2010, 7, 8)).should == Date.new(2010, 7, 7)
118   -#
119   -# center.meeting_day_change_date = Date.new(2010, 10, 17)
120   -# center.meeting_day = :friday
121   -# center.save
122   -#
123   -# center = Center.get(center.id)
124   -#
125   -# center.next_meeting_date_from(Date.new(2010, 10, 12)).should == Date.new(2010, 10, 22)
126   -# center.next_meeting_date_from(Date.new(2010, 10, 13)).should == Date.new(2010, 10, 22)
127   -# center.next_meeting_date_from(Date.new(2010, 10, 15)).should == Date.new(2010, 10, 22)
128   -# center.next_meeting_date_from(Date.new(2010, 10, 17)).should == Date.new(2010, 10, 22)
129   -# center.next_meeting_date_from(Date.new(2010, 10, 20)).should == Date.new(2010, 10, 22)
130   -# center.next_meeting_date_from(Date.new(2010, 10, 21)).should == Date.new(2010, 10, 22)
131   -#
132   -# center.previous_meeting_date_from(Date.new(2010, 10, 12)).should == Date.new(2010, 10, 5)
133   -# center.previous_meeting_date_from(Date.new(2010, 10, 22)).should == Date.new(2010, 10, 12)
134   -# center.next_meeting_date_from(Date.new(2010, 10, 22)).should == Date.new(2010, 10, 29)
135   -#
136   -#
137   -# center = Center.create(:branch => @branch, :name => "center 77", :code => "c77", :creation_date => Date.new(2010, 03, 15),
138   -# :meeting_day => :monday, :manager => @manager)
139   -# center.should be_valid
140   -#
141   -# center.next_meeting_date_from(Date.new(2010, 3, 15)).should == Date.new(2010, 3, 22)
142   -# center.next_meeting_date_from(Date.new(2010, 3, 22)).should == Date.new(2010, 3, 29)
143   -# center.previous_meeting_date_from(Date.new(2010, 3, 29)).should == Date.new(2010, 3, 22)
144   -# center.previous_meeting_date_from(Date.new(2010, 3, 22)).should == Date.new(2010, 3, 15)
145   -#
146   -# center.meeting_day_change_date = Date.new(2010, 7, 14)
147   -# center.meeting_day = :friday
148   -# center.save
149   -# center = Center.get(center.id)
150   -#
151   -# center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 23)
152   -# center.next_meeting_date_from(Date.new(2010, 7, 15)).should == Date.new(2010, 7, 23)
153   -# center.next_meeting_date_from(Date.new(2010, 7, 5)).should == Date.new(2010, 7, 12)
154   -#
155   -# center.previous_meeting_date_from(Date.new(2010, 7, 23)).should == Date.new(2010, 7, 12)
156   -# center.previous_meeting_date_from(Date.new(2010, 7, 20)).should == Date.new(2010, 7, 12)
157   -# center.previous_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 12)
158   -# center.previous_meeting_date_from(Date.new(2010, 7, 14)).should == Date.new(2010, 7, 12)
159   -#
160   -# center.meeting_day_change_date = Date.new(2010, 8, 1)
161   -# center.meeting_day = :none
162   -# center.save
163   -#
164   -# center = Center.get(center.id)
165   -# center.next_meeting_date_from(Date.new(2010, 7, 2)).should == Date.new(2010, 7, 5)
166   -# center.next_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 7, 5)
167   -# center.next_meeting_date_from(Date.new(2010, 7, 21)).should == Date.new(2010, 7, 23)
168   -# center.next_meeting_date_from(Date.new(2010, 7, 31)).should == Date.new(2010, 8, 1)
169   -# center.next_meeting_date_from(Date.new(2010, 8, 1)).should == Date.new(2010, 8, 2)
170   -# center.previous_meeting_date_from(Date.new(2010, 8, 2)).should == Date.new(2010, 8, 1)
171   -# center.previous_meeting_date_from(Date.new(2010, 8, 10)).should == Date.new(2010, 8, 9)
172   -# end
  40 + it "should catalog correctly" do
  41 + @branch_1 = Factory(:branch, :name => "Branch 1", :manager => @manager)
  42 + @branch_2 = Factory(:branch, :name => "Branch 2", :manager => @manager)
  43 + @b1c1 = Factory(:center, :name => "b1c1", :branch => @branch_1)
  44 + @b1c2 = Factory(:center, :name => "b1c2", :branch => @branch_1)
  45 + @b2c1 = Factory(:center, :name => "b2c1", :branch => @branch_2)
  46 + @b2c2 = Factory(:center, :name => "b2c2", :branch => @branch_2)
  47 + Center.catalog.should == {"Hyderabad center 1"=>{3=>"Munnar hill center"}, "Branch 1"=>{5=>"b1c2", 4=>"b1c1"}, "Branch 2"=>{6=>"b2c1", 7=>"b2c2"}}
  48 + end
  49 +
  50 +
  51 + # testing of center meeting days.
  52 + # upon creation, a CenterMeetingDate is assigned to the center. It has blank valid_from and valid_upto fields.
  53 + # The default date vector returned for this center is from creation_date until SEP_DATE (someone else's problem date) field
  54 + # after this, center meeting days may be added and removed at will.
  55 +
  56 +
  57 + it "should have meeting_days" do
  58 + @center.center_meeting_days.length.should eql(1)
  59 + @center.center_meeting_days.first.meeting_day.should == :monday
  60 + end
  61 +
  62 + it "should give correct meeting days in this instance" do
  63 + start_date = Date.new(2010,1,1)
  64 + start_date = start_date - start_date.cwday + 8
  65 + start_date.weekday.should == :monday
  66 + date = start_date
  67 + result = [date]
  68 + while date <= SEP_DATE
  69 + date += 7
  70 + result << date if date <= SEP_DATE
  71 + end
  72 + @center.meeting_dates.should == result
  73 + end
  74 +
  75 +
  76 + it "meeting date change should happen on the date specified" do
  77 + end
  78 +
  79 + it "meeting date change should happen on the date specified" do
  80 + end
  81 +
  82 + it "next and previous meeting dates should be correct" do
  83 + center = Center.create(:branch => @branch, :name => "center 75", :code => "c75", :creation_date => Date.new(2010, 03, 17),
  84 + :meeting_day => :wednesday, :manager => @manager)
  85 + center.should be_valid
  86 +
  87 + center.next_meeting_date_from(Date.new(2010, 6, 30)).should == Date.new(2010, 7, 7)
  88 + center.next_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 7, 7)
  89 + center.next_meeting_date_from(Date.new(2010, 7, 3)).should == Date.new(2010, 7, 7)
  90 + center.next_meeting_date_from(Date.new(2010, 7, 5)).should == Date.new(2010, 7, 7)
  91 + center.next_meeting_date_from(Date.new(2010, 7, 6)).should == Date.new(2010, 7, 7)
  92 +
  93 + center.meeting_day_change_date = Date.new(2010, 7, 8)
  94 + center.meeting_day = :tuesday
  95 + center.save
  96 + center = Center.get(center.id)
  97 +
  98 + center.previous_meeting_date_from(Date.new(2010, 7, 7)).should == Date.new(2010, 6, 30)
  99 + center.previous_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 07)
  100 + center.previous_meeting_date_from(Date.new(2010, 7, 6)).should == Date.new(2010, 6, 30)
  101 + center.previous_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 6, 30)
  102 +
  103 + center.next_meeting_date_from(Date.new(2010, 7, 7)).should == Date.new(2010, 7, 13)
  104 + center.next_meeting_date_from(Date.new(2010, 7, 10)).should == Date.new(2010, 7, 13)
  105 + center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 13)
  106 +
  107 + center.next_meeting_date_from(Date.new(2010, 7, 10)).should == Date.new(2010, 7, 13)
  108 + center.next_meeting_date_from(Date.new(2010, 7, 11)).should == Date.new(2010, 7, 13)
  109 + center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 13)
  110 +
  111 + center.next_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 20)
  112 + center.next_meeting_date_from(Date.new(2010, 7, 15)).should == Date.new(2010, 7, 20)
  113 + center.next_meeting_date_from(Date.new(2010, 7, 19)).should == Date.new(2010, 7, 20)
  114 +
  115 + center.previous_meeting_date_from(Date.new(2010, 7, 20)).should == Date.new(2010, 7, 13)
  116 + center.previous_meeting_date_from(Date.new(2010, 7, 19)).should == Date.new(2010, 7, 13)
  117 + center.previous_meeting_date_from(Date.new(2010, 7, 14)).should == Date.new(2010, 7, 13)
  118 +
  119 + center.previous_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 7)
  120 + center.previous_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 7)
  121 + center.previous_meeting_date_from(Date.new(2010, 7, 8)).should == Date.new(2010, 7, 7)
  122 +
  123 + center.meeting_day_change_date = Date.new(2010, 10, 17)
  124 + center.meeting_day = :friday
  125 + center.save
  126 +
  127 + center = Center.get(center.id)
  128 +
  129 + center.next_meeting_date_from(Date.new(2010, 10, 12)).should == Date.new(2010, 10, 22)
  130 + center.next_meeting_date_from(Date.new(2010, 10, 13)).should == Date.new(2010, 10, 22)
  131 + center.next_meeting_date_from(Date.new(2010, 10, 15)).should == Date.new(2010, 10, 22)
  132 + center.next_meeting_date_from(Date.new(2010, 10, 17)).should == Date.new(2010, 10, 22)
  133 + center.next_meeting_date_from(Date.new(2010, 10, 20)).should == Date.new(2010, 10, 22)
  134 + center.next_meeting_date_from(Date.new(2010, 10, 21)).should == Date.new(2010, 10, 22)
  135 +
  136 + center.previous_meeting_date_from(Date.new(2010, 10, 12)).should == Date.new(2010, 10, 5)
  137 + center.previous_meeting_date_from(Date.new(2010, 10, 22)).should == Date.new(2010, 10, 12)
  138 + center.next_meeting_date_from(Date.new(2010, 10, 22)).should == Date.new(2010, 10, 29)
  139 +
  140 +
  141 + center = Center.create(:branch => @branch, :name => "center 77", :code => "c77", :creation_date => Date.new(2010, 03, 15),
  142 + :meeting_day => :monday, :manager => @manager)
  143 + center.should be_valid
  144 +
  145 + center.next_meeting_date_from(Date.new(2010, 3, 15)).should == Date.new(2010, 3, 22)
  146 + center.next_meeting_date_from(Date.new(2010, 3, 22)).should == Date.new(2010, 3, 29)
  147 + center.previous_meeting_date_from(Date.new(2010, 3, 29)).should == Date.new(2010, 3, 22)
  148 + center.previous_meeting_date_from(Date.new(2010, 3, 22)).should == Date.new(2010, 3, 15)
  149 +
  150 + center.meeting_day_change_date = Date.new(2010, 7, 14)
  151 + center.meeting_day = :friday
  152 + center.save
  153 + center = Center.get(center.id)
  154 +
  155 + center.next_meeting_date_from(Date.new(2010, 7, 12)).should == Date.new(2010, 7, 23)
  156 + center.next_meeting_date_from(Date.new(2010, 7, 15)).should == Date.new(2010, 7, 23)
  157 + center.next_meeting_date_from(Date.new(2010, 7, 5)).should == Date.new(2010, 7, 12)
  158 +
  159 + center.previous_meeting_date_from(Date.new(2010, 7, 23)).should == Date.new(2010, 7, 12)
  160 + center.previous_meeting_date_from(Date.new(2010, 7, 20)).should == Date.new(2010, 7, 12)
  161 + center.previous_meeting_date_from(Date.new(2010, 7, 13)).should == Date.new(2010, 7, 12)
  162 + center.previous_meeting_date_from(Date.new(2010, 7, 14)).should == Date.new(2010, 7, 12)
  163 +
  164 + center.meeting_day_change_date = Date.new(2010, 8, 1)
  165 + center.meeting_day = :none
  166 + center.save
  167 +
  168 + center = Center.get(center.id)
  169 + center.next_meeting_date_from(Date.new(2010, 7, 2)).should == Date.new(2010, 7, 5)
  170 + center.next_meeting_date_from(Date.new(2010, 7, 1)).should == Date.new(2010, 7, 5)
  171 + center.next_meeting_date_from(Date.new(2010, 7, 21)).should == Date.new(2010, 7, 23)
  172 + center.next_meeting_date_from(Date.new(2010, 7, 31)).should == Date.new(2010, 8, 1)
  173 + center.next_meeting_date_from(Date.new(2010, 8, 1)).should == Date.new(2010, 8, 2)
  174 + center.previous_meeting_date_from(Date.new(2010, 8, 2)).should == Date.new(2010, 8, 1)
  175 + center.previous_meeting_date_from(Date.new(2010, 8, 10)).should == Date.new(2010, 8, 9)
  176 + end
173 177
174 178 it "should not be valid with a name shorter than 3 characters" do
175 179 @center.name = "ok"
14 spec/spec_failing_examples.txt
... ... @@ -1,13 +1 @@
1   -Loan .status should give status accoring to changing properties up to it written off
2   -Loan .status should give status accoring to changing properties up to it is repaid
3   -Loan .payments_hash should give correct results
4   -Loan history should be correct
5   -Loan should write the history correctly into the db
6   -Loan should not be deleteable if verified
7   -Loan .installment_dates should correctly deal with holidays
8   -Loan should have correct takeover schedule and balances
9   -Loan should change with center meeting date change
10   -Loan should change with center meeting date change
11   -Loan should not be valid if duplicated
12   -Loan should repay properly
13   -Loan should not disburse if loan fees are not paid
  1 +Center next and previous meeting dates should be correct

0 comments on commit 03ea9d8

Please sign in to comment.
Something went wrong with that request. Please try again.