use Carp;
use Exporter;
use Scalar::Util qw( blessed );
-use Time::Local qw(timelocal_nocheck);
+use Time::Local qw(timelocal timelocal_nocheck);
use Data::Dumper;
use Tie::IxHash;
use Digest::MD5 qw(md5_base64);
use FS::cust_refund;
use FS::part_referral;
use FS::cust_main_county;
+use FS::tax_rate;
use FS::cust_tax_location;
+use FS::part_pkg_taxrate;
use FS::agent;
use FS::cust_main_invoice;
use FS::cust_credit_bill;
if $DEBUG;
my $time = $options{'time'} || time;
+ my $invoice_time = $options{'invoice_time'} || $time;
#put below somehow?
local $SIG{HUP} = 'IGNORE';
foreach my $tax ( keys %taxlisthash ) {
my $tax_object = shift @{ $taxlisthash{$tax} };
warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
- my $listref_or_error = $tax_object->taxline( @{ $taxlisthash{$tax} } );
+ my $listref_or_error =
+ $tax_object->taxline( $taxlisthash{$tax},
+ 'custnum' => $self->custnum,
+ 'invoice_time' => $invoice_time
+ );
unless (ref($listref_or_error)) {
$dbh->rollback if $oldAutoCommit;
return $listref_or_error;
warn "adding ". $listref_or_error->[1].
" as ". $listref_or_error->[0]. "\n"
if $DEBUG > 2;
- $tax{ $tax_object->taxname } += $listref_or_error->[1];
+ $tax{ $tax } += $listref_or_error->[1];
if ( $taxname{ $listref_or_error->[0] } ) {
- push @{ $taxname{ $listref_or_error->[0] } }, $tax_object->taxname;
+ push @{ $taxname{ $listref_or_error->[0] } }, $tax;
}else{
- $taxname{ $listref_or_error->[0] } = [ $tax_object->taxname ];
+ $taxname{ $listref_or_error->[0] } = [ $tax ];
}
}
+ #move the cust_tax_exempt_pkg records to the cust_bill_pkgs we will commit
+ my %packagemap = map { $_->pkgnum => $_ } @cust_bill_pkg;
+ foreach my $tax ( keys %taxlisthash ) {
+ foreach ( @{ $taxlisthash{$tax} }[1 ... scalar(@{ $taxlisthash{$tax} })] ) {
+ next unless ref($_) eq 'FS::cust_bill_pkg'; # shouldn't happen
+
+ push @{ $packagemap{$_->pkgnum}->_cust_tax_exempt_pkg },
+ splice( @{ $_->_cust_tax_exempt_pkg } );
+ }
+ }
+
#some taxes are taxed
my %totlisthash;
# existing taxes
warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
if ( exists( $totlisthash{ $totname } ) ) {
- push @{ $totlisthash{ $totname } }, $tax{ $tax_object->taxname };
+ push @{ $totlisthash{ $totname } }, $tax{ $tax };
}else{
- $totlisthash{ $totname } = [ $tot, $tax{ $tax_object->taxname } ];
+ $totlisthash{ $totname } = [ $tot, $tax{ $tax } ];
}
}
}
my $tax_object = shift @{ $totlisthash{$tax} };
warn "found previously found taxed tax ". $tax_object->taxname. "\n"
if $DEBUG > 2;
- my $listref_or_error = $tax_object->taxline( @{ $totlisthash{$tax} } );
+ my $listref_or_error =
+ $tax_object->taxline( $totlisthash{$tax},
+ 'custnum' => $self->custnum,
+ 'invoice_time' => $invoice_time
+ );
unless (ref($listref_or_error)) {
$dbh->rollback if $oldAutoCommit;
return $listref_or_error;
warn "adding taxed tax amount ". $listref_or_error->[1].
" as ". $tax_object->taxname. "\n"
if $DEBUG;
- $tax{ $tax_object->taxname } += $listref_or_error->[1];
+ $tax{ $tax } += $listref_or_error->[1];
}
#consolidate and create tax line items
warn "adding $taxname\n" if $DEBUG > 1;
foreach my $taxitem ( @{ $taxname{$taxname} } ) {
$tax += $tax{$taxitem} unless $seen{$taxitem};
+ $seen{$taxitem} = 1;
warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
}
next unless $tax;
#create the new invoice
my $cust_bill = new FS::cust_bill ( {
'custnum' => $self->custnum,
- '_date' => ( $options{'invoice_time'} || $time ),
+ '_date' => ( $invoice_time ),
'charged' => $charged,
} );
my $error = $cust_bill->insert;
$letter_data{company_name} = $conf->config('company_name');
- my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
+ my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
DIR => $dir,
SUFFIX => '.tex',