summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/cust_location_http.pm
blob: 460080c74e155746a2f2cc853ac6564a23f76a22 (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
package FS::part_export::cust_location_http;

use strict;
use base qw( FS::part_export::http );
use vars qw( %options %info );

my @location_fields = qw(
  custnum
  address1
  address2
  city
  state
  zip
  country
  locationname
  county
  latitude
  longitude
  prospectnum
  location_type
  location_number
  location_kind
  geocode
  district
  censusyear
  incorporated
);

tie %options, 'Tie::IxHash',
  'method' => { label   =>'Method',
                type    =>'select',
                #options =>[qw(POST GET)],
                options =>[qw(POST)],
                default =>'POST' },
  'url'    => { label   => 'URL', default => 'http://', },
  'ssl_no_verify' => { label => 'Skip SSL certificate validation',
                       type  => 'checkbox',
                     },
  'include_fields' => { 'label' => 'Include fields',
                        'type'  => 'select',
                        'multiple' => 1,
                        'options' => [ @location_fields ] },
  'success_regexp' => {
    label  => 'Success Regexp',
    default => '',
  },
;

%info = (
  'svc'     => [qw( cust_location )],
  'desc'    => 'Send an HTTP or HTTPS GET or POST request, for customer locations',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => <<'END',
Send an HTTP or HTTPS GET or POST to the specified URL on customer location insert
or replace.  Always sends cgi fields action ('insert' or 'replace') and locationnum,
as well as any fields specified below.  Only sends on replace if one of the
specified fields changed.
For HTTPS support, <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a> is required.
END
);

sub http_queue_standard {
  my $self = shift;
  $self->http_queue( '',
    ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
    $self->option('method'),
    $self->option('url'),
    $self->option('success_regexp'),
    @_
  );
}

sub _include_fields {
  my $self = shift;
  split( /\s+/, $self->option('include_fields') );
}

sub _export_command {
  my( $self, $action, $cust_location ) = ( shift, shift, shift );

  return '' unless $action eq 'insert';

  $self->http_queue_standard(
    'action' => $action,
    map { $_ => $cust_location->get($_) } ('locationnum', $self->_include_fields)
  );

}

# currently, only custnum can change (when converting prospect to customer)
# but using more generic logic for ease of adding other changeable fields
sub _export_replace {
  my( $self, $new, $old ) = ( shift, shift, shift );

  my $changed = 0;
  foreach my $field ($self->_include_fields) {
    next if $new->get($field) eq $old->get($field);
    next if ($field =~ /latitude|longitude/) and $new->get($field) == $old->get($field);
    $changed = 1;
  }
  return '' unless $changed;

  $self->http_queue_standard(
    'action' => 'replace',
    map { $_ => $new->get($_) } ('locationnum', $self->_include_fields)
  );
}

1;