Northern 911 export, #29195
[freeside.git] / FS / FS / part_export / northern_911.pm
1 package FS::part_export::northern_911;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Tie::IxHash;
6 use FS::Record qw(qsearch dbh);
7 use base 'FS::part_export';
8 use Data::Dumper;
9
10 tie my %options, 'Tie::IxHash',
11   'vendor_code'   => { label=>'Northern 911 vendor code' },
12   'password'      => { label=>'API passcode' },
13   'test_mode'     => { label=>'Test mode',
14                        type =>'checkbox' },
15   'debug'         => { label=>'Enable debugging',
16                        type =>'checkbox' },
17 ;
18
19 %info = (
20   'svc'     => 'svc_phone',
21   'desc'    => 'Provision E911 to Northern 911',
22   'options' => \%options,
23   'no_machine' => 1,
24   'notes'   => <<'END'
25 Requires installation of
26 <a href="http://search.cpan.org/dist/WebService-Northern911">WebService::Northern911</a>
27 from CPAN.
28 END
29 );
30
31 sub client {
32   my $self = shift;
33
34   if (!$self->get('client')) {
35     local $@;
36     eval "use WebService::Northern911";
37     return "error loading WebService::Northern911 ($@)" if $@;
38     $self->set('client',
39       WebService::Northern911->new(
40         vendor_code => $self->option('vendor_code'),
41         password    => $self->option('password'),
42         live        => ( $self->option('test_mode') ? 0 : 1),
43       )
44     );
45   }
46
47   return $self->get('client');
48 }
49
50 sub export_insert {
51   my( $self, $svc_phone ) = (shift, shift);
52
53   my %location_hash = $svc_phone->location_hash;
54   $location_hash{address1} =~ /^(\w+) +(.*)$/;
55   my %customer = (
56     'PHONE_NUMBER'        => $svc_phone->phonenum,
57     'STREET_NUMBER'       => $1,
58     'STREET_NAME'         => $2,
59     'CITY'                => $location_hash{city},
60     'PROVINCE_STATE'      => $location_hash{state},
61     'POSTAL_CODE_ZIP'     => $location_hash{zip},
62     'OTHER_ADDRESS_INFO'  => $location_hash{address2},
63   );
64
65   if ($self->option('debug')) {
66     warn "\nAddorUpdateCustomer:\n".Dumper(\%customer)."\n\n";
67   }
68   my $response = $self->client->AddorUpdateCustomer(%customer);
69   if (!$response->is_success) {
70     return $response->error_message;
71   }
72   '';
73 }
74
75 sub export_replace {
76   my( $self, $new, $old ) = (shift, shift, shift);
77
78   # except when changing the phone number, exactly like export_insert;
79   if ($new->phonenum ne $old->phonenum) {
80     my $error = $self->export_delete($old);
81     return $error if $error;
82   }
83   $self->export_insert($new);
84 }
85
86 sub export_delete {
87   my ($self, $svc_phone) = (shift, shift);
88
89   if ($self->option('debug')) {
90     warn "\nDeleteCustomer:\n".$svc_phone->phonenum."\n\n";
91   }
92   my $response = $self->client->DeleteCustomer($svc_phone->phonenum);
93   if (!$response->is_success) {
94     return $response->error_message;
95   }
96   '';
97 }
98
99 # export_suspend and _unsuspend do nothing
100
101 sub export_relocate {
102   my ($self, $svc_phone) = (shift, shift);
103   $self->export_insert($svc_phone);
104 }
105
106 1;
107