RT#29296: API stuff: Add new locations
[freeside.git] / FS / FS / part_export / cust_location_http.pm
1 package FS::part_export::cust_location_http;
2
3 use strict;
4 use base qw( FS::part_export::http );
5 use vars qw( %options %info );
6
7 my @location_fields = qw(
8   custnum
9   address1
10   address2
11   city
12   state
13   zip
14   country
15   locationname
16   county
17   latitude
18   longitude
19   prospectnum
20   location_type
21   location_number
22   location_kind
23   geocode
24   district
25   censusyear
26   incorporated
27 );
28
29 tie %options, 'Tie::IxHash',
30   'method' => { label   =>'Method',
31                 type    =>'select',
32                 #options =>[qw(POST GET)],
33                 options =>[qw(POST)],
34                 default =>'POST' },
35   'url'    => { label   => 'URL', default => 'http://', },
36   'ssl_no_verify' => { label => 'Skip SSL certificate validation',
37                        type  => 'checkbox',
38                      },
39   'include_fields' => { 'label' => 'Include fields',
40                         'type'  => 'select',
41                         'multiple' => 1,
42                         'options' => [ @location_fields ] },
43   'success_regexp' => {
44     label  => 'Success Regexp',
45     default => '',
46   },
47 ;
48
49 %info = (
50   'svc'     => [qw( cust_location )],
51   'desc'    => 'Send an HTTP or HTTPS GET or POST request, for customer locations',
52   'options' => \%options,
53   'no_machine' => 1,
54   'notes'   => <<'END',
55 Send an HTTP or HTTPS GET or POST to the specified URL on customer location insert
56 or replace.  Always sends cgi fields action ('insert' or 'replace') and locationnum,
57 as well as any fields specified below.  Only sends on replace if one of the
58 specified fields changed.
59 For HTTPS support, <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
60 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a> is required.
61 END
62 );
63
64 sub http_queue_standard {
65   my $self = shift;
66   $self->http_queue( '',
67     ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
68     $self->option('method'),
69     $self->option('url'),
70     $self->option('success_regexp'),
71     @_
72   );
73 }
74
75 sub _include_fields {
76   my $self = shift;
77   split( /\s+/, $self->option('include_fields') );
78 }
79
80 sub _export_command {
81   my( $self, $action, $cust_location ) = ( shift, shift, shift );
82
83   return '' unless $action eq 'insert';
84
85   $self->http_queue_standard(
86     'action' => $action,
87     map { $_ => $cust_location->get($_) } ('locationnum', $self->_include_fields)
88   );
89
90 }
91
92 # currently, only custnum can change (when converting prospect to customer)
93 # but using more generic logic for ease of adding other changeable fields
94 sub _export_replace {
95   my( $self, $new, $old ) = ( shift, shift, shift );
96
97   my $changed = 0;
98   foreach my $field ($self->_include_fields) {
99     next if $new->get($field) eq $old->get($field);
100     next if ($field =~ /latitude|longitude/) and $new->get($field) == $old->get($field);
101     $changed = 1;
102   }
103   return '' unless $changed;
104
105   $self->http_queue_standard(
106     'action' => 'replace',
107     map { $_ => $new->get($_) } ('locationnum', $self->_include_fields)
108   );
109 }
110
111 1;