on-the-fly alarm vendor / type / CS adding, RT#23694
[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     'alarmsystemnum'  => { label => 'Alarm System Vendor',
79                            type  => 'select-alarm_system',
80                            disable_inventory => 1,
81                            value_callback    => sub {
82                              shift->alarm_system->systemname
83                            },
84                          },
85     'alarmtypenum'    => { label => 'Alarm System Type',
86                            type  => 'select-alarm_type',
87                            disable_inventory => 1,
88                            value_callback    => sub {
89                              shift->alarm_type->typename
90                            },
91                          },
92     'alarmstationnum' => { label => 'Alarm Central Station',
93                            type  => 'select-alarm_station',
94                            disable_inventory => 1,
95                            value_callback    => sub {
96                              shift->alarm_station->stationname
97                            },
98                          },
99   ;
100
101   {
102     'name'                => 'Alarm service',
103     'sorts'               => 'acctnum',
104     'display_weight'      => 80,
105     'cancel_weight'       => 85,
106     'fields'              => \%fields,
107     'addl_process_fields' => [qw( alarmsystemnum_systemname
108                                   alarmtypenum_inputs alarmtypenum_outputs
109                                   alarmstationnum_stationname
110                              )],
111   };
112 }
113
114 sub label {
115   my $self = shift;
116   $self->acctnum . '@'. $self->alarm_station->stationname. #?
117     ' ('. $self->alarm_system->systemname. ' '. $self->alarm_type->typename. ')'
118   ;
119 }
120
121 sub search_sql {
122   my($class, $string) = @_;
123   $class->search_sql_field('acctnum', $string);
124 }
125
126 =item insert
127
128 Adds this record to the database.  If there is an error, returns the error,
129 otherwise returns false.
130
131 =item delete
132
133 Delete this record from the database.
134
135 =item replace OLD_RECORD
136
137 Replaces the OLD_RECORD with this one in the database.  If there is an error,
138 returns the error, otherwise returns false.
139
140 =cut
141
142 sub preinsert_hook_first  { shift->_inline_add(@_); }
143 sub prereplace_hook_first { shift->_inline_add(@_); }
144
145 sub _inline_add {
146   my $self = shift;
147
148   my $agentnum = $self->cust_svc->cust_pkg->cust_main->agentnum;
149
150   if ( $self->alarmsystemnum == -1 ) {
151     my $alarm_system = new FS::alarm_system {
152       'agentnum'   => $agentnum,
153       'systemname' => $self->alarmsystemnum_systemname,
154     };
155     my $error = $alarm_system->insert;
156     return $error if $error;
157     $self->alarmsystemnum($alarm_system->alarmsystemnum);
158   }
159
160   if ( $self->alarmtypenum == -1 ) {
161     my $alarm_type = new FS::alarm_type {
162       'agentnum' => $agentnum,
163       'inputs'   => $self->alarmtypenum_inputs,
164       'outputs'  => $self->alarmtypenum_outputs,
165     };
166     my $error = $alarm_type->insert;
167     return $error if $error;
168     $self->alarmtypenum($alarm_type->alarmtypenum);
169   }
170
171   if ( $self->alarmstationnum == -1 ) {
172     my $alarm_station = new FS::alarm_station {
173       'agentnum'    => $agentnum,
174       'stationname' => $self->alarmstationnum_stationname,
175     };
176     my $error = $alarm_station->insert;
177     return $error if $error;
178     $self->alarmstationnum($alarm_station->alarmstationnum)
179   }
180
181   '';
182 }
183
184 =item check
185
186 Checks all fields to make sure this is a valid service.  If there is
187 an error, returns the error, otherwise returns false.  Called by the insert
188 and replace methods.
189
190 =cut
191
192 sub check {
193   my $self = shift;
194
195   my $x = $self->setfixed;
196   return $x unless ref $x;
197
198   my $error = 
199     $self->ut_numbern('svcnum')
200     || $self->ut_text('acctnum')
201     || $self->ut_alphan('_password')
202     || $self->ut_textn('location')
203     || $self->ut_foreign_key('alarmsystemnum',  'alarm_system',  'systemnum')
204     || $self->ut_foreign_key('alarmtypenum',    'alarm_type',    'typenum')
205     || $self->ut_foreign_key('alarmstationnum', 'alarm_station', 'stationnum')
206   ;
207   return $error if $error;
208
209   $self->SUPER::check;
210 }
211
212 sub alarm_system  {
213   qsearchs('alarm_system',  { alarmsystemnum  => shift->alarmsystemnum  } );
214 }
215 sub alarm_type    {
216   qsearchs('alarm_type',    { alarmtypenum    => shift->alarmtypenum    } );
217 }
218 sub alarm_station {
219   qsearchs('alarm_station', { alarmstationnum => shift->alarmstationnum } );
220 }
221
222 =back
223
224 =head1 SEE ALSO
225
226 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
227
228 =cut
229
230 1;
231