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