summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2016-05-31 12:39:43 -0700
committerMark Wells <mark@freeside.biz>2016-05-31 12:40:09 -0700
commitcfaa3c1e8c173571e17fcd5aafcbec3524e01af3 (patch)
tree97bbe357f64f2ab2fe784383e55883645e1ca0fe
parentad97e38e5136d585d8cd97137ceddffa05e62522 (diff)
fix change_later vs. new package locations, #42397
-rw-r--r--FS/FS/cust_pkg.pm10
-rwxr-xr-xFS/t/suite/07-pkg_change_location.t82
2 files changed, 92 insertions, 0 deletions
diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm
index ed9e1c8..d222da7 100644
--- a/FS/FS/cust_pkg.pm
+++ b/FS/FS/cust_pkg.pm
@@ -2544,6 +2544,16 @@ sub change_later {
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'}
diff --git a/FS/t/suite/07-pkg_change_location.t b/FS/t/suite/07-pkg_change_location.t
new file mode 100755
index 0000000..6744f78
--- /dev/null
+++ b/FS/t/suite/07-pkg_change_location.t
@@ -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;
+