91a8efa74ee1c48fb82bf0041f11de15e36527d9
[freeside.git] / FS / t / suite / 05-prorate_sync_same_day.t
1 #!/usr/bin/perl
2
3 =head2 DESCRIPTION
4
5 Tests the effect of ordering and activating two sync_bill_date packages on
6 the same day. Ref RT#42108.
7
8 Correct: If the packages have prorate_round_day = 1 (round nearest), or 3
9 (round down) then the second package should be prorated one day short. If
10 they have prorate_round_day = 2 (round up), they should be billed
11 for the same amount. In both cases they should have the same next bill date.
12
13 =cut
14
15 use strict;
16 use Test::More tests => 9;
17 use FS::Test;
18 use Date::Parse 'str2time';
19 use Date::Format 'time2str';
20 use Test::MockTime qw(set_fixed_time);
21 use FS::cust_main;
22 use FS::cust_pkg;
23 use FS::Conf;
24 my $FS= FS::Test->new;
25
26 foreach my $prorate_mode (1, 2, 3) {
27   diag("prorate_round_day = $prorate_mode");
28   # Create a package def with the sync_bill_date option.
29   my $error;
30   my $old_part_pkg = $FS->qsearchs('part_pkg', { pkgpart => 5 });
31   my $part_pkg = $old_part_pkg->clone;
32   BAIL_OUT("existing pkgpart 5 is not a flat monthly package")
33     unless $part_pkg->freq eq '1' and $part_pkg->plan eq 'flat';
34   $error = $part_pkg->insert(
35     options => {  $old_part_pkg->options,
36                   'sync_bill_date' => 1,
37                   'prorate_round_day' => $prorate_mode, }
38   );
39
40   BAIL_OUT("can't configure package: $error") if $error;
41
42   my $pkgpart = $part_pkg->pkgpart;
43   # Create a clean customer with no other packages.
44   my $location = FS::cust_location->new({
45       address1  => '123 Example Street',
46       city      => 'Sacramento',
47       state     => 'CA',
48       country   => 'US',
49       zip       => '94901',
50   });
51   my $cust = FS::cust_main->new({
52       agentnum      => 1,
53       refnum        => 1,
54       last          => 'Customer',
55       first         => 'Sync bill date',
56       invoice_email => 'newcustomer@fake.freeside.biz',
57       bill_location => $location,
58       ship_location => $location,
59   });
60   $error = $cust->insert;
61   BAIL_OUT("can't create test customer: $error") if $error;
62
63   my @pkgs;
64   # Create and bill the first package.
65   set_fixed_time(str2time('2016-03-10 08:00'));
66   $pkgs[0] = FS::cust_pkg->new({ pkgpart => $pkgpart });
67   $error = $cust->order_pkg({ 'cust_pkg' => $pkgs[0] });
68   BAIL_OUT("can't order package: $error") if $error;
69   $error = $cust->bill_and_collect;
70   # Check the amount billed.
71   my ($cust_bill_pkg) = $pkgs[0]->cust_bill_pkg;
72   my $recur = $part_pkg->base_recur;
73   ok( $cust_bill_pkg->recur == $recur, "first package recur is $recur" )
74     or diag("first package recur is ".$cust_bill_pkg->recur);
75
76   # Create and bill the second package.
77   set_fixed_time(str2time('2016-03-10 16:00'));
78   $pkgs[1] = FS::cust_pkg->new({ pkgpart => $pkgpart });
79   $error = $cust->order_pkg({ 'cust_pkg' => $pkgs[1] });
80   BAIL_OUT("can't order package: $error") if $error;
81   $error = $cust->bill_and_collect;
82
83   # Check the amount billed.
84   if ( $prorate_mode == 1 or $prorate_mode == 3 ) {
85     # it should be one day short, in March
86     $recur = sprintf('%.2f', $recur * 30/31);
87   }
88   ($cust_bill_pkg) = $pkgs[1]->cust_bill_pkg;
89   ok( $cust_bill_pkg->recur == $recur, "second package recur is $recur" )
90     or diag("second package recur is ".$cust_bill_pkg->recur);
91
92   my @next_bill = map { time2str('%Y-%m-%d', $_->replace_old->get('bill')) } @pkgs;
93
94   ok( $next_bill[0] eq $next_bill[1],
95     "both packages will bill again on $next_bill[0]" )
96     or diag("first package bill date is $next_bill[0], second package is $next_bill[1]");
97 }