[freeside-commits] branch master updated. d16673493b44b0ee6781096938baed32e8012a99

Mark Wells mark at 420.am
Mon May 30 10:09:42 PDT 2016


The branch, master has been updated
       via  d16673493b44b0ee6781096938baed32e8012a99 (commit)
       via  bc62f937601d4e453730cf321f8f507ebd68cb36 (commit)
       via  ee6aef9f16f3c9b766ff41e5dc780d034858bb29 (commit)
      from  af3030ce8b5be0c280f39eb7ffb8bebcb7953f56 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit d16673493b44b0ee6781096938baed32e8012a99
Author: Mark Wells <mark at freeside.biz>
Date:   Mon May 30 10:08:19 2016 -0700

    handle attempts to manually change the first bill date for prorate-deferred packages, #29791

diff --git a/FS/FS/part_pkg/prorate_Mixin.pm b/FS/FS/part_pkg/prorate_Mixin.pm
index 26fdc35..beae6d8 100644
--- a/FS/FS/part_pkg/prorate_Mixin.pm
+++ b/FS/FS/part_pkg/prorate_Mixin.pm
@@ -191,22 +191,35 @@ sub prorate_setup {
   my $self = shift;
   my ($cust_pkg, $sdate) = @_;
   my @cutoff_days = $self->cutoff_day($cust_pkg);
-  if ( ! $cust_pkg->bill
-      and $self->option('prorate_defer_bill',1)
-      and @cutoff_days
-  ) {
-    my ($mnow, $mend, $mstart) = $self->_endpoints($sdate, @cutoff_days);
-    # If today is the cutoff day, set the next bill and setup both to 
-    # midnight today, so that the customer will be billed normally for a 
-    # month starting today.
-    if ( $mnow - $mstart < 86400 ) {
-      $cust_pkg->setup($mstart);
-      $cust_pkg->bill($mstart);
+  if ( @cutoff_days and $self->option('prorate_defer_bill', 1) ) {
+    if ( $cust_pkg->setup ) {
+      # Setup date is already set. Then we're being called indirectly via calc_prorate
+      # to calculate the deferred setup fee. Allow that to happen normally.
+      return 0;
+    } else {
+      # We're going to set the setup date (so that the deferred billing knows when
+      # the package started) and suppress charging the setup fee.
+      if ( $cust_pkg->bill ) {
+        # For some reason (probably user override), the bill date has been set even
+        # though the package isn't billing yet. Start billing as though that was the
+        # start date.
+        $sdate = $cust_pkg->bill;
+        $cust_pkg->setup($cust_pkg->bill);
+      }
+      # Now figure the start and end of the period that contains the start date.
+      my ($mnow, $mend, $mstart) = $self->_endpoints($sdate, @cutoff_days);
+      # If today is the cutoff day, set the next bill and setup both to 
+      # midnight today, so that the customer will be billed normally for a 
+      # month starting today.
+      if ( $mnow - $mstart < 86400 ) {
+        $cust_pkg->setup($mstart);
+        $cust_pkg->bill($mstart);
+      }
+      else {
+        $cust_pkg->bill($mend);
+      }
+      return 1;
     }
-    else {
-      $cust_pkg->bill($mend);
-    }
-    return 1;
   }
   return 0;
 }

commit bc62f937601d4e453730cf321f8f507ebd68cb36
Author: Mark Wells <mark at freeside.biz>
Date:   Mon May 30 10:06:57 2016 -0700

    add convenience method for tests to create a new customer

diff --git a/FS/FS/Test.pm b/FS/FS/Test.pm
index 9854b94..1f2b44b 100644
--- a/FS/FS/Test.pm
+++ b/FS/FS/Test.pm
@@ -235,4 +235,35 @@ sub qsearchs {
   FS::Record::qsearchs(@_);
 }
 
+=item new_customer FIRSTNAME
+
+Returns an L<FS::cust_main> object full of default test data, ready to be inserted.
+This doesn't insert the customer, because you might want to change some things first.
+FIRSTNAME is recommended so you know which test the customer was used for.
+
+=cut
+
+sub new_customer {
+  my $self = shift;
+  my $first = shift || 'No Name';
+  my $location = FS::cust_location->new({
+      address1  => '123 Example Street',
+      city      => 'Sacramento',
+      state     => 'CA',
+      country   => 'US',
+      zip       => '94901',
+  });
+  my $cust = FS::cust_main->new({
+      agentnum      => 1,
+      refnum        => 1,
+      last          => 'Customer',
+      first         => $first,
+      invoice_email => 'newcustomer at fake.freeside.biz',
+      payby         => 'BILL',
+      bill_location => $location,
+      ship_location => $location,
+  });
+  $cust;
+}
+
 1; # End of FS::Test

commit ee6aef9f16f3c9b766ff41e5dc780d034858bb29
Author: Mark Wells <mark at freeside.biz>
Date:   Mon May 30 10:06:28 2016 -0700

    add test for prorate-deferred behavior

diff --git a/FS/t/suite/06-prorate_defer_bill.t b/FS/t/suite/06-prorate_defer_bill.t
new file mode 100755
index 0000000..e14b8ec
--- /dev/null
+++ b/FS/t/suite/06-prorate_defer_bill.t
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+=head2 DESCRIPTION
+
+Tests the prorate_defer_bill behavior when a package is started on the cutoff day,
+and when it's started later in the month.
+
+Correct: The package started on the cutoff day should be charged a setup fee and a
+full period. The package started later in the month should be charged a setup fee,
+a full period, and the partial period.
+
+=cut
+
+use strict;
+use Test::More tests => 11;
+use FS::Test;
+use Date::Parse 'str2time';
+use Date::Format 'time2str';
+use Test::MockTime qw(set_fixed_time);
+use FS::cust_main;
+use FS::cust_pkg;
+use FS::Conf;
+my $FS= FS::Test->new;
+
+my $error;
+
+my $old_part_pkg = $FS->qsearchs('part_pkg', { pkgpart => 2 });
+my $part_pkg = $old_part_pkg->clone;
+BAIL_OUT("existing pkgpart 2 is not a prorated monthly package")
+  unless $part_pkg->freq eq '1' and $part_pkg->plan eq 'prorate';
+$error = $part_pkg->insert(
+  options => {  $old_part_pkg->options,
+                'prorate_defer_bill' => 1,
+                'cutoff_day' => 1,
+                'setup_fee'  => 100,
+                'recur_fee'  => 30,
+              }
+);
+BAIL_OUT("can't configure package: $error") if $error;
+
+my $cust = $FS->new_customer('Prorate defer');
+$error = $cust->insert;
+BAIL_OUT("can't create test customer: $error") if $error;
+
+my @pkgs;
+foreach my $start_day (1, 11) {
+  diag("prorate package starting on day $start_day");
+  # Create and bill the first package.
+  my $date = str2time("2016-04-$start_day");
+  set_fixed_time($date);
+  my $pkg = FS::cust_pkg->new({ pkgpart => $part_pkg->pkgpart });
+  $error = $cust->order_pkg({ 'cust_pkg' => $pkg });
+  BAIL_OUT("can't order package: $error") if $error;
+
+  # bill the customer on the order date
+  $error = $cust->bill_and_collect;
+  $pkg = $pkg->replace_old;
+  push @pkgs, $pkg;
+  my ($cust_bill_pkg) = $pkg->cust_bill_pkg;
+  if ( $start_day == 1 ) {
+    # then it should bill immediately
+    ok($cust_bill_pkg, "package was billed") or next;
+    ok($cust_bill_pkg->setup == 100, "setup fee was charged");
+    ok($cust_bill_pkg->recur == 30, "one month was charged");
+  } elsif ( $start_day == 11 ) {
+    # then not
+    ok(!$cust_bill_pkg, "package billing was deferred");
+    ok($pkg->setup == $date, "package setup date was set");
+  }
+}
+diag("first of month billing...");
+my $date = str2time('2016-05-01');
+set_fixed_time($date);
+my @bill;
+$error = $cust->bill_and_collect(return_bill => \@bill);
+# examine the invoice...
+my $cust_bill = $bill[0] or BAIL_OUT("neither package was billed");
+for my $pkg ($pkgs[0]) {
+  diag("package started day 1:");
+  my ($cust_bill_pkg) = grep {$_->pkgnum == $pkg->pkgnum} $cust_bill->cust_bill_pkg;
+  ok($cust_bill_pkg, "was billed") or next;
+  ok($cust_bill_pkg->setup == 0, "no setup fee was charged");
+  ok($cust_bill_pkg->recur == 30, "one month was charged");
+}
+for my $pkg ($pkgs[1]) {
+  diag("package started day 11:");
+  my ($cust_bill_pkg) = grep {$_->pkgnum == $pkg->pkgnum} $cust_bill->cust_bill_pkg;
+  ok($cust_bill_pkg, "was billed") or next;
+  ok($cust_bill_pkg->setup == 100, "setup fee was charged");
+  ok($cust_bill_pkg->recur == 50, "twenty days + one month was charged");
+}
+

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Test.pm                      |   31 ++++++++++++
 FS/FS/part_pkg/prorate_Mixin.pm    |   43 +++++++++++------
 FS/t/suite/06-prorate_defer_bill.t |   92 ++++++++++++++++++++++++++++++++++++
 3 files changed, 151 insertions(+), 15 deletions(-)
 create mode 100755 FS/t/suite/06-prorate_defer_bill.t




More information about the freeside-commits mailing list