bulk price plan: label as Name <email>, supress extraneous service list, RT#3519
[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   },
33   'fieldorder' => [ 'setup_fee', 'recur_fee', 'svc_setup_fee', 'svc_recur_fee',
34                     'unused_credit', ],
35   'weight' => 50,
36 );
37
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
53   warn "$me billing for bulk services from ". time2str('%x', $last_bill).
54                                       " to ". time2str('%x', $$sdate). "\n"
55     if $DEBUG;
56
57                                            #   END      START
58   foreach my $h_cust_svc ( $cust_pkg->h_cust_svc( $$sdate, $last_bill ) ) {
59
60     my @label = $h_cust_svc->label_long( $$sdate, $last_bill );
61     die "fatal: no historical label found, wtf?" unless scalar(@label); #?
62     my $svc_details = $label[0]. ': '. $label[1]. ': ';
63
64     my $svc_charge = 0;
65
66     my $svc_start = $h_cust_svc->date_inserted;
67     if ( $svc_start < $last_bill ) {
68       $svc_start = $last_bill;
69     } elsif ( $svc_setup_fee ) {
70       $svc_charge += $svc_setup_fee;
71       $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
72     }
73
74     my $svc_end = $h_cust_svc->date_deleted;
75     $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
76
77     $svc_charge = $self->option('svc_recur_fee') * ( $svc_end - $svc_start )
78                                                  / ( $$sdate  - $last_bill );
79
80     $svc_details .= $money_char. sprintf('%.2f', $svc_charge ).
81                     ' ('.  time2str('%x', $svc_start).
82                     ' - '. time2str('%x', $svc_end  ). ')'
83       if $self->option('svc_recur_fee');
84
85     push @$details, $svc_details;
86     $total_svc_charge += $svc_charge;
87
88   }
89
90   sprintf("%.2f", $self->base_recur($cust_pkg) + $total_svc_charge );
91 }
92
93 sub hide_svc_detail {
94   1;
95 }
96
97 sub is_free_options {
98   qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
99 }
100
101 1;
102