2ef564b7e66a2a7dadec7f9725dbcc1e6f2709c5
[freeside.git] / FS / FS / part_event / Action / cust_bill_fsinc_print.pm
1 package FS::part_event::Action::cust_bill_fsinc_print;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5 use LWP::UserAgent;
6 use HTTP::Request::Common qw( POST );
7 use JSON::XS; #use Cpanel::JSON::XS;
8 use CAM::PDF;
9 use FS::Conf;
10
11 sub description { 'Send invoice to Freeside Inc. for printing and mailing'; }
12
13 sub eventtable_hashref {
14   { 'cust_bill' => 1 };
15 }
16
17 sub option_fields {
18   (
19     'modenum'     => { label  => 'Invoice mode',
20                        type   => 'select-invoice_mode',
21                      },
22   );
23 }
24
25 sub default_weight { 52; }
26
27 sub do_action {
28   my( $self, $cust_bill ) = @_;
29
30   $cust_bill->set('mode' => $self->option('modenum'));
31
32   my $url = 'https://ws.freeside.biz/print';
33
34   my $cust_main = $cust_bill->cust_main;
35   my $bill_location = $cust_main->bill_location;
36
37   die 'Extra charges for international mailing; contact support@freeside.biz to enable'
38     if $bill_location->country ne 'US';
39
40   my $conf = new FS::Conf;
41
42   my @company_address = $conf->config('company_address', $agentnum);
43   my ( $company_address1, $company_address2, $company_city, $company_state, $company_zip );
44   if ( $company_address[2] =~ /^\s*(\S.*\S)\s*[\s,](\w\w),?\s*(\d{5}(-\d{4})?)\s*$/ ) {
45     $company_address1 = $company_address[0];
46     $company_address2 = $company_address[1];
47     $company_city  = $1;
48     $company_state = $2;
49     $company_zip   = $3;
50   } elsif ( $company_address[1] =~ /^\s*(\S.*\S)\s*[\s,](\w\w),?\s*(\d{5}(-\d{4})?)\s*$/ ) {
51     $company_address1 = $company_address[0];
52     $company_address2 = '';
53     $company_city  = $1;
54     $company_state = $2;
55     $company_zip   = $3;
56   } else {
57     die 'Unparsable company_address; contact support@freeside.biz';
58   }
59
60   my $file = $cust_bill->print_pdf;
61   my $pages = CAM::PDF->new($file)->numPages;
62
63   my $ua = LWP::UserAgent->new( 'ssl_opts' => { 'verify_hostname'=>0 });
64   my $response = $ua->request( POST $url, [
65     'support-key'      => scalar($conf->config('support-key')),
66     'file'             => $file,
67     'pages'            => $pages,
68
69     #from:
70     'company_name'     => scalar( $conf->config('company_name', $agentnum) ),
71     'company_address1' => $company_address1,
72     'company_address2' => $company_address2,
73     'company_city'     => $company_city
74     'company_state'    => $company_state,
75     'company_zip'      => $company_zip,
76     'company_country'  => 'US',
77     'company_phonenum' => scalar($conf->config('company_phonenum', $agentnum)),
78     'company_email'    => scalar($conf->config('invoice_from', $agentnum)),
79
80     #to:
81     'name'             => ( $cust_main->payname
82                               && $cust_main->payby !~ /^(CARD|DCRD|CHEK|DCHK)$/
83                                 ? $cust_main->payname
84                                 : $cust_main->contact_firstlast
85                           )
86     #'name'             => $cust_main->invoice_attn || $cust_main->contact_firstlast,
87     'address1'         => $bill_location->address1,
88     'address2'         => $bill_location->address2,
89     'city'             => $bill_location->city,
90     'state'            => $bill_location->state,
91     'zip'              => $bill_location->zip,
92     'country'          => $bill_location->country,
93   ]);
94
95   die "Print connection error: ". $response->message
96     unless $response->is_success;
97
98   local $@;
99   my $content = eval { decode_json($response->content) };
100   die "Print JSON error : $@\n" if $@;
101
102   die $content->{error}."\n"
103     if $content->{error};
104 }
105
106 1;