Class: MerchCalendar::RetailCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/merch_calendar/retail_calendar.rb

Constant Summary collapse

QUARTER_1 =
1
QUARTER_2 =
2
QUARTER_3 =
3
QUARTER_4 =
4
FOUR_WEEK_MONTHS =
[2, 5, 8, 11]
FIVE_WEEK_MONTHS =
[3, 6, 9, 12]

Instance Method Summary collapse

Instance Method Details

#end_of_month(year, merch_month) ⇒ Date

The ending date of the given merch month

Parameters:

  • year (Fixnum)

    the retail year

  • merch_month (Fixnum)

    the nth merch month of the retail calendar

Returns:

  • (Date)

    the end date of the merch month



65
66
67
68
69
70
71
# File 'lib/merch_calendar/retail_calendar.rb', line 65

def end_of_month(year, merch_month)
  if merch_month == 12
    end_of_year(year)
  else
    start_of_month(year, merch_month + 1) - 1
  end
end

#end_of_quarter(year, quarter) ⇒ Date

Return the ending date for a particular quarter

Parameters:

  • year (Fixnum)

    the retail year

  • quarter (Fixnum)

    the quarter of the year, a number from 1 - 4

Returns:

  • (Date)

    the end date of the quarter



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/merch_calendar/retail_calendar.rb', line 118

def end_of_quarter(year, quarter)
  case quarter
  when QUARTER_1
    end_of_month(year, 3)
  when QUARTER_2
    end_of_month(year, 6)
  when QUARTER_3
    end_of_month(year, 9)
  when QUARTER_4
    end_of_month(year, 12)
  else
    raise "invalid quarter"
  end
end

#end_of_week(year, month, merch_week) ⇒ Date

Returns the date that corresponds to the last day in the merch week

Parameters:

  • year (Fixnum)

    the retail year

  • month (Fixnum)

    the nth merch month of the retail calendar

  • merch_week (Fixnum)

    the nth week of the merch month

Returns:

  • (Date)

    the end date of the merch week



89
90
91
# File 'lib/merch_calendar/retail_calendar.rb', line 89

def end_of_week(year, month, merch_week)
  start_of_month(year, month) + (6 + ((merch_week - 1) * 7))
end

#end_of_year(year) ⇒ Date

The the first date of the retail year

Parameters:

  • year (Fixnum)

    the retail year

Returns:

  • (Date)

    the first date of the retail year



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/merch_calendar/retail_calendar.rb', line 18

def end_of_year(year)
  year_end = Date.new((year + 1), 1, -1) # Jan 31st
  wday = (year_end.wday + 1) % 7 

  if wday > 3
    year_end += 7 - wday
  else
    year_end -= wday
  end
  year_end
end

#julian_to_merch(julian_month) ⇒ Fixnum

Converts a julian month to a merch month

Parameters:

  • julian_month (Fixnum)

    the julian month to convert

Returns:

  • (Fixnum)

    the merch month



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/merch_calendar/retail_calendar.rb', line 210

def julian_to_merch(julian_month)
  if julian_month > 12 || julian_month <= 0
    raise ArgumentError
  end

  if julian_month == 1
    12
  else
    julian_month - 1
  end
end

#merch_months_in(start_date, end_date) ⇒ Array

Given beginning and end dates it will return an array of Retail Month’s Start date

Parameters:

  • start_date (Date)

    the starting date

  • end_date (Date)

    the ending date

Returns:

  • (Array)

    Array of start dates of each Retail Month from given dates



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/merch_calendar/retail_calendar.rb', line 227

def merch_months_in(start_date, end_date)
  merch_months = []
  prev_date = start_date - 2
  date = start_date
  while date <= end_date do
    date = MerchCalendar.start_of_month(date.year, merch_month: date.month)
    next if prev_date == date
    merch_months.push(date)
    prev_date = date
    date += 14
  end
  merch_months
end

#merch_to_julian(merch_month) ⇒ Fixnum

Converts a merch month to the correct julian month

Parameters:

  • merch_month (Fixnum)

    the merch month to convert

Returns:

  • (Fixnum)

    the julian month



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/merch_calendar/retail_calendar.rb', line 194

def merch_to_julian(merch_month)
  if merch_month > 12 || merch_month <= 0
    raise ArgumentError
  end

  if merch_month == 12
    1
  else
    merch_month + 1
  end
end

#merch_year_from_date(date) ⇒ Fixnum

Given any julian date it will return what retail year it belongs to

Parameters:

  • date (Date)

    the julian date to convert to its Retail Year

Returns:

  • (Fixnum)

    the retail year that the julian date falls into



179
180
181
182
183
184
185
186
187
# File 'lib/merch_calendar/retail_calendar.rb', line 179

def merch_year_from_date(date)
  date_end_of_year = end_of_year(date.year)
  date_start_of_year = start_of_year(date.year) 
  if date < date_start_of_year
    date.year - 1 
  else
    date.year
  end
