Merge branch 'master' of git.freeside.biz:/home/git/freeside
authorIvan Kohler <ivan@freeside.biz>
Sat, 24 Mar 2012 23:53:33 +0000 (16:53 -0700)
committerIvan Kohler <ivan@freeside.biz>
Sat, 24 Mar 2012 23:53:33 +0000 (16:53 -0700)
FS/FS/part_pkg/discount_Mixin.pm
FS/FS/part_pkg/prorate_Mixin.pm
FS/FS/svc_broadband.pm

index 335ff6d..1edf258 100644 (file)
@@ -84,7 +84,9 @@ sub calc_discount {
     $amount += $discount->amount
         if $cust_pkg->pkgpart == $param->{'real_pkgpart'};
     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
-    my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
+    my $chg_months = defined($param->{'months'}) ?
+                      $param->{'months'} :
+                      $cust_pkg->part_pkg->freq;
 
     my $months = $discount->months
     ? min( $chg_months,
index 0f40576..a01b5c4 100644 (file)
@@ -2,7 +2,7 @@ package FS::part_pkg::prorate_Mixin;
 
 use strict;
 use vars qw( %info );
-use Time::Local qw( timelocal );
+use Time::Local qw( timelocal timelocal_nocheck );
 use Date::Format qw( time2str );
 
 %info = ( 
@@ -202,28 +202,17 @@ sub _endpoints {
   }
   my $mend;
   my $mstart;
-  # if cutoff day > 28, force it to the 1st of next month
-  if ( $cutoff_day > 28 ) {
-    $cutoff_day = 1;
-    # and if we are currently after the 28th, roll the current day 
-    # forward to that day
-    if ( $mday > 28 ) {
-      $mday = 1;
-      #set $mnow = $mend so the amount billed will be zero
-      $mnow = timelocal(0,0,0,1,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
-    }
-  }
   if ( $mday >= $cutoff_day ) {
     $mend = 
-      timelocal(0,0,0,$cutoff_day,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
+      timelocal_nocheck(0,0,0,$cutoff_day,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
     $mstart =
-      timelocal(0,0,0,$cutoff_day,$mon,$year);
+      timelocal_nocheck(0,0,0,$cutoff_day,$mon,$year);
   }
   else {
     $mend = 
-      timelocal(0,0,0,$cutoff_day,$mon,$year);
+      timelocal_nocheck(0,0,0,$cutoff_day,$mon,$year);
     $mstart = 
-      timelocal(0,0,0,$cutoff_day,$mon == 0 ? 11 : $mon - 1,$year-($mon==0));
+      timelocal_nocheck(0,0,0,$cutoff_day,$mon == 0 ? 11 : $mon - 1,$year-($mon==0));
   }
   return ($mnow, $mend, $mstart);
 }
index 06a3001..2b6be2c 100755 (executable)
@@ -464,7 +464,7 @@ sub assign_ip_addr {
   my @blocks;
   my $ip_addr;
 
-  if ( $self->blocknum and $self->addr_block->routernum == $self->routernum ) {
+  if ( $self->addr_block and $self->addr_block->routernum == $self->routernum ) {
     # simple case: user chose a block, find an address in that block
     # (this overrides an existing IP address if it's not in the block)
     @blocks = ($self->addr_block);
@@ -483,15 +483,13 @@ sub assign_ip_addr {
       return '';
     }
     $ip_addr = $block->next_free_addr;
-    last if $ip_addr;
-  }
-  if ( $ip_addr ) {
-    $self->set(ip_addr => $ip_addr->addr);
-    return '';
-  }
-  else {
-    return 'No IP address available on this router';
+    if ( $ip_addr ) {
+      $self->set(ip_addr => $ip_addr->addr);
+      $self->set(blocknum => $block->blocknum);
+      return '';
+    }
   }
+  return 'No IP address available on this router';
 }
 
 =item assign_router
@@ -668,6 +666,38 @@ sub _upgrade_data {
         ": no routernum in address block ".$addr_block->cidr.", skipped\n";
     }
   }
+
+  # assign blocknums to services that should have them
+  my @all_blocks = qsearch('addr_block', { });
+  SVC: foreach my $self ( 
+    qsearch({
+        'select' => 'svc_broadband.*',
+        'table' => 'svc_broadband',
+        'addl_from' => 'JOIN router USING (routernum)',
+        'hashref' => {},
+        'extra_sql' => 'WHERE svc_broadband.blocknum IS NULL '.
+                       'AND router.manual_addr IS NULL',
+    }) 
+  ) {
+   
+    next SVC if $self->ip_addr eq '';
+    my $NetAddr = $self->NetAddr;
+    # inefficient, but should only need to run once
+    foreach my $block (@all_blocks) {
+      if ($block->NetAddr->contains($NetAddr)) {
+        $self->set(blocknum => $block->blocknum);
+        my $error = $self->replace;
+        warn "WARNING: error assigning blocknum ".$block->blocknum.
+        " to service ".$self->svcnum."\n$error; skipped\n"
+          if $error;
+        next SVC;
+      }
+    }
+    warn "WARNING: no block found containing ".$NetAddr->addr." for service ".
+      $self->svcnum;
+    #next SVC;
+  }
+
   '';
 }