X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FTemplate_Mixin.pm;h=c97e84e8392027d2142f33492765f9bfdd97186b;hb=57d4a5ffe7b86d032339d6eefe1a22277f3ca113;hp=76cadc3961d7603b4240956c9a444ad5c2d79f38;hpb=213d2aee192d0aa9ccf5d2caa6bdb09611686901;p=freeside.git diff --git a/FS/FS/Template_Mixin.pm b/FS/FS/Template_Mixin.pm index 76cadc396..c97e84e83 100644 --- a/FS/FS/Template_Mixin.pm +++ b/FS/FS/Template_Mixin.pm @@ -10,6 +10,7 @@ use vars qw( $invoice_lines @buf ); #yuck use List::Util qw(sum); #can't import first, it conflicts with cust_main.first use Date::Format; use Date::Language; +use Time::Local qw( timelocal ); use Text::Template 1.20; use File::Temp 0.14; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); @@ -147,6 +148,10 @@ sub print_latex { $template ||= $self->_agent_template if $self->can('_agent_template'); + #the new way + $self->set('mode', $params{mode}) + if $params{mode}; + my $pkey = $self->primary_key; my $tmp_template = $self->table. '.'. $self->$pkey. '.XXXXXXXX'; @@ -561,6 +566,7 @@ sub print_generic { 'notice_name' => $notice_name, # escape? 'current_charges' => sprintf("%.2f", $self->charged), 'duedate' => $self->due_date2str('rdate'), #date_format? + 'duedate_long' => $self->due_date2str('long'), #customer info 'custnum' => $cust_main->display_custnum, @@ -1059,7 +1065,7 @@ sub print_generic { # start setting up summary subtotals my @summary_subtotals; my $method = $conf->config('summary_subtotals_method'); - if ( $method and $method ne $conf->config($tc.'sections_method') ) { + if ( ( ref($self) ne 'FS::quotation' ) and $method and $method ne $conf->config($tc.'sections_method') ) { # then re-section them by the correct method my %section_method = ( by_category => 1 ); if ( $conf->config('summary_subtotals_method') eq 'location' ) { @@ -1150,14 +1156,27 @@ sub print_generic { if ( $invoice_data{finance_section} && $section->{'description'} eq $invoice_data{finance_section} ); - $section->{'subtotal'} = $other_money_char. - sprintf('%.2f', $section->{'subtotal'}) - if $multisection; + if ( $multisection ) { + + if ( ref($section->{'subtotal'}) ) { - # continue some normalization - $section->{'amount'} = $section->{'subtotal'} - if $multisection; + $section->{'subtotal'} = + sprintf("$other_money_char%.2f to $other_money_char%.2f", + $section->{'subtotal'}[0], + $section->{'subtotal'}[1] + ); + + } else { + $section->{'subtotal'} = $other_money_char. + sprintf('%.2f', $section->{'subtotal'}) + + } + + # continue some normalization + $section->{'amount'} = $section->{'subtotal'} + + } if ( $section->{'description'} ) { push @buf, ( [ &$escape_function($section->{'description'}), '' ], @@ -1356,7 +1375,16 @@ sub print_generic { foreach ( @new_total_items ) { my ($item, $amount) = ($_->{'total_item'}, $_->{'total_amount'}); $_->{'total_item'} = &$embolden_function( $item ); + + if ( ref($amount) ) { + $_->{'total_amount'} = &$embolden_function( + $other_money_char.$amount->[0]. ' to '. + $other_money_char.$amount->[1] + ); + } else { $_->{'total_amount'} = &$embolden_function( $other_money_char.$amount ); + } + # but if it's multisection, don't append to @total_items. the adjust # section has all this stuff push @total_items, $_ if !$multisection; @@ -1635,24 +1663,24 @@ sub print_generic { die "no invoice_lines() functions in template?" if ( $format eq 'template' && !$wasfunc ); - if ($format eq 'template') { + if ( $invoice_lines ) { + $invoice_data{'total_pages'} = int( scalar(@buf) / $invoice_lines ); + $invoice_data{'total_pages'}++ + if scalar(@buf) % $invoice_lines; + } - if ( $invoice_lines ) { - $invoice_data{'total_pages'} = int( scalar(@buf) / $invoice_lines ); - $invoice_data{'total_pages'}++ - if scalar(@buf) % $invoice_lines; + #setup subroutine for the template + $invoice_data{invoice_lines} = sub { + my $lines = shift || scalar(@buf); + map { + scalar(@buf) + ? shift @buf + : [ '', '' ]; } + ( 1 .. $lines ); + }; - #setup subroutine for the template - $invoice_data{invoice_lines} = sub { - my $lines = shift || scalar(@buf); - map { - scalar(@buf) - ? shift @buf - : [ '', '' ]; - } - ( 1 .. $lines ); - }; + if ($format eq 'template') { my $lines; my @collect; @@ -1666,6 +1694,13 @@ sub print_generic { } else { # this is where we actually create the invoice + if ( $params{no_addresses} ) { + delete $invoice_data{$_} foreach qw( + payname company address1 address2 city state zip country + ); + $invoice_data{returnaddress} = '~'; + } + warn "filling in template for invoice ". $self->invnum. "\n" if $DEBUG; warn join("\n", map " $_ => ". $invoice_data{$_}, keys %invoice_data). "\n" @@ -1908,6 +1943,12 @@ sub due_date { my $duedate = ''; if ( $self->terms =~ /^\s*Net\s*(\d+)\s*$/ ) { $duedate = $self->_date() + ( $1 * 86400 ); + } elsif ( $self->terms =~ /^End of Month$/ ) { + my ($mon,$year) = (localtime($self->_date) )[4,5]; + $mon++; + until ( $mon < 12 ) { $mon -= 12; $year++; } + my $nextmonth_first = timelocal(0,0,0,1,$mon,$year); + $duedate = $nextmonth_first - 86400; } $duedate; } @@ -1935,8 +1976,12 @@ sub balance_due_msg { # _items_total) and not here # (yes, or if invoice_sections is enabled; this is just for compatibility) if ( $self->due_date ) { - $msg .= ' - ' . $self->mt('Please pay by'). ' '. - $self->due_date2str('short'); + my $please_pay_by = + $self->conf->config('invoice_pay_by_msg', $self->agentnum) + || 'Please pay by [_1]'; + $msg .= ' - ' . $self->mt($please_pay_by, $self->due_date2str('short')). + ' ' + unless $self->conf->config_bool('invoice_omit_due_date',$self->agentnum); } elsif ( $self->terms ) { $msg .= ' - '. $self->mt($self->terms); } @@ -2062,6 +2107,7 @@ Returns an argument list to be passed to L. =cut use MIME::Entity; +use Encode; sub generate_email { @@ -2129,7 +2175,7 @@ sub generate_email { if $DEBUG; # 'print_text' argument is no longer used - @text = $self->print_text(\%args); + @text = map Encode::encode_utf8($_), $self->print_text(\%args); } else { @@ -2224,7 +2270,7 @@ sub generate_email { ' ', ' ', ' ', - $html, + Encode::encode_utf8($html), ' ', '', ], @@ -2345,6 +2391,110 @@ sub mimebuild_pdf { ); } +=item postal_mail_fsinc + +Sends this invoice to the Freeside Internet Services, Inc. print and mail +service. + +=cut + +use CAM::PDF; +use IO::Socket::SSL; +use LWP::UserAgent; +use HTTP::Request::Common qw( POST ); +use JSON::XS; +use MIME::Base64; +sub postal_mail_fsinc { + my ( $self, %opt ) = @_; + + my $url = 'https://ws.freeside.biz/print'; + + my $cust_main = $self->cust_main; + my $agentnum = $cust_main->agentnum; + my $bill_location = $cust_main->bill_location; + + die "Extra charges for international mailing; contact support\@freeside.biz to enable\n" + if $bill_location->country ne 'US'; + + my $conf = new FS::Conf; + + my @company_address = $conf->config('company_address', $agentnum); + my ( $company_address1, $company_address2, $company_city, $company_state, $company_zip ); + if ( $company_address[2] =~ /^\s*(\S.*\S)\s*[\s,](\w\w),?\s*(\d{5}(-\d{4})?)\s*$/ ) { + $company_address1 = $company_address[0]; + $company_address2 = $company_address[1]; + $company_city = $1; + $company_state = $2; + $company_zip = $3; + } elsif ( $company_address[1] =~ /^\s*(\S.*\S)\s*[\s,](\w\w),?\s*(\d{5}(-\d{4})?)\s*$/ ) { + $company_address1 = $company_address[0]; + $company_address2 = ''; + $company_city = $1; + $company_state = $2; + $company_zip = $3; + } else { + die "Unparsable company_address; contact support\@freeside.biz\n"; + } + $company_city =~ s/,$//; + + my $file = $self->print_pdf(%opt, 'no_addresses' => 1); + my $pages = CAM::PDF->new($file)->numPages; + + my $ua = LWP::UserAgent->new( + 'ssl_opts' => { + verify_hostname => 0, + SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, + SSL_version => 'SSLv3', + } + ); + my $response = $ua->request( POST $url, [ + 'support-key' => scalar($conf->config('support-key')), + 'file' => encode_base64($file), + 'pages' => $pages, + + #from: + 'company_name' => scalar( $conf->config('company_name', $agentnum) ), + 'company_address1' => $company_address1, + 'company_address2' => $company_address2, + 'company_city' => $company_city, + 'company_state' => $company_state, + 'company_zip' => $company_zip, + 'company_country' => 'US', + 'company_phonenum' => scalar($conf->config('company_phonenum', $agentnum)), + 'company_email' => scalar($conf->config('invoice_from', $agentnum)), + + #to: + 'name' => ( $cust_main->payname + && $cust_main->payby !~ /^(CARD|DCRD|CHEK|DCHK)$/ + ? $cust_main->payname + : $cust_main->contact_firstlast + ), + 'company' => $cust_main->company, + 'address1' => $bill_location->address1, + 'address2' => $bill_location->address2, + 'city' => $bill_location->city, + 'state' => $bill_location->state, + 'zip' => $bill_location->zip, + 'country' => $bill_location->country, + ]); + + die "Print connection error: ". $response->message. + ' ('. $response->as_string. ")\n" + unless $response->is_success; + + local $@; + my $content = eval { decode_json($response->content) }; + die "Print JSON error : $@\n" if $@; + + die $content->{error}."\n" + if $content->{error}; + + #TODO: store this so we can query for a status later + warn "Invoice printed, ID ". $content->{id}. "\n"; + + $content->{id}; +} + =item _items_sections OPTIONS Generate section information for all items appearing on this invoice. @@ -3064,7 +3214,9 @@ sub _items_cust_bill_pkg { # for location labels: use default location on the invoice date my $default_locationnum; - if ( $self->custnum ) { + if ( $conf->exists('invoice-all_pkg_addresses') ) { + $default_locationnum = 0; # treat them all as non-default + } elsif ( $self->custnum ) { my $h_cust_main; my @h_search = FS::h_cust_main->sql_h_search($self->_date); $h_cust_main = qsearchs({ @@ -3172,7 +3324,7 @@ sub _items_cust_bill_pkg { my @details = $cust_bill_pkg->details; # and I guess they're never bundled either? - if ( $cust_bill_pkg->setup != 0 ) { + if (( $cust_bill_pkg->setup != 0 ) || ( $cust_bill_pkg->setup_show_zero )) { my $description = $desc; $description .= ' Setup' if $cust_bill_pkg->recur != 0 @@ -3193,7 +3345,7 @@ sub _items_cust_bill_pkg { ), }; } - if ( $cust_bill_pkg->recur != 0 ) { + if (( $cust_bill_pkg->recur != 0 ) || ( $cust_bill_pkg->recur_show_zero )) { #push @b, { $r = { 'pkgnum' => $cust_bill_pkg->pkgpart, #so it displays in Ref @@ -3243,6 +3395,7 @@ sub _items_cust_bill_pkg { # append the word 'Setup' to the setup line if there's going to be # a recur line for the same package (i.e. not a one-time charge) + # XXX localization my $description = $desc; $description .= ' Setup' if $cust_bill_pkg->recur != 0 @@ -3263,8 +3416,11 @@ sub _items_cust_bill_pkg { # always pass the svc_label through to the template, even if # not displaying it as an ext_description my @svc_labels = map &{$escape_function}($_), - $cust_pkg->h_labels_short($self->_date, undef, 'I'); - + $cust_pkg->h_labels_short($self->_date, + undef, + 'I', + $self->conf->{locale}, + ); $svc_label = $svc_labels[0]; unless ( $cust_pkg->part_pkg->hide_svc_detail @@ -3354,7 +3510,9 @@ sub _items_cust_bill_pkg { push @dates, undef if !$prev; my @svc_labels = map &{$escape_function}($_), - $cust_pkg->h_labels_short(@dates, 'I'); + $cust_pkg->h_labels_short(@dates, + 'I', + $self->conf->{locale}); $svc_label = $svc_labels[0]; # show service labels, unless...