eWay self-signup fixes
[freeside.git] / FS / FS / rate_time.pm
1 package FS::rate_time;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::rate_time_interval;
7
8 =head1 NAME
9
10 FS::rate_time - Object methods for rate_time records
11
12 =head1 SYNOPSIS
13
14   use FS::rate_time;
15
16   $record = new FS::rate_time \%hash;
17   $record = new FS::rate_time { '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 object represents a time period for selection of CDR billing 
30 rates.  FS::rate_time inherits from FS::Record.  The following fields are 
31 currently supported:
32
33 =over 4
34
35 =item ratetimenum
36
37 primary key
38
39 =item ratetimename
40
41 A label (like "Daytime" or "Weekend").
42
43 =back
44
45 =head1 METHODS
46
47 =over 4
48
49 =item new HASHREF
50
51 Creates a new example.  To add the example to the database, see L<"insert">.
52
53 Note that this stores the hash reference, not a distinct copy of the hash it
54 points to.  You can ask the object for a copy with the I<hash> method.
55
56 =cut
57
58 # the new method can be inherited from FS::Record, if a table method is defined
59
60 sub table { 'rate_time'; }
61
62 =item insert
63
64 Adds this record to the database.  If there is an error, returns the error,
65 otherwise returns false.
66
67 =cut
68
69 # the insert method can be inherited from FS::Record
70
71 =item delete
72
73 Delete this record from the database.
74
75 =cut
76
77 # the delete method can be inherited from FS::Record
78
79 =item replace OLD_RECORD
80
81 Replaces the OLD_RECORD with this one in the database.  If there is an error,
82 returns the error, otherwise returns false.
83
84 =cut
85
86 # the replace method can be inherited from FS::Record
87
88 =item check
89
90 Checks all fields to make sure this is a valid example.  If there is
91 an error, returns the error, otherwise returns false.  Called by the insert
92 and replace methods.
93
94 =cut
95
96 sub check {
97   my $self = shift;
98
99   my $error = 
100     $self->ut_numbern('ratetimenum')
101     || $self->ut_text('ratetimename')
102   ;
103   return $error if $error;
104
105   $self->SUPER::check;
106 }
107
108 =item intervals
109
110 Return the L<FS::rate_time_interval> objects included in this rating period.
111
112 =cut
113
114 sub intervals {
115   my $self = shift;
116   return qsearch({ table    => 'rate_time_interval', 
117                    hashref  => { ratetimenum => $self->ratetimenum },
118                    order_by => 'ORDER BY stime ASC',
119   });
120 }
121
122 =item contains TIME
123
124 Return the L<FS::rate_time_interval> object that contains the specified 
125 time-of-week (in seconds from the start of Sunday).  The primary use of 
126 this is to test whether that time falls within this rating period.
127
128 =cut
129
130 sub contains {
131   my $self = shift;
132   my $weektime = shift;
133   return qsearchs('rate_time_interval', { ratetimenum => $self->ratetimenum,
134                                           stime => { op    => '<=', 
135                                                      value => $weektime },
136                                           etime => { op    => '>',
137                                                      value => $weektime },
138                                         } );
139 }
140
141 =item description
142
143 Returns a list of arrayrefs containing the starting and 
144 ending times of each interval in this period, in a readable
145 format.
146
147 =cut
148
149 sub description {
150   my $self = shift;
151   return map { [ $_->description ] } $self->intervals;
152 }
153
154
155 =back
156
157 =head1 BUGS
158
159 To be seen.
160
161 =head1 SEE ALSO
162
163 L<FS::Record>, schema.html from the base documentation.
164
165 =cut
166
167 1;
168