This commit was generated by cvs2svn to compensate for changes in r8593,
[freeside.git] / FS / FS / phone_device.pm
1 package FS::phone_device;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( dbh qsearchs ); # qsearch );
6 use FS::part_device;
7 use FS::svc_phone;
8
9 =head1 NAME
10
11 FS::phone_device - Object methods for phone_device records
12
13 =head1 SYNOPSIS
14
15   use FS::phone_device;
16
17   $record = new FS::phone_device \%hash;
18   $record = new FS::phone_device { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::phone_device object represents a specific customer phone device, such as
31 a SIP phone or ATA.  FS::phone_device inherits from FS::Record.  The following
32 fields are currently supported:
33
34 =over 4
35
36 =item devicenum
37
38 primary key
39
40 =item devicepart
41
42 devicepart
43
44 =item svcnum
45
46 svcnum
47
48 =item mac_addr
49
50 mac_addr
51
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new record.  To add the record to the database, see 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 { 'phone_device'; }
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 sub insert {
80   my $self = shift;
81
82   local $SIG{HUP} = 'IGNORE';
83   local $SIG{INT} = 'IGNORE';
84   local $SIG{QUIT} = 'IGNORE';
85   local $SIG{TERM} = 'IGNORE';
86   local $SIG{TSTP} = 'IGNORE';
87   local $SIG{PIPE} = 'IGNORE';
88
89   my $oldAutoCommit = $FS::UID::AutoCommit;
90   local $FS::UID::AutoCommit = 0;
91   my $dbh = dbh;
92
93   my $error = $self->SUPER::insert;
94   if ( $error ) {
95     $dbh->rollback if $oldAutoCommit;
96     return $error;
97   }
98
99   $self->svc_phone->export('device_insert', $self); #call device export
100
101   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
102   '';
103 }
104
105
106 =item delete
107
108 Delete this record from the database.
109
110 =cut
111
112 sub delete {
113   my $self = shift;
114
115   local $SIG{HUP} = 'IGNORE';
116   local $SIG{INT} = 'IGNORE';
117   local $SIG{QUIT} = 'IGNORE';
118   local $SIG{TERM} = 'IGNORE';
119   local $SIG{TSTP} = 'IGNORE';
120   local $SIG{PIPE} = 'IGNORE';
121
122   my $oldAutoCommit = $FS::UID::AutoCommit;
123   local $FS::UID::AutoCommit = 0;
124   my $dbh = dbh;
125
126   $self->svc_phone->export('device_delete', $self); #call device export
127
128   my $error = $self->SUPER::delete;
129   if ( $error ) {
130     $dbh->rollback if $oldAutoCommit;
131     return $error;
132   }
133
134   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
135   '';
136 }
137
138 =item replace OLD_RECORD
139
140 Replaces the OLD_RECORD with this one in the database.  If there is an error,
141 returns the error, otherwise returns false.
142
143 =cut
144
145 sub replace {
146   my $new = shift;
147
148   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
149               ? shift
150               : $new->replace_old;
151
152   local $SIG{HUP} = 'IGNORE';
153   local $SIG{INT} = 'IGNORE';
154   local $SIG{QUIT} = 'IGNORE';
155   local $SIG{TERM} = 'IGNORE';
156   local $SIG{TSTP} = 'IGNORE';
157   local $SIG{PIPE} = 'IGNORE';
158
159   my $oldAutoCommit = $FS::UID::AutoCommit;
160   local $FS::UID::AutoCommit = 0;
161   my $dbh = dbh;
162
163   my $error = $new->SUPER::replace($old);
164   if ( $error ) {
165     $dbh->rollback if $oldAutoCommit;
166     return $error;
167   }
168
169   $new->svc_phone->export('device_replace', $new, $old); #call device export
170
171   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
172   '';
173 }
174
175 =item check
176
177 Checks all fields to make sure this is a valid record.  If there is
178 an error, returns the error, otherwise returns false.  Called by the insert
179 and replace methods.
180
181 =cut
182
183 # the check method should currently be supplied - FS::Record contains some
184 # data checking routines
185
186 sub check {
187   my $self = shift;
188
189   my $mac = $self->mac_addr;
190   $mac =~ s/\s+//g;
191   $mac =~ s/://g;
192   $self->mac_addr($mac);
193
194   my $error = 
195     $self->ut_numbern('devicenum')
196     || $self->ut_foreign_key('devicepart', 'part_device', 'devicepart')
197     || $self->ut_foreign_key('svcnum', 'svc_phone', 'svcnum' ) #cust_svc?
198     || $self->ut_hexn('mac_addr')
199   ;
200   return $error if $error;
201
202   $self->SUPER::check;
203 }
204
205 =item part_device
206
207 Returns the device type record (see L<FS::part_device>) associated with this
208 customer device.
209
210 =cut
211
212 sub part_device {
213   my $self = shift;
214   qsearchs( 'part_device', { 'devicepart' => $self->devicepart } );
215 }
216
217 =item svc_phone
218
219 Returns the phone number (see L<FS::svc_phone>) associated with this customer
220 device.
221
222 =cut
223
224 sub svc_phone {
225   my $self = shift;
226   qsearchs( 'svc_phone', { 'svcnum' => $self->svcnum } );
227 }
228
229 =back
230
231 =head1 BUGS
232
233 =head1 SEE ALSO
234
235 L<FS::Record>, schema.html from the base documentation.
236
237 =cut
238
239 1;
240