X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FTemplate_Mixin.pm;h=e597e73a31dcb765f22f642d775a4f928c1b12f0;hb=3d0a1bb06b895c5be6e3f0517d355442a6b1e125;hp=7d8135daf249890a09c18cdf262c1fcea3288a85;hpb=627b53dd54c81b8bf16bf4c6b53184a0b76011a8;p=freeside.git diff --git a/FS/FS/Template_Mixin.pm b/FS/FS/Template_Mixin.pm index 7d8135daf..e597e73a3 100644 --- a/FS/FS/Template_Mixin.pm +++ b/FS/FS/Template_Mixin.pm @@ -106,7 +106,7 @@ sub print_latex { $params{'time'} = $today if $today; $params{'template'} = $template if $template; $params{$_} = $opt{$_} - foreach grep $opt{$_}, qw( unsquelch_cdr notice_name ); + foreach grep $opt{$_}, qw( unsquelch_cdr notice_name no_date no_number ); $template ||= $self->_agent_template if $self->can('_agent_template'); @@ -122,9 +122,7 @@ sub print_latex { UNLINK => 0, ) or die "can't open temp file: $!\n"; - my $cust_main = $self->cust_main; - my $prospect_main = $self->prospect_main; - my $agentnum = $cust_main ? $cust_main->agentnum : $prospect_main->agentnum; + my $agentnum = $self->agentnum; if ( $template && $conf->exists("logo_${template}.eps", $agentnum) ) { print $lh $conf->config_binary("logo_${template}.eps", $agentnum) @@ -176,6 +174,12 @@ sub print_latex { } +sub agentnum { + my $self = shift; + my $cust_main = $self->cust_main; + $cust_main ? $cust_main->agentnum : $self->prospect_main->agentnum; +} + =item print_generic OPTION => VALUE ... Internal method - returns a filled-in template for this invoice as a scalar. @@ -365,14 +369,6 @@ sub print_generic { my $date_format = $date_formats{$format}; - my %embolden_functions = ( 'latex' => sub { return '\textbf{'. shift(). '}' - }, - 'html' => sub { return ''. shift(). '' - }, - 'template' => sub { shift }, - ); - my $embolden_function = $embolden_functions{$format}; - my %newline_tokens = ( 'latex' => '\\\\', 'html' => '
', 'template' => "\n", @@ -449,9 +445,15 @@ sub print_generic { 'agent' => &$escape_function($cust_main->agent->agent), #invoice/quotation info - 'invnum' => $self->invnum, + 'no_number' => $params{'no_number'}, + 'invnum' => ( $params{'no_number'} ? '' : $self->invnum ), 'quotationnum' => $self->quotationnum, - 'date' => time2str($date_format, $self->_date), + 'no_date' => $params{'no_date'}, + '_date' => ( $params{'no_date'} ? '' : $self->_date ), + 'date' => ( $params{'no_date'} + ? '' + : time2str($date_format, $self->_date) + ), 'today' => time2str($date_format_long, $today), 'terms' => $self->terms, 'template' => $template, #params{'template'}, @@ -605,12 +607,60 @@ sub print_generic { # info from customer's last invoice before this one, for some # summary formats $invoice_data{'last_bill'} = {}; - my $last_bill = $pr_cust_bill[-1]; - if ( $last_bill ) { - $invoice_data{'last_bill'} = { - '_date' => $last_bill->_date, #unformatted - # all we need for now - }; + + # returns the last unpaid bill, not the last bill + #my $last_bill = $pr_cust_bill[-1]; + + if ( $self->custnum && $self->invnum ) { + + # THIS returns the customer's last bill before this one + my $last_bill = qsearchs({ + 'table' => 'cust_bill', + 'hashref' => { 'custnum' => $self->custnum, + 'invnum' => { op => '<', value => $self->invnum }, + }, + 'order_by' => ' ORDER BY invnum DESC LIMIT 1' + }); + if ( $last_bill ) { + $invoice_data{'last_bill'} = { + '_date' => $last_bill->_date, #unformatted + # all we need for now + }; + my (@payments, @credits); + # for formats that itemize previous payments + foreach my $cust_pay ( qsearch('cust_pay', { + 'custnum' => $self->custnum, + '_date' => { op => '>=', + value => $last_bill->_date } + } ) ) + { + next if $cust_pay->_date > $self->_date; + push @payments, { + '_date' => $cust_pay->_date, + 'date' => time2str($date_format, $cust_pay->_date), + 'payinfo' => $cust_pay->payby_payinfo_pretty, + 'amount' => sprintf('%.2f', $cust_pay->paid), + }; + # not concerned about applications + } + foreach my $cust_credit ( qsearch('cust_credit', { + 'custnum' => $self->custnum, + '_date' => { op => '>=', + value => $last_bill->_date } + } ) ) + { + next if $cust_credit->_date > $self->_date; + push @credits, { + '_date' => $cust_credit->_date, + 'date' => time2str($date_format, $cust_credit->_date), + 'creditreason'=> $cust_credit->reason, + 'amount' => sprintf('%.2f', $cust_credit->amount), + }; + } + $invoice_data{'previous_payments'} = \@payments; + $invoice_data{'previous_credits'} = \@credits; + } + } my $summarypage = ''; @@ -695,6 +745,11 @@ sub print_generic { my $other_money_char = $other_money_chars{$format}; $invoice_data{'dollar'} = $other_money_char; + my %minus_signs = ( 'latex' => '$-$', + 'html' => '−', + 'template' => '- ' ); + my $minus = $minus_signs{$format}; + my @detail_items = (); my @total_items = (); my @buf = (); @@ -745,7 +800,7 @@ sub print_generic { $adjust_section->{'sort_weight'} = $adjust_weight; my $unsquelched = $params{unsquelch_cdr} || $cust_main->squelch_cdr ne 'Y'; - my $multisection = $conf->exists('invoice_sections', $cust_main->agentnum); + my $multisection = $conf->exists($tc.'sections', $cust_main->agentnum); $invoice_data{'multisection'} = $multisection; my $late_sections = []; my $extra_sections = []; @@ -979,7 +1034,8 @@ sub print_generic { warn "$me adding taxes\n" if $DEBUG > 1; - foreach my $tax ( $self->_items_tax ) { + my @items_tax = $self->_items_tax; + foreach my $tax ( @items_tax ) { $taxtotal += $tax->{'amount'}; @@ -1014,7 +1070,7 @@ sub print_generic { } - if ( $taxtotal ) { + if ( @items_tax ) { my $total = {}; $total->{'total_item'} = $self->mt('Sub-total'); $total->{'total_amount'} = @@ -1041,9 +1097,33 @@ sub print_generic { $money_char. sprintf("%10.2f",$self->charged) ]; push @buf,['','']; - # calculate total, possibly including total owed on previous - # invoices - { + + ### + # Totals + ### + + my %embolden_functions = ( + 'latex' => sub { return '\textbf{'. shift(). '}' }, + 'html' => sub { return ''. shift(). '' }, + 'template' => sub { shift }, + ); + my $embolden_function = $embolden_functions{$format}; + + if ( $self->can('_items_total') ) { # quotations + + $self->_items_total(\@total_items); + + foreach ( @total_items ) { + $_->{'total_item'} = &$embolden_function( $_->{'total_item'} ); + $_->{'total_amount'} = &$embolden_function( $other_money_char. + $_->{'total_amount'} + ); + } + + } else { #normal invoice case + + # calculate total, possibly including total owed on previous + # invoices my $total = {}; my $item = 'Total'; $item = $conf->config('previous_balance-exclude_from_total') @@ -1074,127 +1154,128 @@ sub print_generic { sprintf( '%10.2f', $amount ) ]; push @buf,['','']; - } - # if we're showing previous invoices, also show previous - # credits and payments - if ( $self->enable_previous - and $self->can('_items_credits') - and $self->can('_items_payments') ) - { - #foreach my $thing ( sort { $a->_date <=> $b->_date } $self->_items_credits, $self->_items_payments - - # credits - my $credittotal = 0; - foreach my $credit ( $self->_items_credits('trim_len'=>60) ) { + # if we're showing previous invoices, also show previous + # credits and payments + if ( $self->enable_previous + and $self->can('_items_credits') + and $self->can('_items_payments') ) + { + #foreach my $thing ( sort { $a->_date <=> $b->_date } $self->_items_credits, $self->_items_payments + + # credits + my $credittotal = 0; + foreach my $credit ( $self->_items_credits('trim_len'=>60) ) { + + my $total; + $total->{'total_item'} = &$escape_function($credit->{'description'}); + $credittotal += $credit->{'amount'}; + $total->{'total_amount'} = $minus.$other_money_char.$credit->{'amount'}; + $adjusttotal += $credit->{'amount'}; + if ( $multisection ) { + my $money = $old_latex ? '' : $money_char; + push @detail_items, { + ext_description => [], + ref => '', + quantity => '', + description => &$escape_function($credit->{'description'}), + amount => $money. $credit->{'amount'}, + product_code => '', + section => $adjust_section, + }; + } else { + push @total_items, $total; + } - my $total; - $total->{'total_item'} = &$escape_function($credit->{'description'}); - $credittotal += $credit->{'amount'}; - $total->{'total_amount'} = '-'. $other_money_char. $credit->{'amount'}; - $adjusttotal += $credit->{'amount'}; - if ( $multisection ) { - my $money = $old_latex ? '' : $money_char; - push @detail_items, { - ext_description => [], - ref => '', - quantity => '', - description => &$escape_function($credit->{'description'}), - amount => $money. $credit->{'amount'}, - product_code => '', - section => $adjust_section, - }; - } else { - push @total_items, $total; } + $invoice_data{'credittotal'} = sprintf('%.2f', $credittotal); - } - $invoice_data{'credittotal'} = sprintf('%.2f', $credittotal); - - #credits (again) - foreach my $credit ( $self->_items_credits('trim_len'=>32) ) { - push @buf, [ $credit->{'description'}, $money_char.$credit->{'amount'} ]; - } + #credits (again) + foreach my $credit ( $self->_items_credits('trim_len'=>32) ) { + push @buf, [ $credit->{'description'}, $money_char.$credit->{'amount'} ]; + } - # payments - my $paymenttotal = 0; - foreach my $payment ( $self->_items_payments ) { - my $total = {}; - $total->{'total_item'} = &$escape_function($payment->{'description'}); - $paymenttotal += $payment->{'amount'}; - $total->{'total_amount'} = '-'. $other_money_char. $payment->{'amount'}; - $adjusttotal += $payment->{'amount'}; + # payments + my $paymenttotal = 0; + foreach my $payment ( $self->_items_payments ) { + my $total = {}; + $total->{'total_item'} = &$escape_function($payment->{'description'}); + $paymenttotal += $payment->{'amount'}; + $total->{'total_amount'} = $minus.$other_money_char.$payment->{'amount'}; + $adjusttotal += $payment->{'amount'}; + if ( $multisection ) { + my $money = $old_latex ? '' : $money_char; + push @detail_items, { + ext_description => [], + ref => '', + quantity => '', + description => &$escape_function($payment->{'description'}), + amount => $money. $payment->{'amount'}, + product_code => '', + section => $adjust_section, + }; + }else{ + push @total_items, $total; + } + push @buf, [ $payment->{'description'}, + $money_char. sprintf("%10.2f", $payment->{'amount'}), + ]; + } + $invoice_data{'paymenttotal'} = sprintf('%.2f', $paymenttotal); + if ( $multisection ) { - my $money = $old_latex ? '' : $money_char; - push @detail_items, { - ext_description => [], - ref => '', - quantity => '', - description => &$escape_function($payment->{'description'}), - amount => $money. $payment->{'amount'}, - product_code => '', - section => $adjust_section, - }; - }else{ - push @total_items, $total; + $adjust_section->{'subtotal'} = $other_money_char. + sprintf('%.2f', $adjusttotal); + push @sections, $adjust_section + unless $adjust_section->{sort_weight}; } - push @buf, [ $payment->{'description'}, - $money_char. sprintf("%10.2f", $payment->{'amount'}), - ]; - } - $invoice_data{'paymenttotal'} = sprintf('%.2f', $paymenttotal); - - if ( $multisection ) { - $adjust_section->{'subtotal'} = $other_money_char. - sprintf('%.2f', $adjusttotal); - push @sections, $adjust_section - unless $adjust_section->{sort_weight}; - } - # create Balance Due message - { - my $total; - $total->{'total_item'} = &$embolden_function($self->balance_due_msg); - $total->{'total_amount'} = - &$embolden_function( - $other_money_char. sprintf('%.2f', #why? $summarypage - # ? $self->charged + - # $self->billing_balance - # : - $self->owed + $pr_total - ) - ); - if ( $multisection && !$adjust_section->{sort_weight} ) { - $adjust_section->{'posttotal'} = $total->{'total_item'}. ' '. - $total->{'total_amount'}; - }else{ - push @total_items, $total; + # create Balance Due message + { + my $total; + $total->{'total_item'} = &$embolden_function($self->balance_due_msg); + $total->{'total_amount'} = + &$embolden_function( + $other_money_char. sprintf('%.2f', #why? $summarypage + # ? $self->charged + + # $self->billing_balance + # : + $self->owed + $pr_total + ) + ); + if ( $multisection && !$adjust_section->{sort_weight} ) { + $adjust_section->{'posttotal'} = $total->{'total_item'}. ' '. + $total->{'total_amount'}; + }else{ + push @total_items, $total; + } + push @buf,['','-----------']; + push @buf,[$self->balance_due_msg, $money_char. + sprintf("%10.2f", $balance_due ) ]; } - push @buf,['','-----------']; - push @buf,[$self->balance_due_msg, $money_char. - sprintf("%10.2f", $balance_due ) ]; - } - if ( $conf->exists('previous_balance-show_credit') - and $cust_main->balance < 0 ) { - my $credit_total = { - 'total_item' => &$embolden_function($self->credit_balance_msg), - 'total_amount' => &$embolden_function( - $other_money_char. sprintf('%.2f', -$cust_main->balance) - ), - }; - if ( $multisection ) { - $adjust_section->{'posttotal'} .= $newline_token . - $credit_total->{'total_item'} . ' ' . $credit_total->{'total_amount'}; - } - else { - push @total_items, $credit_total; + if ( $conf->exists('previous_balance-show_credit') + and $cust_main->balance < 0 ) { + my $credit_total = { + 'total_item' => &$embolden_function($self->credit_balance_msg), + 'total_amount' => &$embolden_function( + $other_money_char. sprintf('%.2f', -$cust_main->balance) + ), + }; + if ( $multisection ) { + $adjust_section->{'posttotal'} .= $newline_token . + $credit_total->{'total_item'} . ' ' . $credit_total->{'total_amount'}; + } + else { + push @total_items, $credit_total; + } + push @buf,['','-----------']; + push @buf,[$self->credit_balance_msg, $money_char. + sprintf("%10.2f", -$cust_main->balance ) ]; } - push @buf,['','-----------']; - push @buf,[$self->credit_balance_msg, $money_char. - sprintf("%10.2f", -$cust_main->balance ) ]; } - } + + } #end of default total adding ! can('_items_total') if ( $multisection ) { if ( $conf->exists('svc_phone_sections') @@ -2112,7 +2193,17 @@ sub _taxsort { sub _items_tax { my $self = shift; my @cust_bill_pkg = sort _taxsort grep { ! $_->pkgnum } $self->cust_bill_pkg; - $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_); + my @items = $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_); + + if ( $self->conf->exists('always_show_tax') ) { + my $itemdesc = $self->conf->config('always_show_tax') || 'Tax'; + if (0 == grep { $_->{description} eq $itemdesc } @items) { + push @items, + { 'description' => $itemdesc, + 'amount' => 0.00 }; + } + } + @items; } =item _items_cust_bill_pkg CUST_BILL_PKGS OPTIONS @@ -2208,7 +2299,7 @@ sub _items_cust_bill_pkg { my $type = $display->type; - my $desc = $cust_bill_pkg->desc; + my $desc = $cust_bill_pkg->desc( $cust_main ? $cust_main->locale : '' ); $desc = substr($desc, 0, $maxlength). '...' if $format eq 'latex' && length($desc) > $maxlength; @@ -2285,8 +2376,9 @@ sub _items_cust_bill_pkg { unless $cust_bill_pkg->pkgpart_override; #don't redisplay services $svc_label = $svc_labels[0]; - if ( ! $cust_pkg->locationnum or - $cust_pkg->locationnum != $cust_main->ship_locationnum ) { + my $lnum = $cust_main ? $cust_main->ship_locationnum + : $self->prospect_main->locationnum; + if ( ! $cust_pkg->locationnum or $cust_pkg->locationnum != $lnum ) { my $loc = $cust_pkg->location_label; $loc = substr($loc, 0, $maxlength). '...' if $format eq 'latex' && length($loc) > $maxlength; @@ -2349,17 +2441,17 @@ sub _items_cust_bill_pkg { my $time_period; my $date_style = ''; $date_style = $conf->config( 'cust_bill-line_item-date_style-non_monhtly', - $cust_main->agentnum + $self->agentnum ) if $part_pkg && $part_pkg->freq !~ /^1m?$/; $date_style ||= $conf->config( 'cust_bill-line_item-date_style', - $cust_main->agentnum + $self->agentnum ); if ( defined($date_style) && $date_style eq 'month_of' ) { $time_period = time2str('The month of %B', $cust_bill_pkg->sdate); } elsif ( defined($date_style) && $date_style eq 'X_month' ) { my $desc = $conf->config( 'cust_bill-line_item-date_description', - $cust_main->agentnum + $self->agentnum ); $desc .= ' ' unless $desc =~ /\s$/; $time_period = $desc. time2str('%B', $cust_bill_pkg->sdate); @@ -2400,7 +2492,9 @@ sub _items_cust_bill_pkg { warn "$me _items_cust_bill_pkg done adding service details\n" if $DEBUG > 1; - if ( $cust_pkg->locationnum != $cust_main->ship_locationnum ) { + my $lnum = $cust_main ? $cust_main->ship_locationnum + : $self->prospect_main->locationnum; + if ( $cust_pkg->locationnum != $lnum ) { my $loc = $cust_pkg->location_label; $loc = substr($loc, 0, $maxlength). '...' if $format eq 'latex' && length($loc) > $maxlength;