5 Tests the effect of ordering and activating two sync_bill_date packages on
6 the same day. Ref RT#42108.
8 Formerly correct: If the packages have prorate_round_day = 1 (round
9 nearest), or 3 (round down) then the second package should be prorated one
10 day short. If they have prorate_round_day = 2 (round up), they should be
11 billed for the same amount. In both cases they should have the same next
14 Revised RT#72928: The second package should be prorated one day short only
15 if the rounding mode is 1 (round nearest), as the nearest day is different
21 use Test::More tests => 9;
23 use Date::Parse 'str2time';
24 use Date::Format 'time2str';
25 use Test::MockTime qw(set_fixed_time);
29 my $FS= FS::Test->new;
31 foreach my $prorate_mode (1, 2, 3) {
32 diag("prorate_round_day = $prorate_mode");
33 # Create a package def with the sync_bill_date option.
35 my $old_part_pkg = $FS->qsearchs('part_pkg', { pkgpart => 5 });
36 my $part_pkg = $old_part_pkg->clone;
37 BAIL_OUT("existing pkgpart 5 is not a flat monthly package")
38 unless $part_pkg->freq eq '1' and $part_pkg->plan eq 'flat';
39 $error = $part_pkg->insert(
40 options => { $old_part_pkg->options,
41 'sync_bill_date' => 1,
42 'prorate_round_day' => $prorate_mode, }
45 BAIL_OUT("can't configure package: $error") if $error;
47 my $pkgpart = $part_pkg->pkgpart;
48 # Create a clean customer with no other packages.
49 my $location = FS::cust_location->new({
50 address1 => '123 Example Street',
56 my $cust = FS::cust_main->new({
60 first => 'Sync bill date',
61 invoice_email => 'newcustomer@fake.freeside.biz',
63 bill_location => $location,
64 ship_location => $location,
66 $error = $cust->insert;
67 BAIL_OUT("can't create test customer: $error") if $error;
70 # Create and bill the first package.
71 set_fixed_time(str2time('2016-03-10 08:00'));
72 $pkgs[0] = FS::cust_pkg->new({ pkgpart => $pkgpart });
73 $error = $cust->order_pkg({ 'cust_pkg' => $pkgs[0] });
74 BAIL_OUT("can't order package: $error") if $error;
75 $error = $cust->bill_and_collect;
76 # Check the amount billed.
77 my ($cust_bill_pkg) = $pkgs[0]->cust_bill_pkg;
78 my $recur = $part_pkg->base_recur;
79 ok( $cust_bill_pkg->recur == $recur, "first package recur is $recur" )
80 or diag("first package recur is ".$cust_bill_pkg->recur);
82 # Create and bill the second package.
83 set_fixed_time(str2time('2016-03-10 16:00'));
84 $pkgs[1] = FS::cust_pkg->new({ pkgpart => $pkgpart });
85 $error = $cust->order_pkg({ 'cust_pkg' => $pkgs[1] });
86 BAIL_OUT("can't order package: $error") if $error;
87 $error = $cust->bill_and_collect;
89 # Check the amount billed.
90 if ( $prorate_mode == 1 ) {
91 # it should be one day short, in March
92 $recur = sprintf('%.2f', $recur * 30/31);
94 ($cust_bill_pkg) = $pkgs[1]->cust_bill_pkg;
95 ok( $cust_bill_pkg->recur == $recur, "second package recur is $recur" )
96 or diag("second package recur is ".$cust_bill_pkg->recur);
98 my @next_bill = map { time2str('%Y-%m-%d', $_->replace_old->get('bill')) } @pkgs;
100 ok( $next_bill[0] eq $next_bill[1],
101 "both packages will bill again on $next_bill[0]" )
102 or diag("first package bill date is $next_bill[0], second package is $next_bill[1]");