grandstream device configuration support #4220
[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   if ( $error ) {
96     $dbh->rollback if $oldAutoCommit;
97     return $error;
98   }
99
100   $self->export('device_insert');
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   $self->export('device_delete');
128
129   my $error = $self->SUPER::delete;
130   if ( $error ) {
131     $dbh->rollback if $oldAutoCommit;
132     return $error;
133   }
134
135   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
136   '';
137 }
138
139 =item replace OLD_RECORD
140
141 Replaces the OLD_RECORD with this one in the database.  If there is an error,
142 returns the error, otherwise returns false.
143
144 =cut
145
146 sub replace {
147   my $new = shift;
148
149   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
150               ? shift
151               : $new->replace_old;
152
153   local $SIG{HUP} = 'IGNORE';
154   local $SIG{INT} = 'IGNORE';
155   local $SIG{QUIT} = 'IGNORE';
156   local $SIG{TERM} = 'IGNORE';
157   local $SIG{TSTP} = 'IGNORE';
158   local $SIG{PIPE} = 'IGNORE';
159
160   my $oldAutoCommit = $FS::UID::AutoCommit;
161   local $FS::UID::AutoCommit = 0;
162   my $dbh = dbh;
163
164   my $error = $new->SUPER::replace($old);
165   if ( $error ) {
166     $dbh->rollback if $oldAutoCommit;
167     return $error;
168   }
169
170   $new->export('device_replace', $old);
171
172   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
173   '';
174 }
175
176 =item check
177
178 Checks all fields to make sure this is a valid record.  If there is
179 an error, returns the error, otherwise returns false.  Called by the insert
180 and replace methods.
181
182 =cut
183
184 # the check method should currently be supplied - FS::Record contains some
185 # data checking routines
186
187 sub check {
188   my $self = shift;
189
190   my $mac = $self->mac_addr;
191   $mac =~ s/\s+//g;
192   $mac =~ s/://g;
193   $self->mac_addr($mac);
194
195   my $error = 
196     $self->ut_numbern('devicenum')
197     || $self->ut_foreign_key('devicepart', 'part_device', 'devicepart')
198     || $self->ut_foreign_key('svcnum', 'svc_phone', 'svcnum' ) #cust_svc?
199     || $self->ut_hexn('mac_addr')
200   ;
201   return $error if $error;
202
203   $self->SUPER::check;
204 }
205
206 =item part_device
207
208 Returns the device type record (see L<FS::part_device>) associated with this
209 customer device.
210
211 =cut
212
213 sub part_device {
214   my $self = shift;
215   qsearchs( 'part_device', { 'devicepart' => $self->devicepart } );
216 }
217
218 =item svc_phone
219
220 Returns the phone number (see L<FS::svc_phone>) associated with this customer
221 device.
222
223 =cut
224
225 sub svc_phone {
226   my $self = shift;
227   qsearchs( 'svc_phone', { 'svcnum' => $self->svcnum } );
228 }
229
230 =item export HOOK [ EXPORT_ARGS ]
231
232 Runs the provided export hook (i.e. "device_insert") for this service.
233
234 =cut
235
236 sub export {
237   my( $self, $method ) = ( shift, shift );
238
239   local $SIG{HUP} = 'IGNORE';
240   local $SIG{INT} = 'IGNORE';
241   local $SIG{QUIT} = 'IGNORE';
242   local $SIG{TERM} = 'IGNORE';
243   local $SIG{TSTP} = 'IGNORE';
244   local $SIG{PIPE} = 'IGNORE';
245
246   my $oldAutoCommit = $FS::UID::AutoCommit;
247   local $FS::UID::AutoCommit = 0;
248   my $dbh = dbh;
249
250   my $svc_phone = $self->svc_phone;
251   my $error = $svc_phone->export($method, $self, @_); #call device export
252   if ( $error ) {                                     #netsapiens at least
253     $dbh->rollback if $oldAutoCommit;
254     return "error exporting $method event to svc_phone ". $svc_phone->svcnum.
255            " (transaction rolled back): $error";
256   }
257
258   $method = "export_$method" unless $method =~ /^export_/;
259
260   foreach my $part_export ( $self->part_device->part_export ) {
261     next unless $part_export->can($method);
262     my $error = $part_export->$method($svc_phone, $self, @_);
263     if ( $error ) {
264       $dbh->rollback if $oldAutoCommit;
265       return "error exporting $method event to ". $part_export->exporttype.
266              " (transaction rolled back): $error";
267     }
268   }
269
270   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
271   '';
272
273 }
274
275 =item export_links
276
277 Returns a list of html elements associated with this device's exports.
278
279 =cut
280
281 sub export_links {
282   my $self = shift;
283   my $return = [];
284   $self->export('export_device_links', $return);
285   $return;
286 }
287
288 =back
289
290 =head1 BUGS
291
292 =head1 SEE ALSO
293
294 L<FS::Record>, schema.html from the base documentation.
295
296 =cut
297
298 1;
299