5 Tests crediting a package for unused time when it has sales tax. See
8 The package will be billed for $30.00 with 10% tax, then credited for 1/3
11 Correct: The credit amount will be $11.00.
16 use Test::More tests => 3;
18 use Date::Parse 'str2time';
19 use Date::Format 'time2str';
20 use Test::MockTime qw(set_fixed_time);
25 my $FS= FS::Test->new;
27 # Create a package def
29 my $part_pkg = FS::part_pkg->new({
30 pkg => 'Tax credit test',
38 'recur_temporality' => 'upcoming',
39 'unused_credit_cancel' => '1',
41 $error = $part_pkg->insert(options => \%options);
42 BAIL_OUT("can't create package def: $error") if $error;
44 # Create the customer and order a package
45 my $cust = $FS->new_customer('Credit unused with taxes');
46 $cust->bill_location->state('AK');
47 $error = $cust->insert;
48 BAIL_OUT("can't create test customer: $error") if $error;
50 my $pkg = FS::cust_pkg->new({ pkgpart => $part_pkg->pkgpart });
51 $error = $cust->order_pkg({ cust_pkg => $pkg });
52 BAIL_OUT("can't create test charges: $error") if $error;
55 my $cust_main_county = FS::cust_main_county->new({
58 'exempt_amount' => 0.00,
59 'taxname' => 'Test tax',
62 $error = $cust_main_county->insert;
63 BAIL_OUT("can't create tax definitions: $error") if $error;
65 # Bill the customer on Apr 1
66 # (April because it's 30 days, and also doesn't have DST)
67 set_fixed_time(str2time('2016-04-01 00:00'));
69 $error = $cust->bill( return_bill => \@return );
70 BAIL_OUT("can't bill charges: $error") if $error;
71 my $cust_bill = $return[0] or BAIL_OUT("no invoice generated");
74 my ($tax_item) = grep { $_->itemdesc eq $cust_main_county->taxname }
75 $cust_bill->cust_bill_pkg;
76 ok ( $tax_item && $tax_item->setup == 3.00, "Tax charged = 3.00" );
79 $pkg = $pkg->replace_old;
81 # Pay the bill in two parts
82 set_fixed_time(str2time('2016-04-02 00:00'));
83 foreach my $paid (10.00, 23.00) {
84 my $cust_pay = FS::cust_pay->new({
85 custnum => $cust->custnum,
86 invnum => $cust_bill->invnum,
91 $error = $cust_pay->insert;
92 BAIL_OUT("can't record payment: $error") if $error;
94 # Now cancel with 1/3 of the period left
95 set_fixed_time(str2time('2016-04-21 00:00'));
96 $error = $pkg->cancel();
97 BAIL_OUT("can't cancel package: $error") if $error;
100 my ($credit) = $cust->cust_credit
101 or BAIL_OUT("no credit was created");
102 ok ( $credit->amount == 11.00, "Credited 1/3 of package charge with tax" )
103 or diag("is ". $credit->amount );
105 # the invoice should also be fully paid after that
106 ok ( $cust_bill->owed == 0, "Invoice balance is zero" )
107 or diag("is ". $cust_bill->owed);