fixup per call billing
[freeside.git] / FS / FS / rate_detail.pm
1 package FS::rate_detail;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::rate;
7 use FS::rate_region;
8 use Tie::IxHash;
9
10 @ISA = qw(FS::Record);
11
12 =head1 NAME
13
14 FS::rate_detail - Object methods for rate_detail records
15
16 =head1 SYNOPSIS
17
18   use FS::rate_detail;
19
20   $record = new FS::rate_detail \%hash;
21   $record = new FS::rate_detail { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::rate_detail object represents an call plan rate.  FS::rate_detail
34 inherits from FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item ratedetailnum - primary key
39
40 =item ratenum - rate plan (see L<FS::rate>)
41
42 =item orig_regionnum - call origination region
43
44 =item dest_regionnum - call destination region
45
46 =item min_included - included minutes
47
48 =item min_charge - charge per minute
49
50 =item sec_granularity - granularity in seconds, i.e. 6 or 60; 0 for per-call
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new call plan rate.  To add the call plan rate to the database, see
61 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_detail'; }
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 call plan rate.  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 # the check method should currently be supplied - FS::Record contains some
107 # data checking routines
108
109 sub check {
110   my $self = shift;
111
112   my $error = 
113        $self->ut_numbern('ratedetailnum')
114     || $self->ut_foreign_key('ratenum', 'rate', 'ratenum')
115     || $self->ut_foreign_keyn('orig_regionnum', 'rate_region', 'regionnum' )
116     || $self->ut_foreign_key('dest_regionnum', 'rate_region', 'regionnum' )
117     || $self->ut_number('min_included')
118
119     #|| $self->ut_money('min_charge')
120     #good enough for now...
121     || $self->ut_float('min_charge')
122
123     || $self->ut_number('sec_granularity')
124   ;
125   return $error if $error;
126
127   $self->SUPER::check;
128 }
129
130 =item rate 
131
132 Returns the parent call plan (see L<FS::rate>) associated with this call plan
133 rate.
134
135 =cut
136
137 sub rate {
138   my $self = shift;
139   qsearchs('rate', { 'ratenum' => $self->ratenum } );
140 }
141
142 =item orig_region 
143
144 Returns the origination region (see L<FS::rate_region>) associated with this
145 call plan rate.
146
147 =cut
148
149 sub orig_region {
150   my $self = shift;
151   qsearchs('rate_region', { 'regionnum' => $self->orig_regionnum } );
152 }
153
154 =item dest_region 
155
156 Returns the destination region (see L<FS::rate_region>) associated with this
157 call plan rate.
158
159 =cut
160
161 sub dest_region {
162   my $self = shift;
163   qsearchs('rate_region', { 'regionnum' => $self->dest_regionnum } );
164 }
165
166 =item dest_regionname
167
168 Returns the name of the destination region (see L<FS::rate_region>) associated
169 with this call plan rate.
170
171 =cut
172
173 sub dest_regionname {
174   my $self = shift;
175   $self->dest_region->regionname;
176 }
177
178 =item dest_regionname
179
180 Returns a short list of the prefixes for the destination region
181 (see L<FS::rate_region>) associated with this call plan rate.
182
183 =cut
184
185 sub dest_prefixes_short {
186   my $self = shift;
187   $self->dest_region->prefixes_short;
188 }
189
190
191 =back
192
193 =head1 SUBROUTINES
194
195 =over 4
196
197 =item granularities
198
199   Returns an (ordered) hash of granularity => name pairs
200
201 =cut
202
203 tie my %granularities, 'Tie::IxHash',
204   '1', => '1 second',
205   '6'  => '6 second',
206   '30' => '30 second', # '1/2 minute',
207   '60' => 'minute',
208   '0'  => 'call',
209 ;
210
211 sub granularities {
212   %granularities;
213 }
214
215
216 =back
217
218 =head1 BUGS
219
220 =head1 SEE ALSO
221
222 L<FS::rate>, L<FS::rate_region>, L<FS::Record>,
223 schema.html from the base documentation.
224
225 =cut
226
227 1;
228