add a bulk_simple price plan that behaves more intuitively for intial invoices, RT...
[freeside.git] / FS / FS / part_pkg / bulk_simple.pm
1 package FS::part_pkg::bulk;
2 use base qw( FS::part_pkg::bulk_Common );
3
4 use strict;
5 use vars qw($DEBUG $me %info);
6 use Date::Format;
7 use FS::Conf;
8 use FS::cust_svc_option;
9
10 $DEBUG = 0;
11 $me = '[FS::part_pkg::bulk]';
12
13 %info = (
14   'name' => 'Bulk billing based on number of active services (at invoice generation)',
15   'inherit_fields' => [ 'bulk_Common', 'global_Mixin' ],
16   'weight' => 50,
17 );
18
19 #some false laziness-ish w/agent.pm...  not a lot
20 # more w/bulk.pm's calc_recur
21 sub calc_recur {
22   my($self, $cust_pkg, $sdate, $details ) = @_;
23
24   my $conf = new FS::Conf;
25   my $money_char = $conf->config('money_char') || '$';
26   
27   my $svc_setup_fee = $self->option('svc_setup_fee');
28
29   my $total_svc_charge = 0;
30   my %n_setup = ();
31   my %n_recur = ();
32   my %part_svc_label = ();
33
34   my $summarize = $self->option('summarize_svcs',1);
35
36   foreach my $cust_svc ( $cust_pkg->cust_svc ) {
37
38     my @label = $cust_svc->label_long;
39     #die "fatal: no historical label found, wtf?" unless scalar(@label); #?
40     my $svc_details = $label[0]. ': '. $label[1]. ': ';
41     $part_svc_label{$h_cust_svc->svcpart} ||= $label[0];
42
43     my $svc_charge = 0;
44
45     if ( $svc_setup_fee && ! $cust_svc->option('bulk_setup') ) {
46
47       my $bulk_setup = new FS::cust_svc_option {
48         'svcnum'      => $cust_svc->svcnum,
49         'optionname'  => 'bulk_setup',
50         'optionvalue' => time, #invoice date?
51       };
52       my $error = $bulk_setup->insert;
53       die $error if $error;
54
55       $svc_charge += $svc_setup_fee;
56       $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
57       $n_setup{$cust_svc->svcpart}++;
58     }
59
60     my $recur_charge = $self->option('svc_recur_fee');
61     $svc_details .= $money_char. sprintf('%.2f', $recur_charge );
62
63     $svc_charge += $recur_charge;
64     $n_recur{$h_cust_svc->svcpart}++;
65     push @$details, $svc_details if !$summarize;
66     $total_svc_charge += $svc_charge;
67
68   }
69
70   if ( $summarize ) {
71     foreach my $svcpart (keys %part_svc_label) {
72       push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
73         $n_setup{$svcpart}, $svc_setup_fee )
74         if $svc_setup_fee and $n_setup{$svcpart};
75       push @$details, sprintf('%d services @ '.$money_char.'%.2f',
76         $n_recur{$svcpart}, $self->option('svc_recur_fee') )
77         if $n_recur{$svcpart};
78     }
79   }
80
81   sprintf('%.2f', $self->base_recur($cust_pkg, $sdate) + $total_svc_charge );
82 }
83
84 sub can_discount { 0; }
85
86 sub hide_svc_detail {
87   1;
88 }
89
90 sub is_free_options {
91   qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
92 }
93
94 1;
95