Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,53 @@ Since 1st Jan 2009 in India falls in the 2008-09 financial year
Date.today.beginning_of_year.previous_financial_quarter
=> 1st Apr 2008

##### quarters_for_year(financial_year)

Returns detailed information for all four quarters of the given financial year, based on the configured fiscal year start month. Each quarter is represented as a hash containing:

- Quarter name (`Q1 2024`, `Q2 2024`, etc.)
- Start and end month (`Apr 2024`, `Jun 2024`, etc.)
- Start and end date (`2024-04-01`, `2024-06-30`, etc.)
- Start and end month names (`April`, `June`, etc.)
- Start and end year

```ruby
=> Date.quarters_for_year(2024)
=> [
{
quarter: "Q1 2024", start_month: "Apr 2024", finish_month: "Jun 2024", start_date: "2024-04-01", finish_date: "2024-06-30", start_month_name: "April", finish_month_name: "June", start_year: "2024", finish_year: "2024"
},
{
quarter: "Q2 2024", start_month: "Jul 2024", finish_month: "Sep 2024", start_date: "2024-07-01", finish_date: "2024-09-30", start_month_name: "July", finish_month_name: "September", start_year: "2024", finish_year: "2024"
},
{
quarter: "Q3 2024", start_month: "Oct 2024", finish_month: "Dec 2024", start_date: "2024-10-01", finish_date: "2024-12-31", start_month_name: "October", finish_month_name: "December", start_year: "2024", finish_year: "2024"
},
{
quarter: "Q4 2024", start_month: "Jan 2025", finish_month: "Mar 2025", start_date: "2025-01-01", finish_date: "2025-03-31", start_month_name: "January", finish_month_name: "March", start_year: "2025", finish_year: "2025"
}
]
```

##### months_for_quarter(financial_year, quarter)

Returns the months for a specific quarter of the given financial year, based on the configured fiscal year start month.

```ruby
Date.months_for_quarter(2024, 1)
=> ["Apr 2024", "May 2024", "Jun 2024"]

Date.months_for_quarter(2024, 2)
=> ["Jul 2024", "Aug 2024", "Sep 2024"]

Date.months_for_quarter(2024, 3)
=> ["Oct 2024", "Nov 2024", "Dec 2024"]

Date.months_for_quarter(2024, 4)
=> ["Jan 2025", "Feb 2025", "Mar 2025"]
```

```
## Contributors

* [@mxie](https://github.com/mxie)
Expand Down
25 changes: 25 additions & 0 deletions lib/rising_sun/fiscali.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ def financial_month_of(month)
end
end

def quarters_for_year(financial_year)
fy_start = Date.new(financial_year, Date.fy_start_month, 1)
(1..4).map do |q|
start = fy_start >> ((q - 1) * 3)
finish = start.next_month(3) - 1
{
quarter: "Q#{q} #{financial_year}",
start_month: start.strftime('%b %Y'),
finish_month: finish.strftime('%b %Y'),
start_date: start.strftime('%Y-%m-%d'),
finish_date: finish.strftime('%Y-%m-%d'),
start_month_name: start.strftime('%B'),
finish_month_name: finish.strftime('%B'),
start_year: start.strftime('%Y'),
finish_year: finish.strftime('%Y'),
}
end
end

def months_for_quarter(financial_year, quarter)
fy_start = Date.new(financial_year, Date.fy_start_month, 1)
quarter_start = fy_start >> ((quarter - 1) * 3)
(0..2).map { |i| (quarter_start >> i).strftime('%b %Y') }
end

private

def year_of_financial_year_beginning
Expand Down
47 changes: 47 additions & 0 deletions spec/fiscali_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,51 @@
expect(range.last).to eql(date.end_of_financial_year)
end
end


context '.quarters_for_year' do
let(:date) { Date.new(2024, 1, 1) }

before do
Date.fy_start_month = 4
end

it 'returns 4 quarters for the given year with correct details' do
quarters = date.quarters_for_year(2024)
expect(quarters.length).to eq(4)

expected_quarters = [
{ quarter: "Q1 2024", start_month: "Apr 2024", finish_month: "Jun 2024", start_date: "2024-04-01", finish_date: "2024-06-30" },
{ quarter: "Q2 2024", start_month: "Jul 2024", finish_month: "Sep 2024", start_date: "2024-07-01", finish_date: "2024-09-30" },
{ quarter: "Q3 2024", start_month: "Oct 2024", finish_month: "Dec 2024", start_date: "2024-10-01", finish_date: "2024-12-31" },
{ quarter: "Q4 2024", start_month: "Jan 2025", finish_month: "Mar 2025", start_date: "2025-01-01", finish_date: "2025-03-31" }
]

expected_quarters.each_with_index do |expected, index|
expect(quarters[index][:quarter]).to eq(expected[:quarter])
expect(quarters[index][:start_month]).to eq(expected[:start_month])
expect(quarters[index][:finish_month]).to eq(expected[:finish_month])
expect(quarters[index][:start_date]).to eq(expected[:start_date])
expect(quarters[index][:finish_date]).to eq(expected[:finish_date])
end
end
end

context '.months_for_quarter' do
let(:date) { Date.new(2024, 1, 1) }

before do
Date.fy_start_month = 4
end

it 'returns months for Q1' do
months = date.months_for_quarter(2024, 1)
expect(months).to eq(["Apr 2024", "May 2024", "Jun 2024"])
end

it 'returns months for Q4' do
months = date.months_for_quarter(2024, 4)
expect(months).to eq(["Jan 2025", "Feb 2025", "Mar 2025"])
end
end
end