export host selection per service, RT#17914
[freeside.git] / FS / FS / part_export / dashcs_e911.pm
1 package FS::part_export::dashcs_e911;
2
3 use strict;
4 use vars qw(@ISA %info $me $DEBUG);
5 use Tie::IxHash;
6 use FS::part_export;
7
8 $DEBUG = 0;
9 $me = '['.__PACKAGE__.']';
10
11 @ISA = qw(FS::part_export);
12
13 tie my %options, 'Tie::IxHash',
14   'username'  => { label=>'Dash username', },
15   '_password' => { label=>'Dash password', },
16   'staging' => { label=>'Staging (test mode)', type=>'checkbox', },
17 ;
18
19 %info = (
20   'svc'     => 'svc_phone',
21   'desc'    => 'Provision e911 services via Dash Carrier Services',
22   'notes'   => 'Provision e911 services via Dash Carrier Services',
23   'no_machine' => 1,
24   'options' => \%options,
25 );
26
27 sub rebless { shift; }
28
29 sub _export_insert {
30   my($self, $svc_phone) = (shift, shift);
31   return 'invalid phonenum' unless $svc_phone->phonenum;
32   
33   my $opts = { map{ $_ => $self->option($_) } keys %options };
34   $opts->{wantreturn} = 1;
35
36   my %location_hash = $svc_phone->location_hash;
37   my $location = { 
38     'address1'   => $location_hash{address1},
39     'address2'   => $location_hash{address2},
40     'community'  => $location_hash{city},
41     'state'      => $location_hash{state},
42     'postalcode' => $location_hash{zip},
43   };
44   
45   my $error_or_ref =
46    dash_command($opts, 'validateLocation', { 'location' => $location } );
47   return $error_or_ref unless ref($error_or_ref);
48
49   my $status = $error_or_ref->get_Location->get_status; # hate
50   return $status->get_description unless $status->get_code eq 'GEOCODED';
51   
52   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
53   my $cust_main = $cust_pkg->cust_main if $cust_pkg;
54   my $caller_name = $cust_main ? $cust_main->name_short : 'unknown';
55
56   my $arg = {
57     'uri' => {
58                'uri' => 'tel:'. $svc_phone->countrycode. $svc_phone->phonenum,
59                'callername' => $caller_name,
60              },
61     'location' => $location,
62   };
63
64   $error_or_ref = dash_command($opts, 'addLocation', $arg );
65   return $error_or_ref unless ref($error_or_ref);
66
67   my $id = $error_or_ref->get_Location->get_locationid;
68   $self->_export_command('provisionLocation', { 'locationid' => $id });
69 }
70
71 sub _export_delete {
72   my($self, $svc_phone) = (shift, shift);
73   return '' unless $svc_phone->phonenum;
74   
75   my $arg = { 'uri' => 'tel:'. $svc_phone->countrycode. $svc_phone->phonenum };
76   $self->_export_queue('removeURI', $arg);
77 }
78
79 sub _export_suspend {
80   my($self) = shift;
81   '';
82 }
83
84 sub _export_unsuspend {
85   my($self) = shift;
86   '';
87 }
88
89 sub _export_command {
90   my $self = shift;
91
92   my $opts = { map{ $_ => $self->option($_) } keys %options };
93
94   dash_command($opts, @_);
95
96 }
97
98 sub _export_replace {
99   my($self, $new, $old ) = (shift, shift, shift);
100
101   # this could succeed in unprovision but fail to provision
102   my $arg = { 'uri' => 'tel:'. $old->countrycode. $old->phonenum };
103   $self->_export_command('removeURI', $arg) || $self->_export_insert($new);  
104 }
105
106 #a good idea to queue anything that could fail or take any time
107 sub _export_queue {
108   my $self = shift;
109
110   my $opts = { map{ $_ => $self->option($_) } keys %options };
111
112   my $queue = new FS::queue {
113     'job'    => "FS::part_export::dashcs_e911::dash_command",
114   };
115   $queue->insert( $opts, @_ );
116 }
117
118 sub dash_command {
119   my ( $opt, $method, $arg ) = (shift, shift, shift);
120
121   warn "$me: dash_command called with method $method\n" if $DEBUG;
122
123   my @module = qw(
124     Net::DashCS::Interfaces::EmergencyProvisioningService::EmergencyProvisioningPort
125     SOAP::Lite
126   );
127
128   foreach my $module ( @module ) {
129     eval "use $module;";
130     die $@ if $@;
131   }
132
133   local *SOAP::Transport::HTTP::Client::get_basic_credentials = sub {
134     return ($opt->{'username'}, $opt->{'_password'});
135   };
136
137   my $service = new Net::DashCS::Interfaces::EmergencyProvisioningService::EmergencyProvisioningPort(
138       { deserializer_args => { strict => 0 } }
139   );
140
141   $service->set_proxy('https://staging-service.dashcs.com/dash-api/soap/emergencyprovisioning/v1')
142     if $opt->{'staging'};
143
144   my $result = $service->$method($arg);
145
146   if (not $result) {
147     warn "returning fault: ". $result->get_faultstring if $DEBUG;
148     return ''.$result->get_faultstring;
149   }
150
151   warn "returning ok: $result\n" if $DEBUG;
152   return $result if $opt->{wantreturn};
153   '';
154 }