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_Common;
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 $last_bill = $cust_pkg->last_bill;
60
61   my $total_svc_charge = 0;
62   my %n_setup = ();
63   my %n_recur = ();
64   my %part_svc_label = ();
65
66   my $summarize = $self->option('summarize_svcs',1);
67
68   foreach my $cust_svc ( $self->_bulk_cust_svc( $cust_pkg, $sdate ) ) {
69
70     my @label = $cust_svc->label_long( $$sdate, $last_bill );
71     die "fatal: no label found, wtf?" unless scalar(@label); #?
72     my $svc_details = $label[0]. ': '. $label[1]. ': ';
73     $part_svc_label{$cust_svc->svcpart} ||= $label[0];
74
75     my $svc_charge = 0;
76
77     my $setup = $self->_bulk_setup($cust_pkg, $cust_svc);
78     if ( $setup ) {
79       $svc_charge += $setup;
80       $svc_details .= $money_char. sprintf('%.2f setup, ', $setup);
81       $n_setup{$cust_svc->svcpart}++;
82     }
83
84     my( $recur, $r_details ) = $self->_bulk_recur($cust_pkg, $cust_svc, $sdate);
85     if ( $recur ) {
86       $svc_charge += $recur;
87       $svc_details .= $money_char. sprintf('%.2f', $recur). $r_details;
88       $n_recur{$cust_svc->svcpart}++;
89       push @$details, $svc_details if !$summarize;
90     }
91
92     $total_svc_charge += $svc_charge;
93
94   }
95
96   if ( $summarize ) {
97     foreach my $svcpart (keys %part_svc_label) {
98       push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
99         $n_setup{$svcpart}, $self->option('svc_setup_fee') )
100         if $self->option('svc_setup_fee') and $n_setup{$svcpart};
101       push @$details, sprintf('%d services @ '.$money_char.'%.2f',
102         $n_recur{$svcpart}, $self->option('svc_recur_fee') )
103         if $n_recur{$svcpart};
104     }
105   }
106
107   sprintf('%.2f', $self->base_recur($cust_pkg, $sdate) + $total_svc_charge );
108 }
109
110 sub can_discount { 0; }
111
112 sub hide_svc_detail { 1; }
113
114 sub is_free_options {
115   qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
116 }
117
118 1;
119