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