summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/northern_911.pm
blob: 789d839e2573b6c41d0c751dbcabe9c797b48c7b (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
package FS::part_export::northern_911;
use base 'FS::part_export';

use strict;
use vars qw(@ISA %info);
use Tie::IxHash;
use FS::Record qw(qsearch dbh);
use WebService::Northern911;
use Data::Dumper;

tie my %options, 'Tie::IxHash',
  'vendor_code'   => { label=>'Northern 911 vendor code' },
  'password'      => { label=>'API passcode' },
  'test_mode'     => { label=>'Test mode',
                       type =>'checkbox' },
  'debug'         => { label=>'Enable debugging',
                       type =>'checkbox' },
;

%info = (
  'svc'     => 'svc_phone',
  'desc'    => 'Provision E911 to Northern 911',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => <<'END'
END
);

sub client {
  my $self = shift;

  if (!$self->get('client')) {
    $self->set('client',
      WebService::Northern911->new(
        vendor_code => $self->option('vendor_code'),
        password    => $self->option('password'),
        live        => ( $self->option('test_mode') ? 0 : 1),
      )
    );
  }

  return $self->get('client');
}

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

  my %location_hash = $svc_phone->location_hash;
  $location_hash{address1} =~ /^(\w+) +(.*)$/;

  my %customer = (
    'PHONE_NUMBER'        => $svc_phone->phonenum,
    'STREET_NUMBER'       => $1,
    'STREET_NAME'         => $2,
    'CITY'                => $location_hash{city},
    'PROVINCE_STATE'      => $location_hash{state},
    'POSTAL_CODE_ZIP'     => $location_hash{zip},
    'OTHER_ADDRESS_INFO'  => $location_hash{address2},
  );
  my $phone_name = $svc_phone->phone_name;
  if ( $phone_name ) {
    # could be a personal name or a business...
    if ( $svc_phone->e911_class and
        grep { $_ eq $svc_phone->e911_class }
         ( 2, 4, 5, 6, 7, 0, 'A', 'D', 'E', 'K')
       )
    {
      # one of the "Business" classes, Centrex, a payphone, or 
      # VoIP Enterprise class
      $customer{'LAST_NAME'} = $phone_name;
    } else {
      # assume residential, and try (inaccurately) to make a first/last
      # name out of it.
      @customer{'FIRST_NAME', 'LAST_NAME'} = split(' ', $phone_name, 2);
    }
  } else {
    my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
    if ($cust_main->company) {
      $customer{'LAST_NAME'} = $cust_main->company;
    } else {
      $customer{'LAST_NAME'} = $cust_main->last;
      $customer{'FIRST_NAME'} = $cust_main->first;
    }
  }

  if ($self->option('debug')) {
    warn "\nAddorUpdateCustomer:\n".Dumper(\%customer)."\n\n";
  }
  my $response = $self->client->AddorUpdateCustomer(%customer);
  if (!$response->is_success) {
    return $response->error_message;
  }
  '';
}

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

  # except when changing the phone number, exactly like export_insert;
  if ($new->phonenum ne $old->phonenum) {
    my $error = $self->export_delete($old);
    return $error if $error;
  }
  $self->export_insert($new);
}

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

  if ($self->option('debug')) {
    warn "\nDeleteCustomer:\n".$svc_phone->phonenum."\n\n";
  }
  my $response = $self->client->DeleteCustomer($svc_phone->phonenum);
  if (!$response->is_success) {
    return $response->error_message;
  }
  '';
}

# export_suspend and _unsuspend do nothing

sub export_relocate {
  my ($self, $svc_phone) = (shift, shift);
  $self->export_insert($svc_phone);
}

1;