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

4-4-5 support? #27

Open
yoelblum opened this issue Jun 10, 2020 · 2 comments
Open

4-4-5 support? #27

yoelblum opened this issue Jun 10, 2020 · 2 comments

Comments

@yoelblum
Copy link

Hello,
I am using the gem and may have the requirement to support 4-4-5 calendar. Is it possible to do with little monkey patching or is it big changes and I'll have to look at some other solutions?
I thought perhaps changing these constants could do the trick or is it not that simple?

  class RetailCalendar
    LAST_MONTH_OF_THE_YEAR = "January"
    LAST_DAY_OF_THE_YEAR = 31

    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]
@yoelblum
Copy link
Author

yoelblum commented Jul 2, 2020

If anyone still looking for a solution for this, I made a gem https://github.com/yoelblum/retail_calendar/

@foxhard
Copy link

foxhard commented Feb 26, 2021

@yoelblum, I think you can do the trick by overriding the start_of_month method of RetailCalendar class. Something like this:

class Calendar445 < MerchCalendar::RetailCalendar
  FOUR_WEEK_MONTHS = [2, 5, 8, 11].freeze
  FIVE_WEEK_MONTHS = [3, 6, 9, 12].freeze

  def start_of_month(year, merch_month)
    # 91 = number of days in a single 4-4-5 set
    # Merch months
    # ============
    # row 1 (4 weeks): feb(1) may(4) aug(7) nov(10)
    # row 2 (4 weeks): mar(2) jun(5) sep(8) dec(11)
    # row 3 (5 weeks): apr(3) jul(6) oct(9) jan(12)

    # This line will get the start of the month for any of the merch months in row 1
    start = start_of_year(year) + ((merch_month - 1) / 3).to_i * 91

    # This code will get the start of the month of any of the merch months in row 2 or row 3
    case merch_month
    when *FOUR_WEEK_MONTHS
      # 28 = 4 weeks
      start += 28
    when *FIVE_WEEK_MONTHS
      # 56 = 8 weeks
      start += 56
    end

    start
  end
end

So you can use it as this:

merch_week = MerchCalendar::MerchWeek.from_date('2022-01-25', calendar: Calendar445.new)
puts merch_week.start_of_month # 2021-12-26

If you need to get the start or end date of the quarter, you can monkey patching the MerchWeek class like this:

 module MerchCalendar
  class MerchWeek
    def end_of_quarter
      calendar.end_of_quarter(year, quarter)
    end

    def start_of_quarter
      calendar.start_of_quarter(year, quarter)
    end
  end
end

So you can use the stuff like this:

merch_week = MerchCalendar::MerchWeek.from_date('2022-01-25', calendar: Calendar445.new)

puts merch_week.start_of_quarter # 2021-10-31
puts merch_week.end_of_quarter  # 2022-01-29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants