ce780fefe88afd6bf039a1efd22cf9bd5a37b719
[freeside.git] / FS / FS / rate_prefix.pm
1 package FS::rate_prefix;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::rate_region;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::rate_prefix - Object methods for rate_prefix records
13
14 =head1 SYNOPSIS
15
16   use FS::rate_prefix;
17
18   $record = new FS::rate_prefix \%hash;
19   $record = new FS::rate_prefix { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::rate_prefix object represents an call rating prefix.  FS::rate_prefix
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item prefixnum - primary key
37
38 =item regionnum - call ration region (see L<FS::rate_region>)
39
40 =item countrycode
41
42 =item npa
43
44 =item nxx
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Creates a new prefix.  To add the prefix to the database, see L<"insert">.
55
56 Note that this stores the hash reference, not a distinct copy of the hash it
57 points to.  You can ask the object for a copy with the I<hash> method.
58
59 =cut
60
61 # the new method can be inherited from FS::Record, if a table method is defined
62
63 sub table { 'rate_prefix'; }
64
65 =item insert
66
67 Adds this record to the database.  If there is an error, returns the error,
68 otherwise returns false.
69
70 =cut
71
72 # the insert method can be inherited from FS::Record
73
74 =item delete
75
76 Delete this record from the database.
77
78 =cut
79
80 # the delete method can be inherited from FS::Record
81
82 =item replace OLD_RECORD
83
84 Replaces the OLD_RECORD with this one in the database.  If there is an error,
85 returns the error, otherwise returns false.
86
87 =cut
88
89 # the replace method can be inherited from FS::Record
90
91 =item check
92
93 Checks all fields to make sure this is a valid prefix.  If there is
94 an error, returns the error, otherwise returns false.  Called by the insert
95 and replace methods.
96
97 =cut
98
99 # the check method should currently be supplied - FS::Record contains some
100 # data checking routines
101
102 sub check {
103   my $self = shift;
104
105   my $error =
106        $self->ut_numbern('prefixnum')
107     || $self->ut_foreign_key('regionnum', 'rate_region', 'regionnum' )
108     || $self->ut_number('countrycode')
109     || $self->ut_numbern('npa')
110     || $self->ut_numbern('nxx')
111   ;
112   return $error if $error;
113
114   $self->SUPER::check;
115 }
116
117 =item rate_region
118
119 Returns the rate region (see L<FS::rate_region>) for this prefix.
120
121 =cut
122
123 sub rate_region {
124   my $self = shift;
125   qsearchs('rate_region', { 'regionnum' => $self->regionnum } );
126 }
127
128 =back
129
130 =head1 CLASS METHODS
131
132 =over 4
133
134 =item all_countrycodes
135
136 Returns a list of all countrycodes listed in rate_prefix
137
138 =cut
139
140 sub all_countrycodes { 
141   #my $class = shift;
142   my $sql =
143     "SELECT DISTINCT(countrycode) FROM rate_prefix ORDER BY countrycode";
144   my $sth = dbh->prepare($sql) or die  dbh->errstr;
145   $sth->execute                or die $sth->errstr;
146   map $_->[0], @{ $sth->fetchall_arrayref };
147 }
148
149 =back
150
151 =head1 BUGS
152
153 =head1 SEE ALSO
154
155 L<FS::rate_region>, L<FS::Record>, schema.html from the base documentation.
156
157 =cut
158
159 1;
160