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