02e1e5a40a4e5e187183b74fd948eb9d572da3ec
[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 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->invoice_attn
82                             || $cust_main->contact_firstlast,
83     'address1'         => $bill_location->address1,
84     'address2'         => $bill_location->address2,
85     'city'             => $bill_location->city,
86     'state'            => $bill_location->state,
87     'zip'              => $bill_location->zip,
88     'country'          => $bill_location->country,
89   ]);
90
91   die "Print connection error: ". $response->message
92     unless $response->is_success;
93
94   local $@;
95   my $content = eval { decode_json($response->content) };
96   die "Print JSON error : $@\n" if $@;
97
98   die $content->{error}."\n"
99     if $content->{error};
100
101 }
102
103 1;