discounts with bulk packages, RT#24655
[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   },
27   'fieldorder' => [ 'svc_setup_fee', 'svc_recur_fee',
28                     'summarize_svcs', 'no_prorate' ],
29   'weight' => 51,
30 );
31
32 sub price_info {
33     my $self = shift;
34     my $str = $self->SUPER::price_info;
35     my $svc_setup_fee = $self->option('svc_setup_fee');
36     my $svc_recur_fee = $self->option('svc_recur_fee');
37     my $conf = new FS::Conf;
38     my $money_char = $conf->config('money_char') || '$';
39     $str .= " , bulk" if $str;
40     $str .= ": $money_char" . $svc_setup_fee . " one-time per service" 
41         if $svc_setup_fee;
42     $str .= ", " if ($svc_setup_fee && $svc_recur_fee);
43     $str .= $money_char . $svc_recur_fee . " recurring per service"
44         if $svc_recur_fee;
45     $str;
46 }
47
48 #some false laziness-ish w/agent.pm...  not a lot
49 sub calc_recur {
50   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
51
52   my $conf = new FS::Conf;
53   my $money_char = $conf->config('money_char') || '$';
54   
55   my $last_bill = $cust_pkg->last_bill;
56
57   my $total_svc_charge = 0;
58   my %n_setup = ();
59   my %n_recur = ();
60   my %part_svc_label = ();
61
62   my $summarize = $self->option('summarize_svcs',1);
63
64   foreach my $cust_svc ( $self->_bulk_cust_svc( $cust_pkg, $sdate ) ) {
65
66     my @label = $cust_svc->label_long( $$sdate, $last_bill );
67     die "fatal: no label found, wtf?" unless scalar(@label); #?
68     my $svc_details = $label[0]. ': '. $label[1]. ': ';
69     $part_svc_label{$cust_svc->svcpart} ||= $label[0];
70
71     my $svc_charge = 0;
72
73     my $setup = $self->_bulk_setup($cust_pkg, $cust_svc);
74     if ( $setup ) {
75       $svc_charge += $setup;
76       $svc_details .= $money_char. sprintf('%.2f setup, ', $setup);
77       $n_setup{$cust_svc->svcpart}++;
78     }
79
80     my( $recur, $r_details ) = $self->_bulk_recur($cust_pkg, $cust_svc, $sdate);
81     if ( $recur ) {
82       $svc_charge += $recur;
83       $svc_details .= $money_char. sprintf('%.2f', $recur). $r_details;
84       $n_recur{$cust_svc->svcpart}++;
85       push @$details, $svc_details if !$summarize;
86     }
87
88     $total_svc_charge += $svc_charge;
89
90   }
91
92   if ( $summarize ) {
93     foreach my $svcpart (keys %part_svc_label) {
94       push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
95         $n_setup{$svcpart}, $self->option('svc_setup_fee') )
96         if $self->option('svc_setup_fee') and $n_setup{$svcpart};
97       push @$details, sprintf('%d services @ '.$money_char.'%.2f',
98         $n_recur{$svcpart}, $self->option('svc_recur_fee') )
99         if $n_recur{$svcpart};
100     }
101   }
102
103   my $charge = $self->base_recur($cust_pkg, $sdate) + $total_svc_charge;
104
105   $param->{'override_charges'} = $total_svc_charge / $self->freq;
106   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
107
108   sprintf('%.2f', $charge - $discount );
109 }
110
111 sub hide_svc_detail { 1; }
112
113 sub is_free_options {
114   qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
115 }
116
117 1;
118