Class: BillingLogic::BillingCycle

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/billing_logic/billing_cycle.rb

Constant Summary collapse

TIME_UNITS =
{ :day => 1, :week => 7, :month => 365/12.0, :semimonth=> 365/24, :year => 365 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BillingCycle

Creates a new BillingCycle instance

Parameters:

  • opts (Hash) (defaults to: {})

    holds :period, :frequency, and :anniversary



11
12
13
14
15
# File 'lib/billing_logic/billing_cycle.rb', line 11

def initialize(opts = {})
  self.period = opts[:period]
  self.frequency = opts[:frequency] || 1
  self.anniversary = opts[:anniversary]
end

Instance Attribute Details

#anniversaryObject

Returns the value of attribute anniversary.



4
5
6
# File 'lib/billing_logic/billing_cycle.rb', line 4

def anniversary
  @anniversary
end

#frequencyObject

Returns the value of attribute frequency.



4
5
6
# File 'lib/billing_logic/billing_cycle.rb', line 4

def frequency
  @frequency
end

#periodObject

Returns the value of attribute period.



4
5
6
# File 'lib/billing_logic/billing_cycle.rb', line 4

def period
  @period
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Compares self against another BillingCycle instance by #periodicity

Parameters:

Returns:

  • (-1, 0, 1)

    integer determined by which BillingCycle is longer



21
22
23
# File 'lib/billing_logic/billing_cycle.rb', line 21

def <=>(other)
  self.periodicity <=> other.periodicity
end

#advance_date_by_period(date, revert = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/billing_logic/billing_cycle.rb', line 56

def advance_date_by_period(date, revert = false)
  operators =   {:month => revert ? :<< : :>>, 
                 :day   => revert ? :-  : :+ }
  case self.period
  when :year
    date.send(operators[:month], (self.frequency * 12))
  when :month
    date.send(operators[:month], self.frequency)
  when :week
    date.send(operators[:month], (self.frequency * 7))
  when :day
    date.send(operators[:month], self.frequency)
  end
end

#closest_anniversary_date_including(date) ⇒ Object

Used for prorationing in the single payment strategy Not currently in use



41
42
43
44
# File 'lib/billing_logic/billing_cycle.rb', line 41

def closest_anniversary_date_including(date) 
  date_in_past = date < anniversary
  advance_date_by_period(anniversary, date_in_past)
end

#closest_future_anniversary_date_including(date) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/billing_logic/billing_cycle.rb', line 46

def closest_future_anniversary_date_including(date)
  return anniversary if anniversary > date
  return advance_date_by_period(anniversary.dup) if anniversary == date
  next_anniversary = anniversary.dup
  while(date > next_anniversary) 
    next_anniversary = advance_date_by_period(next_anniversary)
  end
  next_anniversary
end

#days_in_billing_cycle_including(date) ⇒ Object



29
30
31
# File 'lib/billing_logic/billing_cycle.rb', line 29

def days_in_billing_cycle_including(date)
  (closest_anniversary_date_including(date) - anniversary).abs
end

#next_payment_dateObject

Date on which the next payment is due and scheduled to be paid anniversary will always equal date



35
36
37
# File 'lib/billing_logic/billing_cycle.rb', line 35

def next_payment_date
  closest_future_anniversary_date_including(anniversary)
end

#periodicityObject



25
26
27
# File 'lib/billing_logic/billing_cycle.rb', line 25

def periodicity
  time_unit_measure * frequency
end