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