end

#quarter(merch_month) ⇒ Date

Returns the quarter that the merch month falls in

Parameters:

  • merch_month (Fixnum)

    merch month

Returns:

  • (Date)

    the quarter that the merch_month falls in



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/merch_calendar/retail_calendar.rb', line 137

def quarter(merch_month)
  case merch_month
  when 1,2,3
    return QUARTER_1
  when 4,5,6
    return QUARTER_2
  when 7,8,9
    return QUARTER_3
  when 10,11,12
    return QUARTER_4
  else
    raise "invalid merch month"
  end
end

#season(merch_month) ⇒ String

Returns the season given for the merch_month

Parameters:

  • merch_month (Fixnum)

    the nth month of the retail calendar

Returns:

  • (String)

    the season that the merch_month falls under



156
157
158
159
160
161
162
163
164
165
# File 'lib/merch_calendar/retail_calendar.rb', line 156

def season(merch_month)
  case merch_month
  when 1,2,3,4,5,6
    "Spring/Summer"
  when 7,8,9,10,11,12
    "Fall/Winter"
  else
    raise "invalid merch month"
  end
end

#start_of_month(year, merch_month) ⇒ Date

The starting date of the given merch month

Parameters:

  • year (Fixnum)

    the retail year

  • merch_month (Fixnum)

    the nth merch month of the retail calendar

Returns:

  • (Date)

    the start date of the merch month



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/merch_calendar/retail_calendar.rb', line 43

def start_of_month(year, merch_month)
  # 91 = number of days in a single 4-5-4 set 
  start = start_of_year(year) + ((merch_month - 1) / 3).to_i * 91

  case merch_month
  when  *FOUR_WEEK_MONTHS
    # 28 = 4 weeks
    start = start + 28
  when *FIVE_WEEK_MONTHS
    # The 5 week months
    # 63 = 4 weeks + 5 weeks
    start = start + 63
  end
  
  start
end

#start_of_quarter(year, quarter) ⇒ Date

Return the starting date for a particular quarter

Parameters:

  • year (Fixnum)

    the retail year

  • quarter (Fixnum)

    the quarter of the year, a number from 1 - 4

Returns:

  • (Date)

    the start date of the quarter



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/merch_calendar/retail_calendar.rb', line 98

def start_of_quarter(year, quarter)
  case quarter
  when QUARTER_1
    start_of_month(year, 1)
  when QUARTER_2
    start_of_month(year, 4)
  when QUARTER_3
    start_of_month(year, 7)
  when QUARTER_4
    start_of_month(year, 10)
  else
    raise "invalid quarter"
  end
end

#start_of_week(year, month, merch_week) ⇒ Date

Returns the date that corresponds to the first day in the merch week

Parameters:

  • year (Fixnum)

    the retail year

  • month (Fixnum)

    the nth merch month of the retail calendar

  • merch_week (Fixnum)

    the nth week of the merch month

Returns:

  • (Date)

    the start date of the merch week



79
80
81
# File 'lib/merch_calendar/retail_calendar.rb', line 79

def start_of_week(year, month, merch_week)
  start_of_month(year, month) + ((merch_week - 1) * 7)
end

#start_of_year(year) ⇒ Date

The last date of the retail year

Parameters:

  • year (Fixnum)

    the retail year

Returns:

  • (Date)

    the last date of the retail year



34
35
36
# File 'lib/merch_calendar/retail_calendar.rb', line 34

def start_of_year(year)
  end_of_year(year - 1) + 1
end

#weeks_for_month(year, month_param) ⇒ Array

Returns an array of Merch Weeks that pertains to the Julian Month of a Retail Year

Parameters:

  • year (Fixnum)

    the Retail year

  • month_param (Fixnum)

    the julian month

Returns:

  • (Array)

    Array of MerchWeeks



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/merch_calendar/retail_calendar.rb', line 246

def weeks_for_month(year, month_param)
  merch_month = get_merch_month_param(month_param)

  start_date = start_of_month(year, merch_month)

  weeks = (end_of_month(year, merch_month) - start_date + 1) / 7

  (1..weeks).map do |week_num|
    week_start = start_date + ((week_num - 1) * 7)
    week_end = week_start + 6

    MerchWeek.new(week_start, { 
      start_of_week: week_start, 
      end_of_week: week_end, 
      week: week_num, 
      calendar: RetailCalendar.new
    })
  end
end

#weeks_in_year(year) ⇒ Fixnum

Returns the number of weeks in the retail year

Parameters:

  • year (Fixnum)

    the retail year

Returns:

  • (Fixnum)

    the number of weeks within the retail year



171
172
173
# File 'lib/merch_calendar/retail_calendar.rb', line 171

def weeks_in_year(year)
  ((start_of_year(year + 1) - start_of_year(year)) / 7).to_i
end