1 package FS::cust_bill_ApplicationCommon;
4 use vars qw( @ISA $DEBUG );
5 use FS::Schema qw( dbdef );
6 use FS::Record qw( qsearch qsearchs dbh );
8 @ISA = qw( FS::Record );
14 FS::cust_bill_ApplicationCommon - Base class for bill application classes
18 use FS::cust_bill_ApplicationCommon;
20 @ISA = qw( FS::cust_bill_ApplicationCommon );
22 sub _app_source_name { 'payment'; }
23 sub _app_source_table { 'cust_pay'; }
24 sub _app_lineitem_breakdown_table { 'cust_bill_pay_pkg'; }
28 FS::cust_bill_ApplicationCommon is intended as a base class for classes which
29 represent application of things to invoices, currently payments
30 (see L<FS::cust_bill_pay>) or credits (see L<FS::cust_credit_bill>).
41 local $SIG{HUP} = 'IGNORE';
42 local $SIG{INT} = 'IGNORE';
43 local $SIG{QUIT} = 'IGNORE';
44 local $SIG{TERM} = 'IGNORE';
45 local $SIG{TSTP} = 'IGNORE';
46 local $SIG{PIPE} = 'IGNORE';
48 my $oldAutoCommit = $FS::UID::AutoCommit;
49 local $FS::UID::AutoCommit = 0;
52 my $error = $self->SUPER::insert(@_)
53 || $self->apply_to_lineitems;
55 $dbh->rollback if $oldAutoCommit;
59 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
72 local $SIG{HUP} = 'IGNORE';
73 local $SIG{INT} = 'IGNORE';
74 local $SIG{QUIT} = 'IGNORE';
75 local $SIG{TERM} = 'IGNORE';
76 local $SIG{TSTP} = 'IGNORE';
77 local $SIG{PIPE} = 'IGNORE';
79 my $oldAutoCommit = $FS::UID::AutoCommit;
80 local $FS::UID::AutoCommit = 0;
83 foreach my $app ( $self->lineitem_applications ) {
84 my $error = $app->delete;
86 $dbh->rollback if $oldAutoCommit;
91 my $error = $self->SUPER::delete(@_);
93 $dbh->rollback if $oldAutoCommit;
97 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
103 =item apply_to_lineitems
105 Auto-applies this invoice application to specific line items, if possible.
109 sub apply_to_lineitems {
114 local $SIG{HUP} = 'IGNORE';
115 local $SIG{INT} = 'IGNORE';
116 local $SIG{QUIT} = 'IGNORE';
117 local $SIG{TERM} = 'IGNORE';
118 local $SIG{TSTP} = 'IGNORE';
119 local $SIG{PIPE} = 'IGNORE';
121 my $oldAutoCommit = $FS::UID::AutoCommit;
122 local $FS::UID::AutoCommit = 0;
125 my @open = $self->cust_bill->open_cust_bill_pkg; #FOR UPDATE...?
126 warn scalar(@open). " open line items for invoice ".
127 $self->cust_bill->invnum. "\n"
130 $total += $_->setup + $_->recur foreach @open;
131 $total = sprintf('%.2f', $total);
133 if ( $self->amount > $total ) {
134 dbh->rollback if $oldAutoCommit;
135 return "Can't apply a ". $self->_app_source_name. ' of $'. $self->amount.
136 " greater than the remaining owed on line items (\$$total)";
140 # - one lineitem (a simple special case of:)
141 # - amount is for whole invoice (well, all of remaining lineitem links)
142 if ( $self->amount == $total ) {
144 #@apply = map { [ $_, $_->amount ]; } @open;
145 @apply = map { [ $_, $_->setup || $_->recur ]; } @open;
149 #slightly magic case:
150 # - amount exactly and uniquely matches a single open lineitem
151 # (you must be trying to pay or credit that item, then)
153 my @same = grep { $_->setup == $self->amount
154 || $_->recur == $self->amount
157 @apply = map { [ $_, $self->amount ]; } @same
158 if scalar(@same) == 1;
163 # - leave unapplied, for now
164 # - eventually, auto-apply? sequentially? pro-rated against total remaining?
166 # do the applicaiton(s)
167 my $table = $self->lineitem_breakdown_table;
168 my $source_key = dbdef->table($self->table)->primary_key;
169 foreach my $apply ( @apply ) {
170 my ( $cust_bill_pkg, $amount ) = @$apply;
171 my $application = "FS::$table"->new( {
172 $source_key => $self->$source_key(),
173 'billpkgnum' => $cust_bill_pkg->billpkgnum,
175 'setuprecur' => ( $cust_bill_pkg->setup > 0 ? 'setup' : 'recur' ),
176 'sdate' => $cust_bill_pkg->sdate,
177 'edate' => $cust_bill_pkg->edate,
179 my $error = $application->insert;
181 dbh->rollbck if $oldAutoCommit;
190 =item lineitem_applications
192 Returns all the specific line item applications for this invoice application.
196 sub lineitem_applications {
198 my $primary_key = dbdef->table($self->table)->primary_key;
200 'table' => $self->lineitem_breakdown_table,
201 'hashref' => { $primary_key => $self->$primary_key() },
208 Returns the invoice (see L<FS::cust_bill>)
214 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
217 =item lineitem_breakdown_table
221 sub lineitem_breakdown_table {
223 $self->_load_table($self->_app_lineitem_breakdown_table);
227 my( $self, $table ) = @_;
228 eval "use FS::$table";
239 L<FS::cust_bill_pay> and L<FS::cust_bill_pay_pkg>,
240 L<FS::cust_credit_bill> and L<FS::cust_credit_bill_pkg>