+ # break down lineitem amounts for tax lines
+ # could expand @open above, instead, for a slightly different magic effect
+ my @result = ();
+ foreach my $apply ( @apply ) {
+ my @sub_lines = $apply->[0]->cust_bill_pkg_tax_Xlocation;
+ my $amount = $apply->[1];
+ warn "applying ". $apply->[1]. " to ". $apply->[0]->desc
+ if $DEBUG;
+
+ foreach my $subline ( @sub_lines ) {
+ my $owed = $subline->owed;
+ push @result, [ $apply->[0],
+ sprintf('%.2f', min($amount, $owed) ),
+ { $subline->primary_key => $subline->get($subline->primary_key) },
+ ];
+ $amount -= $owed;
+ $amount = 0 if $amount < 0;
+ last unless $amount;
+ }
+ if ( $amount > 0 ) {
+ push @result, [ $apply->[0], sprintf('%.2f', $amount), {} ];
+ }
+ }
+
+ \@result;
+
+}
+
+sub apply_to_lineitems {
+ #my $self = shift;
+ my( $self, %options ) = @_;
+
+ return '' if $skip_apply_to_lineitems_hack;
+
+ my $conf = new FS::Conf;
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $listref_or_error = $self->calculate_applications(%options);
+ unless (ref($listref_or_error)) {
+ $dbh->rollback if $oldAutoCommit;
+ return $listref_or_error;
+ }
+
+ my @apply = @$listref_or_error;
+