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