voip_cdr call rating by day and time, RT#4763
[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
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 example.  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
116   $self->SUPER::check;
117 }
118
119 =item rate_time
120
121 Returns the L<FS::rate_time> comprising this interval.
122
123 =cut
124
125 sub rate_time {
126   my $self = shift;
127   FS::rate_time->by_key($self->ratetimenum);
128 }
129
130 =item description
131
132 Returns two strings containing stime and etime, formatted 
133 "Day HH:MM:SS AM/PM".  Example: "Mon 5:00 AM".
134
135 =cut
136
137 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
138
139 sub description {
140   my $self = shift;
141   return map { 
142             sprintf('%s %02d:%02d:%02d %s',
143             $days[int($_/86400) % 7],
144             int($_/3600) % 12,
145             int($_/60) % 60,
146             $_ % 60,
147             (($_/3600) % 24 < 12) ? 'AM' : 'PM' )
148        } ( $self->stime, $self->etime );
149 }
150
151 =back
152
153 =head1 BUGS
154
155 =head1 SEE ALSO
156
157 L<FS::rate_time>, L<FS::Record>, schema.html from the base documentation.
158
159 =cut
160
161 1;
162