File Coverage

blib/lib/DateTime/Fiscal/Year.pm
Criterion Covered Total %
statement 66 78 84.6
branch 17 28 60.7
condition n/a
subroutine 12 12 100.0
pod 5 5 100.0
total 100 123 81.3


line stmt bran cond sub pod time code
1             package DateTime::Fiscal::Year;
2              
3 4     4   1070000 use strict;
  4         11  
  4         159  
4              
5 4     4   24 use DateTime;
  4         9  
  4         94  
6 4     4   23 use Params::Validate qw( validate validate_pos);
  4         13  
  4         288  
7 4     4   20 use vars qw($VERSION);
  4         8  
  4         372  
8              
9             $VERSION = '0.02';
10              
11             my ( @MonthLengths, @LeapYearMonthLengths );
12              
13             BEGIN {
14 4     4   15 @MonthLengths =
15             ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
16              
17 4         11 @LeapYearMonthLengths = @MonthLengths;
18 4         3540 $LeapYearMonthLengths[1]++;
19             }
20              
21             sub new {
22 12     12 1 10387 my $class = shift;
23 12         171 my %args = validate( @_, { start => { isa => 'DateTime' } });
24              
25 12         532 my $self = { start => $args{start} };
26 12         32 bless $self, $class;
27 12         45 return $self;
28             }
29              
30              
31             sub day_of_fiscal_year {
32 40     40 1 78 my $self = shift;
33              
34             # return $self->{day_of_fiscal_year} if defined($self->{day_of_fiscal_year});
35              
36 40         660 my @args = validate_pos( @_, { isa => 'DateTime' } );
37 40         1056 my $dofy;
38              
39 40         129 my $offset = ($self->{start}->day_of_year);
40              
41 40 100       207 my $dify = ( $self->{start}->is_leap_year ? 366 : 365 );
42 40         350 my $doy = $args[0]->day_of_year;
43              
44 40 100       164 if ( $doy >= $offset ) { $dofy = ( $doy - $offset + 1 ); } else { $dofy = (( $dify - $offset) + $doy + 1); }
  36         88  
  4         7  
45 40         225 $self->{day_of_fiscal_year} = $dofy;
46             }
47              
48             sub week_of_fiscal_year {
49 10     10 1 21 my $self = shift;
50            
51             # return $self->{week_of_fiscal_year} if defined($self->{week_of_fiscal_year});
52 10         135 my @args = validate_pos( @_, { isa => 'DateTime' } );
53              
54 10         311 my $wofy;
55              
56 10         28 $wofy = $self->day_of_fiscal_year( $args[0] ) / 7;
57 10 100       45 $wofy = ( $wofy == int($wofy) ? $wofy : int($wofy + 1) );
58 10 100       28 if ($wofy == 53 ) { $wofy = 52 ;}
  4         8  
59 10         56 $self->{week_of_fiscal_year} = $wofy;
60            
61             }
62              
63             sub period_of_fiscal_year {
64 2     2 1 7 my $self = shift;
65              
66             # return $self->{period_of_fiscal_year} if defined($self->{period_of_fiscal_year});
67              
68 2         8 $self->{number_of_fiscal_periods} = shift;
69 2         32 my @args = validate_pos( @_, { isa => 'DateTime' } );
70              
71              
72              
73 2         64 $self->_align_fiscal_periods;
74 2 50       5 if ( $self->{number_of_fiscal_periods} == 12 ) {
75 2         8 $self->{period_of_fiscal_year} = $self->_period_index( $args[0] );
76 2 50       8 $self->{period_of_fiscal_year}++ if ( $self->{start}->month == 1 );
77             }
78              
79 2 50       16 if ( $self->{number_of_fiscal_periods} == 13 ) {
80 0         0 $self->{period_of_fiscal_year} = $self->{fiscal_periods}->[$self->week_of_fiscal_year( $args[0] )];
81             }
82              
83 2         11 $self->{period_of_fiscal_year};
84             }
85              
86             sub quarter_of_fiscal_year {
87 1     1 1 3 my $self = shift;
88              
89 1         24 $self->period_of_fiscal_year( shift, shift );
90            
91 1 50       6 if ( $self->{number_of_fiscal_periods} == 12 ) {
92 1         10 return $self->{quarter_of_fiscal_year} = int((( $self->{period_of_fiscal_year} - 1)/3) + 1);
93             }
94              
95 0 0       0 if ( $self->{number_of_fiscal_periods} == 13 ) {
96 0         0 return $self->{quarter_of_fiscal_year} = int((( $self->{period_of_fiscal_year} - 1)/4) + 1);
97             }
98             }
99              
100             sub _align_fiscal_periods {
101 2     2   5 my $self = shift;
102              
103 2 50       10 if ( $self->{number_of_fiscal_periods} == 12 ) {
104 2 50       10 my @periods = $self->{start}->is_leap_year ? @LeapYearMonthLengths : @MonthLengths;
105 2         30 for ( my $i = 1; $i < $self->{start}->month; $i++ ) {
106 2         20 push @periods, shift @periods;
107             }
108 2         13 push @periods, 0;
109 2         10 for ( my $i = 0; $i <= $#periods; $i++ ) {
110 26         57 $periods[$i] += $periods[$i - 1];
111             }
112 2         14 unshift @periods, 0;
113 2         5 $self->{fiscal_periods} = \@periods;
114             };
115              
116 2 50       8 if ( $self->{number_of_fiscal_periods} == 13 ) {
117 0         0 my @periods = 0;
118 0         0 my $counter = 0;
119 0         0 my $period_index = 1;
120              
121 0         0 for ( my $i = 1; $i <= 52; $i++, $counter++ ) {
122 0 0       0 if ( $counter == 4 ) { $counter = 0; $period_index++; }
  0         0  
  0         0  
123 0         0 $periods[$i] = $period_index;
124             }
125 0         0 $self->{fiscal_periods} = \@periods;
126             }
127 2         5 $self->{fiscal_periods};
128             }
129              
130             sub _period_index {
131 2     2   12 my $self = shift;
132              
133 2         5 foreach my $index ( reverse 1..12 ) {
134 20 100       54 return $index
135             if $self->{fiscal_periods}->[$index - 1] < $self->day_of_fiscal_year( @_ );
136             }
137             }
138              
139             1;
140              
141             __END__