credit line items interface: set credit amounts, RT#18676
[freeside.git] / FS / FS / cust_credit.pm
1 package FS::cust_credit;
2
3 use strict;
4 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Record );
5 use vars qw( $conf $unsuspendauto $me $DEBUG
6              $otaker_upgrade_kludge $ignore_empty_reasonnum
7            );
8 use List::Util qw( min );
9 use Date::Format;
10 use FS::UID qw( dbh getotaker );
11 use FS::Misc qw(send_email);
12 use FS::Record qw( qsearch qsearchs dbdef );
13 use FS::CurrentUser;
14 use FS::cust_main;
15 use FS::cust_pkg;
16 use FS::cust_refund;
17 use FS::cust_credit_bill;
18 use FS::part_pkg;
19 use FS::reason_type;
20 use FS::reason;
21 use FS::cust_event;
22
23 $me = '[ FS::cust_credit ]';
24 $DEBUG = 0;
25
26 $otaker_upgrade_kludge = 0;
27 $ignore_empty_reasonnum = 0;
28
29 #ask FS::UID to run this stuff for us later
30 $FS::UID::callback{'FS::cust_credit'} = sub { 
31
32   $conf = new FS::Conf;
33   $unsuspendauto = $conf->exists('unsuspendauto');
34
35 };
36
37 our %reasontype_map = ( 'referral_credit_type' => 'Referral Credit',
38                         'cancel_credit_type'   => 'Cancellation Credit',
39                         'signup_credit_type'   => 'Self-Service Credit',
40                       );
41
42 =head1 NAME
43
44 FS::cust_credit - Object methods for cust_credit records
45
46 =head1 SYNOPSIS
47
48   use FS::cust_credit;
49
50   $record = new FS::cust_credit \%hash;
51   $record = new FS::cust_credit { 'column' => 'value' };
52
53   $error = $record->insert;
54
55   $error = $new_record->replace($old_record);
56
57   $error = $record->delete;
58
59   $error = $record->check;
60
61 =head1 DESCRIPTION
62
63 An FS::cust_credit object represents a credit; the equivalent of a negative
64 B<cust_bill> record (see L<FS::cust_bill>).  FS::cust_credit inherits from
65 FS::Record.  The following fields are currently supported:
66
67 =over 4
68
69 =item crednum
70
71 Primary key (assigned automatically for new credits)
72
73 =item custnum
74
75 Customer (see L<FS::cust_main>)
76
77 =item amount
78
79 Amount of the credit
80
81 =item _date
82
83 Specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
84 L<Time::Local> and L<Date::Parse> for conversion functions.
85
86 =item usernum
87
88 Order taker (see L<FS::access_user>)
89
90 =item reason
91
92 Text ( deprecated )
93
94 =item reasonnum
95
96 Reason (see L<FS::reason>)
97
98 =item addlinfo
99
100 Text
101
102 =item closed
103
104 Books closed flag, empty or `Y'
105
106 =item pkgnum
107
108 Desired pkgnum when using experimental package balances.
109
110 =back
111
112 =head1 METHODS
113
114 =over 4
115
116 =item new HASHREF
117
118 Creates a new credit.  To add the credit to the database, see L<"insert">.
119
120 =cut
121
122 sub table { 'cust_credit'; }
123 sub cust_linked { $_[0]->cust_main_custnum; } 
124 sub cust_unlinked_msg {
125   my $self = shift;
126   "WARNING: can't find cust_main.custnum ". $self->custnum.
127   ' (cust_credit.crednum '. $self->crednum. ')';
128 }
129
130 =item insert
131
132 Adds this credit to the database ("Posts" the credit).  If there is an error,
133 returns the error, otherwise returns false.
134
135 =cut
136
137 sub insert {
138   my ($self, %options) = @_;
139
140   local $SIG{HUP} = 'IGNORE';
141   local $SIG{INT} = 'IGNORE';
142   local $SIG{QUIT} = 'IGNORE';
143   local $SIG{TERM} = 'IGNORE';
144   local $SIG{TSTP} = 'IGNORE';
145   local $SIG{PIPE} = 'IGNORE';
146
147   my $oldAutoCommit = $FS::UID::AutoCommit;
148   local $FS::UID::AutoCommit = 0;
149   my $dbh = dbh;
150
151   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
152   my $old_balance = $cust_main->balance;
153
154   unless ($self->reasonnum) {
155     my $result = $self->reason( $self->getfield('reason'),
156                                 exists($options{ 'reason_type' })
157                                   ? ('reason_type' => $options{ 'reason_type' })
158                                   : (),
159                               );
160     unless($result) {
161       $dbh->rollback if $oldAutoCommit;
162       return "failed to set reason for $me"; #: ". $dbh->errstr;
163     }
164   }
165
166   $self->setfield('reason', '');
167
168   my $error = $self->SUPER::insert;
169   if ( $error ) {
170     $dbh->rollback if $oldAutoCommit;
171     return "error inserting $self: $error";
172   }
173
174   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
175
176   #false laziness w/ cust_pay::insert
177   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
178     my @errors = $cust_main->unsuspend;
179     #return 
180     # side-fx with nested transactions?  upstack rolls back?
181     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
182          join(' / ', @errors)
183       if @errors;
184   }
185   #eslaf
186
187   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188
189   '';
190
191 }
192
193 =item delete
194
195 Unless the closed flag is set, deletes this credit and all associated
196 applications (see L<FS::cust_credit_bill>).  In most cases, you want to use
197 the void method instead to leave a record of the deleted credit.
198
199 =cut
200
201 # very similar to FS::cust_pay::delete
202 sub delete {
203   my $self = shift;
204   return "Can't delete closed credit" if $self->closed =~ /^Y/i;
205
206   local $SIG{HUP} = 'IGNORE';
207   local $SIG{INT} = 'IGNORE';
208   local $SIG{QUIT} = 'IGNORE';
209   local $SIG{TERM} = 'IGNORE';
210   local $SIG{TSTP} = 'IGNORE';
211   local $SIG{PIPE} = 'IGNORE';
212
213   my $oldAutoCommit = $FS::UID::AutoCommit;
214   local $FS::UID::AutoCommit = 0;
215   my $dbh = dbh;
216
217   foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
218     my $error = $cust_credit_bill->delete;
219     if ( $error ) {
220       $dbh->rollback if $oldAutoCommit;
221       return $error;
222     }
223   }
224
225   foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
226     my $error = $cust_credit_refund->delete;
227     if ( $error ) {
228       $dbh->rollback if $oldAutoCommit;
229       return $error;
230     }
231   }
232
233   my $error = $self->SUPER::delete(@_);
234   if ( $error ) {
235     $dbh->rollback if $oldAutoCommit;
236     return $error;
237   }
238
239   if ( $conf->config('deletecredits') ne '' ) {
240
241     my $cust_main = $self->cust_main;
242
243     my $error = send_email(
244       'from'    => $conf->config('invoice_from', $self->cust_main->agentnum),
245                                  #invoice_from??? well as good as any
246       'to'      => $conf->config('deletecredits'),
247       'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
248       'body'    => [
249         "This is an automatic message from your Freeside installation\n",
250         "informing you that the following credit has been deleted:\n",
251         "\n",
252         'crednum: '. $self->crednum. "\n",
253         'custnum: '. $self->custnum.
254           " (". $cust_main->last. ", ". $cust_main->first. ")\n",
255         'amount: $'. sprintf("%.2f", $self->amount). "\n",
256         'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
257         'reason: '. $self->reason. "\n",
258       ],
259     );
260
261     if ( $error ) {
262       $dbh->rollback if $oldAutoCommit;
263       return "can't send credit deletion notification: $error";
264     }
265
266   }
267
268   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
269
270   '';
271
272 }
273
274 =item replace [ OLD_RECORD ]
275
276 You can, but probably shouldn't modify credits... 
277
278 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
279 supplied, replaces this record.  If there is an error, returns the error,
280 otherwise returns false.
281
282 =cut
283
284 sub replace {
285   my $self = shift;
286   return "Can't modify closed credit" if $self->closed =~ /^Y/i;
287   $self->SUPER::replace(@_);
288 }
289
290 =item check
291
292 Checks all fields to make sure this is a valid credit.  If there is an error,
293 returns the error, otherwise returns false.  Called by the insert and replace
294 methods.
295
296 =cut
297
298 sub check {
299   my $self = shift;
300
301   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
302
303   my $error =
304     $self->ut_numbern('crednum')
305     || $self->ut_number('custnum')
306     || $self->ut_numbern('_date')
307     || $self->ut_money('amount')
308     || $self->ut_alphan('otaker')
309     || $self->ut_textn('reason')
310     || $self->ut_textn('addlinfo')
311     || $self->ut_enum('closed', [ '', 'Y' ])
312     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
313     || $self->ut_foreign_keyn('eventnum', 'cust_event', 'eventnum')
314   ;
315   return $error if $error;
316
317   my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
318   $error = $self->$method('reasonnum', 'reason', 'reasonnum');
319   return $error if $error;
320
321   return "amount must be > 0 " if $self->amount <= 0;
322
323   return "amount must be greater or equal to amount applied"
324     if $self->unapplied < 0 && ! $otaker_upgrade_kludge;
325
326   return "Unknown customer"
327     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
328
329   $self->_date(time) unless $self->_date;
330
331   $self->SUPER::check;
332 }
333
334 =item cust_credit_refund
335
336 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
337
338 =cut
339
340 sub cust_credit_refund {
341   my $self = shift;
342   map { $_ } #return $self->num_cust_credit_refund unless wantarray;
343   sort { $a->_date <=> $b->_date }
344     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
345   ;
346 }
347
348 =item cust_credit_bill
349
350 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
351 credit.
352
353 =cut
354
355 sub cust_credit_bill {
356   my $self = shift;
357   map { $_ } #return $self->num_cust_credit_bill unless wantarray;
358   sort { $a->_date <=> $b->_date }
359     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
360   ;
361 }
362
363 =item unapplied
364
365 Returns the amount of this credit that is still unapplied/outstanding; 
366 amount minus all refund applications (see L<FS::cust_credit_refund>) and
367 applications to invoices (see L<FS::cust_credit_bill>).
368
369 =cut
370
371 sub unapplied {
372   my $self = shift;
373   my $amount = $self->amount;
374   $amount -= $_->amount foreach ( $self->cust_credit_refund );
375   $amount -= $_->amount foreach ( $self->cust_credit_bill );
376   sprintf( "%.2f", $amount );
377 }
378
379 =item credited
380
381 Deprecated name for the unapplied method.
382
383 =cut
384
385 sub credited {
386   my $self = shift;
387   #carp "cust_credit->credited deprecated; use ->unapplied";
388   $self->unapplied(@_);
389 }
390
391 =item cust_main
392
393 Returns the customer (see L<FS::cust_main>) for this credit.
394
395 =cut
396
397 sub cust_main {
398   my $self = shift;
399   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
400 }
401
402
403 =item reason
404
405 Returns the text of the associated reason (see L<FS::reason>) for this credit.
406
407 =cut
408
409 sub reason {
410   my ($self, $value, %options) = @_;
411   my $dbh = dbh;
412   my $reason;
413   my $typenum = $options{'reason_type'};
414
415   my $oldAutoCommit = $FS::UID::AutoCommit;  # this should already be in
416   local $FS::UID::AutoCommit = 0;            # a transaction if it matters
417
418   if ( defined( $value ) ) {
419     my $hashref = { 'reason' => $value };
420     $hashref->{'reason_type'} = $typenum if $typenum;
421     my $addl_from = "LEFT JOIN reason_type ON ( reason_type = typenum ) ";
422     my $extra_sql = " AND reason_type.class='R'"; 
423
424     $reason = qsearchs( { 'table'     => 'reason',
425                           'hashref'   => $hashref,
426                           'addl_from' => $addl_from,
427                           'extra_sql' => $extra_sql,
428                        } );
429
430     if (!$reason && $typenum) {
431       $reason = new FS::reason( { 'reason_type' => $typenum,
432                                   'reason' => $value,
433                                   'disabled' => 'Y', 
434                               } );
435       my $error = $reason->insert;
436       if ( $error ) {
437         warn "error inserting reason: $error\n";
438         $reason = undef;
439       }
440     }
441
442     $self->reasonnum($reason ? $reason->reasonnum : '') ;
443     warn "$me reason used in set mode with non-existant reason -- clearing"
444       unless $reason;
445   }
446   $reason = qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
447
448   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
449
450   ( $reason ? $reason->reason : '' ).
451   ( $self->addlinfo ? ' '.$self->addlinfo : '' );
452 }
453
454 # _upgrade_data
455 #
456 # Used by FS::Upgrade to migrate to a new database.
457
458 sub _upgrade_data {  # class method
459   my ($class, %opts) = @_;
460
461   warn "$me upgrading $class\n" if $DEBUG;
462
463   if (defined dbdef->table($class->table)->column('reason')) {
464
465     warn "$me Checking for unmigrated reasons\n" if $DEBUG;
466
467     my @cust_credits = qsearch({ 'table'     => $class->table,
468                                  'hashref'   => {},
469                                  'extra_sql' => 'WHERE reason IS NOT NULL',
470                               });
471
472     if (scalar(grep { $_->getfield('reason') =~ /\S/ } @cust_credits)) {
473       warn "$me Found unmigrated reasons\n" if $DEBUG;
474       my $hashref = { 'class' => 'R', 'type' => 'Legacy' };
475       my $reason_type = qsearchs( 'reason_type', $hashref );
476       unless ($reason_type) {
477         $reason_type  = new FS::reason_type( $hashref );
478         my $error   = $reason_type->insert();
479         die "$class had error inserting FS::reason_type into database: $error\n"
480           if $error;
481       }
482
483       $hashref = { 'reason_type' => $reason_type->typenum,
484                    'reason' => '(none)'
485                  };
486       my $noreason = qsearchs( 'reason', $hashref );
487       unless ($noreason) {
488         $hashref->{'disabled'} = 'Y';
489         $noreason = new FS::reason( $hashref );
490         my $error  = $noreason->insert();
491         die "can't insert legacy reason '(none)' into database: $error\n"
492           if $error;
493       }
494
495       foreach my $cust_credit ( @cust_credits ) {
496         my $reason = $cust_credit->getfield('reason');
497         warn "Contemplating reason $reason\n" if $DEBUG > 1;
498         if ($reason =~ /\S/) {
499           $cust_credit->reason($reason, 'reason_type' => $reason_type->typenum)
500             or die "can't insert legacy reason $reason into database\n";
501         }else{
502           $cust_credit->reasonnum($noreason->reasonnum);
503         }
504
505         $cust_credit->setfield('reason', '');
506         my $error = $cust_credit->replace;
507
508         warn "*** WARNING: error replacing reason in $class ".
509              $cust_credit->crednum. ": $error ***\n"
510           if $error;
511       }
512     }
513
514     warn "$me Ensuring existance of auto reasons\n" if $DEBUG;
515
516     foreach ( keys %reasontype_map ) {
517       unless ($conf->config($_)) {       # hmmmm
518 #       warn "$me Found $_ reason type lacking\n" if $DEBUG;
519 #       my $hashref = { 'class' => 'R', 'type' => $reasontype_map{$_} };
520         my $hashref = { 'class' => 'R', 'type' => 'Legacy' };
521         my $reason_type = qsearchs( 'reason_type', $hashref );
522         unless ($reason_type) {
523           $reason_type  = new FS::reason_type( $hashref );
524           my $error   = $reason_type->insert();
525           die "$class had error inserting FS::reason_type into database: $error\n"
526             if $error;
527         }
528         $conf->set($_, $reason_type->typenum);
529       }
530     }
531
532     warn "$me Ensuring commission packages have a reason type\n" if $DEBUG;
533
534     my $hashref = { 'class' => 'R', 'type' => 'Legacy' };
535     my $reason_type = qsearchs( 'reason_type', $hashref );
536     unless ($reason_type) {
537       $reason_type  = new FS::reason_type( $hashref );
538       my $error   = $reason_type->insert();
539       die "$class had error inserting FS::reason_type into database: $error\n"
540         if $error;
541     }
542
543     my @plans = qw( flat_comission flat_comission_cust flat_comission_pkg );
544     foreach my $plan ( @plans ) {
545       foreach my $pkg ( qsearch('part_pkg', { 'plan' => $plan } ) ) {
546         unless ($pkg->option('reason_type', 1) ) { 
547           my $plandata = $pkg->plandata.
548                         "reason_type=". $reason_type->typenum. "\n";
549           $pkg->plandata($plandata);
550           my $error =
551             $pkg->replace( undef,
552                            'pkg_svc' => { map { $_->svcpart => $_->quantity }
553                                           $pkg->pkg_svc
554                                         },
555                            'primary_svc' => $pkg->svcpart,
556                          );
557             die "failed setting reason_type option: $error"
558               if $error;
559         }
560       }
561     }
562   }
563
564   local($otaker_upgrade_kludge) = 1;
565   local($ignore_empty_reasonnum) = 1;
566   $class->_upgrade_otaker(%opts);
567
568 }
569
570 =back
571
572 =head1 CLASS METHODS
573
574 =over 4
575
576 =item unapplied_sql
577
578 Returns an SQL fragment to retreive the unapplied amount.
579
580 =cut
581
582 sub unapplied_sql {
583   my ($class, $start, $end) = @_;
584
585   my $bill_start   = $start ? "AND cust_credit_bill._date <= $start"   : '';
586   my $bill_end     = $end   ? "AND cust_credit_bill._date > $end"     : '';
587   my $refund_start = $start ? "AND cust_credit_refund._date <= $start" : '';
588   my $refund_end   = $end   ? "AND cust_credit_refund._date > $end"   : '';
589
590   "amount
591         - COALESCE(
592                     ( SELECT SUM(amount) FROM cust_credit_refund
593                         WHERE cust_credit.crednum = cust_credit_refund.crednum
594                         $refund_start $refund_end )
595                     ,0
596                   )
597         - COALESCE(
598                     ( SELECT SUM(amount) FROM cust_credit_bill
599                         WHERE cust_credit.crednum = cust_credit_bill.crednum
600                         $bill_start $bill_end )
601                     ,0
602                   )
603   ";
604
605 }
606
607 =item credited_sql
608
609 Deprecated name for the unapplied_sql method.
610
611 =cut
612
613 sub credited_sql {
614   #my $class = shift;
615
616   #carp "cust_credit->credited_sql deprecated; use ->unapplied_sql";
617
618   #$class->unapplied_sql(@_);
619   unapplied_sql();
620 }
621
622 =item credit_lineitems
623
624 Example:
625
626   my $error = FS::cust_credit->credit_lineitems(
627
628     #the lineitems to credit
629     'billpkgnums'       => \@billpkgnums,
630     'setuprecurs'       => \@setuprecurs,
631     'amounts'           => \@amounts,
632
633     #the credit
634     'newreasonnum'      => scalar($cgi->param('newreasonnum')),
635     'newreasonnum_type' => scalar($cgi->param('newreasonnumT')),
636     map { $_ => scalar($cgi->param($_)) }
637       #fields('cust_credit')  
638       qw( custnum _date amount reason reasonnum addlinfo ), #pkgnum eventnum
639
640   );
641
642 =cut
643
644 #maybe i should just be an insert with extra args instead of a class method
645 use FS::cust_bill_pkg;
646 sub credit_lineitems {
647   my( $class, %arg ) = @_;
648
649   my $curuser = $FS::CurrentUser::CurrentUser;
650
651   #some false laziness w/misc/xmlhttp-cust_bill_pkg-calculate_taxes.html
652
653   my $cust_main = qsearchs({
654     'table'     => 'cust_main',
655     'hashref'   => { 'custnum' => $arg{custnum} },
656     'extra_sql' => ' AND '. $curuser->agentnums_sql,
657   }) or return 'unknown customer';
658
659
660   local $SIG{HUP} = 'IGNORE';
661   local $SIG{INT} = 'IGNORE';
662   local $SIG{QUIT} = 'IGNORE';
663   local $SIG{TERM} = 'IGNORE';
664   local $SIG{TSTP} = 'IGNORE';
665   local $SIG{PIPE} = 'IGNORE';
666
667   my $oldAutoCommit = $FS::UID::AutoCommit;
668   local $FS::UID::AutoCommit = 0;
669   my $dbh = dbh;
670
671   #my @cust_bill_pkg = qsearch({
672   #  'select'    => 'cust_bill_pkg.*',
673   #  'table'     => 'cust_bill_pkg',
674   #  'addl_from' => ' LEFT JOIN cust_bill USING (invnum)  '.
675   #                 ' LEFT JOIN cust_main USING (custnum) ',
676   #  'extra_sql' => ' WHERE custnum = $custnum AND billpkgnum IN ('.
677   #                     join( ',', @{$arg{billpkgnums}} ). ')',
678   #  'order_by'  => 'ORDER BY invnum ASC, billpkgnum ASC',
679   #});
680
681   my $error = '';
682   if ($arg{reasonnum} == -1) {
683
684     $error = 'Enter a new reason (or select an existing one)'
685       unless $arg{newreasonnum} !~ /^\s*$/;
686     my $reason = new FS::reason {
687                    'reason'      => $arg{newreasonnum},
688                    'reason_type' => $arg{newreasonnum_type},
689                  };
690     $error ||= $reason->insert;
691     if ( $error ) {
692       $dbh->rollback if $oldAutoCommit;
693       return "Error inserting reason: $error";
694     }
695     $arg{reasonnum} = $reason->reasonnum;
696   }
697
698   my $cust_credit = new FS::cust_credit ( {
699     map { $_ => $arg{$_} }
700       #fields('cust_credit')
701       qw( custnum _date amount reason reasonnum addlinfo ), #pkgnum eventnum
702   } );
703   $error = $cust_credit->insert;
704   if ( $error ) {
705     $dbh->rollback if $oldAutoCommit;
706     return "Error inserting credit: $error";
707   }
708
709   #my $subtotal = 0;
710   my $taxlisthash = {};
711   my %cust_credit_bill = ();
712   my %cust_bill_pkg = ();
713   my %cust_credit_bill_pkg = ();
714   foreach my $billpkgnum ( @{$arg{billpkgnums}} ) {
715     my $setuprecur = shift @{$arg{setuprecurs}};
716     my $amount = shift @{$arg{amounts}};
717
718     my $cust_bill_pkg = qsearchs({
719       'table'     => 'cust_bill_pkg',
720       'hashref'   => { 'billpkgnum' => $billpkgnum },
721       'addl_from' => 'LEFT JOIN cust_bill USING (invnum)',
722       'extra_sql' => 'AND custnum = '. $cust_main->custnum,
723     }) or die "unknown billpkgnum $billpkgnum";
724
725     if ( $setuprecur eq 'setup' ) {
726       $cust_bill_pkg->setup($amount);
727       $cust_bill_pkg->recur(0);
728       $cust_bill_pkg->unitrecur(0);
729       $cust_bill_pkg->type('');
730     } else {
731       $setuprecur = 'recur'; #in case its a usage classnum?
732       $cust_bill_pkg->recur($amount);
733       $cust_bill_pkg->setup(0);
734       $cust_bill_pkg->unitsetup(0);
735     }
736
737     push @{$cust_bill_pkg{$cust_bill_pkg->invnum}}, $cust_bill_pkg;
738
739     #unapply any payments applied to this line item (other credits too?)
740     foreach my $cust_bill_pay_pkg ( $cust_bill_pkg->cust_bill_pay_pkg($setuprecur) ) {
741       $error = $cust_bill_pay_pkg->delete;
742       if ( $error ) {
743         $dbh->rollback if $oldAutoCommit;
744         return "Error unapplying payment: $error";
745       }
746     }
747
748     #$subtotal += $amount;
749     $cust_credit_bill{$cust_bill_pkg->invnum} += $amount;
750     push @{ $cust_credit_bill_pkg{$cust_bill_pkg->invnum} },
751       new FS::cust_credit_bill_pkg {
752         'billpkgnum' => $cust_bill_pkg->billpkgnum,
753         'amount'     => $amount,
754         'setuprecur' => $setuprecur,
755         'sdate'      => $cust_bill_pkg->sdate,
756         'edate'      => $cust_bill_pkg->edate,
757       };
758
759     my $part_pkg = $cust_bill_pkg->part_pkg;
760     $cust_main->_handle_taxes( $part_pkg,
761                                $taxlisthash,
762                                $cust_bill_pkg,
763                                $cust_bill_pkg->cust_pkg,
764                                $cust_bill_pkg->cust_bill->_date,
765                                $cust_bill_pkg->cust_pkg->pkgpart,
766                              );
767   }
768
769   ###
770   # now loop through %cust_credit_bill and insert those
771   ###
772
773   # (hack to prevent cust_credit_bill_pkg insertion)
774   local($FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack) = 1;
775
776   foreach my $invnum ( sort { $a <=> $b } keys %cust_credit_bill ) {
777
778     #taxes
779
780     if ( @{ $cust_bill_pkg{$invnum} } ) {
781
782       my $listref_or_error = 
783         $cust_main->calculate_taxes( $cust_bill_pkg{$invnum}, $taxlisthash, $cust_bill_pkg{$invnum}->[0]->cust_bill->_date );
784
785       unless ( ref( $listref_or_error ) ) {
786         $dbh->rollback if $oldAutoCommit;
787         return "Error calculating taxes: $listref_or_error";
788       }
789
790       # so, loop through the taxlines, apply just that amount to the tax line
791       #  item (save for later insert) & add to $
792
793       #my @taxlines = ();
794       #my $taxtotal = 0;
795       foreach my $taxline ( @$listref_or_error ) {
796
797         #find equivalent tax line items on the existing invoice
798         # (XXX need a more specific/deterministic way to find these than itemdesc..)
799         my $tax_cust_bill_pkg = qsearchs('cust_bill_pkg', {
800           'invnum'   => $invnum,
801           'pkgnum'   => 0, #$taxline->invnum
802           'itemdesc' => $taxline->desc,
803         });
804
805         my $amount = $taxline->setup;
806         my $desc = $taxline->desc;
807
808         foreach my $location ( $tax_cust_bill_pkg->cust_bill_pkg_tax_Xlocation ) {
809
810           #support partial credits: use $amount if smaller
811           # (so just distribute to the first location?   perhaps should
812           #  do so evenly...)
813           my $loc_amount = min( $amount, $location->amount);
814
815           $amount -= $loc_amount;
816
817           #push @taxlines,
818           #  #[ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
819           #  [ $location->desc, $location->amount, $taxlocnum, $taxratelocnum ];
820           $cust_credit_bill{$invnum} += $loc_amount;
821           push @{ $cust_credit_bill_pkg{$invnum} },
822             new FS::cust_credit_bill_pkg {
823               'billpkgnum'                => $tax_cust_bill_pkg->billpkgnum,
824               'amount'                    => $loc_amount,
825               'setuprecur'                => 'setup',
826               'billpkgtaxlocationnum'     => $location->billpkgtaxlocationnum,
827               'billpkgtaxratelocationnum' => $location->billpkgtaxratelocationnum,
828             };
829
830         }
831         if ($amount > 0) {
832           #$taxtotal += $amount;
833           #push @taxlines,
834           #  [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
835
836           $cust_credit_bill{$invnum} += $amount;
837           push @{ $cust_credit_bill_pkg{$invnum} },
838             new FS::cust_credit_bill_pkg {
839               'billpkgnum' => $tax_cust_bill_pkg->billpkgnum,
840               'amount'     => $amount,
841               'setuprecur' => 'setup',
842             };
843
844         }
845       }
846
847     }
848
849     #insert cust_credit_bill
850
851     my $cust_credit_bill = new FS::cust_credit_bill {
852       'crednum' => $cust_credit->crednum,
853       'invnum'  => $invnum,
854       'amount'  => $cust_credit_bill{$invnum},
855     };
856     $error = $cust_credit_bill->insert;
857     if ( $error ) {
858       $dbh->rollback if $oldAutoCommit;
859       return "Error applying credit of $cust_credit_bill{$invnum} ".
860              " to invoice $invnum: $error";
861     }
862
863     #and then insert cust_credit_bill_pkg for each cust_bill_pkg
864     foreach my $cust_credit_bill_pkg ( @{$cust_credit_bill_pkg{$invnum}} ) {
865       $cust_credit_bill_pkg->creditbillnum( $cust_credit_bill->creditbillnum );
866       $error = $cust_credit_bill_pkg->insert;
867       if ( $error ) {
868         $dbh->rollback if $oldAutoCommit;
869         return "Error applying credit to line item: $error";
870       }
871     }
872
873   }
874
875   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
876   '';
877
878 }
879
880 =back
881
882 =head1 BUGS
883
884 The delete method.  The replace method.
885
886 B<credited> and B<credited_sql> are now called B<unapplied> and
887 B<unapplied_sql>.  The old method names should start to give warnings.
888
889 =head1 SEE ALSO
890
891 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
892 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
893 documentation.
894
895 =cut
896
897 1;
898