1 package FS::part_event::Action::cust_bill_fsinc_print;
4 use base qw( FS::part_event::Action );
6 use HTTP::Request::Common qw( POST );
7 use JSON::XS; #use Cpanel::JSON::XS;
11 sub description { 'Send invoice to Freeside Inc. for printing and mailing'; }
13 sub eventtable_hashref {
19 'modenum' => { label => 'Invoice mode',
20 type => 'select-invoice_mode',
25 sub default_weight { 52; }
28 my( $self, $cust_bill ) = @_;
30 $cust_bill->set('mode' => $self->option('modenum'));
32 my $url = 'https://ws.freeside.biz/print';
34 my $cust_main = $cust_bill->cust_main;
35 my $bill_location = $cust_main->bill_location;
37 die 'Extra charges for international mailing; contact support@freeside.biz to enable'
38 if $bill_location->country ne 'US';
40 my $conf = new FS::Conf;
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];
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 = '';
57 die 'Unparsable company_address; contact support@freeside.biz';
60 my $file = $cust_bill->print_pdf;
61 my $pages = CAM::PDF->new($file)->numPages;
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')),
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)),
81 'name' => ( $cust_main->payname
82 && $cust_main->payby !~ /^(CARD|DCRD|CHEK|DCHK)$/
84 : $cust_main->contact_firstlast
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,
95 die "Print connection error: ". $response->message
96 unless $response->is_success;
99 my $content = eval { decode_json($response->content) };
100 die "Print JSON error : $@\n" if $@;
102 die $content->{error}."\n"
103 if $content->{error};