autoload methods returning foreign records, RT#13971
[freeside.git] / FS / FS / svc_alarm.pm
1 package FS::svc_alarm;
2 use base qw( FS::svc_Common );
3
4 use strict;
5 use Tie::IxHash;
6 use FS::alarm_system;
7 use FS::alarm_type;
8 use FS::alarm_station;
9
10 =head1 NAME
11
12 FS::svc_alarm - Object methods for svc_alarm records
13
14 =head1 SYNOPSIS
15
16   use FS::svc_alarm;
17
18   $record = new FS::svc_alarm \%hash;
19   $record = new FS::svc_alarm { '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_alarm object represents an alarm service.  FS::svc_alarm inherits
32 from FS::svc_Common.
33
34 The following fields are currently supported:
35
36 =over 4
37
38 =item svcnum - Primary key
39
40 =item alarmsystemnum - Alarm System Vendor (see L<FS::alarm_system>)
41
42 =item alarmtypenum - Alarm System Type (inputs/outputs) (see L<FS::alarm_type>)
43
44 =item alarmstationnum - Alarm central station (see L<FS::alarm_station>)
45
46 =item acctnum - Account number
47
48 =item _password - Password
49
50 =item location - Location on property
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new svc_dish object.
61
62 =cut
63
64 sub table { 'svc_alarm'; }
65
66 sub table_info {
67   my %opts = ( 'type' => 'text', 
68                #'disable_select' => 1,
69                'disable_inventory' => 1,
70              );
71
72   tie my %fields, 'Tie::IxHash',
73     'svcnum'    => { label => 'Service' },
74     'acctnum'         => { label => 'Account #', %opts },
75     '_password'       => { label => 'Password' , %opts },
76     'location'        => { label => 'Location',  %opts },
77     'alarmsystemnum'  => { label => 'Alarm System Vendor',
78                            type  => 'select-alarm_system',
79                            disable_inventory => 1,
80                            value_callback    => sub {
81                              shift->alarm_system->systemname
82                            },
83                          },
84     'alarmtypenum'    => { label => 'Alarm System Type',
85                            type  => 'select-alarm_type',
86                            disable_inventory => 1,
87                            value_callback    => sub {
88                              shift->alarm_type->typename
89                            },
90                          },
91     'alarmstationnum' => { label => 'Alarm Central Station',
92                            type  => 'select-alarm_station',
93                            disable_inventory => 1,
94                            value_callback    => sub {
95                              shift->alarm_station->stationname
96                            },
97                          },
98   ;
99
100   {
101     'name'                => 'Alarm service',
102     'sorts'               => 'acctnum',
103     'display_weight'      => 80,
104     'cancel_weight'       => 85,
105     'fields'              => \%fields,
106     'addl_process_fields' => [qw( alarmsystemnum_systemname
107                                   alarmtypenum_inputs alarmtypenum_outputs
108                                   alarmstationnum_stationname
109                              )],
110   };
111 }
112
113 sub label {
114   my $self = shift;
115   $self->acctnum . '@'. $self->alarm_station->stationname. #?
116     ' ('. $self->alarm_system->systemname. ' '. $self->alarm_type->typename. ')'
117   ;
118 }
119
120 sub search_sql {
121   my($class, $string) = @_;
122   $class->search_sql_field('acctnum', $string);
123 }
124
125 =item insert
126
127 Adds this record to the database.  If there is an error, returns the error,
128 otherwise returns false.
129
130 =item delete
131
132 Delete this record from the database.
133
134 =item replace OLD_RECORD
135
136 Replaces the OLD_RECORD with this one in the database.  If there is an error,
137 returns the error, otherwise returns false.
138
139 =cut
140
141 sub preinsert_hook_first  { shift->_inline_add(@_); }
142 sub prereplace_hook_first { shift->_inline_add(@_); }
143
144 sub _inline_add {
145   my $self = shift;
146
147   my $agentnum = $self->cust_svc->cust_pkg->cust_main->agentnum;
148
149   if ( $self->alarmsystemnum == -1 ) {
150     my $alarm_system = new FS::alarm_system {
151       'agentnum'   => $agentnum,
152       'systemname' => $self->alarmsystemnum_systemname,
153     };
154     my $error = $alarm_system->insert;
155     return $error if $error;
156     $self->alarmsystemnum($alarm_system->alarmsystemnum);
157   }
158
159   if ( $self->alarmtypenum == -1 ) {
160     my $alarm_type = new FS::alarm_type {
161       'agentnum' => $agentnum,
162       'inputs'   => $self->alarmtypenum_inputs,
163       'outputs'  => $self->alarmtypenum_outputs,
164     };
165     my $error = $alarm_type->insert;
166     return $error if $error;
167     $self->alarmtypenum($alarm_type->alarmtypenum);
168   }
169
170   if ( $self->alarmstationnum == -1 ) {
171     my $alarm_station = new FS::alarm_station {
172       'agentnum'    => $agentnum,
173       'stationname' => $self->alarmstationnum_stationname,
174     };
175     my $error = $alarm_station->insert;
176     return $error if $error;
177     $self->alarmstationnum($alarm_station->alarmstationnum)
178   }
179
180   '';
181 }
182
183 =item check
184
185 Checks all fields to make sure this is a valid service.  If there is
186 an error, returns the error, otherwise returns false.  Called by the insert
187 and replace methods.
188
189 =cut
190
191 sub check {
192   my $self = shift;
193
194   my $x = $self->setfixed;
195   return $x unless ref $x;
196
197   my $error = 
198     $self->ut_numbern('svcnum')
199     || $self->ut_text('acctnum')
200     || $self->ut_alphan('_password')
201     || $self->ut_textn('location')
202     || $self->ut_foreign_key('alarmsystemnum',  'alarm_system',  'systemnum')
203     || $self->ut_foreign_key('alarmtypenum',    'alarm_type',    'typenum')
204     || $self->ut_foreign_key('alarmstationnum', 'alarm_station', 'stationnum')
205   ;
206   return $error if $error;
207
208   $self->SUPER::check;
209 }
210
211 =back
212
213 =head1 SEE ALSO
214
215 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
216
217 =cut
218
219 1;
220