per-agent disable_previous_balance, #15863
[freeside.git] / FS / FS / rate_time_interval.pm
1 package FS::rate_time_interval;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use List::Util 'first';
7
8 =head1 NAME
9
10 FS::rate_time_interval - Object methods for rate_time_interval records
11
12 =head1 SYNOPSIS
13
14   use FS::rate_time_interval;
15
16   $record = new FS::rate_time_interval \%hash;
17   $record = new FS::rate_time_interval { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::rate_time_interval object represents an interval of clock time during 
30 the week, such as "Monday, 7 AM to 8 PM".  FS::rate_time_interval inherits 
31 from FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item intervalnum
36
37 primary key
38
39 =item stime
40
41 Start of the interval, in seconds from midnight on Sunday.
42
43 =item etime
44
45 End of the interval.
46
47 =item ratetimenum
48
49 A foreign key to an L<FS::rate_time> object representing the set of intervals 
50 to which this belongs.
51
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new example.  To add the example to the database, see L<"insert">.
62
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to.  You can ask the object for a copy with the I<hash> method.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'rate_time_interval'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =cut
78
79 # the insert method can be inherited from FS::Record
80
81 =item delete
82
83 Delete this record from the database.
84
85 =cut
86
87 # the delete method can be inherited from FS::Record
88
89 =item replace OLD_RECORD
90
91 Replaces the OLD_RECORD with this one in the database.  If there is an error,
92 returns the error, otherwise returns false.
93
94 =cut
95
96 # the replace method can be inherited from FS::Record
97
98 =item check
99
100 Checks all fields to make sure this is a valid interval.  If there is
101 an error, returns the error, otherwise returns false.  Called by the insert
102 and replace methods.
103
104 =cut
105
106 sub check {
107   my $self = shift;
108
109   my $error = 
110     $self->ut_numbern('intervalnum')
111     || $self->ut_number('stime')
112     || $self->ut_number('etime')
113     || $self->ut_number('ratetimenum')
114   ;
115   return $error if $error;
116   # Disallow backward intervals. As a special case, an etime of 0 
117   # should roll to the last second of the week.
118   $self->etime(7*24*60*60) if $self->etime == 0;
119   return "end of interval is before start" if ($self->etime < $self->stime);
120
121   # Detect overlap between intervals within the same rate_time.
122   # Since intervals are added one at a time, we only need to look 
123   # for an existing interval that contains one of the endpoints of
124   # this one or that is completely inside this one.
125   my $overlap = $self->rate_time->contains($self->stime + 1) ||
126                 $self->rate_time->contains($self->etime - 1) ||
127                 first { $self->stime <= $_->stime && $self->etime >= $_->etime }
128                     ( $self->rate_time->intervals );
129   return "interval overlap: (".join('-',$self->description).') with ('.
130       join('-',$overlap->description).')' if $overlap;
131
132   $self->SUPER::check;
133 }
134
135 =item rate_time
136
137 Returns the L<FS::rate_time> comprising this interval.
138
139 =cut
140
141 sub rate_time {
142   my $self = shift;
143   FS::rate_time->by_key($self->ratetimenum);
144 }
145
146 =item description
147
148 Returns two strings containing stime and etime, formatted 
149 "Day HH:MM AM/PM".  Example: "Mon 5:00 AM".  Seconds are 
150 not displayed, so be careful.
151
152 =cut
153
154 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
155
156 sub description {
157   my $self = shift;
158   return map { 
159             sprintf('%s %02d:%02d %s',
160             $days[int($_/86400) % 7],
161             (int($_/3600) % 12 || 12),
162             int($_/60) % 60,
163             (($_/3600) % 24 < 12) ? 'AM' : 'PM' )
164        } ( $self->stime, $self->etime );
165 }
166
167 =back
168
169 =head1 BUGS
170
171 =head1 SEE ALSO
172
173 L<FS::rate_time>, L<FS::Record>, schema.html from the base documentation.
174
175 =cut
176
177 1;
178