Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 );
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     'apply'             => 1, #0 leaves the credit unapplied
633
634     #the credit
635     'newreasonnum'      => scalar($cgi->param('newreasonnum')),
636     'newreasonnum_type' => scalar($cgi->param('newreasonnumT')),
637     map { $_ => scalar($cgi->param($_)) }
638       #fields('cust_credit')  
639       qw( custnum _date amount reason reasonnum addlinfo ), #pkgnum eventnum
640
641   );
642
643 =cut
644
645 #maybe i should just be an insert with extra args instead of a class method
646 use FS::cust_bill_pkg;
647 sub credit_lineitems {
648   my( $class, %arg ) = @_;
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   unless ( $arg{'apply'} ) {
710     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
711     return '';
712   }
713
714   #my $subtotal = 0;
715   # keys in all of these are invoice numbers
716   my %cust_credit_bill = ();
717   my %cust_bill_pkg = ();
718   my %cust_credit_bill_pkg = ();
719   my %taxlisthash = ();
720   my %unapplied_payments = (); #invoice numbers, and then billpaynums
721   foreach my $billpkgnum ( @{$arg{billpkgnums}} ) {
722     my $setuprecur = shift @{$arg{setuprecurs}};
723     my $amount = shift @{$arg{amounts}};
724
725     my $cust_bill_pkg = qsearchs({
726       'table'     => 'cust_bill_pkg',
727       'hashref'   => { 'billpkgnum' => $billpkgnum },
728       'addl_from' => 'LEFT JOIN cust_bill USING (invnum)',
729       'extra_sql' => 'AND custnum = '. $cust_main->custnum,
730     }) or die "unknown billpkgnum $billpkgnum";
731   
732     my $invnum = $cust_bill_pkg->invnum;
733
734     if ( $setuprecur eq 'setup' ) {
735       $cust_bill_pkg->setup($amount);
736       $cust_bill_pkg->recur(0);
737       $cust_bill_pkg->unitrecur(0);
738       $cust_bill_pkg->type('');
739     } else {
740       $setuprecur = 'recur'; #in case its a usage classnum?
741       $cust_bill_pkg->recur($amount);
742       $cust_bill_pkg->setup(0);
743       $cust_bill_pkg->unitsetup(0);
744     }
745
746     push @{$cust_bill_pkg{$invnum}}, $cust_bill_pkg;
747
748     #unapply any payments applied to this line item (other credits too?)
749     foreach my $cust_bill_pay_pkg ( $cust_bill_pkg->cust_bill_pay_pkg($setuprecur) ) {
750       $error = $cust_bill_pay_pkg->delete;
751       if ( $error ) {
752         $dbh->rollback if $oldAutoCommit;
753         return "Error unapplying payment: $error";
754       }
755       $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum}
756         += $cust_bill_pay_pkg->amount;
757     }
758
759     #$subtotal += $amount;
760     $cust_credit_bill{$invnum} += $amount;
761     push @{ $cust_credit_bill_pkg{$invnum} },
762       new FS::cust_credit_bill_pkg {
763         'billpkgnum' => $cust_bill_pkg->billpkgnum,
764         'amount'     => sprintf('%.2f',$amount),
765         'setuprecur' => $setuprecur,
766         'sdate'      => $cust_bill_pkg->sdate,
767         'edate'      => $cust_bill_pkg->edate,
768       };
769
770     # recalculate taxes with new amounts
771     $taxlisthash{$invnum} ||= {};
772     my $part_pkg = $cust_bill_pkg->part_pkg;
773     $cust_main->_handle_taxes( $part_pkg,
774                                $taxlisthash{$invnum},
775                                $cust_bill_pkg,
776                                $cust_bill_pkg->cust_pkg,
777                                $cust_bill_pkg->cust_bill->_date, #invoice time
778                                $cust_bill_pkg->cust_pkg->pkgpart,
779                              );
780   }
781
782   ###
783   # now loop through %cust_credit_bill and insert those
784   ###
785
786   # (hack to prevent cust_credit_bill_pkg insertion)
787   local($FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack) = 1;
788
789   foreach my $invnum ( sort { $a <=> $b } keys %cust_credit_bill ) {
790
791     my $arrayref_or_error =
792       $cust_main->calculate_taxes(
793         $cust_bill_pkg{$invnum}, # list of taxable items that we're crediting
794         $taxlisthash{$invnum},   # list of tax-item bindings
795         $cust_bill_pkg{$invnum}->[0]->cust_bill->_date, # invoice time
796       );
797
798     unless ( ref( $arrayref_or_error ) ) {
799       $dbh->rollback if $oldAutoCommit;
800       return "Error calculating taxes: $arrayref_or_error";
801     }
802     
803     my %tax_links; # {tax billpkgnum}{nontax billpkgnum}
804
805     #taxes
806     foreach my $cust_bill_pkg ( @{ $cust_bill_pkg{$invnum} } ) {
807       my $billpkgnum = $cust_bill_pkg->billpkgnum;
808       my %hash = ( 'taxable_billpkgnum' => $billpkgnum );
809       # gather up existing tax links (we need their billpkgtaxlocationnums)
810       my @tax_links = qsearch('cust_bill_pkg_tax_location', \%hash),
811                       qsearch('cust_bill_pkg_tax_rate_location', \%hash);
812
813       foreach ( @tax_links ) {
814         $tax_links{$_->billpkgnum} ||= {};
815         $tax_links{$_->billpkgnum}{$_->taxable_billpkgnum} = $_;
816       }
817     }
818
819     foreach my $taxline ( @$arrayref_or_error ) {
820
821       my $amount = $taxline->setup;
822
823       # find equivalent tax line item on the existing invoice
824       my $tax_item = qsearchs('cust_bill_pkg', {
825           'invnum'    => $invnum,
826           'pkgnum'    => 0,
827           'itemdesc'  => $taxline->desc,
828       });
829       if (!$tax_item) {
830         # or should we just exit if this happens?
831         $cust_credit->set('amount', 
832           sprintf('%.2f', $cust_credit->get('amount') - $amount)
833         );
834         my $error = $cust_credit->replace;
835         if ( $error ) {
836           $dbh->rollback if $oldAutoCommit;
837           return "error correcting credit for missing tax line: $error";
838         }
839       }
840
841       # but in the new era, we no longer have the problem of uniquely
842       # identifying the tax_Xlocation record.  The billpkgnums of the 
843       # tax and the taxed item are known.
844       foreach my $new_loc
845         ( @{ $taxline->get('cust_bill_pkg_tax_location') },
846           @{ $taxline->get('cust_bill_pkg_tax_rate_location') } )
847       {
848         # the existing tax_Xlocation object
849         my $old_loc =
850           $tax_links{$tax_item->billpkgnum}{$new_loc->taxable_billpkgnum};
851
852         next if !$old_loc; # apply the leftover amount nonspecifically
853
854         #support partial credits: use $amount if smaller
855         # (so just distribute to the first location?   perhaps should
856         #  do so evenly...)
857         my $loc_amount = min( $amount, $new_loc->amount);
858
859         $amount -= $loc_amount;
860
861         $cust_credit_bill{$invnum} += $loc_amount;
862         push @{ $cust_credit_bill_pkg{$invnum} },
863           new FS::cust_credit_bill_pkg {
864             'billpkgnum'                => $tax_item->billpkgnum,
865             'amount'                    => $loc_amount,
866             'setuprecur'                => 'setup',
867             'billpkgtaxlocationnum'     => $old_loc->billpkgtaxlocationnum,
868             'billpkgtaxratelocationnum' => $old_loc->billpkgtaxratelocationnum,
869           };
870
871       } #foreach my $new_loc
872
873       # we still have to deal with the possibility that the tax links don't
874       # cover the whole amount of tax because of an incomplete upgrade...
875       if ($amount > 0) {
876         $cust_credit_bill{$invnum} += $amount;
877         push @{ $cust_credit_bill_pkg{$invnum} },
878           new FS::cust_credit_bill_pkg {
879             'billpkgnum' => $tax_item->billpkgnum,
880             'amount'     => $amount,
881             'setuprecur' => 'setup',
882           };
883
884       } # if $amount > 0
885
886       #unapply any payments applied to the tax
887       foreach my $cust_bill_pay_pkg
888         ( $tax_item->cust_bill_pay_pkg('setup') )
889       {
890         $error = $cust_bill_pay_pkg->delete;
891         if ( $error ) {
892           $dbh->rollback if $oldAutoCommit;
893           return "Error unapplying payment: $error";
894         }
895         $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum}
896           += $cust_bill_pay_pkg->amount;
897       }
898     } #foreach $taxline
899
900     # if we unapplied any payments from line items, also unapply that 
901     # amount from the invoice
902     foreach my $billpaynum (keys %{$unapplied_payments{$invnum}}) {
903       my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum)
904         or die "broken payment application $billpaynum";
905       my @subapps = $cust_bill_pay->lineitem_applications;
906       $error = $cust_bill_pay->delete; # can't replace
907
908       my $new_cust_bill_pay = FS::cust_bill_pay->new({
909           $cust_bill_pay->hash,
910           billpaynum => '',
911           amount => sprintf('%.2f', 
912               $cust_bill_pay->amount 
913               - $unapplied_payments{$invnum}{$billpaynum}),
914       });
915
916       if ( $new_cust_bill_pay->amount > 0 ) {
917         $error ||= $new_cust_bill_pay->insert;
918         # Also reapply it to everything it was applied to before.
919         # Note that we've already deleted cust_bill_pay_pkg records for the
920         # items we're crediting, so they aren't on this list.
921         foreach my $cust_bill_pay_pkg (@subapps) {
922           $cust_bill_pay_pkg->billpaypkgnum('');
923           $cust_bill_pay_pkg->billpaynum($new_cust_bill_pay->billpaynum);
924           $error ||= $cust_bill_pay_pkg->insert;
925         }
926       }
927       if ( $error ) {
928         $dbh->rollback if $oldAutoCommit;
929         return "Error unapplying payment: $error";
930       }
931     }
932     #insert cust_credit_bill
933
934     my $cust_credit_bill = new FS::cust_credit_bill {
935       'crednum' => $cust_credit->crednum,
936       'invnum'  => $invnum,
937       'amount'  => sprintf('%.2f', $cust_credit_bill{$invnum}),
938     };
939     $error = $cust_credit_bill->insert;
940     if ( $error ) {
941       $dbh->rollback if $oldAutoCommit;
942       return "Error applying credit of $cust_credit_bill{$invnum} ".
943              " to invoice $invnum: $error";
944     }
945
946     #and then insert cust_credit_bill_pkg for each cust_bill_pkg
947     foreach my $cust_credit_bill_pkg ( @{$cust_credit_bill_pkg{$invnum}} ) {
948       $cust_credit_bill_pkg->creditbillnum( $cust_credit_bill->creditbillnum );
949       $error = $cust_credit_bill_pkg->insert;
950       if ( $error ) {
951         $dbh->rollback if $oldAutoCommit;
952         return "Error applying credit to line item: $error";
953       }
954     }
955
956   }
957
958   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
959   '';
960
961 }
962
963 =back
964
965 =head1 BUGS
966
967 The delete method.  The replace method.
968
969 B<credited> and B<credited_sql> are now called B<unapplied> and
970 B<unapplied_sql>.  The old method names should start to give warnings.
971
972 =head1 SEE ALSO
973
974 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
975 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
976 documentation.
977
978 =cut
979
980 1;
981