1 package FS::cust_bill_ApplicationCommon;
4 use vars qw( @ISA $DEBUG $me );
5 use List::Util qw(min);
6 use FS::Schema qw( dbdef );
7 use FS::Record qw( qsearch qsearchs dbh );
9 @ISA = qw( FS::Record );
12 $me = '[FS::cust_bill_ApplicationCommon]';
16 FS::cust_bill_ApplicationCommon - Base class for bill application classes
20 use FS::cust_bill_ApplicationCommon;
22 @ISA = qw( FS::cust_bill_ApplicationCommon );
24 sub _app_source_name { 'payment'; }
25 sub _app_source_table { 'cust_pay'; }
26 sub _app_lineitem_breakdown_table { 'cust_bill_pay_pkg'; }
30 FS::cust_bill_ApplicationCommon is intended as a base class for classes which
31 represent application of things to invoices, currently payments
32 (see L<FS::cust_bill_pay>) or credits (see L<FS::cust_credit_bill>).
43 local $SIG{HUP} = 'IGNORE';
44 local $SIG{INT} = 'IGNORE';
45 local $SIG{QUIT} = 'IGNORE';
46 local $SIG{TERM} = 'IGNORE';
47 local $SIG{TSTP} = 'IGNORE';
48 local $SIG{PIPE} = 'IGNORE';
50 my $oldAutoCommit = $FS::UID::AutoCommit;
51 local $FS::UID::AutoCommit = 0;
54 my $error = $self->SUPER::insert(@_)
55 || $self->apply_to_lineitems;
57 $dbh->rollback if $oldAutoCommit;
61 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
74 local $SIG{HUP} = 'IGNORE';
75 local $SIG{INT} = 'IGNORE';
76 local $SIG{QUIT} = 'IGNORE';
77 local $SIG{TERM} = 'IGNORE';
78 local $SIG{TSTP} = 'IGNORE';
79 local $SIG{PIPE} = 'IGNORE';
81 my $oldAutoCommit = $FS::UID::AutoCommit;
82 local $FS::UID::AutoCommit = 0;
85 foreach my $app ( $self->lineitem_applications ) {
86 my $error = $app->delete;
88 $dbh->rollback if $oldAutoCommit;
93 my $error = $self->SUPER::delete(@_);
95 $dbh->rollback if $oldAutoCommit;
99 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
105 =item apply_to_lineitems
107 Auto-applies this invoice application to specific line items, if possible.
111 sub apply_to_lineitems {
116 local $SIG{HUP} = 'IGNORE';
117 local $SIG{INT} = 'IGNORE';
118 local $SIG{QUIT} = 'IGNORE';
119 local $SIG{TERM} = 'IGNORE';
120 local $SIG{TSTP} = 'IGNORE';
121 local $SIG{PIPE} = 'IGNORE';
123 my $oldAutoCommit = $FS::UID::AutoCommit;
124 local $FS::UID::AutoCommit = 0;
127 my @open = $self->cust_bill->open_cust_bill_pkg; #FOR UPDATE...?
128 warn "$me ". scalar(@open). " open line items for invoice ".
129 $self->cust_bill->invnum. "\n"
132 $total += $_->setup + $_->recur foreach @open;
133 $total = sprintf('%.2f', $total);
135 if ( $self->amount > $total ) {
136 $dbh->rollback if $oldAutoCommit;
137 return "Can't apply a ". $self->_app_source_name. ' of $'. $self->amount.
138 " greater than the remaining owed on line items (\$$total)";
142 # - one lineitem (a simple special case of:)
143 # - amount is for whole invoice (well, all of remaining lineitem links)
144 if ( $self->amount == $total ) {
146 warn "$me application amount covers remaining balance of invoice in full;".
147 "applying to those lineitems\n"
150 #@apply = map { [ $_, $_->amount ]; } @open;
151 @apply = map { [ $_, $_->setup || $_->recur ]; } @open;
155 #slightly magic case:
156 # - amount exactly and uniquely matches a single open lineitem
157 # (you must be trying to pay or credit that item, then)
159 my @same = grep { $_->setup == $self->amount
160 || $_->recur == $self->amount
163 if ( scalar(@same) == 1 ) {
164 warn "$me application amount exactly and uniquely matches one lineitem;".
165 " applying to that lineitem\n"
167 @apply = map { [ $_, $self->amount ]; } @same
174 warn "$me applying amount based on package weights\n"
178 # - apply based on weights...
180 my $weight_col = $self->_app_part_pkg_weight_column;
181 my @openweight = map { [ $_, ($_->cust_pkg->part_pkg->$weight_col()||0) ] }
185 my @weights = sort { $b <=> $a } # highest weight first
186 grep { ! $saw{$_}++ } # want a list of unique weights
190 my $remaining_amount = $self->amount;
191 foreach my $weight ( @weights ) {
193 #i hate it when my schwartz gets tangled
194 my @items = map { $_->[0] } grep { $weight == $_->[1] } @openweight;
197 foreach my $item (@items) { $itemtotal += $item->setup || $item->recur; }
198 my $applytotal = min( $itemtotal, $remaining_amount );
199 $remaining_amount -= $applytotal;
201 warn "$me applying $applytotal ($remaining_amount remaining)".
202 " to ". scalar(@items). " lineitems with weight $weight\n"
205 #if some items are less than applytotal/num_items, then apply then in full
210 #no, not sprintf("%.2f",
211 # we want this rounded DOWN for purposes of checking for line items
212 # less than it, we don't want .66666 becoming .67 and causing this
213 # to trigger when it shouldn't
214 my $applyeach = int( 100 * $applytotal / scalar(@items) ) / 100;
217 foreach my $item ( @items ) {
218 my $itemamount = $item->setup || $item->recur;
219 if ( $itemamount < $applyeach ) {
220 warn "$me applying full $itemamount".
221 " to small line item (cust_bill_pkg ". $item->billpkgnum. ")\n"
223 push @apply, [ $item, $itemamount ];
224 $applytotal -= $itemamount;
227 push @newitems, $item;
232 } while ( $lessflag );
234 #and now that we've fallen out of the loop, distribute the rest equally...
236 # should cust_bill_pay_pkg and cust_credit_bill_pkg amount columns
237 # become real instead of numeric(10,2) ??? no..
238 my $applyeach = sprintf("%.2f", $applytotal / scalar(@items) );
240 my @equi_apply = map { [ $_, $applyeach ] } @items;
242 # or should we futz with pennies instead? yes, bah!
244 sprintf('%.0f', 100 * ( $applytotal - $applyeach * scalar(@items) ) );
245 $diff = 0 if $diff eq '-0'; #yay ieee fp
246 if ( abs($diff) > scalar(@items) ) {
247 #we must have done something really wrong, the difference is more than
249 $dbh->rollback if $oldAutoCommit;
250 return 'Error distributing pennies applying '. $self->_app_source_name.
251 " - can't distribute difference of $diff pennies".
252 ' among '. scalar(@items). ' line items';
255 warn "$me futzing with $diff pennies difference\n"
259 while ( $diff != 0 && $futz < scalar(@equi_apply) ) {
261 $equi_apply[$futz++]->[1] += .01;
263 } elsif ( $diff < 0 ) {
264 $equi_apply[$futz++]->[1] -= .01;
267 die "guru exception #5 (in fortran tongue the answer)";
271 if ( sprintf('%.0f', $diff ) ) {
272 $dbh->rollback if $oldAutoCommit;
273 return "couldn't futz with pennies enough: still $diff left";
277 warn "$me applying ". $_->[1].
278 " to line item (cust_bill_pkg ". $_->[0]->billpkgnum. ")\n"
283 push @apply, @equi_apply;
285 #$remaining_amount -= $applytotal;
286 last unless $remaining_amount;
292 # do the applicaiton(s)
293 my $table = $self->lineitem_breakdown_table;
294 my $source_key = dbdef->table($self->table)->primary_key;
296 foreach my $apply ( @apply ) {
297 my ( $cust_bill_pkg, $amount ) = @$apply;
299 my $application = "FS::$table"->new( {
300 $source_key => $self->$source_key(),
301 'billpkgnum' => $cust_bill_pkg->billpkgnum,
302 'amount' => sprintf('%.2f', $amount),
303 'setuprecur' => ( $cust_bill_pkg->setup > 0 ? 'setup' : 'recur' ),
304 'sdate' => $cust_bill_pkg->sdate,
305 'edate' => $cust_bill_pkg->edate,
307 my $error = $application->insert;
309 $dbh->rollback if $oldAutoCommit;
314 #everything should always be applied to line items in full now... sanity check
315 $applied = sprintf('%.2f', $applied);
316 unless ( $applied == $self->amount ) {
317 $dbh->rollback if $oldAutoCommit;
318 return 'Error applying '. $self->_app_source_name. ' of $'. $self->amount.
319 ' to line items - only $'. $applied. ' was applied.';
322 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
327 =item lineitem_applications
329 Returns all the specific line item applications for this invoice application.
333 sub lineitem_applications {
335 my $primary_key = dbdef->table($self->table)->primary_key;
337 'table' => $self->lineitem_breakdown_table,
338 'hashref' => { $primary_key => $self->$primary_key() },
345 Returns the invoice (see L<FS::cust_bill>)
351 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
354 =item lineitem_breakdown_table
358 sub lineitem_breakdown_table {
360 $self->_load_table($self->_app_lineitem_breakdown_table);
364 my( $self, $table ) = @_;
365 eval "use FS::$table";
376 L<FS::cust_bill_pay> and L<FS::cust_bill_pay_pkg>,
377 L<FS::cust_credit_bill> and L<FS::cust_credit_bill_pkg>