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