add omit_countrycode option, RT#7379
[freeside.git] / FS / FS / cust_bill_ApplicationCommon.pm
index 30243e2..4e221dc 100644 (file)
@@ -5,6 +5,11 @@ use vars qw( @ISA $DEBUG $me $skip_apply_to_lineitems_hack );
 use List::Util qw(min);
 use FS::Schema qw( dbdef );
 use FS::Record qw( qsearch qsearchs dbh );
+use FS::cust_pkg;
+use FS::cust_svc;
+use FS::cust_bill_pkg;
+use FS::part_svc;
+use FS::part_export;
 
 @ISA = qw( FS::Record );
 
@@ -154,7 +159,10 @@ sub calculate_applications {
        $self->cust_bill->invnum. ": ". join(', ', @open). "\n"
     if $DEBUG;
   my $total = 0;
-  $total += $_->owed_setup + $_->owed_recur foreach @open;
+  foreach (@open) {
+    $total += $_->owed_setup if $_->setup;
+    $total += $_->owed_recur if $_->recur;
+  }
   $total = sprintf('%.2f', $total);
 
   if ( $self->amount > $total ) {
@@ -172,7 +180,8 @@ sub calculate_applications {
       if $DEBUG;
 
     #@apply = map { [ $_, $_->amount ]; } @open;
-    @apply = map { [ $_, $_->owed_setup + 0 || $_->owed_recur + 0 ]; } @open;
+    #@apply = map { [ $_, $_->owed_setup + 0 || $_->owed_recur + 0 ]; } @open;
+    @apply = map { [ $_, $_->setup ? $_->owed_setup : $_->owed_recur ]; } @open;
 
   } else {
 
@@ -180,8 +189,8 @@ sub calculate_applications {
     # - amount exactly and uniquely matches a single open lineitem
     #   (you must be trying to pay or credit that item, then)
 
-    my @same = grep {    $_->owed_setup == $self->amount
-                      || $_->owed_recur == $self->amount
+    my @same = grep {    ( $_->setup && $_->owed_setup == $self->amount )
+                      || ( $_->recur && $_->owed_recur == $self->amount )
                     }
                     @open;
     if ( scalar(@same) == 1 ) {
@@ -226,7 +235,10 @@ sub calculate_applications {
       my @items = map { $_->[0] } grep { $weight == $_->[1] } @openweight;
 
       my $itemtotal = 0;
-      foreach my $item (@items) { $itemtotal += $item->owed_setup + 0 || $item->owed_recur + 0; }
+      foreach my $item (@items) {
+        $itemtotal += $item->owed_setup if $item->setup;
+        $itemtotal += $item->owed_recur if $item->recur;
+      }
       my $applytotal = min( $itemtotal, $remaining_amount );
       $remaining_amount -= $applytotal;
 
@@ -247,7 +259,7 @@ sub calculate_applications {
 
        my @newitems = ();
        foreach my $item ( @items ) {
-         my $itemamount = $item->owed_setup + 0 || $item->owed_recur + 0;
+         my $itemamount = $item->setup ? $item->owed_setup : $item->owed_recur;
           if ( $itemamount < $applyeach ) {
            warn "$me applying full $itemamount".
                 " to small line item (cust_bill_pkg ". $item->billpkgnum. ")\n"
@@ -353,8 +365,6 @@ sub apply_to_lineitems {
 
   return '' if $skip_apply_to_lineitems_hack;
 
-
-
   my $conf = new FS::Conf;
 
   local $SIG{HUP} = 'IGNORE';
@@ -397,6 +407,30 @@ sub apply_to_lineitems {
       $dbh->rollback if $oldAutoCommit;
       return $error;
     }
+
+    # trigger export_insert_on_payment
+    if ( $conf->exists('trigger_export_insert_on_payment')
+      && $cust_bill_pkg->pkgnum > 0 )
+    {
+      if ( my $cust_pkg = $cust_bill_pkg->cust_pkg ) {
+
+        foreach my $cust_svc ( $cust_pkg->cust_svc ) {
+          my $svc_x = $cust_svc->svc_x;
+          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 ) {
+            $error = $part_export->_export_insert_on_payment($svc_x);
+            if ( $error ) {
+              $dbh->rollback if $oldAutoCommit;
+              return $error;
+            }
+          }
+        }
+      }
+    }
+    # done trigger export_insert_on_payment
+
   }
 
   #everything should always be applied to line items in full now... sanity check