1 package FS::part_pkg::bulk;
4 use vars qw(@ISA $DEBUG $me %info);
6 use FS::part_pkg::flat;
8 @ISA = qw(FS::part_pkg::flat);
11 $me = '[FS::part_pkg::bulk]';
14 'name' => 'Bulk billing based on number of active services',
16 'setup_fee' => { 'name' => 'Setup fee for the entire bulk package',
19 'recur_fee' => { 'name' => 'Recurring fee for the entire bulk package',
22 'svc_setup_fee' => { 'name' => 'Setup fee for each new service',
25 'svc_recur_fee' => { 'name' => 'Recurring fee for each service',
28 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
29 ' of service at cancellation',
32 'summarize_svcs'=> { 'name' => 'Show a count of services on the invoice, '.
33 'instead of a detailed list',
36 'no_prorate' => { 'name' => 'Don\'t prorate recurring fees on new '.
41 'fieldorder' => [ 'setup_fee', 'recur_fee', 'svc_setup_fee', 'svc_recur_fee',
42 'unused_credit', 'summarize_svcs', 'no_prorate' ],
46 #some false laziness-ish w/agent.pm... not a lot
48 my($self, $cust_pkg, $sdate, $details ) = @_;
50 my $conf = new FS::Conf;
51 my $money_char = $conf->config('money_char') || '$';
53 my $svc_setup_fee = $self->option('svc_setup_fee');
55 my $last_bill = $cust_pkg->last_bill;
57 return sprintf("%.2f", $self->base_recur($cust_pkg) )
58 unless $$sdate > $last_bill;
60 my $total_svc_charge = 0;
63 my %part_svc_label = ();
65 my $summarize = $self->option('summarize_svcs',1);
67 warn "$me billing for bulk services from ". time2str('%x', $last_bill).
68 " to ". time2str('%x', $$sdate). "\n"
72 foreach my $h_cust_svc ( $cust_pkg->h_cust_svc( $$sdate, $last_bill ) ) {
74 my @label = $h_cust_svc->label_long( $$sdate, $last_bill );
75 die "fatal: no historical label found, wtf?" unless scalar(@label); #?
76 my $svc_details = $label[0]. ': '. $label[1]. ': ';
77 $part_svc_label{$h_cust_svc->svcpart} ||= $label[0];
81 my $svc_start = $h_cust_svc->date_inserted;
82 if ( $svc_start < $last_bill ) {
83 $svc_start = $last_bill;
84 } elsif ( $svc_setup_fee ) {
85 $svc_charge += $svc_setup_fee;
86 $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
87 $n_setup{$h_cust_svc->svcpart}++;
90 my $svc_end = $h_cust_svc->date_deleted;
91 $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
94 if ( $self->option('no_prorate',1) ) {
95 $recur_charge = $self->option('svc_recur_fee');
98 $recur_charge = $self->option('svc_recur_fee')
99 * ( $svc_end - $svc_start )
100 / ( $$sdate - $last_bill );
103 $svc_details .= $money_char. sprintf('%.2f', $recur_charge ).
104 ' ('. time2str('%x', $svc_start).
105 ' - '. time2str('%x', $svc_end ). ')'
108 $svc_charge += $recur_charge;
109 $n_recur{$h_cust_svc->svcpart}++;
110 push @$details, $svc_details if !$summarize;
111 $total_svc_charge += $svc_charge;
115 foreach my $svcpart (keys %part_svc_label) {
116 push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
117 $n_setup{$svcpart}, $svc_setup_fee )
118 if $svc_setup_fee and $n_setup{$svcpart};
119 push @$details, sprintf('%d services @ '.$money_char.'%.2f',
120 $n_recur{$svcpart}, $self->option('svc_recur_fee') )
121 if $n_recur{$svcpart};
125 sprintf('%.2f', $self->base_recur($cust_pkg) + $total_svc_charge );
128 sub can_discount { 0; }
130 sub hide_svc_detail {
134 sub is_free_options {
135 qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );