voip innovations v3 api, RT#25641
[freeside.git] / FS / FS / part_export / voip_innovations3.pm
1 package FS::part_export::voip_innovations3;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::Record qw(qsearch dbh);
6 use FS::part_export;
7 use FS::phone_avail;
8 use Data::Dumper;
9
10 @ISA = qw(FS::part_export);
11
12 tie my %options, 'Tie::IxHash',
13   'login'         => { label=>'VoIP Innovations API login' },
14   'password'      => { label=>'VoIP Innovations API password' },
15   'endpointgroup' => { label=>'VoIP Innovations endpoint group number' },
16   'e911'          => { label=>'Provision E911 data',
17                        type=>'checkbox',
18                      },
19   'no_did_provision' => { label=>'Disable DID provisioning',
20                        type=>'checkbox',
21                      },
22   'dry_run'       => { label=>"Test mode - don't actually provision",
23                        type=>'checkbox',
24                      },
25 ;
26
27 %info = (
28   'svc'     => 'svc_phone',
29   'desc'    => 'Provision phone numbers / E911 to VoIP Innovations (API 3.0)',
30   'options' => \%options,
31   'no_machine' => 1,
32   'notes'   => <<'END'
33 Requires installation of
34 <a href="http://search.cpan.org/dist/Net-VoIP_Innovations">Net::VoIP_Innovations</a>
35 from CPAN.
36 END
37 );
38
39 sub rebless { shift; }
40
41 sub can_get_dids { 0; } #with API 3.0?  not yet
42
43 sub vi_command {
44   my( $self, $command, @args ) = @_;
45
46   eval "use Net::VoIP_Innovations 3.00;";
47   if ( $@ ) {
48     warn $@;
49     die $@;
50   }
51
52   my $gp = Net::VoIP_Innovations->new(
53     'login'    => $self->option('login'),
54     'password' => $self->option('password'),
55     #'debug'    => $debug,
56   );
57
58   $gp->$command(@args);
59 }
60
61
62 sub _export_insert {
63   my( $self, $svc_phone ) = (shift, shift);
64
65   return '' if $self->option('dry_run');
66
67   #we want to provision and catch errors now, not queue
68
69   unless ( $self->option('no_provision_did') ) {
70
71     ###
72     # reserveDID
73     ###
74
75     my $r = $self->vi_command('reserveDID',
76       'did'           => $svc_phone->phonenum,
77       'minutes'       => 1,
78       'endpointgroup' => $self->option('endpointgroup'),
79     );
80
81     my $rdid = $r->{did};
82
83     if ( $rdid->{'statuscode'} != 100 ) {
84       return "Error running VoIP Innovations reserveDID: ".
85              $rdid->{'statuscode'}. ': '. $rdid->{'status'};
86     }
87
88     ###
89     # assignDID
90     ###
91
92     my $a = $self->vi_command('assignDID',
93       'did'           => $svc_phone->phonenum,
94       'endpointgroup' => $self->option('endpointgroup'),
95       #'rewrite'
96       #'cnam'
97     );
98
99     my $adid = $a->{did};
100
101     if ( $adid->{'statuscode'} != 100 ) {
102       return "Error running VoIP Innovations assignDID: ".
103              $adid->{'statuscode'}. ': '. $adid->{'status'};
104     }
105
106   }
107
108   ###
109   # 911Insert
110   ###
111
112   if ( $self->option('e911') ) {
113
114     my %location_hash = $svc_phone->location_hash;
115     my( $zip, $plus4 ) = split('-', $location_hash->{zip});
116     my $e = $self->vi_command('911Insert',
117       'did'        => $svc_phone->phonenum,
118       'Address1'   => $location_hash{address1},
119       'Address2'   => $location_hash{address2},
120       'City'       => $location_hash{city},
121       'State'      => $location_hash{state},
122       'ZipCode'    => $zip,
123       'PlusFour'   => $plus4,
124       'CallerName' =>
125         $svc_phone->phone_name
126           || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
127     );
128
129     my $edid = $e->{did};
130
131     if ( $edid->{'statuscode'} != 100 ) {
132       return "Error running VoIP Innovations 911Insert: ".
133              $edid->{'statuscode'}. ': '. $edid->{'status'};
134     }
135
136   }
137
138   '';
139 }
140
141 sub _export_replace {
142   my( $self, $new, $old ) = (shift, shift, shift);
143
144   #hmm, anything to change besides E911 data?
145
146   ###
147   # 911Update
148   ###
149
150   if ( $self->option('e911') ) {
151
152     my %location_hash = $svc_phone->location_hash;
153     my( $zip, $plus4 ) = split('-', $location_hash->{zip});
154     my $e = $self->vi_command('911Update',
155       'did'        => $svc_phone->phonenum,
156       'Address1'   => $location_hash{address1},
157       'Address2'   => $location_hash{address2},
158       'City'       => $location_hash{city},
159       'State'      => $location_hash{state},
160       'ZipCode'    => $zip,
161       'PlusFour'   => $plus4,
162       'CallerName' =>
163         $svc_phone->phone_name
164           || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
165     );
166
167     my $edid = $e->{did};
168
169     if ( $edid->{'statuscode'} != 100 ) {
170       return "Error running VoIP Innovations 911Update: ".
171              $edid->{'statuscode'}. ': '. $edid->{'status'};
172     }
173
174   }
175
176   '';
177 }
178
179 sub _export_delete {
180   my( $self, $svc_phone ) = (shift, shift);
181
182   return '' if $self->option('dry_run')
183             || $self->option('no_provision_did');
184
185   #probably okay to queue the deletion...?
186   #but hell, let's do it inline anyway, who wants phone numbers hanging around
187
188   my $r = $self->vi_command('releaseDID',
189     'did'           => $svc_phone->phonenum,
190   );
191
192   my $rdid = $r->{did};
193
194   if ( $rdid->{'statuscode'} != 100 ) {
195     return "Error running VoIP Innovations releaseDID: ".
196            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
197   }
198
199   #delete e911 information?  assuming release clears all that
200
201   '';
202 }
203
204 sub _export_suspend {
205   my( $self, $svc_phone ) = (shift, shift);
206   #nop for now
207   '';
208 }
209
210 sub _export_unsuspend {
211   my( $self, $svc_phone ) = (shift, shift);
212   #nop for now
213   '';
214 }
215
216 1;
217