1 package FS::rate_time_interval;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use List::Util 'first';
10 FS::rate_time_interval - Object methods for rate_time_interval records
14 use FS::rate_time_interval;
16 $record = new FS::rate_time_interval \%hash;
17 $record = new FS::rate_time_interval { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
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:
41 Start of the interval, in seconds from midnight on Sunday.
49 A foreign key to an L<FS::rate_time> object representing the set of intervals
50 to which this belongs.
61 Creates a new example. To add the example to the database, see L<"insert">.
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.
68 # the new method can be inherited from FS::Record, if a table method is defined
70 sub table { 'rate_time_interval'; }
74 Adds this record to the database. If there is an error, returns the error,
75 otherwise returns false.
79 # the insert method can be inherited from FS::Record
83 Delete this record from the database.
87 # the delete method can be inherited from FS::Record
89 =item replace OLD_RECORD
91 Replaces the OLD_RECORD with this one in the database. If there is an error,
92 returns the error, otherwise returns false.
96 # the replace method can be inherited from FS::Record
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
110 $self->ut_numbern('intervalnum')
111 || $self->ut_number('stime')
112 || $self->ut_number('etime')
113 || $self->ut_number('ratetimenum')
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);
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;
137 Returns the L<FS::rate_time> comprising this interval.
143 FS::rate_time->by_key($self->ratetimenum);
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.
154 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
159 sprintf('%s %02d:%02d %s',
160 $days[int($_/86400) % 7],
163 (($_/3600) % 24 < 12) ? 'AM' : 'PM' )
164 } ( $self->stime, $self->etime );
173 L<FS::rate_time>, L<FS::Record>, schema.html from the base documentation.