diff options
author | Ivan Kohler <ivan@freeside.biz> | 2016-06-03 17:23:02 -0700 |
---|---|---|
committer | Ivan Kohler <ivan@freeside.biz> | 2016-06-03 17:23:02 -0700 |
commit | d22baa4e71bfa9e153c1fe1152ff4c748f1d935c (patch) | |
tree | 16c12b200e2b071679856699f38e5570ffc9abc2 /FS/t | |
parent | a60ce94f914fb7380546d19713fece0ed208bdc6 (diff) | |
parent | 1efab29ccbb2aa15bcb94f1cb46069f32dcabf9f (diff) |
Merge branch 'master' of git.freeside.biz:/home/git/freeside
Diffstat (limited to 'FS/t')
-rwxr-xr-x | FS/t/suite/06-prorate_defer_bill.t | 92 | ||||
-rwxr-xr-x | FS/t/suite/07-pkg_change_location.t | 82 |
2 files changed, 174 insertions, 0 deletions
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 000000000..e14b8ec21 --- /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"); +} + 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 000000000..6744f78ef --- /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; + |