add a bulk_simple price plan that behaves more intuitively for intial invoices, RT...
[freeside.git] / FS / FS / part_pkg / bulk_Common.pm
1 package FS::part_pkg::bulk;
2 use base qw( FS::part_pkg::flat );
3
4 use strict;
5 use vars qw($DEBUG $me %info);
6 use Date::Format;
7 use FS::Conf;
8
9 $DEBUG = 0;
10 $me = '[FS::part_pkg::bulk_Common]';
11
12 %info = (
13   'disabled' => 1,
14   'inherit_fields' => [ 'global_Mixin' ],
15   'fields' => {
16     'svc_setup_fee' => { 'name'    => 'Setup fee for each new service',
17                          'default' => 0,
18                        },
19     'svc_recur_fee' => { 'name'    => 'Recurring fee for each service',
20                          'default' => 0,
21                        },
22     'summarize_svcs'=> { 'name' => 'Show a count of services on the invoice, '.
23                                    'instead of a detailed list',
24                          'type' => 'checkbox',
25                        },
26     'no_prorate'    => { 'name' => 'Don\'t prorate recurring fees on services '.
27                                    'active for a partial month',
28                          'type' => 'checkbox',
29                        },
30   },
31   'fieldorder' => [ 'svc_setup_fee', 'svc_recur_fee',
32                     'summarize_svcs', 'no_prorate' ],
33   'weight' => 51,
34 );
35
36 sub price_info {
37     my $self = shift;
38     my $str = $self->SUPER::price_info;
39     my $svc_setup_fee = $self->option('svc_setup_fee');
40     my $svc_recur_fee = $self->option('svc_recur_fee');
41     my $conf = new FS::Conf;
42     my $money_char = $conf->config('money_char') || '$';
43     $str .= " , bulk" if $str;
44     $str .= ": $money_char" . $svc_setup_fee . " one-time per service" 
45         if $svc_setup_fee;
46     $str .= ", " if ($svc_setup_fee && $svc_recur_fee);
47     $str .= $money_char . $svc_recur_fee . " recurring per service"
48         if $svc_recur_fee;
49     $str;
50 }
51
52 #some false laziness-ish w/agent.pm...  not a lot
53 sub calc_recur {
54   my($self, $cust_pkg, $sdate, $details ) = @_;
55
56   my $conf = new FS::Conf;
57   my $money_char = $conf->config('money_char') || '$';
58   
59   my $svc_setup_fee = $self->option('svc_setup_fee');
60
61   my $last_bill = $cust_pkg->last_bill;
62
63   return sprintf("%.2f", $self->base_recur($cust_pkg, $sdate) )
64     unless $$sdate > $last_bill;
65
66   my $total_svc_charge = 0;
67   my %n_setup = ();
68   my %n_recur = ();
69   my %part_svc_label = ();
70
71   my $summarize = $self->option('summarize_svcs',1);
72
73   warn "$me billing for bulk services from ". time2str('%x', $last_bill).
74                                       " to ". time2str('%x', $$sdate). "\n"
75     if $DEBUG;
76
77                                            #   END      START
78   foreach my $h_cust_svc ( $cust_pkg->h_cust_svc( $$sdate, $last_bill ) ) {
79
80     my @label = $h_cust_svc->label_long( $$sdate, $last_bill );
81     die "fatal: no historical label found, wtf?" unless scalar(@label); #?
82     my $svc_details = $label[0]. ': '. $label[1]. ': ';
83     $part_svc_label{$h_cust_svc->svcpart} ||= $label[0];
84
85     my $svc_charge = 0;
86
87     my $svc_start = $h_cust_svc->date_inserted;
88     if ( $svc_start < $last_bill ) {
89       $svc_start = $last_bill;
90     } elsif ( $svc_setup_fee ) {
91       $svc_charge += $svc_setup_fee;
92       $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
93       $n_setup{$h_cust_svc->svcpart}++;
94     }
95
96     my $svc_end = $h_cust_svc->date_deleted;
97     $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
98
99     my $recur_charge;
100     if ( $self->option('no_prorate',1) ) {
101       $recur_charge = $self->option('svc_recur_fee');
102     }
103     else {
104       $recur_charge = $self->option('svc_recur_fee') 
105                                      * ( $svc_end - $svc_start )
106                                      / ( $$sdate  - $last_bill );
107     }
108
109     $svc_details .= $money_char. sprintf('%.2f', $recur_charge ).
110                     ' ('.  time2str('%x', $svc_start).
111                     ' - '. time2str('%x', $svc_end  ). ')'
112       if $recur_charge;
113
114     $svc_charge += $recur_charge;
115     $n_recur{$h_cust_svc->svcpart}++;
116     push @$details, $svc_details if !$summarize;
117     $total_svc_charge += $svc_charge;
118
119   }
120   if ( $summarize ) {
121     foreach my $svcpart (keys %part_svc_label) {
122       push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
123         $n_setup{$svcpart}, $svc_setup_fee )
124         if $svc_setup_fee and $n_setup{$svcpart};
125       push @$details, sprintf('%d services @ '.$money_char.'%.2f',
126         $n_recur{$svcpart}, $self->option('svc_recur_fee') )
127         if $n_recur{$svcpart};
128     }
129   }
130
131   sprintf('%.2f', $self->base_recur($cust_pkg, $sdate) + $total_svc_charge );
132 }
133
134 sub can_discount { 0; }
135
136 sub hide_svc_detail {
137   1;
138 }
139
140 sub is_free_options {
141   qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
142 }
143
144 1;
145