RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / northern_911.pm
1 package FS::part_export::northern_911;
2 use base 'FS::part_export';
3
4 use strict;
5 use vars qw(@ISA %info);
6 use Tie::IxHash;
7 use FS::Record qw(qsearch dbh);
8 use WebService::Northern911;
9 use Data::Dumper;
10
11 tie my %options, 'Tie::IxHash',
12   'vendor_code'   => { label=>'Northern 911 vendor code' },
13   'password'      => { label=>'API passcode' },
14   'test_mode'     => { label=>'Test mode',
15                        type =>'checkbox' },
16   'debug'         => { label=>'Enable debugging',
17                        type =>'checkbox' },
18 ;
19
20 %info = (
21   'svc'     => 'svc_phone',
22   'desc'    => 'Provision E911 to Northern 911',
23   'options' => \%options,
24   'no_machine' => 1,
25   'notes'   => <<'END'
26 END
27 );
28
29 sub client {
30   my $self = shift;
31
32   if (!$self->get('client')) {
33     $self->set('client',
34       WebService::Northern911->new(
35         vendor_code => $self->option('vendor_code'),
36         password    => $self->option('password'),
37         live        => ( $self->option('test_mode') ? 0 : 1),
38       )
39     );
40   }
41
42   return $self->get('client');
43 }
44
45 sub _export_insert {
46   my( $self, $svc_phone ) = (shift, shift);
47
48   my %location_hash = $svc_phone->location_hash;
49   $location_hash{address1} =~ /^(\w+) +(.*)$/;
50
51   my %customer = (
52     'PHONE_NUMBER'        => $svc_phone->phonenum,
53     'STREET_NUMBER'       => $1,
54     'STREET_NAME'         => $2,
55     'CITY'                => $location_hash{city},
56     'PROVINCE_STATE'      => $location_hash{state},
57     'POSTAL_CODE_ZIP'     => $location_hash{zip},
58     'OTHER_ADDRESS_INFO'  => $location_hash{address2},
59   );
60   my $phone_name = $svc_phone->phone_name;
61   if ( $phone_name ) {
62     # could be a personal name or a business...
63     if ( $svc_phone->e911_class and
64         grep { $_ eq $svc_phone->e911_class }
65          ( 2, 4, 5, 6, 7, 0, 'A', 'D', 'E', 'K')
66        )
67     {
68       # one of the "Business" classes, Centrex, a payphone, or 
69       # VoIP Enterprise class
70       $customer{'LAST_NAME'} = $phone_name;
71     } else {
72       # assume residential, and try (inaccurately) to make a first/last
73       # name out of it.
74       @customer{'FIRST_NAME', 'LAST_NAME'} = split(' ', $phone_name, 2);
75     }
76   } else {
77     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
78     if ($cust_main->company) {
79       $customer{'LAST_NAME'} = $cust_main->company;
80     } else {
81       $customer{'LAST_NAME'} = $cust_main->last;
82       $customer{'FIRST_NAME'} = $cust_main->first;
83     }
84   }
85
86   if ($self->option('debug')) {
87     warn "\nAddorUpdateCustomer:\n".Dumper(\%customer)."\n\n";
88   }
89   my $response = $self->client->AddorUpdateCustomer(%customer);
90   if (!$response->is_success) {
91     return $response->error_message;
92   }
93   '';
94 }
95
96 sub _export_replace {
97   my( $self, $new, $old ) = (shift, shift, shift);
98
99   # except when changing the phone number, exactly like export_insert;
100   if ($new->phonenum ne $old->phonenum) {
101     my $error = $self->export_delete($old);
102     return $error if $error;
103   }
104   $self->export_insert($new);
105 }
106
107 sub _export_delete {
108   my ($self, $svc_phone) = (shift, shift);
109
110   if ($self->option('debug')) {
111     warn "\nDeleteCustomer:\n".$svc_phone->phonenum."\n\n";
112   }
113   my $response = $self->client->DeleteCustomer($svc_phone->phonenum);
114   if (!$response->is_success) {
115     return $response->error_message;
116   }
117   '';
118 }
119
120 # export_suspend and _unsuspend do nothing
121
122 sub export_relocate {
123   my ($self, $svc_phone) = (shift, shift);
124   $self->export_insert($svc_phone);
125 }
126
127 1;
128