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