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