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