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_model;
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 { ( '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     'modelnum'   => { label             => 'Model',
77                       type              => 'select-cable_model',
78                       disable_inventory => 1,
79                       disable_select    => 1,
80                       value_callback    => sub {
81                                              my $svc = shift;
82                                              $svc->cable_model->model_name;
83                                            },
84                     },
85     'serialnum'  => 'Serial number',
86     'mac_addr'   => { label          => 'MAC address',
87                       type           => 'input-mac_addr',
88                       value_callback => sub {
89                                           my $svc = shift;
90                                           join(':', $svc->mac_addr =~ /../g);
91                                         },
92                     },
93   ;
94
95   {
96     'name'            => 'Cable Subscriber',
97     #'name_plural'     => '', #optional,
98     #'longname_plural' => '', #optional
99     'fields'          => \%fields,
100     'sorts'           => [ 'svcnum', 'serialnum', 'mac_addr', ],
101     'display_weight'  => 54,
102     'cancel_weight'   => 70, #?  no deps, so
103   };
104 }
105
106 =item insert
107
108 Adds this record to the database.  If there is an error, returns the error,
109 otherwise returns false.
110
111 =item delete
112
113 Delete this record from the database.
114
115 =item replace OLD_RECORD
116
117 Replaces the OLD_RECORD with this one in the database.  If there is an error,
118 returns the error, otherwise returns false.
119
120 =item check
121
122 Checks all fields to make sure this is a valid record.  If there is
123 an error, returns the error, otherwise returns false.  Called by the insert
124 and replace methods.
125
126 =cut
127
128 sub check {
129   my $self = shift;
130
131   my $error = 
132        $self->ut_numbern('svcnum')
133     || $self->ut_foreign_key('modelnum', 'cable_model', 'modelnum')
134     || $self->ut_alpha('serialnum')
135     || $self->ut_mac_addr('mac_addr')
136   ;
137   return $error if $error;
138
139   $self->SUPER::check;
140 }
141
142 =item cable_model
143
144 Returns the cable_model object for this record.
145
146 =cut
147
148 sub cable_model {
149   my $self = shift;
150   qsearchs('cable_model', { 'modelnum'=>$self->modelnum } );
151 }
152
153 =back
154
155 =head1 BUGS
156
157 =head1 SEE ALSO
158
159 L<FS::Record>, schema.html from the base documentation.
160
161 =cut
162
163 1;
164