X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_bill.pm;h=5ae4f3686649ce7447204a936d5743237573ad92;hb=2cdb0b3f9e3b778fd914d847fc7851948a9930e4;hp=36ecbea3a1f84636a07c0d99107adf24b06263d8;hpb=f2ca904433495eff9e00802aa37da902b6518546;p=freeside.git diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 36ecbea3a..5ae4f3686 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -11,6 +11,7 @@ use Fcntl qw(:flock); #for spool_csv use Cwd; use List::Util qw(min max sum); use Date::Format; +use DateTime; use File::Temp 0.14; use HTML::Entities; use Storable qw( freeze thaw ); @@ -105,13 +106,13 @@ Deprecated fields =over 4 =item billing_balance - the customer's balance immediately before generating -this invoice. DEPRECATED. Use the L method +this invoice. DEPRECATED. Use the L method to determine the customer's balance at a specific time. =item previous_balance - the customer's balance immediately after generating the invoice before this one. DEPRECATED. -=item printed - formerly used to track the number of times an invoice had +=item printed - formerly used to track the number of times an invoice had been printed; no longer used. =back @@ -163,7 +164,7 @@ sub notice_name { $self->conf->config('notice_name') || 'Invoice' } -sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum } +sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum } sub cust_unlinked_msg { my $self = shift; "WARNING: can't find cust_main.custnum ". $self->custnum. @@ -279,15 +280,15 @@ sub void { # internal-only and discourage use # # =item delete -# +# # DO NOT USE THIS METHOD. Instead, apply a credit against the invoice, or use # the B method. -# +# # This is only for internal use by V, which is what you should be using. -# +# # DO NOT USE THIS METHOD. Whatever reason you think you have is almost certainly # wrong. Use B, that's what it is for. Really. This means you. -# +# # =cut sub _delete { @@ -460,9 +461,30 @@ sub previous_bill { $self->get('previous_bill'); } +=item following_bill + +Returns the customer's invoice that follows this one + +=cut + +sub following_bill { + my $self = shift; + if (!$self->get('following_bill')) { + $self->set('following_bill', qsearchs({ + table => 'cust_bill', + hashref => { + custnum => $self->custnum, + invnum => { op => '>', value => $self->invnum }, + }, + order_by => 'ORDER BY invnum ASC LIMIT 1', + })); + } + $self->get('following_bill'); +} + =item previous -Returns a list consisting of the total previous balance for this customer, +Returns a list consisting of the total previous balance for this customer, followed by the previous outstanding invoices (as FS::cust_bill objects also). =cut @@ -477,7 +499,7 @@ sub previous { qsearch( 'cust_bill', { 'custnum' => $self->custnum, #'_date' => { op=>'<', value=>$self->_date }, 'invnum' => { op=>'<', value=>$self->invnum }, - } ) + } ) ; foreach ( @cust_bill ) { $total += $_->owed; } $self->set('previous', [$total, @cust_bill]); @@ -618,7 +640,7 @@ sub num_cust_event { my $sql = "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ". " WHERE tablenum = ? AND eventtable = 'cust_bill'"; - my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql"; + my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql"; $sth->execute($self->invnum) or die $sth->errstr. " executing $sql"; $sth->fetchrow_arrayref->[0]; } @@ -638,8 +660,8 @@ Returns a list: an empty list on success or a list of errors. sub suspend { my $self = shift; - grep { $_->suspend(@_) } - grep {! $_->getfield('cancel') } + grep { $_->suspend(@_) } + grep {! $_->getfield('cancel') } $self->cust_pkg; } @@ -690,7 +712,7 @@ sub cancel { grep { $_ } map { $_->cancel(%opt) } - grep { ! $_->getfield('cancel') } + grep { ! $_->getfield('cancel') } @pkgs; } @@ -822,7 +844,7 @@ sub cust_bill_batch { =item discount_plans -Returns all discount plans (L) for this invoice, as a +Returns all discount plans (L) for this invoice, as a hash keyed by term length. =cut @@ -865,6 +887,35 @@ sub owed { $balance; } +=item owed_on_invoice + +Returns the amount to be displayed as the "Balance Due" on this +invoice. Amount returned depends on conf flags for invoicing + +See L for the true amount currently owed + +=cut + +sub owed_on_invoice { + my $self = shift; + + #return $self->owed() + # unless $self->conf->exists('previous_balance-payments_since') + + # Add charges from this invoice + my $owed = $self->charged(); + + # Add carried balances from previous invoices + # If previous items aren't to be displayed on the invoice, + # _items_previous() is aware of this and responds appropriately. + $owed += $_->{amount} for $self->_items_previous(); + + # Subtract payments and credits displayed on this invoice + $owed -= $_->{amount} for $self->_items_payments(), $self->_items_credits(); + + return $owed; +} + sub owed_pkgnum { my( $self, $pkgnum ) = @_; @@ -927,7 +978,7 @@ sub apply_payments_and_credits { $self->select_for_update; #mutex - my @payments = grep { $_->unapplied > 0 } + my @payments = grep { $_->unapplied > 0 } grep { !$_->no_auto_apply } $self->cust_main->cust_pay; my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit; @@ -956,7 +1007,7 @@ sub apply_payments_and_credits { ); my $max_credit_weight = max( map { $_->part_pkg->credit_weight || 0 } - grep { $_ } + grep { $_ } map { $_->cust_pkg } @open_lineitems ); @@ -967,7 +1018,7 @@ sub apply_payments_and_credits { } else { $app = 'credit'; } - + } elsif ( @payments ) { $app = 'pay'; } elsif ( @credits ) { @@ -1041,7 +1092,7 @@ I overrides the default email invoice From: address. I: obsolete, does nothing -I overrides "Invoice" as the name of the sent document +I overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required). I overrides the system 'lpr' option as the command to print a document @@ -1118,7 +1169,7 @@ sub queueable_email { $self->set('mode', $opt{mode}) if $opt{mode}; - my %args = map {$_ => $opt{$_}} + my %args = map {$_ => $opt{$_}} grep { $opt{$_} } qw( from notice_name no_coupon template ); @@ -1155,7 +1206,7 @@ sub pdf_filename { Returns the postscript or plaintext for this invoice as an arrayref. -Options must be passed as a hashref. Positional parameters are no longer +Options must be passed as a hashref. Positional parameters are no longer allowed. I