show package pricing details on customer view, RT11372
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
2
3 use strict;
4 use vars qw( @ISA %info
5              %usage_recharge_fields @usage_recharge_fieldorder
6            );
7 use Tie::IxHash;
8 use List::Util qw(min); # max);
9 #use FS::Record qw(qsearch);
10 use FS::UI::bytecount;
11 use FS::Conf;
12 use FS::part_pkg;
13
14 @ISA = qw(FS::part_pkg 
15           FS::part_pkg::prorate_Mixin
16           FS::part_pkg::discount_Mixin);
17
18 tie my %temporalities, 'Tie::IxHash',
19   'upcoming'  => "Upcoming (future)",
20   'preceding' => "Preceding (past)",
21 ;
22
23 tie my %contract_years, 'Tie::IxHash', (
24   '' => '(none)',
25   map { $_*12 => $_ } (1..5),
26 );
27
28 %info = (
29   'name' => 'Flat rate (anniversary billing)',
30   'shortname' => 'Anniversary',
31   'inherit_fields' => [ 'usage_Mixin', 'global_Mixin' ],
32   'fields' => {
33     #false laziness w/voip_cdr.pm
34     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
35                              'type' => 'select',
36                              'select_options' => \%temporalities,
37                            },
38
39     #used in cust_pkg.pm so could add to any price plan
40     'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
41                        },
42     'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
43                        },
44     'contract_end_months'=> { 
45                         'name' => 'Auto-add a contract end date this number of years out',
46                         'type' => 'select',
47                         'select_options' => \%contract_years,
48                       },
49     #used in cust_pkg.pm so could add to any price plan where it made sense
50     'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
51                          'type' => 'checkbox',
52                        },
53     'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
54                                     'with the customer\'s other packages',
55                           'type' => 'checkbox',
56                         },
57     'prorate_defer_bill' => { 
58                           'name' => 'When synchronizing, defer the bill until '.
59                                     'the customer\'s next bill date',
60                           'type' => 'checkbox',
61                         },
62     'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
63                         'type' => 'checkbox',
64                       },
65     'unsuspend_adjust_bill' => 
66                         { 'name' => 'Adjust next bill date forward when '.
67                                     'unsuspending',
68                           'type' => 'checkbox',
69                         },
70
71     'externalid' => { 'name'   => 'Optional External ID',
72                       'default' => '',
73                     },
74   },
75   'fieldorder' => [ qw( recur_temporality 
76                         expire_months adjourn_months
77                         contract_end_months
78                         start_1st sync_bill_date prorate_defer_bill
79                         suspend_bill unsuspend_adjust_bill
80                         externalid ),
81                   ],
82   'weight' => 10,
83 );
84
85 sub price_info {
86     my $self = shift;
87     my $conf = new FS::Conf;
88     my $money_char = $conf->config('money_char') || '$';
89     my $setup = $self->option('setup_fee') || 0;
90     my $recur = $self->option('recur_fee', 1) || 0;
91     my $str = '';
92     $str = $money_char . $setup . ' one-time' if $setup;
93     $str .= ', ' if ($setup && $recur);
94     $str .= $money_char . $recur . ' recurring ' if $recur;
95     $str;
96 }
97
98 sub calc_setup {
99   my($self, $cust_pkg, $sdate, $details ) = @_;
100
101   return 0 if $self->prorate_setup($cust_pkg, $sdate);
102
103   my $i = 0;
104   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
105   while ($i < $count) {
106     push @$details, $self->option( 'additional_info' . $i++ );
107   }
108
109   my $quantity = $cust_pkg->quantity || 1;
110
111   my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
112   sprintf('%.2f', $charge);
113 }
114
115 sub unit_setup {
116   my($self, $cust_pkg, $sdate, $details ) = @_;
117
118   $self->option('setup_fee') || 0;
119 }
120
121 sub calc_recur {
122   my $self = shift;
123   my($cust_pkg, $sdate, $details, $param ) = @_;
124
125   #my $last_bill = $cust_pkg->last_bill;
126   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
127
128   return 0
129     if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
130
131   my $charge = $self->base_recur($cust_pkg, $sdate);
132   if ( my $cutoff_day = $self->cutoff_day($cust_pkg) ) {
133     $charge = $self->calc_prorate(@_);
134   }
135   elsif ( $param->{freq_override} ) {
136     # XXX not sure if this should be mutually exclusive with sync_bill_date.
137     # Given the very specific problem that freq_override is meant to 'solve',
138     # it probably should.
139     $charge *= $param->{freq_override} if $param->{freq_override};
140   }
141
142   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
143   return sprintf('%.2f', $charge - $discount);
144 }
145
146 sub cutoff_day {
147   my $self = shift;
148   my $cust_pkg = shift;
149   if ( $self->option('sync_bill_date',1) ) {
150     my $next_bill = $cust_pkg->cust_main->next_bill_date;
151     if ( defined($next_bill) ) {
152       return (localtime($next_bill))[3];
153     }
154   }
155   return 0;
156 }
157
158 sub base_recur {
159   my($self, $cust_pkg, $sdate) = @_;
160   $self->option('recur_fee', 1) || 0;
161 }
162
163 sub base_recur_permonth {
164   my($self, $cust_pkg) = @_;
165
166   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
167
168   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
169 }
170
171 sub calc_remain {
172   my ($self, $cust_pkg, %options) = @_;
173
174   my $time;
175   if ($options{'time'}) {
176     $time = $options{'time'};
177   } else {
178     $time = time;
179   }
180
181   my $next_bill = $cust_pkg->getfield('bill') || 0;
182
183   return 0 if    ! $self->base_recur($cust_pkg, \$time)
184               || ! $next_bill
185               || $next_bill < $time;
186
187   my %sec = (
188     'h' =>    3600, # 60 * 60
189     'd' =>   86400, # 60 * 60 * 24
190     'w' =>  604800, # 60 * 60 * 24 * 7
191     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
192   );
193
194   $self->freq =~ /^(\d+)([hdwm]?)$/
195     or die 'unparsable frequency: '. $self->freq;
196   my $freq_sec = $1 * $sec{$2||'m'};
197   return 0 unless $freq_sec;
198
199   sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
200
201 }
202
203 sub is_free_options {
204   qw( setup_fee recur_fee );
205 }
206
207 sub is_prepaid { 0; } #no, we're postpaid
208
209 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
210 sub can_discount {
211   my $self = shift;
212   $self->freq =~ /^\d+$/ && $self->freq > 0;
213 }
214
215 sub usage_valuehash {
216   my $self = shift;
217   map { $_, $self->option($_) }
218     grep { $self->option($_, 'hush') } 
219     qw(seconds upbytes downbytes totalbytes);
220 }
221
222 sub reset_usage {
223   my($self, $cust_pkg, %opt) = @_;
224   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
225   my %values = $self->usage_valuehash;
226   if ($self->option('usage_rollover', 1)) {
227     $cust_pkg->recharge(\%values);
228   }else{
229     $cust_pkg->set_usage(\%values, %opt);
230   }
231 }
232
233 1;