BroadWorks export, phase 1, #25927
[freeside.git] / FS / FS / phone_device.pm
1 package FS::phone_device;
2
3 use strict;
4 use base qw( FS::Record );
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( dbh qsearchs ); # qsearch );
7 use FS::part_device;
8 use FS::svc_phone;
9
10 =head1 NAME
11
12 FS::phone_device - Object methods for phone_device records
13
14 =head1 SYNOPSIS
15
16   use FS::phone_device;
17
18   $record = new FS::phone_device \%hash;
19   $record = new FS::phone_device { '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::phone_device object represents a specific customer phone device, such as
32 a SIP phone or ATA.  FS::phone_device inherits from FS::Record.  The following
33 fields are currently supported:
34
35 =over 4
36
37 =item devicenum
38
39 primary key
40
41 =item devicepart
42
43 devicepart
44
45 =item svcnum
46
47 svcnum
48
49 =item mac_addr
50
51 mac_addr
52
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new record.  To add the record to the database, see L<"insert">.
63
64 Note that this stores the hash reference, not a distinct copy of the hash it
65 points to.  You can ask the object for a copy with the I<hash> method.
66
67 =cut
68
69 # the new method can be inherited from FS::Record, if a table method is defined
70
71 sub table { 'phone_device'; }
72
73 =item insert
74
75 Adds this record to the database.  If there is an error, returns the error,
76 otherwise returns false.
77
78 =cut
79
80 sub insert {
81   my $self = shift;
82
83   local $SIG{HUP} = 'IGNORE';
84   local $SIG{INT} = 'IGNORE';
85   local $SIG{QUIT} = 'IGNORE';
86   local $SIG{TERM} = 'IGNORE';
87   local $SIG{TSTP} = 'IGNORE';
88   local $SIG{PIPE} = 'IGNORE';
89
90   my $oldAutoCommit = $FS::UID::AutoCommit;
91   local $FS::UID::AutoCommit = 0;
92   my $dbh = dbh;
93
94   my $error = $self->SUPER::insert;
95   $error ||= $self->export('device_insert');
96   if ( $error ) {
97     $dbh->rollback if $oldAutoCommit;
98     return $error;
99   }
100
101
102   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
103   '';
104 }
105
106
107 =item delete
108
109 Delete this record from the database.
110
111 =cut
112
113 sub delete {
114   my $self = shift;
115
116   local $SIG{HUP} = 'IGNORE';
117   local $SIG{INT} = 'IGNORE';
118   local $SIG{QUIT} = 'IGNORE';
119   local $SIG{TERM} = 'IGNORE';
120   local $SIG{TSTP} = 'IGNORE';
121   local $SIG{PIPE} = 'IGNORE';
122
123   my $oldAutoCommit = $FS::UID::AutoCommit;
124   local $FS::UID::AutoCommit = 0;
125   my $dbh = dbh;
126
127
128   my $error = $self->export('device_delete') || $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               || $new->export('device_replace', $old);
165   if ( $error ) {
166     $dbh->rollback if $oldAutoCommit;
167     return $error;
168   }
169
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 =item export HOOK [ EXPORT_ARGS ]
230
231 Runs the provided export hook (i.e. "device_insert") for this service.
232
233 =cut
234
235 sub export {
236   my( $self, $method ) = ( shift, shift );
237
238   local $SIG{HUP} = 'IGNORE';
239   local $SIG{INT} = 'IGNORE';
240   local $SIG{QUIT} = 'IGNORE';
241   local $SIG{TERM} = 'IGNORE';
242   local $SIG{TSTP} = 'IGNORE';
243   local $SIG{PIPE} = 'IGNORE';
244
245   my $oldAutoCommit = $FS::UID::AutoCommit;
246   local $FS::UID::AutoCommit = 0;
247   my $dbh = dbh;
248
249   my $svc_phone = $self->svc_phone;
250   my $error = $svc_phone->export($method, $self, @_); #call device export
251   if ( $error ) {                                     #netsapiens at least
252     $dbh->rollback if $oldAutoCommit;
253     return "error exporting $method event to svc_phone ". $svc_phone->svcnum.
254            " (transaction rolled back): $error";
255   }
256
257   $method = "export_$method" unless $method =~ /^export_/;
258
259   foreach my $part_export ( $self->part_device->part_export ) {
260     next unless $part_export->can($method);
261     my $error = $part_export->$method($svc_phone, $self, @_);
262     if ( $error ) {
263       $dbh->rollback if $oldAutoCommit;
264       return "error exporting $method event to ". $part_export->exporttype.
265              " (transaction rolled back): $error";
266     }
267   }
268
269   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
270   '';
271
272 }
273
274 =item export_links
275
276 Returns a list of html elements associated with this device's exports.
277
278 =cut
279
280 sub export_links {
281   my $self = shift;
282   my $return = [];
283   $self->export('export_device_links', $return);
284   $return;
285 }
286
287 =back
288
289 =head1 BUGS
290
291 =head1 SEE ALSO
292
293 L<FS::Record>, schema.html from the base documentation.
294
295 =cut
296
297 1;
298