fix change_later vs. new package locations, #42397
authorMark Wells <mark@freeside.biz>
Tue, 31 May 2016 19:39:43 +0000 (12:39 -0700)
committerMark Wells <mark@freeside.biz>
Tue, 31 May 2016 19:40:14 +0000 (12:40 -0700)
FS/FS/cust_pkg.pm
FS/t/suite/07-pkg_change_location.t [new file with mode: 0755]

index 1cc8235..d15eb89 100644 (file)
@@ -2537,6 +2537,16 @@ sub change_later {
     return "start_date $date is in the past";
   }
 
     return "start_date $date is in the past";
   }
 
+  # If the user entered a new location, set it up now.
+  if ( $opt->{'cust_location'} ) {
+    $error = $opt->{'cust_location'}->find_or_insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "creating location record: $error";
+    }
+    $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
+  }
+
   if ( $self->change_to_pkgnum ) {
     my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
     my $new_pkgpart = $opt->{'pkgpart'}
   if ( $self->change_to_pkgnum ) {
     my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
     my $new_pkgpart = $opt->{'pkgpart'}
diff --git a/FS/t/suite/07-pkg_change_location.t b/FS/t/suite/07-pkg_change_location.t
new file mode 100755 (executable)
index 0000000..6744f78
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+
+=head2 DESCRIPTION
+
+Test scheduling a package location change through the UI, then billing
+on the day of the scheduled change.
+
+=cut
+
+use Test::More tests => 6;
+use FS::Test;
+use Date::Parse 'str2time';
+use Date::Format 'time2str';
+use Test::MockTime qw(set_fixed_time);
+use FS::cust_pkg;
+my $FS = FS::Test->new;
+my $error;
+
+# set up a customer with an active package
+my $cust = $FS->new_customer('Future location change');
+$error = $cust->insert;
+my $pkg = FS::cust_pkg->new({pkgpart => 2});
+$error ||= $cust->order_pkg({ cust_pkg => $pkg });
+my $date = str2time('2016-04-01');
+set_fixed_time($date);
+$error ||= $cust->bill_and_collect;
+BAIL_OUT($error) if $error;
+
+# get the form
+my %args = ( pkgnum  => $pkg->pkgnum,
+             pkgpart => $pkg->pkgpart,
+             locationnum => -1);
+$FS->post('/misc/change_pkg.cgi', %args);
+my $form = $FS->form('OrderPkgForm');
+
+# Schedule the package change two days from now.
+$date += 86400*2;
+my $date_str = time2str('%x', $date);
+
+my %params = (
+  start_date              => $date_str,
+  delay                   => 1,
+  address1                => int(rand(1000)) . ' Changed Street',
+  city                    => 'New City',
+  state                   => 'CA',
+  zip                     => '90001',
+  country                 => 'US',
+);
+
+diag "requesting location change to $params{address1}";
+
+foreach (keys %params) {
+  $form->value($_, $params{$_});
+}
+$FS->post($form);
+ok( $FS->error eq '' , 'form posted' );
+if ( ok( $FS->page =~ m[location.reload], 'location change accepted' )) {
+  #nothing
+} else {
+  $FS->post($FS->redirect);
+  BAIL_OUT( $FS->error);
+}
+# check that the package change is set
+$pkg = $pkg->replace_old;
+my $new_pkgnum = $pkg->change_to_pkgnum;
+ok( $new_pkgnum, 'package change is scheduled' );
+
+# run it and check that the package change happened
+diag("billing customer on $date_str");
+set_fixed_time($date);
+my $error = $cust->bill_and_collect;
+BAIL_OUT($error) if $error;
+
+$pkg = $pkg->replace_old;
+ok($pkg->get('cancel'), "old package is canceled");
+my $new_pkg = $FS->qsearchs('cust_pkg', { pkgnum => $new_pkgnum });
+ok($new_pkg->setup, "new package is active");
+ok($new_pkg->cust_location->address1 eq $params{'address1'}, "new location is correct")
+  or diag $new_pkg->cust_location->address1;
+
+1;
+