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