summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/voip_innovations3.pm
blob: 3c76899223a594dee8f86ebb0eb349cb3a5c15f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package FS::part_export::voip_innovations3;

use vars qw(@ISA %info);
use Tie::IxHash;
use FS::Record qw(qsearch dbh);
use FS::part_export;
use FS::phone_avail;
use Data::Dumper;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'login'         => { label=>'VoIP Innovations API login' },
  'password'      => { label=>'VoIP Innovations API password' },
  'endpointgroup' => { label=>'VoIP Innovations endpoint group number' },
  'e911'          => { label=>'Provision E911 data',
                       type=>'checkbox',
                     },
  'no_provision_did' => { label=>'Disable DID provisioning',
                          type=>'checkbox',
                        },
#not particularly useful unless we can_get_dids
#  'dry_run'       => { label=>"Test mode - don't actually provision",
#                       type=>'checkbox',
#                     },
  'sandbox'       => { label=>'Communicatino with the VoIP Innovations sandbox'.
                              ' instead of the live server',
                       type => 'checkbox',
                     },
;

%info = (
  'svc'     => 'svc_phone',
  'desc'    => 'Provision phone numbers / E911 to VoIP Innovations (API 3.0)',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => <<'END'
Requires installation of
<a href="http://search.cpan.org/dist/Net-VoIP_Innovations">Net::VoIP_Innovations</a>
from CPAN.
END
);

sub rebless { shift; }

sub can_get_dids { 0; } #with API 3.0?  not yet

sub vi_command {
  my( $self, $command, @args ) = @_;

  eval "use Net::VoIP_Innovations 3.00;";
  if ( $@ ) {
    warn $@;
    die $@;
  }

  my $vi = Net::VoIP_Innovations->new(
    'login'    => $self->option('login'),
    'password' => $self->option('password'),
    'sandbox'  => $self->option('sandbox'),
  );

  $vi->$command(@args);
}


sub _export_insert {
  my( $self, $svc_phone ) = (shift, shift);

  return '' if $self->option('dry_run');

  #we want to provision and catch errors now, not queue

  unless ( $self->option('no_provision_did') ) {

    return "can't yet provision to VoIP Innovations v3 API"; #XXX

    ###
    # reserveDID
    ###

    my $r = $self->vi_command('reserveDID',
      'did'           => $svc_phone->phonenum,
      'minutes'       => 1,
      'endpointgroup' => $self->option('endpointgroup'),
    );

    my $rdid = $r->{did};

    if ( $rdid->{'statuscode'} != 100 ) {
      return "Error running VoIP Innovations reserveDID: ".
             $rdid->{'statuscode'}. ': '. $rdid->{'status'};
    }

    ###
    # assignDID
    ###

    my $a = $self->vi_command('assignDID',
      'did'           => $svc_phone->phonenum,
      'endpointgroup' => $self->option('endpointgroup'),
      #'rewrite'
      #'cnam'
    );

    my $adid = $a->{did};

    if ( $adid->{'statuscode'} != 100 ) {
      return "Error running VoIP Innovations assignDID: ".
             $adid->{'statuscode'}. ': '. $adid->{'status'};
    }

  }

  ###
  # insert911
  ###

  if ( $self->option('e911') ) {

    my %location_hash = $svc_phone->location_hash;
    my( $zip, $plus4 ) = split('-', $location_hash->{zip});
    my $resp = $self->vi_command('insert911',
      'did'        => $svc_phone->phonenum,
      'address1'   => $location_hash{address1},
      'address2'   => $location_hash{address2},
      'city'       => $location_hash{city},
      'state'      => $location_hash{state},
      'zip'        => $zip,
      'plusFour'   => $plus4,
      'callerName' =>
        $svc_phone->phone_name
          || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
    );

    if ( $resp->{'responseCode'} != 100 ) {
      return "Error running VoIP Innovations insert911: ".
             $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
    }

  }

  '';
}

sub _export_replace {
  my( $self, $new, $old ) = (shift, shift, shift);

  #hmm, anything to change besides E911 data?

  ###
  # 911Update
  ###

  if ( $self->option('e911') ) {

    my %location_hash = $new->location_hash;
    my( $zip, $plus4 ) = split('-', $location_hash->{zip});
    my $resp = $self->vi_command('update911',
      'did'        => $svc_phone->phonenum,
      'address1'   => $location_hash{address1},
      'address2'   => $location_hash{address2},
      'city'       => $location_hash{city},
      'state'      => $location_hash{state},
      'zip'        => $zip,
      'plusFour'   => $plus4,
      'callerName' =>
        $svc_phone->phone_name
          || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
    );

    if ( $resp->{'responseCode'} != 100 ) {
      return "Error running VoIP Innovations update911: ".
             $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
    }

  }

  '';
}

sub _export_delete {
  my( $self, $svc_phone ) = (shift, shift);

  return '' if $self->option('dry_run');

  ###
  # releaseDID
  ###

  unless ( $self->option('no_provision_did') ) {

    return "can't yet provision to VoIP Innovations v3 API"; #XXX

    #probably okay to queue the deletion...?
    #but hell, let's do it inline anyway, who wants phone numbers hanging around

    my $r = $self->vi_command('releaseDID',
      'did'           => $svc_phone->phonenum,
    );

    my $rdid = $r->{did};

    if ( $rdid->{'statuscode'} != 100 ) {
      return "Error running VoIP Innovations releaseDID: ".
             $rdid->{'statuscode'}. ': '. $rdid->{'status'};
    }

  }

  ###
  # remove911
  ###

  if ( $self->option('e911') ) {

    my $resp = $self->vi_command('remove911',
      'did'        => $svc_phone->phonenum,
    );

    if ( $resp->{'responseCode'} != 100 ) {
      return "Error running VoIP Innovations remove911: ".
             $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
    }

  }

  '';
}

sub _export_suspend {
  my( $self, $svc_phone ) = (shift, shift);
  #nop for now
  '';
}

sub _export_unsuspend {
  my( $self, $svc_phone ) = (shift, shift);
  #nop for now
  '';
}

1;