fix discounts vs. quantities for prorate packages, RT#23530
[freeside.git] / FS / FS / part_pkg / prorate.pm
1 package FS::part_pkg::prorate;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Time::Local qw(timelocal);
6 #use FS::Record qw(qsearch qsearchs);
7 use FS::part_pkg::flat;
8
9 @ISA = qw(FS::part_pkg::flat);
10
11 %info = (
12   'name' => 'First partial month pro-rated, then flat-rate (selectable billing day)',
13   'shortname' => 'Prorate (Nth of month billing)',
14   'inherit_fields' => [ 'flat', 'usage_Mixin', 'global_Mixin' ],
15   'fields' => {
16     'recur_temporality' => {'disabled' => 1},
17     'sync_bill_date' => {'disabled' => 1},
18     'cutoff_day' => { 'name' => 'Billing Day (1 - 28)',
19                       'default' => 1,
20                     },
21
22     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
23                                     'for one full period after that',
24                           'type' => 'checkbox',
25                         },
26     'prorate_round_day'=> {
27                           'name' => 'Round the prorated period to the nearest '.
28                                     'full day',
29                           'type' => 'checkbox',
30                         },
31     'prorate_defer_bill'=> {
32                         'name' => 'Defer the first bill until the billing day',
33                         'type' => 'checkbox',
34                         },
35     'prorate_verbose' => {
36                         'name' => 'Show prorate details on the invoice',
37                         'type' => 'checkbox',
38                         },
39   },
40   'fieldorder' => [ 'cutoff_day', 'prorate_defer_bill', 'add_full_period', 'prorate_round_day', 'prorate_verbose' ],
41   'freq' => 'm',
42   'weight' => 20,
43 );
44
45 sub calc_recur {
46   my $self = shift;
47   my $cust_pkg = $_[0];
48
49   my $charge = $self->calc_prorate(@_, $self->cutoff_day($cust_pkg));
50   my $discount = $self->calc_discount(@_);
51
52   sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
53
54 }
55
56 sub cutoff_day {
57   my( $self, $cust_pkg ) = @_;
58   my $prorate_day = $cust_pkg->cust_main->prorate_day;
59   $prorate_day ? ( $prorate_day )
60                : split(/\s*,\s*/, $self->option('cutoff_day', 1) || '1');
61 }
62
63 1;