9e818e142f9d2d12455664aa5588ff3486c01783
[freeside.git] / FS / FS / svc_cable.pm
1 package FS::svc_cable;
2 use base qw( FS::MAC_Mixin
3              FS::svc_Common
4            ); #FS::device_Common
5
6 use strict;
7 use Tie::IxHash;
8
9 =head1 NAME
10
11 FS::svc_cable - Object methods for svc_cable records
12
13 =head1 SYNOPSIS
14
15   use FS::svc_cable;
16
17   $record = new FS::svc_cable \%hash;
18   $record = new FS::svc_cable { '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::svc_cable object represents a cable subscriber.  FS::svc_cable inherits
31 from FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item svcnum
36
37 primary key
38
39 =back
40
41 =head1 METHODS
42
43 =over 4
44
45 =item new HASHREF
46
47 Creates a new record.  To add the record to the database, see L<"insert">.
48
49 Note that this stores the hash reference, not a distinct copy of the hash it
50 points to.  You can ask the object for a copy with the I<hash> method.
51
52 =cut
53
54 sub table { 'svc_cable'; }
55
56 sub table_dupcheck_fields { ( 'serialnum', 'mac_addr' ); }
57
58 sub search_sql {
59   my( $class, $string ) = @_;
60   if ( $string =~ /^([A-F0-9]{12})$/i ) {
61     $class->search_sql_field('mac_addr', uc($string));
62   } elsif ( $string =~ /^(([A-F0-9]{2}:){5}([A-F0-9]{2}))$/i ) {
63     $string =~ s/://g;
64     $class->search_sql_field('mac_addr', uc($string) );
65   } elsif ( $string =~ /^(\w+)$/ ) {
66     $class->search_sql_field('serialnum', $1);
67   } else {
68     '1 = 0'; #false
69   }
70 }
71
72 sub table_info {
73
74   tie my %fields, 'Tie::IxHash',
75     'svcnum'      => 'Service',
76     'providernum' => { label             => 'Provider',
77                        type              => 'select-cable_provider',
78                        disable_inventory => 1,
79                        disable_select    => 1,
80                        value_callback    => sub {
81                                               my $svc = shift;
82                                               my $p = $svc->cable_provider;
83                                               $p ? $p->provider : '';
84                                             },
85                      },
86     'ordernum'    => 'Order number', #XXX "Circuit ID/Order number"
87     'modelnum'    => { label             => 'Model',
88                        type              => 'select-cable_model',
89                        disable_inventory => 1,
90                        disable_select    => 1,
91                        value_callback    => sub {
92                                               my $svc = shift;
93                                               $svc->cable_model->model_name;
94                                             },
95                      },
96     'serialnum'   => 'Serial number',
97     'mac_addr'    => { label          => 'MAC address',
98                        type           => 'input-mac_addr',
99                        value_callback => sub {
100                                            my $svc = shift;
101                                            $svc->mac_addr_formatted('U',':');
102                                          },
103                      },
104   ;
105
106   {
107     'name'            => 'Cable Subscriber',
108     #'name_plural'     => '', #optional,
109     #'longname_plural' => '', #optional
110     'fields'          => \%fields,
111     'sorts'           => [ 'svcnum', 'serialnum', 'mac_addr', ],
112     'display_weight'  => 54,
113     'cancel_weight'   => 70, #?  no deps, so
114   };
115 }
116
117 =item label
118
119 Returns the MAC address and serial number.
120
121 =cut
122
123 sub label {
124   my $self = shift;
125   my $part_svc = $self->cust_svc->part_svc;
126   my @label = ();
127   push @label, 'MAC:'. $self->mac_addr_pretty
128     if $self->mac_addr;
129   if ( $self->serialnum ) {
130     my $serialnum_label = $part_svc->part_svc_column('serialnum');
131     push @label,
132       ($serialnum_label && $serialnum_label->columnlabel || 'Serial#').
133       $self->serialnum;
134   }
135   return join(', ', @label);
136 }
137
138 =item insert
139
140 Adds this record to the database.  If there is an error, returns the error,
141 otherwise returns false.
142
143 =item delete
144
145 Delete this record from the database.
146
147 =item replace OLD_RECORD
148
149 Replaces the OLD_RECORD with this one in the database.  If there is an error,
150 returns the error, otherwise returns false.
151
152 =item check
153
154 Checks all fields to make sure this is a valid record.  If there is
155 an error, returns the error, otherwise returns false.  Called by the insert
156 and replace methods.
157
158 =cut
159
160 sub check {
161   my $self = shift;
162
163   my $error = 
164        $self->ut_numbern('svcnum')
165     || $self->ut_foreign_keyn('providernum', 'cable_provider', 'providernum')
166     || $self->ut_alphan('ordernum')
167     || $self->ut_foreign_key('modelnum', 'cable_model', 'modelnum')
168     || $self->ut_alphan('serialnum')
169     || $self->ut_mac_addrn('mac_addr')
170   ;
171   return $error if $error;
172
173   $self->SUPER::check;
174 }
175
176 sub _check_duplicate {
177   my $self = shift;
178
179   # Not reliable checks because the table isn't locked, but that's why we have
180   # unique indices.  These are just to give friendlier error messages.
181
182   if ( $self->mac_addr ) {
183     my @dup_mac;
184     @dup_mac = $self->find_duplicates('global', 'mac_addr');
185     if ( @dup_mac ) {
186       return "MAC address in use (svcnum ".$dup_mac[0]->svcnum.")";
187     }
188   }
189
190   if ( $self->serialnum ) {
191     my @dup_serial;
192     @dup_serial = $self->find_duplicates('global', 'serialnum');
193     if ( @dup_serial ) {
194       return "Serial number in use (svcnum ".$dup_serial[0]->svcnum.")";
195     }
196   }
197
198   '';
199 }
200
201 =item cable_provider
202
203 Returns the cable_provider object for this record.
204
205 =item cable_model
206
207 Returns the cable_model object for this record.
208
209 =back
210
211 =head1 BUGS
212
213 =head1 SEE ALSO
214
215 L<FS::Record>, schema.html from the base documentation.
216
217 =cut
218
219 1;
220