RT# 78547 - consolidate noexport_hack
[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
56   my %customer = (
57     'PHONE_NUMBER'        => $svc_phone->phonenum,
58     'STREET_NUMBER'       => $1,
59     'STREET_NAME'         => $2,
60     'CITY'                => $location_hash{city},
61     'PROVINCE_STATE'      => $location_hash{state},
62     'POSTAL_CODE_ZIP'     => $location_hash{zip},
63     'OTHER_ADDRESS_INFO'  => $location_hash{address2},
64   );
65   my $phone_name = $svc_phone->phone_name;
66   if ( $phone_name ) {
67     # could be a personal name or a business...
68     if ( $svc_phone->e911_class and
69         grep { $_ eq $svc_phone->e911_class }
70          ( 2, 4, 5, 6, 7, 0, 'A', 'D', 'E', 'K')
71        )
72     {
73       # one of the "Business" classes, Centrex, a payphone, or 
74       # VoIP Enterprise class
75       $customer{'LAST_NAME'} = $phone_name;
76     } else {
77       # assume residential, and try (inaccurately) to make a first/last
78       # name out of it.
79       @customer{'FIRST_NAME', 'LAST_NAME'} = split(' ', $phone_name, 2);
80     }
81   } else {
82     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
83     if ($cust_main->company) {
84       $customer{'LAST_NAME'} = $cust_main->company;
85     } else {
86       $customer{'LAST_NAME'} = $cust_main->last;
87       $customer{'FIRST_NAME'} = $cust_main->first;
88     }
89   }
90
91   if ($self->option('debug')) {
92     warn "\nAddorUpdateCustomer:\n".Dumper(\%customer)."\n\n";
93   }
94   my $response = $self->client->AddorUpdateCustomer(%customer);
95   if (!$response->is_success) {
96     return $response->error_message;
97   }
98   '';
99 }
100
101 sub _export_replace {
102   my( $self, $new, $old ) = (shift, shift, shift);
103
104   # except when changing the phone number, exactly like export_insert;
105   if ($new->phonenum ne $old->phonenum) {
106     my $error = $self->export_delete($old);
107     return $error if $error;
108   }
109   $self->export_insert($new);
110 }
111
112 sub _export_delete {
113   my ($self, $svc_phone) = (shift, shift);
114
115   if ($self->option('debug')) {
116     warn "\nDeleteCustomer:\n".$svc_phone->phonenum."\n\n";
117   }
118   my $response = $self->client->DeleteCustomer($svc_phone->phonenum);
119   if (!$response->is_success) {
120     return $response->error_message;
121   }
122   '';
123 }
124
125 # export_suspend and _unsuspend do nothing
126
127 sub export_relocate {
128   my ($self, $svc_phone) = (shift, shift);
129   $self->export_insert($svc_phone);
130 }
131
132 1;
133