enable CardFortress in test database, #71513
[freeside.git] / FS / FS / cust_bill_ApplicationCommon.pm
index 322728f..75d2647 100644 (file)
@@ -1,9 +1,11 @@
 package FS::cust_bill_ApplicationCommon;
 
 use strict;
-use vars qw( @ISA $DEBUG $me $skip_apply_to_lineitems_hack );
+use vars qw( @ISA $DEBUG $me $skip_apply_to_lineitems_hack $date_format );
 use List::Util qw(min);
+use Date::Format;
 use FS::Schema qw( dbdef );
+use FS::UID;
 use FS::Record qw( qsearch qsearchs dbh );
 use FS::cust_pkg;
 use FS::cust_svc;
@@ -18,6 +20,11 @@ $me = '[FS::cust_bill_ApplicationCommon]';
 
 $skip_apply_to_lineitems_hack = 0;
 
+FS::UID->install_callback( sub { 
+  my $conf = new FS::Conf;
+  $date_format = $conf->config('date_format') || '%x'; #/YY
+} );
+
 =head1 NAME
 
 FS::cust_bill_ApplicationCommon - Base class for bill application classes
@@ -337,6 +344,7 @@ sub calculate_applications {
   # could expand @open above, instead, for a slightly different magic effect
   my @result = ();
   foreach my $apply ( @apply ) {
+    # $apply = [ FS::cust_bill_pkg_tax_location record, amount ]
     my @sub_lines = $apply->[0]->cust_bill_pkg_tax_Xlocation;
     my $amount = $apply->[1];
     warn "applying ". $apply->[1]. " to ". $apply->[0]->desc
@@ -346,6 +354,10 @@ sub calculate_applications {
       my $owed = $subline->owed;
       push @result, [ $apply->[0],
                       sprintf('%.2f', min($amount, $owed) ),
+                      # $subline->primary_key is "billpkgtaxlocationnum"
+                      # or "billpkgtaxratelocationnum"
+                      # This is the ONLY place either of those fields will 
+                      # be set.
                       { $subline->primary_key => $subline->get($subline->primary_key) },
                     ];
       $amount -= $owed;
@@ -418,10 +430,10 @@ sub apply_to_lineitems {
 
         foreach my $cust_svc ( $cust_pkg->cust_svc ) {
           my $svc_x = $cust_svc->svc_x;
-          my @part_export = grep { $_->can('export_insert_on_payment') }
+          my @part_export = grep { $_->can('_export_insert_on_payment') }
                                  $cust_svc->part_svc->part_export;
       
-          foreach my $part_export ( $cust_svc->part_svc->part_export ) {
+          foreach my $part_export ( @part_export ) {
             $error = $part_export->_export_insert_on_payment($svc_x);
             if ( $error ) {
               $dbh->rollback if $oldAutoCommit;
@@ -435,6 +447,17 @@ sub apply_to_lineitems {
 
   }
 
+  # unset promised payment date if there is one
+  my $cust_bill = $self->cust_bill;
+  if ( $cust_bill->promised_date and $cust_bill->owed <= 0 ) {
+    $cust_bill->set('promised_date', '');
+    my $error = $cust_bill->replace;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+  
   #everything should always be applied to line items in full now... sanity check
   $applied = sprintf('%.2f', $applied);
   unless ( $applied == $self->amount ) {
@@ -480,11 +503,40 @@ sub cust_bill {
 Returns a string representing the invoice (see L<FS::cust_bill>), for example:
 "applied to Invoice #54 (3/20/2008)"
 
+Intended for back-end context, with regard to translation and date formatting.
+
 =cut
 
 sub applied_to_invoice {
   my $self = shift;
-  'applied to '. $self->cust_bill->invnum_date_pretty;
+  my $string = 'applied to '. $self->cust_bill->invnum_date_pretty;
+
+  #show application date if over 24 hours after (or before) payment/credit date
+  $string .= ' on '. $self->_date_pretty
+    if abs( $self->_date - $self->_app_source_object->_date ) > 86400;
+
+  $string;
+}
+
+=item _app_source_object 
+
+=cut
+
+sub _app_source_object {
+  my $self = shift;
+  my $source_table = $self->_app_source_table;
+  $self->$source_table();
+}
+
+=item _date_pretty
+
+Returns a string with the application date, for example: "3/20/2008"
+
+=cut
+
+sub _date_pretty {
+  my $self = shift;
+  time2str($date_format, $self->_date);
 }
 
 =item lineitem_breakdown_table